1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Plugin settings - Fixed settings not being singletons

Data model visualization - Order properties by their declaration
This commit is contained in:
SpoinkyNL 2020-10-13 23:22:26 +02:00
parent ef62e30c6f
commit c93a743e24
5 changed files with 40 additions and 24 deletions

View File

@ -1,4 +1,5 @@
using System.Linq; using System.Collections.Generic;
using System.Linq;
using Artemis.Core.Services; using Artemis.Core.Services;
using Artemis.Storage.Repositories.Interfaces; using Artemis.Storage.Repositories.Interfaces;
using Ninject.Activation; using Ninject.Activation;
@ -7,6 +8,7 @@ namespace Artemis.Core.Ninject
{ {
internal class PluginSettingsProvider : Provider<PluginSettings> internal class PluginSettingsProvider : Provider<PluginSettings>
{ {
private static readonly List<PluginSettings> PluginSettings = new List<PluginSettings>();
private readonly IPluginRepository _pluginRepository; private readonly IPluginRepository _pluginRepository;
private readonly IPluginService _pluginService; private readonly IPluginService _pluginService;
@ -28,12 +30,19 @@ namespace Artemis.Core.Ninject
pluginInfo = _pluginService.GetPluginByAssembly(parentRequest.Service.Assembly)?.PluginInfo; pluginInfo = _pluginService.GetPluginByAssembly(parentRequest.Service.Assembly)?.PluginInfo;
// Fall back to assembly based detection // Fall back to assembly based detection
if (pluginInfo == null) if (pluginInfo == null)
{
throw new ArtemisCoreException("PluginSettings can only be injected with the PluginInfo parameter provided " + throw new ArtemisCoreException("PluginSettings can only be injected with the PluginInfo parameter provided " +
"or into a class defined in a plugin assembly"); "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;
}
} }
} }
} }

View File

@ -136,7 +136,7 @@ namespace Artemis.Core.DataModelExpansions
/// <typeparam name="T">The type of data model you expect</typeparam> /// <typeparam name="T">The type of data model you expect</typeparam>
/// <param name="key">The unique key of the dynamic data model</param> /// <param name="key">The unique key of the dynamic data model</param>
/// <returns>If found, the dynamic data model otherwise <c>null</c></returns> /// <returns>If found, the dynamic data model otherwise <c>null</c></returns>
public T DynamicChild<T>(string key) where T : DataModel public T? DynamicChild<T>(string key) where T : DataModel
{ {
_dynamicDataModels.TryGetValue(key, out DataModel value); _dynamicDataModels.TryGetValue(key, out DataModel value);
return value as T; return value as T;

View File

@ -102,19 +102,22 @@ namespace Artemis.Core
if (!PluginInfo.Instance.Enabled) if (!PluginInfo.Instance.Enabled)
return; return;
TimeSpan interval = DateTime.Now - _lastEvent; lock (this)
_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); TimeSpan interval = DateTime.Now - _lastEvent;
task.Wait(); _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();
}
} }
} }

View File

@ -11,17 +11,21 @@ namespace Artemis.Core
/// </summary> /// </summary>
public class PluginSettings public class PluginSettings
{ {
private readonly PluginInfo _pluginInfo;
private readonly IPluginRepository _pluginRepository; private readonly IPluginRepository _pluginRepository;
private readonly Dictionary<string, object> _settingEntities; private readonly Dictionary<string, object> _settingEntities;
internal PluginSettings(PluginInfo pluginInfo, IPluginRepository pluginRepository) internal PluginSettings(PluginInfo pluginInfo, IPluginRepository pluginRepository)
{ {
_pluginInfo = pluginInfo; PluginInfo = pluginInfo;
_pluginRepository = pluginRepository; _pluginRepository = pluginRepository;
_settingEntities = new Dictionary<string, object>(); _settingEntities = new Dictionary<string, object>();
} }
/// <summary>
/// Gets the info of the plugin this setting belongs to
/// </summary>
public PluginInfo PluginInfo { get; }
/// <summary> /// <summary>
/// Gets the setting with the provided name. If the setting does not exist yet, it is created. /// Gets the setting with the provided name. If the setting does not exist yet, it is created.
/// </summary> /// </summary>
@ -37,15 +41,15 @@ namespace Artemis.Core
if (_settingEntities.ContainsKey(name)) if (_settingEntities.ContainsKey(name))
return (PluginSetting<T>) _settingEntities[name]; return (PluginSetting<T>) _settingEntities[name];
// Try to find in database // 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 not found, create a new one
if (settingEntity == null) 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); _pluginRepository.AddSetting(settingEntity);
} }
PluginSetting<T> pluginSetting = new PluginSetting<T>(_pluginInfo, _pluginRepository, settingEntity); PluginSetting<T> pluginSetting = new PluginSetting<T>(PluginInfo, _pluginRepository, settingEntity);
// This overrides null with the default value, I'm not sure if that's desirable because you // 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 // might expect something to go null and you might not

View File

@ -1,4 +1,4 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
@ -188,7 +188,7 @@ namespace Artemis.UI.Shared
Type modelType = Parent == null || Parent.IsRootViewModel ? DataModel.GetType() : DataModelPath.GetPropertyType(); Type modelType = Parent == null || Parent.IsRootViewModel ? DataModel.GetType() : DataModelPath.GetPropertyType();
// Add missing static children // 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); string childPath = AppendToPath(propertyInfo.Name);
if (Children.Any(c => c.Path != null && c.Path.Equals(childPath))) if (Children.Any(c => c.Path != null && c.Path.Equals(childPath)))