Merge branch 'development' into VisualScripting

This commit is contained in:
Robert committed 2021-09-30 16:56:19 +02:00
commit 4f5d76de88
23 files changed
+554 -469

No files matched your search

@@ -457,6 +457,7 @@ namespace Artemis.Core
{
// Other properties are computed
DeviceEntity.Id = Identifier;
DeviceEntity.DeviceProvider = DeviceProvider.Plugin.Guid.ToString();
DeviceEntity.InputIdentifiers.Clear();
foreach (ArtemisDeviceInputIdentifier identifier in InputIdentifiers)
@@ -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;
}
+1 -1
View File
@@ -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();
@@ -10,6 +10,7 @@ using Artemis.Core.DeviceProviders;
using Artemis.Core.Ninject;
using Artemis.Storage.Entities.General;
using Artemis.Storage.Entities.Plugins;
using Artemis.Storage.Entities.Surface;
using Artemis.Storage.Repositories.Interfaces;
using McMaster.NETCore.Plugins;
using Ninject;
@@ -28,15 +29,17 @@ namespace Artemis.Core.Services
private readonly IKernel _kernel;
private readonly ILogger _logger;
private readonly IPluginRepository _pluginRepository;
private readonly IDeviceRepository _deviceRepository;
private readonly IQueuedActionRepository _queuedActionRepository;
private readonly List<Plugin> _plugins;
private bool _isElevated;
public PluginManagementService(IKernel kernel, ILogger logger, IPluginRepository pluginRepository, IQueuedActionRepository queuedActionRepository)
public PluginManagementService(IKernel kernel, ILogger logger, IPluginRepository pluginRepository, IDeviceRepository deviceRepository, IQueuedActionRepository queuedActionRepository)
{
_kernel = kernel;
_logger = logger;
_pluginRepository = pluginRepository;
_deviceRepository = deviceRepository;
_queuedActionRepository = queuedActionRepository;
_plugins = new List<Plugin>();
@@ -572,7 +575,11 @@ namespace Artemis.Core.Services
{
if (plugin.IsEnabled)
throw new ArtemisCoreException("Cannot remove the settings of an enabled plugin");
_pluginRepository.RemoveSettings(plugin.Guid);
foreach (DeviceEntity deviceEntity in _deviceRepository.GetAll().Where(e => e.DeviceProvider == plugin.Guid.ToString()))
_deviceRepository.Remove(deviceEntity);
plugin.Settings?.ClearSettings();
}
+1 -1
View File
@@ -35,7 +35,7 @@ namespace Artemis.Core.Services
_logger = logger;
_pluginManagementService = pluginManagementService;
_deviceRepository = deviceRepository;
_targetFrameRateSetting = settingsService.GetSetting("Core.TargetFrameRate", 25);
_targetFrameRateSetting = settingsService.GetSetting("Core.TargetFrameRate", 30);
_renderScaleSetting = settingsService.GetSetting("Core.RenderScale", 0.25);
Surface = new RGBSurface();
@@ -16,6 +16,12 @@ namespace Artemis.Core.Services
{
return _pluginSettings.GetSetting(name, defaultValue);
}
/// <inheritdoc />
public void SaveAllSettings()
{
_pluginSettings.SaveAllSettings();
}
}
/// <summary>
@@ -32,5 +38,10 @@ namespace Artemis.Core.Services
/// <param name="defaultValue">The default value to use if the setting does not exist yet</param>
/// <returns></returns>
PluginSetting<T> GetSetting<T>(string name, T? defaultValue = default);
/// <summary>
/// Saves all settings, obviously
/// </summary>
void SaveAllSettings();
}
}