mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-11 11:26:27 +00:00
Merge branch 'development' into VisualScripting
This commit is contained in:
commit
4f5d76de88
23 files changed
+554
-469
No files matched your search
@@ -261,7 +261,12 @@ namespace Artemis.Core.Modules
|
||||
internal T? GetDynamicChildValue<T>(string key)
|
||||
{
|
||||
if (TryGetDynamicChild(key, out DynamicChild? dynamicChild) && dynamicChild.BaseValue != null)
|
||||
return (T) dynamicChild.BaseValue;
|
||||
{
|
||||
if (dynamicChild.BaseValue is T value)
|
||||
return value;
|
||||
return default;
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
|
||||
@@ -240,7 +240,7 @@ namespace Artemis.Core.Modules
|
||||
/// <returns></returns>
|
||||
public virtual DataModelPropertyAttribute GetDataModelDescription()
|
||||
{
|
||||
return new DataModelPropertyAttribute {Name = Plugin.Info.Name, Description = Plugin.Info.Description};
|
||||
return new() {Name = Info.Name, Description = Info.Description};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -11,18 +11,14 @@ namespace Artemis.Core
|
||||
/// Represents a setting tied to a plugin of type <typeparamref name="T" />
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The value type of the setting</typeparam>
|
||||
public class PluginSetting<T> : CorePropertyChanged
|
||||
public class PluginSetting<T> : CorePropertyChanged, IPluginSetting
|
||||
{
|
||||
// TODO: Why? Should have included that...
|
||||
// ReSharper disable once NotAccessedField.Local
|
||||
private readonly Plugin _plugin;
|
||||
private readonly IPluginRepository _pluginRepository;
|
||||
private readonly PluginSettingEntity _pluginSettingEntity;
|
||||
private T _value;
|
||||
|
||||
internal PluginSetting(Plugin plugin, IPluginRepository pluginRepository, PluginSettingEntity pluginSettingEntity)
|
||||
internal PluginSetting(IPluginRepository pluginRepository, PluginSettingEntity pluginSettingEntity)
|
||||
{
|
||||
_plugin = plugin;
|
||||
_pluginRepository = pluginRepository;
|
||||
_pluginSettingEntity = pluginSettingEntity;
|
||||
|
||||
@@ -37,9 +33,7 @@ namespace Artemis.Core
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The name of the setting, unique to this plugin
|
||||
/// </summary>
|
||||
/// <inheritdoc />
|
||||
public string Name { get; }
|
||||
|
||||
/// <summary>
|
||||
@@ -63,28 +57,19 @@ namespace Artemis.Core
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the setting has been changed
|
||||
/// </summary>
|
||||
/// <inheritdoc />
|
||||
public bool HasChanged => CoreJson.SerializeObject(Value) != _pluginSettingEntity.Value;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets whether changes must automatically be saved
|
||||
/// <para>Note: When set to <c>true</c> <see cref="HasChanged" /> is always <c>false</c></para>
|
||||
/// </summary>
|
||||
/// <inheritdoc />
|
||||
public bool AutoSave { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Resets the setting to the last saved value
|
||||
/// </summary>
|
||||
/// <inheritdoc />
|
||||
public void RejectChanges()
|
||||
{
|
||||
Value = CoreJson.DeserializeObject<T>(_pluginSettingEntity.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves the setting
|
||||
/// </summary>
|
||||
/// <inheritdoc />
|
||||
public void Save()
|
||||
{
|
||||
if (!HasChanged)
|
||||
@@ -95,14 +80,10 @@ namespace Artemis.Core
|
||||
OnSettingSaved();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when the value of the setting has been changed
|
||||
/// </summary>
|
||||
/// <inheritdoc />
|
||||
public event EventHandler? SettingChanged;
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when the value of the setting has been saved
|
||||
/// </summary>
|
||||
/// <inheritdoc />
|
||||
public event EventHandler? SettingSaved;
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -127,4 +108,46 @@ namespace Artemis.Core
|
||||
SettingSaved?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a setting tied to a plugin
|
||||
/// </summary>
|
||||
public interface IPluginSetting
|
||||
{
|
||||
/// <summary>
|
||||
/// The name of the setting, unique to this plugin
|
||||
/// </summary>
|
||||
string Name { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the setting has been changed
|
||||
/// </summary>
|
||||
bool HasChanged { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets whether changes must automatically be saved
|
||||
/// <para>Note: When set to <c>true</c> <see cref="HasChanged" /> is always <c>false</c></para>
|
||||
/// </summary>
|
||||
bool AutoSave { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Resets the setting to the last saved value
|
||||
/// </summary>
|
||||
void RejectChanges();
|
||||
|
||||
/// <summary>
|
||||
/// Saves the setting
|
||||
/// </summary>
|
||||
void Save();
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when the value of the setting has been changed
|
||||
/// </summary>
|
||||
event EventHandler? SettingChanged;
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when the value of the setting has been saved
|
||||
/// </summary>
|
||||
event EventHandler? SettingSaved;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using Artemis.Storage.Entities.Plugins;
|
||||
using Artemis.Storage.Repositories.Interfaces;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Artemis.Core
|
||||
{
|
||||
@@ -12,15 +11,15 @@ namespace Artemis.Core
|
||||
public class PluginSettings
|
||||
{
|
||||
private readonly IPluginRepository _pluginRepository;
|
||||
private readonly Dictionary<string, object> _settingEntities;
|
||||
private readonly Dictionary<string, IPluginSetting> _settingEntities;
|
||||
|
||||
internal PluginSettings(Plugin plugin, IPluginRepository pluginRepository)
|
||||
{
|
||||
Plugin = plugin;
|
||||
Plugin.Settings = this;
|
||||
|
||||
|
||||
_pluginRepository = pluginRepository;
|
||||
_settingEntities = new Dictionary<string, object>();
|
||||
_settingEntities = new Dictionary<string, IPluginSetting>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -49,14 +48,14 @@ namespace Artemis.Core
|
||||
{
|
||||
settingEntity = new PluginSettingEntity
|
||||
{
|
||||
Name = name,
|
||||
PluginGuid = Plugin.Guid,
|
||||
Name = name,
|
||||
PluginGuid = Plugin.Guid,
|
||||
Value = CoreJson.SerializeObject(defaultValue)
|
||||
};
|
||||
_pluginRepository.AddSetting(settingEntity);
|
||||
}
|
||||
|
||||
PluginSetting<T> pluginSetting = new(Plugin, _pluginRepository, settingEntity);
|
||||
PluginSetting<T> pluginSetting = new(_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
|
||||
@@ -68,6 +67,15 @@ namespace Artemis.Core
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves all currently loaded settings
|
||||
/// </summary>
|
||||
public void SaveAllSettings()
|
||||
{
|
||||
foreach (var (_, pluginSetting) in _settingEntities)
|
||||
pluginSetting.Save();
|
||||
}
|
||||
|
||||
internal void ClearSettings()
|
||||
{
|
||||
_settingEntities.Clear();
|
||||
|
||||
Reference in new issue
Block a user