using System;
using System.Collections.Generic;
using System.Linq;
using Artemis.Storage.Entities.Plugins;
using Humanizer;
namespace Artemis.Core;
///
/// Represents basic info about a plugin feature and contains a reference to the instance of said feature
///
public class PluginFeatureInfo : IPrerequisitesSubject
{
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; internal set; }
///
/// The name of the feature
///
public string Name { get; }
///
/// A short description of the feature
///
public string? Description { get; }
///
/// 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
///
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; internal set; }
///
public List Prerequisites { get; } = [];
///
public IEnumerable PlatformPrerequisites => Prerequisites.Where(p => p.AppliesToPlatform());
///
public bool ArePrerequisitesMet()
{
return PlatformPrerequisites.All(p => p.IsMet());
}
///
public override string ToString()
{
return Instance?.Id ?? "Uninitialized feature";
}
///
/// Returns a boolean indicating whether this feature info matches the provided search string
///
/// The search string to match
/// A boolean indicating whether this plugin info matches the provided search string
public bool MatchesSearch(string search)
{
return Name.Contains(search, StringComparison.InvariantCultureIgnoreCase) ||
(Description != null && Description.Contains(search, StringComparison.InvariantCultureIgnoreCase));
}
internal PluginFeatureEntity Entity { get; }
}