From c93a743e249c49695c39bb83a9f1dbfb69ca07a5 Mon Sep 17 00:00:00 2001 From: SpoinkyNL Date: Tue, 13 Oct 2020 23:22:26 +0200 Subject: [PATCH] Plugin settings - Fixed settings not being singletons Data model visualization - Order properties by their declaration --- .../Ninject/PluginSettingsProvider.cs | 17 +++++++++--- .../Plugins/DataModelExpansions/DataModel.cs | 2 +- .../Plugins/PluginUpdateRegistration.cs | 27 ++++++++++--------- .../Plugins/Settings/PluginSettings.cs | 14 ++++++---- .../Shared/DataModelVisualizationViewModel.cs | 4 +-- 5 files changed, 40 insertions(+), 24 deletions(-) diff --git a/src/Artemis.Core/Ninject/PluginSettingsProvider.cs b/src/Artemis.Core/Ninject/PluginSettingsProvider.cs index 1a72cd8ff..d8ea16a06 100644 --- a/src/Artemis.Core/Ninject/PluginSettingsProvider.cs +++ b/src/Artemis.Core/Ninject/PluginSettingsProvider.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System.Collections.Generic; +using System.Linq; using Artemis.Core.Services; using Artemis.Storage.Repositories.Interfaces; using Ninject.Activation; @@ -7,6 +8,7 @@ namespace Artemis.Core.Ninject { internal class PluginSettingsProvider : Provider { + private static readonly List PluginSettings = new List(); private readonly IPluginRepository _pluginRepository; private readonly IPluginService _pluginService; @@ -28,12 +30,19 @@ namespace Artemis.Core.Ninject pluginInfo = _pluginService.GetPluginByAssembly(parentRequest.Service.Assembly)?.PluginInfo; // Fall back to assembly based detection if (pluginInfo == null) - { throw new ArtemisCoreException("PluginSettings can only be injected with the PluginInfo parameter provided " + "or into a class defined in a plugin assembly"); - } - return new PluginSettings(pluginInfo, _pluginRepository); + lock (PluginSettings) + { + PluginSettings? existingSettings = PluginSettings.FirstOrDefault(p => p.PluginInfo == pluginInfo); + if (existingSettings != null) + return existingSettings; + + PluginSettings? settings = new PluginSettings(pluginInfo, _pluginRepository); + PluginSettings.Add(settings); + return settings; + } } } } \ No newline at end of file diff --git a/src/Artemis.Core/Plugins/DataModelExpansions/DataModel.cs b/src/Artemis.Core/Plugins/DataModelExpansions/DataModel.cs index b6d68045d..3e8647d4d 100644 --- a/src/Artemis.Core/Plugins/DataModelExpansions/DataModel.cs +++ b/src/Artemis.Core/Plugins/DataModelExpansions/DataModel.cs @@ -136,7 +136,7 @@ namespace Artemis.Core.DataModelExpansions /// The type of data model you expect /// The unique key of the dynamic data model /// If found, the dynamic data model otherwise null - public T DynamicChild(string key) where T : DataModel + public T? DynamicChild(string key) where T : DataModel { _dynamicDataModels.TryGetValue(key, out DataModel value); return value as T; diff --git a/src/Artemis.Core/Plugins/PluginUpdateRegistration.cs b/src/Artemis.Core/Plugins/PluginUpdateRegistration.cs index 9d658556c..9dcc035ca 100644 --- a/src/Artemis.Core/Plugins/PluginUpdateRegistration.cs +++ b/src/Artemis.Core/Plugins/PluginUpdateRegistration.cs @@ -102,19 +102,22 @@ namespace Artemis.Core if (!PluginInfo.Instance.Enabled) return; - TimeSpan interval = DateTime.Now - _lastEvent; - _lastEvent = DateTime.Now; - - // Modules don't always want to update, honor that - if (PluginInfo.Instance is Module module && !module.IsUpdateAllowed) - return; - - if (Action != null) - Action(interval.TotalSeconds); - else if (AsyncAction != null) + lock (this) { - Task task = AsyncAction(interval.TotalSeconds); - task.Wait(); + TimeSpan interval = DateTime.Now - _lastEvent; + _lastEvent = DateTime.Now; + + // Modules don't always want to update, honor that + if (PluginInfo.Instance is Module module && !module.IsUpdateAllowed) + return; + + if (Action != null) + Action(interval.TotalSeconds); + else if (AsyncAction != null) + { + Task task = AsyncAction(interval.TotalSeconds); + task.Wait(); + } } } diff --git a/src/Artemis.Core/Plugins/Settings/PluginSettings.cs b/src/Artemis.Core/Plugins/Settings/PluginSettings.cs index 28eaac4e0..f47f50a68 100644 --- a/src/Artemis.Core/Plugins/Settings/PluginSettings.cs +++ b/src/Artemis.Core/Plugins/Settings/PluginSettings.cs @@ -11,17 +11,21 @@ namespace Artemis.Core /// public class PluginSettings { - private readonly PluginInfo _pluginInfo; private readonly IPluginRepository _pluginRepository; private readonly Dictionary _settingEntities; internal PluginSettings(PluginInfo pluginInfo, IPluginRepository pluginRepository) { - _pluginInfo = pluginInfo; + PluginInfo = pluginInfo; _pluginRepository = pluginRepository; _settingEntities = new Dictionary(); } + /// + /// Gets the info of the plugin this setting belongs to + /// + public PluginInfo PluginInfo { get; } + /// /// Gets the setting with the provided name. If the setting does not exist yet, it is created. /// @@ -37,15 +41,15 @@ namespace Artemis.Core if (_settingEntities.ContainsKey(name)) return (PluginSetting) _settingEntities[name]; // Try to find in database - PluginSettingEntity settingEntity = _pluginRepository.GetSettingByNameAndGuid(name, _pluginInfo.Guid); + PluginSettingEntity settingEntity = _pluginRepository.GetSettingByNameAndGuid(name, PluginInfo.Guid); // If not found, create a new one if (settingEntity == null) { - settingEntity = new PluginSettingEntity {Name = name, PluginGuid = _pluginInfo.Guid, Value = JsonConvert.SerializeObject(defaultValue)}; + settingEntity = new PluginSettingEntity {Name = name, PluginGuid = PluginInfo.Guid, Value = JsonConvert.SerializeObject(defaultValue)}; _pluginRepository.AddSetting(settingEntity); } - PluginSetting pluginSetting = new PluginSetting(_pluginInfo, _pluginRepository, settingEntity); + PluginSetting pluginSetting = new PluginSetting(PluginInfo, _pluginRepository, settingEntity); // This overrides null with the default value, I'm not sure if that's desirable because you // might expect something to go null and you might not diff --git a/src/Artemis.UI.Shared/DataModelVisualization/Shared/DataModelVisualizationViewModel.cs b/src/Artemis.UI.Shared/DataModelVisualization/Shared/DataModelVisualizationViewModel.cs index 8fee846e5..4b490c686 100644 --- a/src/Artemis.UI.Shared/DataModelVisualization/Shared/DataModelVisualizationViewModel.cs +++ b/src/Artemis.UI.Shared/DataModelVisualization/Shared/DataModelVisualizationViewModel.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; @@ -188,7 +188,7 @@ namespace Artemis.UI.Shared Type modelType = Parent == null || Parent.IsRootViewModel ? DataModel.GetType() : DataModelPath.GetPropertyType(); // Add missing static children - foreach (PropertyInfo propertyInfo in modelType.GetProperties(BindingFlags.Public | BindingFlags.Instance)) + foreach (PropertyInfo propertyInfo in modelType.GetProperties(BindingFlags.Public | BindingFlags.Instance).OrderBy(t => t.MetadataToken)) { string childPath = AppendToPath(propertyInfo.Name); if (Children.Any(c => c.Path != null && c.Path.Equals(childPath)))