1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Core - Removed JsonConvert.DefaultSettings assignement

This could break libraries used by plugins, this closes #501
This commit is contained in:
Robert 2020-11-26 19:15:52 +01:00
parent 81bb278abe
commit 190d64b497
12 changed files with 56 additions and 44 deletions

View File

@ -1,7 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using Artemis.Core.JsonConverters;
using Artemis.Storage.Entities.Plugins; using Artemis.Storage.Entities.Plugins;
using Newtonsoft.Json;
namespace Artemis.Core namespace Artemis.Core
{ {
@ -35,7 +37,7 @@ namespace Artemis.Core
/// </summary> /// </summary>
public static readonly PluginInfo CorePluginInfo = new PluginInfo public static readonly PluginInfo CorePluginInfo = new PluginInfo
{ {
Guid = Guid.Parse("ffffffff-ffff-ffff-ffff-ffffffffffff"), Name = "Artemis Core", Version = new Version(2,0) Guid = Guid.Parse("ffffffff-ffff-ffff-ffff-ffffffffffff"), Name = "Artemis Core", Version = new Version(2, 0)
}; };
/// <summary> /// <summary>
@ -46,6 +48,16 @@ namespace Artemis.Core
internal static readonly CorePluginFeature CorePluginFeature = new CorePluginFeature {Plugin = CorePlugin}; internal static readonly CorePluginFeature CorePluginFeature = new CorePluginFeature {Plugin = CorePlugin};
internal static readonly EffectPlaceholderPlugin EffectPlaceholderPlugin = new EffectPlaceholderPlugin {Plugin = CorePlugin}; internal static readonly EffectPlaceholderPlugin EffectPlaceholderPlugin = new EffectPlaceholderPlugin {Plugin = CorePlugin};
internal static JsonSerializerSettings JsonConvertSettings = new JsonSerializerSettings
{
Converters = new List<JsonConverter> {new SKColorConverter(), new ForgivingIntConverter()}
};
internal static JsonSerializerSettings JsonConvertTypedSettings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All,
Converters = new List<JsonConverter> { new SKColorConverter(), new ForgivingIntConverter() }
};
/// <summary> /// <summary>
/// A read-only collection containing all primitive numeric types /// A read-only collection containing all primitive numeric types
/// </summary> /// </summary>

View File

@ -115,7 +115,7 @@ namespace Artemis.Core
try try
{ {
rightSideValue = JsonConvert.DeserializeObject(Entity.RightStaticValue, leftSideType); rightSideValue = JsonConvert.DeserializeObject(Entity.RightStaticValue, leftSideType, Constants.JsonConvertSettings);
} }
// If deserialization fails, use the type's default // If deserialization fails, use the type's default
catch (JsonSerializationException e) catch (JsonSerializationException e)
@ -129,7 +129,7 @@ namespace Artemis.Core
else else
{ {
// Hope for the best... // Hope for the best...
UpdateRightSideStatic(JsonConvert.DeserializeObject(Entity.RightStaticValue)); UpdateRightSideStatic(JsonConvert.DeserializeObject(Entity.RightStaticValue, Constants.JsonConvertSettings));
} }
} }
catch (JsonReaderException e) catch (JsonReaderException e)
@ -349,7 +349,7 @@ namespace Artemis.Core
RightPath?.Save(); RightPath?.Save();
Entity.RightPath = RightPath?.Entity; Entity.RightPath = RightPath?.Entity;
Entity.RightStaticValue = JsonConvert.SerializeObject(RightStaticValue); Entity.RightStaticValue = JsonConvert.SerializeObject(RightStaticValue, Constants.JsonConvertSettings);
if (Operator?.Plugin != null) if (Operator?.Plugin != null)
{ {

View File

@ -74,7 +74,7 @@ namespace Artemis.Core
Entity.Condition = Condition.Entity; Entity.Condition = Condition.Entity;
Condition.Save(); Condition.Save();
Entity.Value = JsonConvert.SerializeObject(Value); Entity.Value = JsonConvert.SerializeObject(Value, Constants.JsonConvertSettings);
Entity.Order = Order; Entity.Order = Order;
} }
@ -88,7 +88,7 @@ namespace Artemis.Core
? new DataModelConditionGroup(null, Entity.Condition) ? new DataModelConditionGroup(null, Entity.Condition)
: new DataModelConditionGroup(null); : new DataModelConditionGroup(null);
Value = (Entity.Value == null ? default : JsonConvert.DeserializeObject<TProperty>(Entity.Value))!; Value = (Entity.Value == null ? default : JsonConvert.DeserializeObject<TProperty>(Entity.Value, Constants.JsonConvertSettings))!;
Order = Entity.Order; Order = Entity.Order;
} }

