131 lines
4 KiB
C#
131 lines
4 KiB
C#
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using JD.Collections;
|
|
using JD.TeaBoxAddinSystem.Addins.Description;
|
|
|
|
namespace JD.TeaBoxAddinSystem.Addins.Internal
|
|
{
|
|
/// <summary>
|
|
/// Dependency manager.
|
|
/// </summary>
|
|
internal class DependencyManager
|
|
{
|
|
#region Variables
|
|
|
|
private readonly MultiDictionary<string, Module> m_dependencies = new MultiDictionary<string, Module>();
|
|
|
|
#endregion
|
|
|
|
#region Methods
|
|
|
|
internal void RegisterModule(Module module)
|
|
{
|
|
foreach (AddinReference reference in module.Dependencies)
|
|
{
|
|
AddDependency(module, reference.Name);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a dependency of <paramref name="module"/> towards the given <paramref name="addinId"/>.
|
|
/// </summary>
|
|
/// <param name="module"></param>
|
|
/// <param name="addinId"></param>
|
|
private void AddDependency(Module module, string addinId)
|
|
{
|
|
Debug.Assert(module != null);
|
|
Debug.Assert(!string.IsNullOrEmpty(addinId));
|
|
Debug.Assert(!m_dependencies.Contains(addinId, module));
|
|
m_dependencies.Add(addinId, module);
|
|
}
|
|
|
|
internal void UnregisterModule(Module module)
|
|
{
|
|
foreach (AddinReference reference in module.Dependencies)
|
|
{
|
|
RemoveDependency(module, reference.Name);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes a dependency of <paramref name="module"/> towards the given <paramref name="addinId"/>.
|
|
/// </summary>
|
|
/// <param name="module"></param>
|
|
/// <param name="addinId"></param>
|
|
private void RemoveDependency(Module module, string addinId)
|
|
{
|
|
Debug.Assert(module != null);
|
|
Debug.Assert(!string.IsNullOrEmpty(addinId));
|
|
Debug.Assert(m_dependencies.Contains(addinId, module));
|
|
m_dependencies.Remove(addinId, module);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clears all dependency info.
|
|
/// </summary>
|
|
internal void Clear()
|
|
{
|
|
m_dependencies.Clear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets all the dependencies (weak and strong) on the given <paramref name="addinId"/>.
|
|
/// </summary>
|
|
/// <param name="addinId"></param>
|
|
/// <returns></returns>
|
|
public IEnumerable<Module> GetDependencies(string addinId)
|
|
{
|
|
Debug.Assert(!string.IsNullOrEmpty(addinId));
|
|
return m_dependencies[addinId];
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the modules that depend weakly on the given <paramref name="addinId"/>.
|
|
/// </summary>
|
|
/// <param name="addinId"></param>
|
|
/// <returns></returns>
|
|
public IEnumerable<Module> GetWeakDependencies(string addinId)
|
|
{
|
|
foreach (Module module in GetDependencies(addinId))
|
|
{
|
|
if (!module.IsMainModule)
|
|
{
|
|
yield return module;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the modules that depend strongly on the given <paramref name="addinId"/>.
|
|
/// </summary>
|
|
/// <param name="addinId"></param>
|
|
/// <returns></returns>
|
|
public IEnumerable<Module> GetStrongDependencies(string addinId)
|
|
{
|
|
foreach (Module module in GetDependencies(addinId))
|
|
{
|
|
if (module.IsMainModule)
|
|
{
|
|
yield return module;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets whether there is an addin that depends on the given <paramref name="addinId"/>.
|
|
/// </summary>
|
|
/// <param name="addinId"></param>
|
|
/// <returns></returns>
|
|
public bool HasStrongDependencies(string addinId)
|
|
{
|
|
foreach (Module module in GetStrongDependencies(addinId))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|