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.Storage.Repositories.Interfaces;
using Ninject.Activation;
@ -7,6 +8,7 @@ namespace Artemis.Core.Ninject
{
internal class PluginSettingsProvider : Provider<PluginSettings>
{
private static readonly List<PluginSettings> PluginSettings = new List<PluginSettings>();
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;
}
}
}
}

View File

@ -136,7 +136,7 @@ namespace Artemis.Core.DataModelExpansions
/// <typeparam name="T">The type of data model you expect</typeparam>
/// <param name="key">The unique key of the dynamic data model</param>
/// <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);
return value as T;

View File

@ -102,6 +102,8 @@ namespace Artemis.Core
if (!PluginInfo.Instance.Enabled)
return;
lock (this)
{
TimeSpan interval = DateTime.Now - _lastEvent;
_lastEvent = DateTime.Now;
@ -117,6 +119,7 @@ namespace Artemis.Core
task.Wait();
}
}
}
private void InstanceOnPluginEnabled(object sender, EventArgs e)
{

View File

@ -11,17 +11,21 @@ namespace Artemis.Core
/// </summary>
public class PluginSettings
{
private readonly PluginInfo _pluginInfo;
private readonly IPluginRepository _pluginRepository;
private readonly Dictionary<string, object> _settingEntities;
internal PluginSettings(PluginInfo pluginInfo, IPluginRepository pluginRepository)
{
_pluginInfo = pluginInfo;
PluginInfo = pluginInfo;
_pluginRepository = pluginRepository;
_settingEntities = new Dictionary<string, object>();
}
/// <summary>
/// Gets the info of the plugin this setting belongs to
/// </summary>
public PluginInfo PluginInfo { get; }
/// <summary>
/// Gets the setting with the provided name. If the setting does not exist yet, it is created.
/// </summary>
@ -37,15 +41,15 @@ namespace Artemis.Core
if (_settingEntities.ContainsKey(name))
return (PluginSetting<T>) _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<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
// 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.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)))