Plugins - Fixed GetFeature<T> always returning null

Plugins - Added AlwaysEnabled property to [PluginFeature] attribute
This commit is contained in:
Robert committed 2021-03-26 19:39:48 +01:00
1 parent 0a7c724de0
commit 5545bdb783
7 files changed
+72 -19

No files matched your search

+6 -1
View File
@@ -107,7 +107,7 @@ namespace Artemis.Core
/// <returns>If found, the instance of the feature</returns>
public T? GetFeature<T>() where T : PluginFeature
{
return _features.FirstOrDefault(i => i.Instance is T) as T;
return _features.FirstOrDefault(i => i.Instance is T)?.Instance as T;
}
/// <inheritdoc />
@@ -164,6 +164,11 @@ namespace Artemis.Core
}
}
internal bool HasEnabledFeatures()
{
return Entity.Features.Any(f => f.IsEnabled) || Features.Any(f => f.AlwaysEnabled);
}
#region IDisposable
/// <summary>
@@ -23,5 +23,10 @@ namespace Artemis.Core
/// available icons
/// </summary>
public string? Icon { get; set; }
/// <summary>
/// Marks the feature to always be enabled as long as the plugin is enabled
/// </summary>
public bool AlwaysEnabled { get; set; }
}
}
}
@@ -28,7 +28,8 @@ namespace Artemis.Core
Name = attribute?.Name ?? featureType.Name.Humanize(LetterCasing.Title);
Description = attribute?.Description;
Icon = attribute?.Icon;
AlwaysEnabled = attribute?.AlwaysEnabled ?? false;
if (Icon != null) return;
if (typeof(BaseDataModelExpansion).IsAssignableFrom(featureType))
Icon = "TableAdd";
@@ -55,6 +56,7 @@ namespace Artemis.Core
Name = attribute?.Name ?? instance.GetType().Name.Humanize(LetterCasing.Title);
Description = attribute?.Description;
Icon = attribute?.Icon;
AlwaysEnabled = attribute?.AlwaysEnabled ?? false;
Instance = instance;
if (Icon != null) return;
@@ -111,6 +113,12 @@ namespace Artemis.Core
set => SetAndNotify(ref _icon, value);
}
/// <summary>
/// Marks the feature to always be enabled as long as the plugin is enabled and cannot be disabled
/// </summary>
[JsonProperty]
public bool AlwaysEnabled { get; }
/// <summary>
/// Gets the feature this info is associated with
/// </summary>
@@ -207,7 +207,7 @@ namespace Artemis.Core.Services
// ReSharper disable InconsistentlySynchronizedField - It's read-only, idc
_logger.Debug("Loaded {count} plugin(s)", _plugins.Count);
bool adminRequired = _plugins.Any(p => p.Info.RequiresAdmin && p.Entity.IsEnabled && p.Entity.Features.Any(f => f.IsEnabled));
bool adminRequired = _plugins.Any(p => p.Info.RequiresAdmin && p.Entity.IsEnabled && p.HasEnabledFeatures());
if (!isElevated && adminRequired)
{
_logger.Information("Restarting because one or more plugins requires elevation");
@@ -340,7 +340,7 @@ namespace Artemis.Core.Services
if (plugin.Assembly == null)
throw new ArtemisPluginException(plugin, "Cannot enable a plugin that hasn't successfully been loaded");
if (plugin.Info.RequiresAdmin && plugin.Entity.Features.Any(f => f.IsEnabled) && !_isElevated)
if (plugin.Info.RequiresAdmin && plugin.HasEnabledFeatures() && !_isElevated)
{
if (!saveState)
throw new ArtemisCoreException("Cannot enable a plugin that requires elevation without saving it's state.");
@@ -387,7 +387,7 @@ namespace Artemis.Core.Services
}
// Activate features after they are all loaded
foreach (PluginFeatureInfo pluginFeature in plugin.Features.Where(f => f.Instance != null && f.Instance.Entity.IsEnabled))
foreach (PluginFeatureInfo pluginFeature in plugin.Features.Where(f => f.Instance != null && (f.Instance.Entity.IsEnabled || f.AlwaysEnabled)))
{
try
{