Skip to content

Commit

Permalink
fix json parsing of scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Aytackydln committed Dec 30, 2023
1 parent 09b94f7 commit 3888c69
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
1 change: 1 addition & 0 deletions Project-Aurora/Project-Aurora/Profiles/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ public void ResetProfile()
jsonSerializerSettings.Converters.Add(new TypeAnnotatedObjectConverter());
jsonSerializerSettings.Converters.Add(new DictionaryJsonConverterAdapter());
jsonSerializerSettings.Converters.Add(new ConcurrentDictionaryJsonConverterAdapter());
jsonSerializerSettings.Converters.Add(new VariableRegistryDictionaryConverter());

var serializer = JsonSerializer.Create(jsonSerializerSettings);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ private void OnScriptChanged()
public override void SetApplication(Application profile)
{
Application = profile;

if (Application.EffectScripts.ContainsKey(Properties.Script))
{
var scriptRegistry = (VariableRegistry)Application.EffectScripts[Properties.Script].Properties.Clone();
Properties.ScriptProperties.Combine(scriptRegistry, true);
}

base.SetApplication(profile);
}

Expand Down
1 change: 1 addition & 0 deletions Project-Aurora/Project-Aurora/Settings/ProfileImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,7 @@ private static void ImportJson(this Application app, string filepath)
jsonSerializerSettings.Converters.Add(new TypeAnnotatedObjectConverter());
jsonSerializerSettings.Converters.Add(new DictionaryJsonConverterAdapter());
jsonSerializerSettings.Converters.Add(new ConcurrentDictionaryJsonConverterAdapter());
jsonSerializerSettings.Converters.Add(new VariableRegistryDictionaryConverter());
var inProf = (ApplicationProfile)JsonConvert.DeserializeObject(json, typeof(ApplicationProfile), jsonSerializerSettings);

// Create a new profile on the current application (so that profiles can be imported from different applications)
Expand Down
69 changes: 68 additions & 1 deletion Project-Aurora/Project-Aurora/Utils/JSONUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text.Json;
using System.Text.RegularExpressions;
using Aurora.Settings.Overrides.Logic;
Expand Down Expand Up @@ -313,3 +314,69 @@ public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSer
}
}

public class VariableRegistryDictionaryConverter : JsonConverter<IDictionary<string, VariableRegistryItem>>
{
public override bool CanWrite => false;

public override void WriteJson(JsonWriter writer, IDictionary<string, VariableRegistryItem>? value, JsonSerializer serializer)
{
throw new NotImplementedException();
}

public override IDictionary<string, VariableRegistryItem>? ReadJson(JsonReader reader, Type objectType, IDictionary<string, VariableRegistryItem>? existingValue, bool hasExistingValue, JsonSerializer serializer)
{
var readerTokenType = reader.TokenType;
switch (readerTokenType)
{
case JsonToken.StartObject:
var json = serializer.Deserialize<JObject>(reader);
if (json == null)
{
return existingValue;
}

var value = json["$values"];
var valueReader = value?.CreateReader();
if (valueReader != null)
return ParsePairArray(existingValue, hasExistingValue, serializer, valueReader);

var map = existingValue ?? new Dictionary<string, VariableRegistryItem>();
foreach (var prop in json.Children<JProperty>())
{
if (prop.Name.Equals("$type"))
{
continue;
}

var key = prop.Name;
var item = serializer.Deserialize<VariableRegistryItem>(prop.Value.CreateReader())!;
map.TryAdd(key, item);
}

return map;

case JsonToken.StartArray:
return serializer.Deserialize(reader, typeof(IDictionary<dynamic, dynamic>)) as IDictionary<string, VariableRegistryItem>;
}

throw new JsonSerializationException("Unexpected json state");
}

private static IDictionary<string, VariableRegistryItem>? ParsePairArray(IDictionary<string, VariableRegistryItem>? existingValue, bool hasExistingValue, JsonSerializer serializer,
JsonReader valueReader)
{
var list = serializer.Deserialize(valueReader, typeof(List<(string, VariableRegistryItem)>)) as List<(string, VariableRegistryItem)>;
if (!hasExistingValue || existingValue == null)
return list?.ToDictionary(e => e.Item1, e => e.Item2);

if (list == null)
{
return existingValue;
}
foreach (var valueTuple in list)
{
existingValue[valueTuple.Item1] = valueTuple.Item2;
}
return existingValue;
}
}

0 comments on commit 3888c69

Please sign in to comment.