Plugin settings - Fixed settings not being singletons

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

No files matched your search

@@ -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;
@@ -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();
}
}
}
@@ -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