Skip to content

Commit

Permalink
Add support for serializing StringItem and StringItemEffect. Bump ver…
Browse files Browse the repository at this point in the history
…sion.
  • Loading branch information
homothetyhk committed Mar 2, 2024
1 parent 2c9a177 commit c0d82a9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
16 changes: 16 additions & 0 deletions RandomizerCore.Json/Converters/LogicContractResolver.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using RandomizerCore.Logic;
using RandomizerCore.StringItems;
using System.Reflection;

namespace RandomizerCore.Json.Converters
Expand Down Expand Up @@ -75,6 +76,21 @@ public void ModifyObjectContract(Type objectType, JsonObjectContract c)
c.CreatorParameters.Add(c.Properties["Logic"]);
c.OverrideCreator = (args) => LM.CreateRPNLogicDef(new((string)args[0], (string)args[1]));
}
else if (objectType == typeof(StringItem))
{
foreach (JsonProperty p in c.Properties)
{
if (p.UnderlyingName != nameof(StringItem.Name) && p.UnderlyingName != nameof(StringItem.EffectString)) p.Ignored = true;
}

JsonProperty name = c.Properties[nameof(StringItem.Name)];
JsonProperty effect = c.Properties[nameof(StringItem.EffectString)];
effect.PropertyName = nameof(StringItem.Effect);
c.CreatorParameters.Clear();
c.CreatorParameters.Add(name);
c.CreatorParameters.Add(effect);
c.OverrideCreator = (args) => LM.FromItemString((string)args[0], (string)args[1]);
}
}
}
}
8 changes: 8 additions & 0 deletions RandomizerCore.Json/Converters/RCContractResolver.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using RandomizerCore.Logic;
using RandomizerCore.StringItems;
using System.Reflection;

namespace RandomizerCore.Json.Converters
Expand Down Expand Up @@ -29,6 +30,13 @@ protected override JsonObjectContract CreateObjectContract(Type objectType)
{
c.Properties[nameof(RandoLocation.Name)].Ignored = true;
}
else if (objectType == typeof(FirstOfEffect) || objectType == typeof(AllOfEffect))
{
JsonProperty p = base.CreateProperty(objectType.GetField("Effects", BindingFlags.Instance | BindingFlags.NonPublic), MemberSerialization.Fields);
c.Properties.AddProperty(p);
c.CreatorParameters.Clear();
c.CreatorParameters.Add(p);
}

return c;
}
Expand Down
2 changes: 1 addition & 1 deletion RandomizerCore.Json/RandomizerCore.Json.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<ProjectGuid>967174ad-0f7f-42b7-b53e-7fd661d92787</ProjectGuid>
<VersionPrefix>1.0.0</VersionPrefix>
<VersionPrefix>1.0.1</VersionPrefix>
<VersionSuffix></VersionSuffix>
<RootNamespace>RandomizerCore.Json</RootNamespace>
<AssemblyName>RandomizerCore.Json</AssemblyName>
Expand Down
21 changes: 20 additions & 1 deletion RandomizerCore.JsonTests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,26 @@ public void CtxRoundTripIdentity()
//js.SerializeToFile("C:\\dev\\RandomizerCore.Json\\RandomizerCore.JsonTests\\Resources\\2024-01-20-stable.json", ctx);
js.SerializeToString(ctx).Should().Be(json);
}


[Fact]
public void NonLMStringItemSerialization()
{
LogicManagerBuilder lmb = new();
lmb.GetOrAddTerm("A");
lmb.GetOrAddTerm("B");
LogicManager lm = new(lmb);
LogicItem i1 = lm.FromItemString("I", "`A<1 | B<1 | A<2 + B<2` => (`A + B` => (A++ >> B++) >|> `A<1 + B>1` => A += 2 >|> B++)");
JsonSerializer js = JsonUtil.GetLogicSerializer(lm);
string j = js.SerializeToString(i1);
LogicItem i2 = js.DeserializeFromString<LogicItem>(j);
i1.Should().Be(i2);

j = js.SerializeToString(((StringItem)i1).Effect, typeof(StringItemEffect));
StringItemEffect e = js.DeserializeFromString<StringItemEffect>(j);
LogicItem i3 = new StringItem("I", "`A<1 | B<1 | A<2 + B<2` => (`A + B` => (A++ >> B++) >|> `A<1 + B>1` => A += 2 >|> B++)", e);
i1.Should().Be(i3);
}


}
}

0 comments on commit c0d82a9

Please sign in to comment.