Plugins - Adjusted namespaces

This commit is contained in:
SpoinkyNL committed 2020-08-21 22:32:46 +02:00
1 parent 625fcbafdd
commit e27f658cea
144 files changed
+267 -252

No files matched your search

@@ -0,0 +1,93 @@
using System;
using Artemis.Storage.Entities.Plugins;
using Artemis.Storage.Repositories.Interfaces;
using Newtonsoft.Json;
using Stylet;
namespace Artemis.Core.Plugins.Settings
{
public class PluginSetting<T> : PropertyChangedBase
{
// ReSharper disable once NotAccessedField.Local
private readonly PluginInfo _pluginInfo;
private readonly IPluginRepository _pluginRepository;
private readonly PluginSettingEntity _pluginSettingEntity;
private T _value;
internal PluginSetting(PluginInfo pluginInfo, IPluginRepository pluginRepository, PluginSettingEntity pluginSettingEntity)
{
_pluginInfo = pluginInfo;
_pluginRepository = pluginRepository;
_pluginSettingEntity = pluginSettingEntity;
Name = pluginSettingEntity.Name;
try
{
Value = JsonConvert.DeserializeObject<T>(pluginSettingEntity.Value);
}
catch (JsonReaderException)
{
Value = default;
}
}
/// <summary>
/// The name of the setting, unique to this plugin
/// </summary>
public string Name { get; }
/// <summary>
/// The value of the setting
/// </summary>
public T Value
{
get => _value;
set
{
if (!Equals(_value, value))
{
_value = value;
OnSettingChanged();
NotifyOfPropertyChange(nameof(Value));
}
}
}
/// <summary>
/// Determines whether the setting has been changed
/// </summary>
public bool HasChanged => JsonConvert.SerializeObject(Value) != _pluginSettingEntity.Value;
/// <summary>
/// Resets the setting to the last saved value
/// </summary>
public void RejectChanges()
{
Value = JsonConvert.DeserializeObject<T>(_pluginSettingEntity.Value);
}
/// <summary>
/// Saves the setting
/// </summary>
public void Save()
{
if (!HasChanged)
return;
_pluginSettingEntity.Value = JsonConvert.SerializeObject(Value);
_pluginRepository.SaveSetting(_pluginSettingEntity);
}
public event EventHandler<EventArgs> SettingChanged;
public override string ToString()
{
return $"{nameof(Name)}: {Name}, {nameof(Value)}: {Value}, {nameof(HasChanged)}: {HasChanged}";
}
protected virtual void OnSettingChanged()
{
SettingChanged?.Invoke(this, EventArgs.Empty);
}
}
}
@@ -0,0 +1,60 @@
using System.Collections.Generic;
using Artemis.Storage.Entities.Plugins;
using Artemis.Storage.Repositories.Interfaces;
using Newtonsoft.Json;
namespace Artemis.Core.Plugins.Settings
{
/// <summary>
/// <para>This contains all the settings for your plugin. To access a setting use <see cref="GetSetting{T}" />.</para>
/// <para>To use this class, inject it into the constructor of your plugin.</para>
/// </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;
_pluginRepository = pluginRepository;
_settingEntities = new Dictionary<string, object>();
}
/// <summary>
/// Gets the setting with the provided name. If the setting does not exist yet, it is created.
/// </summary>
/// <typeparam name="T">The type of the setting, can be any serializable type</typeparam>
/// <param name="name">The name of the setting</param>
/// <param name="defaultValue">The default value to use if the setting does not exist yet</param>
/// <returns></returns>
public PluginSetting<T> GetSetting<T>(string name, T defaultValue = default)
{
lock (_settingEntities)
{
// Return cached value if available
if (_settingEntities.ContainsKey(name))
return (PluginSetting<T>) _settingEntities[name];
// Try to find in database
var 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)};
_pluginRepository.AddSetting(settingEntity);
}
var 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
// if (pluginSetting.Value == null && defaultValue != null)
// pluginSetting.Value = defaultValue;
_settingEntities.Add(name, pluginSetting);
return pluginSetting;
}
}
}
}