using System; using System.Collections.Generic; using System.Linq; using Artemis.Core.DeviceProviders; using Artemis.Core.LayerBrushes; using Artemis.Core.LayerEffects; using Artemis.Core.Modules; using Artemis.Storage.Entities.Plugins; using Humanizer; using Newtonsoft.Json; namespace Artemis.Core; /// /// Represents basic info about a plugin feature and contains a reference to the instance of said feature /// [JsonObject(MemberSerialization.OptIn)] public class PluginFeatureInfo : CorePropertyChanged, IPrerequisitesSubject { private string? _description; private string? _icon; private PluginFeature? _instance; private Exception? _loadException; private string _name = null!; internal PluginFeatureInfo(Plugin plugin, Type featureType, PluginFeatureEntity pluginFeatureEntity, PluginFeatureAttribute? attribute) { Plugin = plugin ?? throw new ArgumentNullException(nameof(plugin)); FeatureType = featureType ?? throw new ArgumentNullException(nameof(featureType)); Entity = pluginFeatureEntity; Name = attribute?.Name ?? featureType.Name.Humanize(LetterCasing.Title); Description = attribute?.Description; AlwaysEnabled = attribute?.AlwaysEnabled ?? false; } internal PluginFeatureInfo(Plugin plugin, PluginFeatureAttribute? attribute, PluginFeature instance) { if (instance == null) throw new ArgumentNullException(nameof(instance)); Plugin = plugin ?? throw new ArgumentNullException(nameof(plugin)); FeatureType = instance.GetType(); Entity = new PluginFeatureEntity(); Name = attribute?.Name ?? instance.GetType().Name.Humanize(LetterCasing.Title); Description = attribute?.Description; AlwaysEnabled = attribute?.AlwaysEnabled ?? false; Instance = instance; } /// /// Gets the plugin this feature info is associated with /// public Plugin Plugin { get; } /// /// Gets the type of the feature /// public Type FeatureType { get; } /// /// Gets the exception thrown while loading /// public Exception? LoadException { get => _loadException; internal set => SetAndNotify(ref _loadException, value); } /// /// The name of the feature /// [JsonProperty(Required = Required.Always)] public string Name { get => _name; internal set => SetAndNotify(ref _name, value); } /// /// A short description of the feature /// [JsonProperty] public string? Description { get => _description; set => SetAndNotify(ref _description, value); } /// /// Marks the feature to always be enabled as long as the plugin is enabled and cannot be disabled. /// Note: always if this is the plugin's only feature /// [JsonProperty] public bool AlwaysEnabled { get; internal set; } /// /// Gets a boolean indicating whether the feature is enabled in persistent storage /// public bool EnabledInStorage => Entity.IsEnabled; /// /// Gets the feature this info is associated with /// Note: if the associated is disabled /// public PluginFeature? Instance { get => _instance; internal set => SetAndNotify(ref _instance, value); } internal PluginFeatureEntity Entity { get; } /// public override string ToString() { return Instance?.Id ?? "Uninitialized feature"; } /// public List Prerequisites { get; } = new(); /// public IEnumerable PlatformPrerequisites => Prerequisites.Where(p => p.AppliesToPlatform()); /// public bool ArePrerequisitesMet() { return PlatformPrerequisites.All(p => p.IsMet()); } }