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.Collections.Generic;
using System.IO;
using Artemis.Core.JsonConverters;
using Artemis.Storage.Entities.Plugins;
using Newtonsoft.Json;
namespace Artemis.Core
{
@ -35,7 +37,7 @@ namespace Artemis.Core
/// </summary>
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>
@ -46,6 +48,16 @@ namespace Artemis.Core
internal static readonly CorePluginFeature CorePluginFeature = new CorePluginFeature {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>
/// A read-only collection containing all primitive numeric types
/// </summary>

View File

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

View File

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

View File

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

View File

@ -29,7 +29,7 @@ namespace Artemis.Core
Profile = Parent.Profile;
Name = name;
Enabled = true;
Parent.AddChild(this);
}
@ -43,7 +43,7 @@ namespace Artemis.Core
Name = folderEntity.Name;
Enabled = folderEntity.Enabled;
Order = folderEntity.Order;
Load();
}
@ -122,8 +122,9 @@ namespace Artemis.Core
if (Parent == null)
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>(JsonConvert.SerializeObject(FolderEntity, settings), settings)!;
FolderEntity entityCopy = JsonConvert.DeserializeObject<FolderEntity>(
JsonConvert.SerializeObject(FolderEntity, Constants.JsonConvertTypedSettings), Constants.JsonConvertTypedSettings
)!;
entityCopy.Id = Guid.NewGuid();
entityCopy.Name += " - Copy";

View File

@ -39,9 +39,9 @@ namespace Artemis.Core
Enabled = true;
_general = new LayerGeneralProperties();
_transform = new LayerTransformProperties();
_leds = new List<ArtemisLed>();
Initialize();
Parent.AddChild(this);
}
@ -55,7 +55,7 @@ namespace Artemis.Core
Parent = parent;
_general = new LayerGeneralProperties();
_transform = new LayerTransformProperties();
_leds = new List<ArtemisLed>();
Load();
@ -123,8 +123,9 @@ namespace Artemis.Core
if (Parent == null)
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>(JsonConvert.SerializeObject(LayerEntity, settings), settings)!;
LayerEntity entityCopy = JsonConvert.DeserializeObject<LayerEntity>(
JsonConvert.SerializeObject(LayerEntity, Constants.JsonConvertTypedSettings), Constants.JsonConvertTypedSettings
)!;
entityCopy.Id = Guid.NewGuid();
entityCopy.Name += " - Copy";
@ -640,7 +641,7 @@ namespace Artemis.Core
if (current == null)
return;
LayerBrushDescriptor? descriptor = current.LayerBrushProviderId != null && current.BrushType != null
LayerBrushDescriptor? descriptor = current.LayerBrushProviderId != null && current.BrushType != null
? LayerBrushStore.Get(current.LayerBrushProviderId, current.BrushType)?.LayerBrushDescriptor
: null;
descriptor?.CreateInstance(this);

View File

@ -208,7 +208,7 @@ namespace Artemis.Core
if (_disposed)
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);
}
@ -512,7 +512,7 @@ namespace Artemis.Core
try
{
if (Entity.Value != null)
BaseValue = JsonConvert.DeserializeObject<T>(Entity.Value);
BaseValue = JsonConvert.DeserializeObject<T>(Entity.Value, Constants.JsonConvertSettings)!;
}
catch (JsonException)
{
@ -525,9 +525,11 @@ namespace Artemis.Core
_keyframes.Clear();
try
{
_keyframes.AddRange(
Entity.KeyframeEntities.Where(k => k.Position <= ProfileElement.Timeline.Length)
.Select(k => new LayerPropertyKeyframe<T>(JsonConvert.DeserializeObject<T>(k.Value), k.Position, (Easings.Functions) k.EasingFunction, this))
_keyframes.AddRange(Entity.KeyframeEntities
.Where(k => k.Position <= ProfileElement.Timeline.Length)
.Select(k => new LayerPropertyKeyframe<T>(
JsonConvert.DeserializeObject<T>(k.Value, Constants.JsonConvertSettings)!, k.Position, (Easings.Functions) k.EasingFunction, this
))
);
}
catch (JsonException)
@ -555,12 +557,12 @@ namespace Artemis.Core
if (!_isInitialized)
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.KeyframeEntities.Clear();
Entity.KeyframeEntities.AddRange(Keyframes.Select(k => new KeyframeEntity
{
Value = JsonConvert.SerializeObject(k.Value),
Value = JsonConvert.SerializeObject(k.Value, Constants.JsonConvertSettings),
Position = k.Position,
EasingFunction = (int) k.EasingFunction
}));

View File

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

View File

@ -45,7 +45,12 @@ namespace Artemis.Core
// If not found, create a new one
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);
}

View File

@ -51,7 +51,6 @@ namespace Artemis.Core.Services
_frameStopWatch = new Stopwatch();
UpdatePluginCache();
ConfigureJsonConvert();
_rgbService.Surface.Updating += SurfaceOnUpdating;
_rgbService.Surface.Updated += SurfaceOnUpdated;
@ -135,14 +134,6 @@ namespace Artemis.Core.Services
_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()
{
_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);
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
DirectoryInfo? match = pluginDirectory.EnumerateDirectories().FirstOrDefault(d => d.Name == Path.GetFileNameWithoutExtension(zipFile.Name));
@ -99,7 +99,7 @@ namespace Artemis.Core.Services
try
{
// 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)
{
@ -226,7 +226,7 @@ namespace Artemis.Core.Services
_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 pluginInfo = JsonConvert.DeserializeObject<PluginInfo>(File.ReadAllText(metadataFile));
PluginInfo pluginInfo = JsonConvert.DeserializeObject<PluginInfo>(File.ReadAllText(metadataFile), Constants.JsonConvertSettings)!;
if (pluginInfo.Guid == Constants.CorePluginInfo.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
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
foreach (LayerEntity profileEntityLayer in profileEntity.Layers)
profileEntityLayer.Leds.AddRange(_surfaceService.ActiveSurface.Devices.SelectMany(d => d.Leds).Select(l => new LedEntity