310 lines
7.7 KiB
C#
310 lines
7.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using JD.Kernel.StaticHelpers;
|
|
using JD.Kernel.Structs;
|
|
using JD.TeaBoxAddinSystem.Addins.Description;
|
|
using JD.TeaBoxAddinSystem.Addins.Description.Conditions;
|
|
using JD.TeaBoxAddinSystem.Addins.Internal;
|
|
|
|
namespace JD.TeaBoxAddinSystem.Addins
|
|
{
|
|
/// <summary>
|
|
/// An addin is a plugin that adds or extends some functionality. It can also define extension points for other addins.
|
|
/// </summary>
|
|
public sealed class Addin
|
|
{
|
|
#region Inner Types
|
|
|
|
private class ExtensionTypeSetCollection : AddinObjectKeyedCollection<string, ExtensionTypeSet>
|
|
{
|
|
#region Constructor
|
|
|
|
internal ExtensionTypeSetCollection(Module owner)
|
|
: base(owner)
|
|
{
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Methods
|
|
|
|
/// <summary>
|
|
/// Overriden.
|
|
/// </summary>
|
|
/// <param name="item"></param>
|
|
/// <returns></returns>
|
|
protected override string GetKeyForItem(ExtensionTypeSet item)
|
|
{
|
|
if (item == null)
|
|
{
|
|
throw new ArgumentNullException("item");
|
|
}
|
|
|
|
System.Diagnostics.Debug.Assert(!string.IsNullOrEmpty(item.Name));
|
|
return item.Name;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
|
|
private class ModuleCollection : Collection<Module>
|
|
{
|
|
#region Variables
|
|
|
|
private readonly Addin m_addin;
|
|
|
|
#endregion
|
|
|
|
#region Constructor
|
|
|
|
public ModuleCollection(Addin addin)
|
|
{
|
|
m_addin = addin;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Methods
|
|
|
|
protected override void InsertItem(int index, Module item)
|
|
{
|
|
m_addin.ThrowIfLoaded();
|
|
|
|
base.InsertItem(index, item);
|
|
item.SetAddin(m_addin);
|
|
}
|
|
|
|
protected override void SetItem(int index, Module item)
|
|
{
|
|
m_addin.ThrowIfLoaded();
|
|
|
|
this[index].SetAddin(null);
|
|
base.SetItem(index, item);
|
|
item.SetAddin(m_addin);
|
|
}
|
|
|
|
protected override void RemoveItem(int index)
|
|
{
|
|
m_addin.ThrowIfLoaded();
|
|
|
|
this[index].SetAddin(null);
|
|
base.RemoveItem(index);
|
|
}
|
|
|
|
protected override void ClearItems()
|
|
{
|
|
m_addin.ThrowIfLoaded();
|
|
|
|
foreach (Module module in this)
|
|
{
|
|
module.SetAddin(null);
|
|
}
|
|
base.ClearItems();
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Variables
|
|
|
|
private readonly AddinManifest m_manifest;
|
|
private readonly Module m_mainModule = new Module();
|
|
private readonly ModuleCollection m_modules;
|
|
private readonly ExtensionPointCollection m_extensionPoints;
|
|
private readonly ExtensionTypeSetCollection m_extensionTypeSets;
|
|
private readonly NodeTypeCollection<ConditionType> m_conditionTypes;
|
|
|
|
private FileLocation? m_fileLocation;
|
|
|
|
private readonly List<string> m_partialAddinFilenames = new List<string>();
|
|
|
|
#endregion
|
|
|
|
#region Constructor
|
|
|
|
/// <summary>
|
|
/// Constructor.
|
|
/// </summary>
|
|
public Addin()
|
|
: this(null)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Constructor.
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
public Addin(string id)
|
|
{
|
|
m_manifest = new AddinManifest(this, id);
|
|
m_mainModule.SetAddin(this);
|
|
m_modules = new ModuleCollection(this);
|
|
m_extensionPoints = new ExtensionPointCollection(MainModule);
|
|
m_extensionTypeSets = new ExtensionTypeSetCollection(MainModule);
|
|
m_conditionTypes = new NodeTypeCollection<ConditionType>(MainModule);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Properties
|
|
|
|
/// <summary>
|
|
/// Full id of the addin.
|
|
/// </summary>
|
|
public string FullId
|
|
{
|
|
get
|
|
{
|
|
return Manifest.FullId;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns whether this addin is laoded.
|
|
/// </summary>
|
|
public bool IsLoaded
|
|
{
|
|
get
|
|
{
|
|
return AddinManager.IsAddinLoaded(this);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Manifest of this addin.
|
|
/// </summary>
|
|
public AddinManifest Manifest
|
|
{
|
|
get
|
|
{
|
|
return m_manifest;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Main module.
|
|
/// </summary>
|
|
public Module MainModule
|
|
{
|
|
get
|
|
{
|
|
return m_mainModule;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// All modules, including the main module.
|
|
/// </summary>
|
|
public IEnumerable<Module> AllModules
|
|
{
|
|
get
|
|
{
|
|
yield return MainModule;
|
|
foreach (Module module in Modules)
|
|
{
|
|
yield return module;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Optional modules of this addin.
|
|
/// </summary>
|
|
public IList<Module> Modules
|
|
{
|
|
get
|
|
{
|
|
return m_modules;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// <see cref="ExtensionPoint"/>s defined in this addin.
|
|
/// </summary>
|
|
public KeyedCollection<string, ExtensionPoint> ExtensionPoints
|
|
{
|
|
get
|
|
{
|
|
return m_extensionPoints;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// The <see cref="ExtensionTypeSet"/>s defined in this addin.
|
|
/// </summary>
|
|
public KeyedCollection<string, ExtensionTypeSet> ExtensionTypeSets
|
|
{
|
|
get
|
|
{
|
|
return m_extensionTypeSets;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// The <see cref="ConditionType"/>s defined in this addin.
|
|
/// </summary>
|
|
public KeyedCollection<string, ConditionType> ConditionTypes
|
|
{
|
|
get
|
|
{
|
|
return m_conditionTypes;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// File Location of the addin.
|
|
/// </summary>
|
|
public FileLocation? FileLocation
|
|
{
|
|
get
|
|
{
|
|
return m_fileLocation;
|
|
}
|
|
set
|
|
{
|
|
ThrowIfLoaded();
|
|
m_fileLocation = value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Filenames of the partial addins that contributed to this addin.
|
|
/// </summary>
|
|
public IList<string> PartialAddinFilenames
|
|
{
|
|
get
|
|
{
|
|
return m_partialAddinFilenames.AsReadOnly();
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Methods
|
|
|
|
internal void AddPartialAddinFilename(string filename)
|
|
{
|
|
m_partialAddinFilenames.Add(filename);
|
|
}
|
|
|
|
internal void ThrowIfLoaded()
|
|
{
|
|
Throw.InvalidOperationIf(IsLoaded, "Cannot modify an addin while it is loaded.");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Overriden.
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public override string ToString()
|
|
{
|
|
return
|
|
$"[{Manifest.FullId}{(IsLoaded ? string.Empty : " (not loaded)")}: {Modules.Count} optional modules, {ExtensionPoints.Count} extension points, {ExtensionTypeSets.Count} extension type sets]";
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|