// // Licensed under the MIT License. See LICENSE file in the project root for full license information. // namespace MUnique.OpenMU.PlugIns; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Reflection; using System.Runtime.CompilerServices; using System.Text.Json.Serialization; /// /// Configuration for plugins. /// public class PlugInConfiguration : INotifyPropertyChanged { private bool _isActive; private string? _customConfiguration; /// public event PropertyChangedEventHandler? PropertyChanged; /// /// Gets or sets the type identifier of the plugin. /// public Guid TypeId { get; set; } /// /// Gets or sets a value indicating whether the plugin is active. /// public bool IsActive { get => this._isActive; set { if (value == this._isActive) { return; } this._isActive = value; this.OnPropertyChanged(); } } /// /// Gets or sets the custom plug in source which will be compiled at run-time. /// public string? CustomPlugInSource { get; set; } /// /// Gets or sets the name of the external assembly which will be loaded at run-time. /// public string? ExternalAssemblyName { get; set; } /// /// Gets or sets a custom configuration. /// public string? CustomConfiguration { get => this._customConfiguration; set { if (value == this._customConfiguration) { return; } this._customConfiguration = value; this.OnPropertyChanged(); } } /// /// Gets the (display) name of this plugin. /// [JsonIgnore] public string Name { get { var plugInType = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(GetTypesSafely) .FirstOrDefault(t => t.GUID == this.TypeId); var plugInAttribute = plugInType?.GetCustomAttribute(inherit: false); return plugInAttribute?.GetName() ?? this.TypeId.ToString(); } } /// public override string ToString() { return this.Name; } /// /// Triggers the event. /// /// The name of the changed property. protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null) { this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } private static IEnumerable GetTypesSafely(Assembly assembly) { try { return assembly.DefinedTypes; } catch (ReflectionTypeLoadException ex) { return ex.Types.Where(t => t != null).Select(t => t!.GetTypeInfo()); } catch { return Enumerable.Empty(); } } }