View File

@ -209,8 +209,8 @@ namespace Artemis.Core
try try
{ {
staticValue = parameterType != null staticValue = parameterType != null
? JsonConvert.DeserializeObject(Entity.ParameterStaticValue, parameterType) ? JsonConvert.DeserializeObject(Entity.ParameterStaticValue, parameterType, Constants.JsonConvertSettings)
: JsonConvert.DeserializeObject(Entity.ParameterStaticValue); : JsonConvert.DeserializeObject(Entity.ParameterStaticValue, Constants.JsonConvertSettings);
} }
// If deserialization fails, use the type's default // If deserialization fails, use the type's default
catch (JsonSerializationException e) catch (JsonSerializationException e)
@ -252,7 +252,7 @@ namespace Artemis.Core
ParameterPath?.Save(); ParameterPath?.Save();
Entity.ParameterPath = ParameterPath?.Entity; Entity.ParameterPath = ParameterPath?.Entity;
Entity.ParameterStaticValue = JsonConvert.SerializeObject(ParameterStaticValue); Entity.ParameterStaticValue = JsonConvert.SerializeObject(ParameterStaticValue, Constants.JsonConvertSettings);
} }
/// <inheritdoc /> /// <inheritdoc />

View File

@ -122,8 +122,9 @@ namespace Artemis.Core
if (Parent == null) if (Parent == null)
throw new ArtemisCoreException("Cannot create a copy of a folder without a parent"); throw new ArtemisCoreException("Cannot create a copy of a folder without a parent");
JsonSerializerSettings settings = new JsonSerializerSettings {TypeNameHandling = TypeNameHandling.All}; FolderEntity entityCopy = JsonConvert.DeserializeObject<FolderEntity>(
FolderEntity entityCopy = JsonConvert.DeserializeObject<FolderEntity>(JsonConvert.SerializeObject(FolderEntity, settings), settings)!; JsonConvert.SerializeObject(FolderEntity, Constants.JsonConvertTypedSettings), Constants.JsonConvertTypedSettings
)!;
entityCopy.Id = Guid.NewGuid(); entityCopy.Id = Guid.NewGuid();
entityCopy.Name += " - Copy"; entityCopy.Name += " - Copy";

View File

@ -123,8 +123,9 @@ namespace Artemis.Core
if (Parent == null) if (Parent == null)
throw new ArtemisCoreException("Cannot create a copy of a layer without a parent"); throw new ArtemisCoreException("Cannot create a copy of a layer without a parent");
JsonSerializerSettings settings = new JsonSerializerSettings {TypeNameHandling = TypeNameHandling.All}; LayerEntity entityCopy = JsonConvert.DeserializeObject<LayerEntity>(
LayerEntity entityCopy = JsonConvert.DeserializeObject<LayerEntity>(JsonConvert.SerializeObject(LayerEntity, settings), settings)!; JsonConvert.SerializeObject(LayerEntity, Constants.JsonConvertTypedSettings), Constants.JsonConvertTypedSettings
)!;
entityCopy.Id = Guid.NewGuid(); entityCopy.Id = Guid.NewGuid();
entityCopy.Name += " - Copy"; entityCopy.Name += " - Copy";

View File

