// // Licensed under the MIT License. See LICENSE file in the project root for full license information. // namespace MUnique.OpenMU.GameLogic; using System.Collections.Concurrent; using MUnique.OpenMU.PlugIns; /// /// A plugin container for s. /// public class FeaturePlugInContainer : PlugInContainerBase, ICustomPlugInContainer { private readonly ConcurrentDictionary _currentlyEffectivePlugIns = new(); /// /// Initializes a new instance of the class. /// /// The plugin manager which manages this instance. public FeaturePlugInContainer(PlugInManager manager) : base(manager) { foreach (var plugIn in this.Manager.GetActivePlugInsOf()) { if (!this._currentlyEffectivePlugIns.ContainsKey(plugIn.GetType())) { this.AddPlugIn(plugIn, true); } } } /// public T? GetPlugIn() where T : class, IFeaturePlugIn { if (this._currentlyEffectivePlugIns.TryGetValue(typeof(T), out var plugIn) && plugIn is T t) { return t; } return default; } /// protected override void ActivatePlugIn(IFeaturePlugIn plugIn) { var plugInType = plugIn.GetType(); if (this._currentlyEffectivePlugIns.ContainsKey(plugInType)) { return; } base.ActivatePlugIn(plugIn); this._currentlyEffectivePlugIns.TryAdd(plugInType, plugIn); } /// protected override void DeactivatePlugIn(IFeaturePlugIn plugIn) { base.DeactivatePlugIn(plugIn); this._currentlyEffectivePlugIns.TryRemove(plugIn.GetType(), out _); } /// protected override void BeforeActivatePlugInType(Type plugInType) { base.BeforeActivatePlugInType(plugInType); var knownPlugIn = this.FindKnownPlugin(plugInType); if (knownPlugIn is null) { this.AddPlugIn((IFeaturePlugIn)Activator.CreateInstance(plugInType)!, true); } } }