From 190d64b4971dfd06d64c4dedd0dfa571e6ce283e Mon Sep 17 00:00:00 2001 From: Robert Date: Thu, 26 Nov 2020 19:15:52 +0100 Subject: [PATCH] Core - Removed JsonConvert.DefaultSettings assignement This could break libraries used by plugins, this closes #501 --- src/Artemis.Core/Constants.cs | 14 +++++++++++++- .../Abstract/DataModelConditionPredicate.cs | 6 +++--- .../Modes/Conditional/DataBindingCondition.cs | 4 ++-- .../Modes/Direct/DataBindingModifier.cs | 6 +++--- src/Artemis.Core/Models/Profile/Folder.cs | 9 +++++---- src/Artemis.Core/Models/Profile/Layer.cs | 13 +++++++------ .../Profile/LayerProperties/LayerProperty.cs | 16 +++++++++------- .../Plugins/Settings/PluginSetting.cs | 8 ++++---- .../Plugins/Settings/PluginSettings.cs | 7 ++++++- src/Artemis.Core/Services/CoreService.cs | 9 --------- .../Services/PluginManagementService.cs | 6 +++--- src/Artemis.Core/Utilities/IntroAnimation.cs | 2 +- 12 files changed, 56 insertions(+), 44 deletions(-) diff --git a/src/Artemis.Core/Constants.cs b/src/Artemis.Core/Constants.cs index 9a0ba95c4..caf0a2bad 100644 --- a/src/Artemis.Core/Constants.cs +++ b/src/Artemis.Core/Constants.cs @@ -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 /// 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) }; /// @@ -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 {new SKColorConverter(), new ForgivingIntConverter()} + }; + internal static JsonSerializerSettings JsonConvertTypedSettings = new JsonSerializerSettings + { + TypeNameHandling = TypeNameHandling.All, + Converters = new List { new SKColorConverter(), new ForgivingIntConverter() } + }; + /// /// A read-only collection containing all primitive numeric types /// diff --git a/src/Artemis.Core/Models/Profile/Conditions/Abstract/DataModelConditionPredicate.cs b/src/Artemis.Core/Models/Profile/Conditions/Abstract/DataModelConditionPredicate.cs index af81fcaca..a800272d8 100644 --- a/src/Artemis.Core/Models/Profile/Conditions/Abstract/DataModelConditionPredicate.cs +++ b/src/Artemis.Core/Models/Profile/Conditions/Abstract/DataModelConditionPredicate.cs @@ -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) { diff --git a/src/Artemis.Core/Models/Profile/DataBindings/Modes/Conditional/DataBindingCondition.cs b/src/Artemis.Core/Models/Profile/DataBindings/Modes/Conditional/DataBindingCondition.cs index c82d8111b..c726f261a 100644 --- a/src/Artemis.Core/Models/Profile/DataBindings/Modes/Conditional/DataBindingCondition.cs +++ b/src/Artemis.Core/Models/Profile/DataBindings/Modes/Conditional/DataBindingCondition.cs @@ -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(Entity.Value))!; + Value = (Entity.Value == null ? default : JsonConvert.DeserializeObject(Entity.Value, Constants.JsonConvertSettings))!; Order = Entity.Order; } diff --git a/src/Artemis.Core/Models/Profile/DataBindings/Modes/Direct/DataBindingModifier.cs b/src/Artemis.Core/Models/Profile/DataBindings/Modes/Direct/DataBindingModifier.cs index 49a921fea..ca14351a6 100644 --- a/src/Artemis.Core/Models/Profile/DataBindings/Modes/Direct/DataBindingModifier.cs +++ b/src/Artemis.Core/Models/Profile/DataBindings/Modes/Direct/DataBindingModifier.cs @@ -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); } /// diff --git a/src/Artemis.Core/Models/Profile/Folder.cs b/src/Artemis.Core/Models/Profile/Folder.cs index 5b2c695d3..c811b9b11 100644 --- a/src/Artemis.Core/Models/Profile/Folder.cs +++ b/src/Artemis.Core/Models/Profile/Folder.cs @@ -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(JsonConvert.SerializeObject(FolderEntity, settings), settings)!; + FolderEntity entityCopy = JsonConvert.DeserializeObject( + JsonConvert.SerializeObject(FolderEntity, Constants.JsonConvertTypedSettings), Constants.JsonConvertTypedSettings + )!; entityCopy.Id = Guid.NewGuid(); entityCopy.Name += " - Copy"; diff --git a/src/Artemis.Core/Models/Profile/Layer.cs b/src/Artemis.Core/Models/Profile/Layer.cs index 259a13e03..c776591b7 100644 --- a/src/Artemis.Core/Models/Profile/Layer.cs +++ b/src/Artemis.Core/Models/Profile/Layer.cs @@ -39,9 +39,9 @@ namespace Artemis.Core Enabled = true; _general = new LayerGeneralProperties(); _transform = new LayerTransformProperties(); - + _leds = new List(); - + Initialize(); Parent.AddChild(this); } @@ -55,7 +55,7 @@ namespace Artemis.Core Parent = parent; _general = new LayerGeneralProperties(); _transform = new LayerTransformProperties(); - + _leds = new List(); 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(JsonConvert.SerializeObject(LayerEntity, settings), settings)!; + LayerEntity entityCopy = JsonConvert.DeserializeObject( + 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); diff --git a/src/Artemis.Core/Models/Profile/LayerProperties/LayerProperty.cs b/src/Artemis.Core/Models/Profile/LayerProperties/LayerProperty.cs index 2c658ccb2..0c17692a5 100644 --- a/src/Artemis.Core/Models/Profile/LayerProperties/LayerProperty.cs +++ b/src/Artemis.Core/Models/Profile/LayerProperties/LayerProperty.cs @@ -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(json), time); } @@ -512,7 +512,7 @@ namespace Artemis.Core try { if (Entity.Value != null) - BaseValue = JsonConvert.DeserializeObject(Entity.Value); + BaseValue = JsonConvert.DeserializeObject(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(JsonConvert.DeserializeObject(k.Value), k.Position, (Easings.Functions) k.EasingFunction, this)) + _keyframes.AddRange(Entity.KeyframeEntities + .Where(k => k.Position <= ProfileElement.Timeline.Length) + .Select(k => new LayerPropertyKeyframe( + JsonConvert.DeserializeObject(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 })); diff --git a/src/Artemis.Core/Plugins/Settings/PluginSetting.cs b/src/Artemis.Core/Plugins/Settings/PluginSetting.cs index 02508f220..006df6970 100644 --- a/src/Artemis.Core/Plugins/Settings/PluginSetting.cs +++ b/src/Artemis.Core/Plugins/Settings/PluginSetting.cs @@ -29,7 +29,7 @@ namespace Artemis.Core Name = pluginSettingEntity.Name; try { - _value = JsonConvert.DeserializeObject(pluginSettingEntity.Value); + _value = JsonConvert.DeserializeObject(pluginSettingEntity.Value, Constants.JsonConvertSettings); } catch (JsonReaderException) { @@ -66,7 +66,7 @@ namespace Artemis.Core /// /// Determines whether the setting has been changed /// - public bool HasChanged => JsonConvert.SerializeObject(Value) != _pluginSettingEntity.Value; + public bool HasChanged => JsonConvert.SerializeObject(Value, Constants.JsonConvertSettings) != _pluginSettingEntity.Value; /// /// Gets or sets whether changes must automatically be saved @@ -79,7 +79,7 @@ namespace Artemis.Core /// public void RejectChanges() { - Value = JsonConvert.DeserializeObject(_pluginSettingEntity.Value); + Value = JsonConvert.DeserializeObject(_pluginSettingEntity.Value, Constants.JsonConvertSettings); } /// @@ -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(); } diff --git a/src/Artemis.Core/Plugins/Settings/PluginSettings.cs b/src/Artemis.Core/Plugins/Settings/PluginSettings.cs index 65a176e2c..075a70d29 100644 --- a/src/Artemis.Core/Plugins/Settings/PluginSettings.cs +++ b/src/Artemis.Core/Plugins/Settings/PluginSettings.cs @@ -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); } diff --git a/src/Artemis.Core/Services/CoreService.cs b/src/Artemis.Core/Services/CoreService.cs index 782b83589..d916d5d4d 100644 --- a/src/Artemis.Core/Services/CoreService.cs +++ b/src/Artemis.Core/Services/CoreService.cs @@ -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().Where(p => p.IsEnabled).ToList(); } - private void ConfigureJsonConvert() - { - JsonConvert.DefaultSettings = () => new JsonSerializerSettings - { - Converters = new List {new SKColorConverter(), new ForgivingIntConverter()} - }; - } - private void ApplyLoggingLevel() { _logger.Information("Setting logging level to {loggingLevel}", _loggingLevel.Value); diff --git a/src/Artemis.Core/Services/PluginManagementService.cs b/src/Artemis.Core/Services/PluginManagementService.cs index 5919c0c6a..44ddf7f5f 100644 --- a/src/Artemis.Core/Services/PluginManagementService.cs +++ b/src/Artemis.Core/Services/PluginManagementService.cs @@ -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(reader.ReadToEnd()); + PluginInfo builtInPluginInfo = JsonConvert.DeserializeObject(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(File.ReadAllText(metadataFile)); + PluginInfo pluginInfo = JsonConvert.DeserializeObject(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(File.ReadAllText(metadataFile)); + PluginInfo pluginInfo = JsonConvert.DeserializeObject(File.ReadAllText(metadataFile), Constants.JsonConvertSettings)!; if (pluginInfo.Guid == Constants.CorePluginInfo.Guid) throw new ArtemisPluginException($"Plugin cannot use reserved GUID {pluginInfo.Guid}"); diff --git a/src/Artemis.Core/Utilities/IntroAnimation.cs b/src/Artemis.Core/Utilities/IntroAnimation.cs index 604191a94..56777015f 100644 --- a/src/Artemis.Core/Utilities/IntroAnimation.cs +++ b/src/Artemis.Core/Utilities/IntroAnimation.cs @@ -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(json); + ProfileEntity profileEntity = JsonConvert.DeserializeObject(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