@ -208,7 +208,7 @@ namespace Artemis.Core
if (_disposed) if (_disposed)
throw new ObjectDisposedException("LayerProperty"); throw new ObjectDisposedException("LayerProperty");
string json = JsonConvert.SerializeObject(DefaultValue, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }); string json = JsonConvert.SerializeObject(DefaultValue, Constants.JsonConvertTypedSettings);
SetCurrentValue(JsonConvert.DeserializeObject<T>(json), time); SetCurrentValue(JsonConvert.DeserializeObject<T>(json), time);
} }
@ -512,7 +512,7 @@ namespace Artemis.Core
try try
{ {
if (Entity.Value != null) if (Entity.Value != null)
BaseValue = JsonConvert.DeserializeObject<T>(Entity.Value); BaseValue = JsonConvert.DeserializeObject<T>(Entity.Value, Constants.JsonConvertSettings)!;
} }
catch (JsonException) catch (JsonException)
{ {
@ -525,9 +525,11 @@ namespace Artemis.Core
_keyframes.Clear(); _keyframes.Clear();
try try
{ {
_keyframes.AddRange( _keyframes.AddRange(Entity.KeyframeEntities
Entity.KeyframeEntities.Where(k => k.Position <= ProfileElement.Timeline.Length) .Where(k => k.Position <= ProfileElement.Timeline.Length)
.Select(k => new LayerPropertyKeyframe<T>(JsonConvert.DeserializeObject<T>(k.Value), k.Position, (Easings.Functions) k.EasingFunction, this)) .Select(k => new LayerPropertyKeyframe<T>(
JsonConvert.DeserializeObject<T>(k.Value, Constants.JsonConvertSettings)!, k.Position, (Easings.Functions) k.EasingFunction, this
))
); );
} }
catch (JsonException) catch (JsonException)
@ -555,12 +557,12 @@ namespace Artemis.Core
if (!_isInitialized) if (!_isInitialized)
throw new ArtemisCoreException("Layer property is not yet initialized"); throw new ArtemisCoreException("Layer property is not yet initialized");
Entity.Value = JsonConvert.SerializeObject(BaseValue); Entity.Value = JsonConvert.SerializeObject(BaseValue, Constants.JsonConvertSettings);
Entity.KeyframesEnabled = KeyframesEnabled; Entity.KeyframesEnabled = KeyframesEnabled;
Entity.KeyframeEntities.Clear(); Entity.KeyframeEntities.Clear();
Entity.KeyframeEntities.AddRange(Keyframes.Select(k => new KeyframeEntity Entity.KeyframeEntities.AddRange(Keyframes.Select(k => new KeyframeEntity
{ {
Value = JsonConvert.SerializeObject(k.Value), Value = JsonConvert.SerializeObject(k.Value, Constants.JsonConvertSettings),
Position = k.Position, Position = k.Position,
EasingFunction = (int) k.EasingFunction EasingFunction = (int) k.EasingFunction
})); }));

View File

@ -29,7 +29,7 @@ namespace Artemis.Core
Name = pluginSettingEntity.Name; Name = pluginSettingEntity.Name;
try try
{ {
_value = JsonConvert.DeserializeObject<T>(pluginSettingEntity.Value); _value = JsonConvert.DeserializeObject<T>(pluginSettingEntity.Value, Constants.JsonConvertSettings);
} }
catch (JsonReaderException) catch (JsonReaderException)
{ {
@ -66,7 +66,7 @@ namespace Artemis.Core
/// <summary> /// <summary>
/// Determines whether the setting has been changed /// Determines whether the setting has been changed
/// </summary> /// </summary>
public bool HasChanged => JsonConvert.SerializeObject(Value) != _pluginSettingEntity.Value; public bool HasChanged => JsonConvert.SerializeObject(Value, Constants.JsonConvertSettings) != _pluginSettingEntity.Value;
/// <summary> /// <summary>
/// Gets or sets whether changes must automatically be saved /// Gets or sets whether changes must automatically be saved
@ -79,7 +79,7 @@ namespace Artemis.Core
/// </summary> /// </summary>
public void RejectChanges() public void RejectChanges()
{ {
Value = JsonConvert.DeserializeObject<T>(_pluginSettingEntity.Value); Value = JsonConvert.DeserializeObject<T>(_pluginSettingEntity.Value, Constants.JsonConvertSettings);
} }
/// <summary> /// <summary>
@ -90,7 +90,7 @@ namespace Artemis.Core
if (!HasChanged) if (!HasChanged)
return; return;
_pluginSettingEntity.Value = JsonConvert.SerializeObject(Value); _pluginSettingEntity.Value = JsonConvert.SerializeObject(Value, Constants.JsonConvertSettings);
_pluginRepository.SaveSetting(_pluginSettingEntity); _pluginRepository.SaveSetting(_pluginSettingEntity);
OnSettingSaved(); OnSettingSaved();
} }

View File

@ -45,7 +45,12 @@ namespace Artemis.Core
// If not found, create a new one // If not found, create a new one
if (settingEntity == null) if (settingEntity == null)
{ {
settingEntity = new PluginSettingEntity {Name = name, PluginGuid = Plugin.Guid, Value = JsonConvert.SerializeObject(defaultValue)}; settingEntity = new PluginSettingEntity
{
Name = name,
PluginGuid = Plugin.Guid,
Value = JsonConvert.SerializeObject(defaultValue, Constants.JsonConvertSettings)
};
_pluginRepository.AddSetting(settingEntity); _pluginRepository.AddSetting(settingEntity);
} }

View File

@ -51,7 +51,6 @@ namespace Artemis.Core.Services
_frameStopWatch = new Stopwatch(); _frameStopWatch = new Stopwatch();
UpdatePluginCache(); UpdatePluginCache();
ConfigureJsonConvert();
_rgbService.Surface.Updating += SurfaceOnUpdating; _rgbService.Surface.Updating += SurfaceOnUpdating;
_rgbService.Surface.Updated += SurfaceOnUpdated; _rgbService.Surface.Updated += SurfaceOnUpdated;
@ -135,14 +134,6 @@ namespace Artemis.Core.Services
_dataModelExpansions = _pluginManagementService.GetFeaturesOfType<BaseDataModelExpansion>().Where(p => p.IsEnabled).ToList(); _dataModelExpansions = _pluginManagementService.GetFeaturesOfType<BaseDataModelExpansion>().Where(p => p.IsEnabled).ToList();
} }
private void ConfigureJsonConvert()
{
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
Converters = new List<JsonConverter> {new SKColorConverter(), new ForgivingIntConverter()}
};
}
private void ApplyLoggingLevel() private void ApplyLoggingLevel()
{ {
_logger.Information("Setting logging level to {loggingLevel}", _loggingLevel.Value); _logger.Information("Setting logging level to {loggingLevel}", _loggingLevel.Value);

View File

@ -78,7 +78,7 @@ namespace Artemis.Core.Services
throw new ArtemisPluginException("Couldn't find a plugin.json in " + zipFile.FullName); throw new ArtemisPluginException("Couldn't find a plugin.json in " + zipFile.FullName);
using StreamReader reader = new StreamReader(metaDataFileEntry.Open()); using StreamReader reader = new StreamReader(metaDataFileEntry.Open());
PluginInfo builtInPluginInfo = JsonConvert.DeserializeObject<PluginInfo>(reader.ReadToEnd()); PluginInfo builtInPluginInfo = JsonConvert.DeserializeObject<PluginInfo>(reader.ReadToEnd(), Constants.JsonConvertSettings)!;
// Find the matching plugin in the plugin folder // Find the matching plugin in the plugin folder
DirectoryInfo? match = pluginDirectory.EnumerateDirectories().FirstOrDefault(d => d.Name == Path.GetFileNameWithoutExtension(zipFile.Name)); DirectoryInfo? match = pluginDirectory.EnumerateDirectories().FirstOrDefault(d => d.Name == Path.GetFileNameWithoutExtension(zipFile.Name));
@ -99,7 +99,7 @@ namespace Artemis.Core.Services
try try
{ {
// Compare versions, copy if the same when debugging // Compare versions, copy if the same when debugging
PluginInfo pluginInfo = JsonConvert.DeserializeObject<PluginInfo>(File.ReadAllText(metadataFile)); PluginInfo pluginInfo = JsonConvert.DeserializeObject<PluginInfo>(File.ReadAllText(metadataFile), Constants.JsonConvertSettings)!;
if (builtInPluginInfo.Version > pluginInfo.Version) if (builtInPluginInfo.Version > pluginInfo.Version)
{ {
@ -226,7 +226,7 @@ namespace Artemis.Core.Services
_logger.Warning(new ArtemisPluginException("Couldn't find the plugins metadata file at " + metadataFile), "Plugin exception"); _logger.Warning(new ArtemisPluginException("Couldn't find the plugins metadata file at " + metadataFile), "Plugin exception");
// PluginInfo contains the ID which we need to move on // PluginInfo contains the ID which we need to move on
PluginInfo pluginInfo = JsonConvert.DeserializeObject<PluginInfo>(File.ReadAllText(metadataFile)); PluginInfo pluginInfo = JsonConvert.DeserializeObject<PluginInfo>(File.ReadAllText(metadataFile), Constants.JsonConvertSettings)!;
if (pluginInfo.Guid == Constants.CorePluginInfo.Guid) if (pluginInfo.Guid == Constants.CorePluginInfo.Guid)
throw new ArtemisPluginException($"Plugin cannot use reserved GUID {pluginInfo.Guid}"); throw new ArtemisPluginException($"Plugin cannot use reserved GUID {pluginInfo.Guid}");

View File

@ -39,7 +39,7 @@ namespace Artemis.Core
{ {
// Load the intro profile from JSON into a ProfileEntity // Load the intro profile from JSON into a ProfileEntity
string json = File.ReadAllText(Path.Combine(Constants.ApplicationFolder, "Resources", "intro-profile.json")); string json = File.ReadAllText(Path.Combine(Constants.ApplicationFolder, "Resources", "intro-profile.json"));
ProfileEntity profileEntity = JsonConvert.DeserializeObject<ProfileEntity>(json); ProfileEntity profileEntity = JsonConvert.DeserializeObject<ProfileEntity>(json, Constants.JsonConvertSettings)!;
// Inject every LED on the surface into each layer // Inject every LED on the surface into each layer
foreach (LayerEntity profileEntityLayer in profileEntity.Layers) foreach (LayerEntity profileEntityLayer in profileEntity.Layers)
profileEntityLayer.Leds.AddRange(_surfaceService.ActiveSurface.Devices.SelectMany(d => d.Leds).Select(l => new LedEntity profileEntityLayer.Leds.AddRange(_surfaceService.ActiveSurface.Devices.SelectMany(d => d.Leds).Select(l => new LedEntity