From 7f4f7f5491ad61ce6e2078f8bcdeac20cffe2708 Mon Sep 17 00:00:00 2001 From: chillu Date: Sat, 3 Aug 2024 16:08:26 +0200 Subject: [PATCH 1/4] feat: initial recipe serialization testing --- ...iBuff.Extensions.Serialization.Json.csproj | 4 +- .../SaveController.cs | 4 +- .../ModiBuff.Tests/SaveLoadRecipeTests.cs | 28 ++--- ModiBuff/ModiBuff.Units/ModiBuff.Units.csproj | 2 +- .../Creation/Recipe/ModifierRecipeSaveLoad.cs | 112 +++++++++++------- .../Creation/Recipe/ModifierRecipes.cs | 6 +- ModiBuff/ModiBuff/ModiBuff.csproj | 2 +- 7 files changed, 89 insertions(+), 69 deletions(-) diff --git a/ModiBuff/ModiBuff.Extensions.Serialization.Json/ModiBuff.Extensions.Serialization.Json.csproj b/ModiBuff/ModiBuff.Extensions.Serialization.Json/ModiBuff.Extensions.Serialization.Json.csproj index ad293bca..204d58c3 100644 --- a/ModiBuff/ModiBuff.Extensions.Serialization.Json/ModiBuff.Extensions.Serialization.Json.csproj +++ b/ModiBuff/ModiBuff.Extensions.Serialization.Json/ModiBuff.Extensions.Serialization.Json.csproj @@ -6,7 +6,7 @@ - + @@ -14,4 +14,4 @@ - + \ No newline at end of file diff --git a/ModiBuff/ModiBuff.Extensions.Serialization.Json/SaveController.cs b/ModiBuff/ModiBuff.Extensions.Serialization.Json/SaveController.cs index 9c1aa6c7..aad54fa6 100644 --- a/ModiBuff/ModiBuff.Extensions.Serialization.Json/SaveController.cs +++ b/ModiBuff/ModiBuff.Extensions.Serialization.Json/SaveController.cs @@ -22,8 +22,8 @@ public SaveController(string fileName) }; } - public string Save(T obj) => JsonSerializer.Serialize(obj, _options); - public string Save(GameState.SaveData obj) => JsonSerializer.Serialize(obj, _options); + public string Save(T obj) => JsonSerializer.Serialize(obj, obj.GetType(), _options); + public string Save(GameState.SaveData obj) => JsonSerializer.Serialize(obj, obj.GetType(), _options); public void SaveToPath(string json, string fileName) => File.WriteAllText(System.IO.Path.Combine(Path, fileName), json); diff --git a/ModiBuff/ModiBuff.Tests/SaveLoadRecipeTests.cs b/ModiBuff/ModiBuff.Tests/SaveLoadRecipeTests.cs index 10c2866d..c5ffea6e 100644 --- a/ModiBuff/ModiBuff.Tests/SaveLoadRecipeTests.cs +++ b/ModiBuff/ModiBuff.Tests/SaveLoadRecipeTests.cs @@ -32,7 +32,7 @@ private void SaveLoadStateAndSetup(ModifierRecipes saveRecipes) Setup(); } - [Test] + //[Test] public void SaveRecipeLoad() { var saveRecipes = new ModifierRecipes(IdManager, EffectTypeIdManager); @@ -65,7 +65,7 @@ public void SaveNamesRecipeLoad() Assert.AreEqual(modifierInfo.Description, description); } - [Test] + //[Test] public void SaveStackRecipeLoad() { var saveRecipes = new ModifierRecipes(IdManager, EffectTypeIdManager); @@ -82,7 +82,7 @@ public void SaveStackRecipeLoad() Assert.AreEqual(UnitHealth - 5 - 2 - 5 - 2 - 2, Unit.Health); } - [Test] + //[Test] public void SaveAddDamageRecipeLoad() { var saveRecipes = new ModifierRecipes(IdManager, EffectTypeIdManager); @@ -95,7 +95,7 @@ public void SaveAddDamageRecipeLoad() Assert.AreEqual(UnitDamage + 5, Unit.Damage); } - [Test] + //[Test] public void SaveIntervalRecipeLoad() { var saveRecipes = new ModifierRecipes(IdManager, EffectTypeIdManager); @@ -110,7 +110,7 @@ public void SaveIntervalRecipeLoad() Assert.AreEqual(UnitHealth - 5, Unit.Health); } - [Test] + //[Test] public void SaveModifierActionRecipeLoad() { var saveRecipes = new ModifierRecipes(IdManager, EffectTypeIdManager); @@ -130,7 +130,7 @@ public void SaveModifierActionRecipeLoad() Assert.AreEqual(UnitHealth - 5 - 5 - 5, Unit.Health); } - [Test] + //[Test] public void SaveRemoveRecipeLoad() { var saveRecipes = new ModifierRecipes(IdManager, EffectTypeIdManager); @@ -146,7 +146,7 @@ public void SaveRemoveRecipeLoad() Assert.False(Unit.ContainsModifier("RemoveDamage")); } - [Test] + //[Test] public void SaveRefreshIntervalRecipeLoad() { var saveRecipes = new ModifierRecipes(IdManager, EffectTypeIdManager); @@ -164,7 +164,7 @@ public void SaveRefreshIntervalRecipeLoad() Assert.AreEqual(UnitHealth, Unit.Health); } - [Test] + //[Test] public void SaveRefreshDurationRecipeLoad() { var saveRecipes = new ModifierRecipes(IdManager, EffectTypeIdManager); @@ -183,7 +183,7 @@ public void SaveRefreshDurationRecipeLoad() Assert.False(Unit.ContainsModifier("RefreshDuration")); } - [Test] + //[Test] public void SaveRemoveStackRecipeLoad() { var saveRecipes = new ModifierRecipes(IdManager, EffectTypeIdManager); @@ -200,7 +200,7 @@ public void SaveRemoveStackRecipeLoad() Assert.False(Unit.ContainsModifier("RemoveStack")); } - [Test] + //[Test] public void SaveRemoveCallbackUnitRecipeLoad() { var saveRecipes = new ModifierRecipes(IdManager, EffectTypeIdManager); @@ -218,7 +218,7 @@ public void SaveRemoveCallbackUnitRecipeLoad() Assert.AreEqual(UnitDamage, Unit.Damage); } - [Test] + //[Test] public void SaveDispelRecipeLoad() { var saveRecipes = new ModifierRecipes(IdManager, EffectTypeIdManager); @@ -239,7 +239,7 @@ public void SaveDispelRecipeLoad() Assert.AreEqual(UnitHealth - 5, Unit.Health); } - [Test] + //[Test] public void SaveTagRecipeLoad() { var saveRecipes = new ModifierRecipes(IdManager, EffectTypeIdManager); @@ -253,7 +253,7 @@ public void SaveTagRecipeLoad() Assert.True(ModifierRecipes.GetTag(id).HasTag(TagType.DurationIgnoresStatusResistance)); } - [Test] + //[Test] public void SaveRemoveTagRecipeLoad() { var saveRecipes = new ModifierRecipes(IdManager, EffectTypeIdManager); @@ -267,7 +267,7 @@ public void SaveRemoveTagRecipeLoad() Assert.False(ModifierRecipes.GetTag(id).HasTag(TagType.Default)); } - [Test] + //[Test] public void SaveSetTagRecipeLoad() { var saveRecipes = new ModifierRecipes(IdManager, EffectTypeIdManager); diff --git a/ModiBuff/ModiBuff.Units/ModiBuff.Units.csproj b/ModiBuff/ModiBuff.Units/ModiBuff.Units.csproj index 5fa6a7f2..e200efe5 100644 --- a/ModiBuff/ModiBuff.Units/ModiBuff.Units.csproj +++ b/ModiBuff/ModiBuff.Units/ModiBuff.Units.csproj @@ -39,7 +39,7 @@ - + \ No newline at end of file diff --git a/ModiBuff/ModiBuff/Core/Modifier/Creation/Recipe/ModifierRecipeSaveLoad.cs b/ModiBuff/ModiBuff/Core/Modifier/Creation/Recipe/ModifierRecipeSaveLoad.cs index e2bec463..a19c1480 100644 --- a/ModiBuff/ModiBuff/Core/Modifier/Creation/Recipe/ModifierRecipeSaveLoad.cs +++ b/ModiBuff/ModiBuff/Core/Modifier/Creation/Recipe/ModifierRecipeSaveLoad.cs @@ -38,7 +38,7 @@ public void LoadState(SaveData saveData) break; case SaveInstruction.Interval.Id: #if MODIBUFF_SYSTEM_TEXT_JSON - Interval(instruction.Values.GetDataFromJsonObject()); + Interval(((SaveInstruction.Interval)instruction).Value); #endif break; case SaveInstruction.Duration.Id: @@ -122,47 +122,29 @@ public void LoadState(SaveData saveData) (IEffect, EffectOn) HandleEffect(SaveInstruction instruction) { - var values = ((System.Text.Json.JsonElement)instruction.Values).EnumerateObject(); + var effect = (SaveInstruction.Effect)instruction; bool failed = false; - int? effectId = null; ConstructorInfo constructor = null; object[] effectStates = null; - EffectOn? effectOn = null; - foreach (var value in values) - { - //TODO Not hardcode/dynamic order? - if (value.NameEquals("Item1")) - { - effectId = value.Value.GetInt32(); - } - - if (value.NameEquals("Item2")) - { - if (effectId == null) - { - Logger.LogWarning( - $"[ModiBuff] Couldn't extract recipe save data because of missing effect id for {Name}"); - continue; - } - var effectType = _effectTypeIdManager.GetEffectType(effectId.Value); - //TODO Find constructor by saved types? Prob not worth - constructor = effectType.GetConstructors()[0]; + int? effectId = effect.EffectId; - var parameters = constructor.GetParameters(); - effectStates = new object[parameters.Length]; - int i = 0; - foreach (var property in value.Value.EnumerateObject()) - { - effectStates[i] = property.Value.GetDataFromJsonObject(parameters[i].ParameterType); - i++; - } - } + EffectOn? effectOn = effect.EffectOn; - if (value.NameEquals("Item3")) - { - effectOn = (EffectOn)value.Value.GetInt32(); - } + { + var effectType = _effectTypeIdManager.GetEffectType(effectId.Value); + //TODO Find constructor by saved types? Prob not worth + constructor = effectType.GetConstructors()[0]; + + var parameters = constructor.GetParameters(); + effectStates = new object[parameters.Length]; + int i = 0; + ; + //foreach (var property in (effect.SaveData as JsonDocument).EnumerateObject()) + //{ + // effectStates[i] = property.Value.GetDataFromJsonObject(parameters[i].ParameterType); + // i++; + //} } if (effectStates == null) @@ -191,6 +173,14 @@ public void LoadState(SaveData saveData) } } + [System.Text.Json.Serialization.JsonPolymorphic] + [System.Text.Json.Serialization.JsonDerivedType(typeof(Initialize), Initialize.Id)] + [System.Text.Json.Serialization.JsonDerivedType(typeof(Interval), Interval.Id)] + [System.Text.Json.Serialization.JsonDerivedType(typeof(Duration), Duration.Id)] + [System.Text.Json.Serialization.JsonDerivedType(typeof(Remove), Remove.Id)] + [System.Text.Json.Serialization.JsonDerivedType(typeof(Refresh), Refresh.Id)] + [System.Text.Json.Serialization.JsonDerivedType(typeof(Stack), Stack.Id)] + [System.Text.Json.Serialization.JsonDerivedType(typeof(Effect), Effect.Id)] public record SaveInstruction { public readonly object Values; @@ -209,16 +199,23 @@ public SaveInstruction(object values, int instructionId) public object[] GetValues(params Type[] types) => ((System.Text.Json.JsonElement)Values).GetValues(types); - public sealed record Initialize : SaveInstruction + public record Initialize : SaveInstruction { public const int Id = BaseId; + public readonly string Name; + public readonly string DisplayName; + public readonly string Description; + #if MODIBUFF_SYSTEM_TEXT_JSON [System.Text.Json.Serialization.JsonConstructor] #endif public Initialize(string name, string displayName, string description) : base((name, displayName, description), Id) { + Name = name; + DisplayName = displayName; + Description = description; } } @@ -226,11 +223,14 @@ public sealed record Interval : SaveInstruction { public const int Id = Initialize.Id + 1; + public readonly float Value; + #if MODIBUFF_SYSTEM_TEXT_JSON [System.Text.Json.Serialization.JsonConstructor] #endif - public Interval(float interval) : base(interval, Id) + public Interval(float value) : base(value, Id) { + Value = value; } } @@ -238,17 +238,24 @@ public sealed record Duration : SaveInstruction { public const int Id = Interval.Id + 1; + public readonly float Value; + #if MODIBUFF_SYSTEM_TEXT_JSON [System.Text.Json.Serialization.JsonConstructor] #endif - public Duration(float duration) : base(duration, Id) + public Duration(float value) : base(value, Id) { + Value = value; } } public sealed record Remove : SaveInstruction { - public const int Id = Duration.Id + 1; + public const int Id = SaveInstruction.Duration.Id + 1; + + public readonly Type RemoveType; + public readonly EffectOn EffectOn; + public readonly float Duration; public enum Type { @@ -259,14 +266,17 @@ public enum Type #if MODIBUFF_SYSTEM_TEXT_JSON [System.Text.Json.Serialization.JsonConstructor] #endif - public Remove(Type type, EffectOn effectOn, float duration = 0) - : base(type switch + public Remove(Type removeType, EffectOn effectOn, float duration = 0) + : base(removeType switch { - Type.RemoveOn => (type, effectOn), - Type.Duration => (type, duration), + Type.RemoveOn => (removeType, effectOn), + Type.Duration => (removeType, duration), _ => throw new ArgumentOutOfRangeException() }, Id) { + RemoveType = removeType; + EffectOn = effectOn; + Duration = duration; } } @@ -274,11 +284,14 @@ public sealed record Refresh : SaveInstruction { public const int Id = Remove.Id + 1; + public readonly RefreshType RefreshType; + #if MODIBUFF_SYSTEM_TEXT_JSON [System.Text.Json.Serialization.JsonConstructor] #endif - public Refresh(RefreshType type) : base(type, Id) + public Refresh(RefreshType refreshType) : base(refreshType, Id) { + RefreshType = refreshType; } } @@ -339,16 +352,23 @@ public CallbackUnit(int callbackUnit) : base(callbackUnit, Id) } } - public sealed record Effect : SaveInstruction + public record Effect : SaveInstruction { public const int Id = CallbackUnit.Id + 1; + public readonly int EffectId; + public readonly object SaveData; + public readonly EffectOn EffectOn; + #if MODIBUFF_SYSTEM_TEXT_JSON [System.Text.Json.Serialization.JsonConstructor] #endif public Effect(int effectId, object saveData, EffectOn effectOn) : base((effectId, saveData, effectOn), Id) { + EffectId = effectId; + SaveData = saveData; + EffectOn = effectOn; } } diff --git a/ModiBuff/ModiBuff/Core/Modifier/Creation/Recipe/ModifierRecipes.cs b/ModiBuff/ModiBuff/Core/Modifier/Creation/Recipe/ModifierRecipes.cs index 7df0f182..5fdb711a 100644 --- a/ModiBuff/ModiBuff/Core/Modifier/Creation/Recipe/ModifierRecipes.cs +++ b/ModiBuff/ModiBuff/Core/Modifier/Creation/Recipe/ModifierRecipes.cs @@ -214,9 +214,9 @@ public void LoadState(SaveData saveData) continue; #if MODIBUFF_SYSTEM_TEXT_JSON - object[] values = instruction.GetValues(typeof(string), typeof(string), typeof(string)); + var init = instruction as ModifierRecipe.SaveInstruction.Initialize; - if (values[0] == null) + if (init == null || init.Name == null) { //TODO Identification Logger.LogError( @@ -224,7 +224,7 @@ public void LoadState(SaveData saveData) break; } - recipe = Add((string)values[0], (string)values[1], (string)values[2]); + recipe = Add(init.Name, init.DisplayName, init.Description); #endif break; } diff --git a/ModiBuff/ModiBuff/ModiBuff.csproj b/ModiBuff/ModiBuff/ModiBuff.csproj index 1872886f..632ea026 100644 --- a/ModiBuff/ModiBuff/ModiBuff.csproj +++ b/ModiBuff/ModiBuff/ModiBuff.csproj @@ -30,7 +30,7 @@ - + From 276dbb0b2979de4006f2c187ad9ada039d68a4dd Mon Sep 17 00:00:00 2001 From: chillu Date: Sat, 3 Aug 2024 18:26:02 +0200 Subject: [PATCH 2/4] feat: recipe serialize record refactor polymorphism --- ModiBuff/ModiBuff.Tests/ModiBuff.Tests.csproj | 6 +- .../ModiBuff.Tests/SaveLoadRecipeTests.cs | 33 ++-- .../Creation/Recipe/ModifierRecipeSaveLoad.cs | 174 +++++++++--------- .../ModiBuff/Core/SerializationExtensions.cs | 7 +- 4 files changed, 102 insertions(+), 118 deletions(-) diff --git a/ModiBuff/ModiBuff.Tests/ModiBuff.Tests.csproj b/ModiBuff/ModiBuff.Tests/ModiBuff.Tests.csproj index 2929ae75..4ae788ea 100644 --- a/ModiBuff/ModiBuff.Tests/ModiBuff.Tests.csproj +++ b/ModiBuff/ModiBuff.Tests/ModiBuff.Tests.csproj @@ -2,16 +2,12 @@ disable - false - 7.2 - Library - net6.0 - false + MODIBUFF_SYSTEM_TEXT_JSON diff --git a/ModiBuff/ModiBuff.Tests/SaveLoadRecipeTests.cs b/ModiBuff/ModiBuff.Tests/SaveLoadRecipeTests.cs index c5ffea6e..53ad7356 100644 --- a/ModiBuff/ModiBuff.Tests/SaveLoadRecipeTests.cs +++ b/ModiBuff/ModiBuff.Tests/SaveLoadRecipeTests.cs @@ -32,7 +32,7 @@ private void SaveLoadStateAndSetup(ModifierRecipes saveRecipes) Setup(); } - //[Test] + [Test] public void SaveRecipeLoad() { var saveRecipes = new ModifierRecipes(IdManager, EffectTypeIdManager); @@ -65,7 +65,7 @@ public void SaveNamesRecipeLoad() Assert.AreEqual(modifierInfo.Description, description); } - //[Test] + [Test] public void SaveStackRecipeLoad() { var saveRecipes = new ModifierRecipes(IdManager, EffectTypeIdManager); @@ -82,7 +82,7 @@ public void SaveStackRecipeLoad() Assert.AreEqual(UnitHealth - 5 - 2 - 5 - 2 - 2, Unit.Health); } - //[Test] + [Test] public void SaveAddDamageRecipeLoad() { var saveRecipes = new ModifierRecipes(IdManager, EffectTypeIdManager); @@ -95,7 +95,7 @@ public void SaveAddDamageRecipeLoad() Assert.AreEqual(UnitDamage + 5, Unit.Damage); } - //[Test] + [Test] public void SaveIntervalRecipeLoad() { var saveRecipes = new ModifierRecipes(IdManager, EffectTypeIdManager); @@ -110,7 +110,7 @@ public void SaveIntervalRecipeLoad() Assert.AreEqual(UnitHealth - 5, Unit.Health); } - //[Test] + [Test] public void SaveModifierActionRecipeLoad() { var saveRecipes = new ModifierRecipes(IdManager, EffectTypeIdManager); @@ -130,7 +130,7 @@ public void SaveModifierActionRecipeLoad() Assert.AreEqual(UnitHealth - 5 - 5 - 5, Unit.Health); } - //[Test] + [Test] public void SaveRemoveRecipeLoad() { var saveRecipes = new ModifierRecipes(IdManager, EffectTypeIdManager); @@ -146,7 +146,7 @@ public void SaveRemoveRecipeLoad() Assert.False(Unit.ContainsModifier("RemoveDamage")); } - //[Test] + [Test] public void SaveRefreshIntervalRecipeLoad() { var saveRecipes = new ModifierRecipes(IdManager, EffectTypeIdManager); @@ -164,7 +164,7 @@ public void SaveRefreshIntervalRecipeLoad() Assert.AreEqual(UnitHealth, Unit.Health); } - //[Test] + [Test] public void SaveRefreshDurationRecipeLoad() { var saveRecipes = new ModifierRecipes(IdManager, EffectTypeIdManager); @@ -183,7 +183,7 @@ public void SaveRefreshDurationRecipeLoad() Assert.False(Unit.ContainsModifier("RefreshDuration")); } - //[Test] + [Test] public void SaveRemoveStackRecipeLoad() { var saveRecipes = new ModifierRecipes(IdManager, EffectTypeIdManager); @@ -200,12 +200,12 @@ public void SaveRemoveStackRecipeLoad() Assert.False(Unit.ContainsModifier("RemoveStack")); } - //[Test] + [Test] public void SaveRemoveCallbackUnitRecipeLoad() { var saveRecipes = new ModifierRecipes(IdManager, EffectTypeIdManager); saveRecipes.Add("InitAddDamageRevertibleCallback") - .Tag(ModiBuff.Core.Units.TagType.StrongDispel) + .Dispel(DispelType.Strong) .Effect(new AddDamageEffect(5, EffectState.IsRevertible), EffectOn.Init) .Remove(RemoveEffectOn.CallbackUnit) .CallbackUnit(CallbackUnitType.StrongDispel); @@ -213,12 +213,11 @@ public void SaveRemoveCallbackUnitRecipeLoad() SaveLoadStateAndSetup(saveRecipes); Unit.AddModifierSelf("InitAddDamageRevertibleCallback"); - Assert.AreEqual(UnitDamage + 5, Unit.Damage); - Unit.StrongDispel(Unit); + Unit.Dispel(DispelType.Strong, Unit); Assert.AreEqual(UnitDamage, Unit.Damage); } - //[Test] + [Test] public void SaveDispelRecipeLoad() { var saveRecipes = new ModifierRecipes(IdManager, EffectTypeIdManager); @@ -239,7 +238,7 @@ public void SaveDispelRecipeLoad() Assert.AreEqual(UnitHealth - 5, Unit.Health); } - //[Test] + [Test] public void SaveTagRecipeLoad() { var saveRecipes = new ModifierRecipes(IdManager, EffectTypeIdManager); @@ -253,7 +252,7 @@ public void SaveTagRecipeLoad() Assert.True(ModifierRecipes.GetTag(id).HasTag(TagType.DurationIgnoresStatusResistance)); } - //[Test] + [Test] public void SaveRemoveTagRecipeLoad() { var saveRecipes = new ModifierRecipes(IdManager, EffectTypeIdManager); @@ -267,7 +266,7 @@ public void SaveRemoveTagRecipeLoad() Assert.False(ModifierRecipes.GetTag(id).HasTag(TagType.Default)); } - //[Test] + [Test] public void SaveSetTagRecipeLoad() { var saveRecipes = new ModifierRecipes(IdManager, EffectTypeIdManager); diff --git a/ModiBuff/ModiBuff/Core/Modifier/Creation/Recipe/ModifierRecipeSaveLoad.cs b/ModiBuff/ModiBuff/Core/Modifier/Creation/Recipe/ModifierRecipeSaveLoad.cs index a19c1480..e4f03c69 100644 --- a/ModiBuff/ModiBuff/Core/Modifier/Creation/Recipe/ModifierRecipeSaveLoad.cs +++ b/ModiBuff/ModiBuff/Core/Modifier/Creation/Recipe/ModifierRecipeSaveLoad.cs @@ -1,6 +1,5 @@ using System; using System.Linq; -using System.Reflection; namespace ModiBuff.Core { @@ -43,62 +42,60 @@ public void LoadState(SaveData saveData) break; case SaveInstruction.Duration.Id: #if MODIBUFF_SYSTEM_TEXT_JSON - Duration(instruction.Values.GetDataFromJsonObject()); + Duration(((SaveInstruction.Duration)instruction).Value); #endif break; case SaveInstruction.Remove.Id: #if MODIBUFF_SYSTEM_TEXT_JSON - var removeType = - (SaveInstruction.Remove.Type)instruction.GetValues(typeof(SaveInstruction.Remove.Type))[0]; - switch (removeType) + var remove = (SaveInstruction.Remove)instruction; + switch (remove.RemoveType) { case SaveInstruction.Remove.Type.RemoveOn: - Remove(((EffectOn)instruction.GetValues(typeof(SaveInstruction.Remove.Type), - typeof(EffectOn))[1]).ToRemoveEffectOn()); + Remove(remove.EffectOn.ToRemoveEffectOn()); break; case SaveInstruction.Remove.Type.Duration: - Remove((float)instruction - .GetValues(typeof(SaveInstruction.Remove.Type), typeof(float))[1]); + Remove(remove.Duration); break; default: - Logger.LogError($"[ModiBuff] Loaded remove instruction with unknown type {removeType}"); + Logger.LogError( + $"[ModiBuff] Loaded remove instruction with unknown type {remove.RemoveType}"); break; } #endif break; case SaveInstruction.Refresh.Id: #if MODIBUFF_SYSTEM_TEXT_JSON - Refresh(instruction.Values.GetDataFromJsonObject()); + Refresh(((SaveInstruction.Refresh)instruction).RefreshType); #endif break; case SaveInstruction.Stack.Id: #if MODIBUFF_SYSTEM_TEXT_JSON - object[] stackValues = instruction.GetValues(typeof(WhenStackEffect), typeof(int), typeof(int), - typeof(float), typeof(float)); - Stack((WhenStackEffect)stackValues[0], (int)stackValues[1], (int)stackValues[2], - (float)stackValues[3], (float)stackValues[4]); + var stack = (SaveInstruction.Stack)instruction; + Stack(stack.WhenStackEffect, stack.MaxStacks, stack.EveryXStacks, stack.SingleStackTime, + stack.IndependentStackTime); #endif break; case SaveInstruction.Dispel.Id: #if MODIBUFF_SYSTEM_TEXT_JSON - Dispel(instruction.Values.GetDataFromJsonObject()); + Dispel(((SaveInstruction.Dispel)instruction).Type); #endif break; case SaveInstruction.Tag.Id: #if MODIBUFF_SYSTEM_TEXT_JSON - object[] tagValues = instruction.GetValues(typeof(SaveInstruction.Tag.Type), typeof(TagType)); - _ = (SaveInstruction.Tag.Type)tagValues[0] switch + var tag = (SaveInstruction.Tag)instruction; + _ = tag.InstructionTagType switch { - SaveInstruction.Tag.Type.Add => Tag((TagType)tagValues[1]), - SaveInstruction.Tag.Type.Remove => RemoveTag((TagType)tagValues[1]), - SaveInstruction.Tag.Type.Set => SetTag((TagType)tagValues[1]), + SaveInstruction.Tag.Type.Add => Tag(tag.TagType), + SaveInstruction.Tag.Type.Remove => RemoveTag(tag.TagType), + SaveInstruction.Tag.Type.Set => SetTag(tag.TagType), _ => throw new ArgumentOutOfRangeException() }; #endif break; case SaveInstruction.CallbackUnit.Id: #if MODIBUFF_SYSTEM_TEXT_JSON - CallbackUnit(instruction.Values.GetDataFromJsonObject()); + CallbackUnit( + (TUnitCallback)(object)((SaveInstruction.CallbackUnit)instruction).CallbackUnitType); #endif break; case SaveInstruction.Effect.Id: @@ -110,8 +107,8 @@ public void LoadState(SaveData saveData) break; case SaveInstruction.ModifierAction.Id: #if MODIBUFF_SYSTEM_TEXT_JSON - object[] actionValues = instruction.GetValues(typeof(ModifierAction), typeof(EffectOn)); - ModifierAction((ModifierAction)actionValues[0], (EffectOn)actionValues[1]); + var action = (SaveInstruction.ModifierAction)instruction; + ModifierAction(action.ModifierActionFlags, action.EffectOn); #endif break; default: @@ -124,52 +121,35 @@ public void LoadState(SaveData saveData) { var effect = (SaveInstruction.Effect)instruction; bool failed = false; - ConstructorInfo constructor = null; - object[] effectStates = null; - int? effectId = effect.EffectId; + int effectId = effect.EffectId; + EffectOn effectOn = effect.EffectOn; - EffectOn? effectOn = effect.EffectOn; + var effectType = _effectTypeIdManager.GetEffectType(effectId); + //TODO Find constructor by saved types? Prob not worth + var constructor = effectType.GetConstructors()[0]; + var parameters = constructor.GetParameters(); + object[] effectStates = new object[parameters.Length]; + int i = 0; + foreach (var property in ((System.Text.Json.JsonElement)effect.SaveData).EnumerateObject()) { - var effectType = _effectTypeIdManager.GetEffectType(effectId.Value); - //TODO Find constructor by saved types? Prob not worth - constructor = effectType.GetConstructors()[0]; - - var parameters = constructor.GetParameters(); - effectStates = new object[parameters.Length]; - int i = 0; - ; - //foreach (var property in (effect.SaveData as JsonDocument).EnumerateObject()) - //{ - // effectStates[i] = property.Value.GetDataFromJsonObject(parameters[i].ParameterType); - // i++; - //} - } - - if (effectStates == null) - { - Logger.LogError( - $"[ModiBuff] Failed to load effect state from save data by {Name} {(effectId != -1 ? $"for effect {effectId}" : "")}"); - failed = true; - } - - if (effectOn == null) - { - Logger.LogError($"[ModiBuff] Failed to load effect on from save data by {Name}"); - failed = true; - } + object value = property.Value.ToValue(parameters[i].ParameterType); + if (value == null) + { + Logger.LogError( + $"[ModiBuff] Failed to load effect state from save data by {Name} for effect {effectId}"); + failed = true; + } - if (effectId == null) - { - Logger.LogError($"[ModiBuff] Failed to load effect id from save data by {Name}"); - failed = true; + effectStates[i] = value; + i++; } if (failed) return (null, EffectOn.None); - return ((IEffect)constructor.Invoke(effectStates), effectOn.Value); + return ((IEffect)constructor.Invoke(effectStates), effectOn); } } @@ -180,10 +160,13 @@ public void LoadState(SaveData saveData) [System.Text.Json.Serialization.JsonDerivedType(typeof(Remove), Remove.Id)] [System.Text.Json.Serialization.JsonDerivedType(typeof(Refresh), Refresh.Id)] [System.Text.Json.Serialization.JsonDerivedType(typeof(Stack), Stack.Id)] + [System.Text.Json.Serialization.JsonDerivedType(typeof(Dispel), Dispel.Id)] + [System.Text.Json.Serialization.JsonDerivedType(typeof(Tag), Tag.Id)] + [System.Text.Json.Serialization.JsonDerivedType(typeof(CallbackUnit), CallbackUnit.Id)] [System.Text.Json.Serialization.JsonDerivedType(typeof(Effect), Effect.Id)] + [System.Text.Json.Serialization.JsonDerivedType(typeof(ModifierAction), ModifierAction.Id)] public record SaveInstruction { - public readonly object Values; public readonly int InstructionId; private const int BaseId = 0; @@ -191,14 +174,11 @@ public record SaveInstruction #if MODIBUFF_SYSTEM_TEXT_JSON [System.Text.Json.Serialization.JsonConstructor] #endif - public SaveInstruction(object values, int instructionId) + public SaveInstruction(int instructionId) { - Values = values; InstructionId = instructionId; } - public object[] GetValues(params Type[] types) => ((System.Text.Json.JsonElement)Values).GetValues(types); - public record Initialize : SaveInstruction { public const int Id = BaseId; @@ -210,8 +190,7 @@ public record Initialize : SaveInstruction #if MODIBUFF_SYSTEM_TEXT_JSON [System.Text.Json.Serialization.JsonConstructor] #endif - public Initialize(string name, string displayName, string description) - : base((name, displayName, description), Id) + public Initialize(string name, string displayName, string description) : base(Id) { Name = name; DisplayName = displayName; @@ -228,7 +207,7 @@ public sealed record Interval : SaveInstruction #if MODIBUFF_SYSTEM_TEXT_JSON [System.Text.Json.Serialization.JsonConstructor] #endif - public Interval(float value) : base(value, Id) + public Interval(float value) : base(Id) { Value = value; } @@ -243,7 +222,7 @@ public sealed record Duration : SaveInstruction #if MODIBUFF_SYSTEM_TEXT_JSON [System.Text.Json.Serialization.JsonConstructor] #endif - public Duration(float value) : base(value, Id) + public Duration(float value) : base(Id) { Value = value; } @@ -266,13 +245,7 @@ public enum Type #if MODIBUFF_SYSTEM_TEXT_JSON [System.Text.Json.Serialization.JsonConstructor] #endif - public Remove(Type removeType, EffectOn effectOn, float duration = 0) - : base(removeType switch - { - Type.RemoveOn => (removeType, effectOn), - Type.Duration => (removeType, duration), - _ => throw new ArgumentOutOfRangeException() - }, Id) + public Remove(Type removeType, EffectOn effectOn, float duration = 0) : base(Id) { RemoveType = removeType; EffectOn = effectOn; @@ -289,7 +262,7 @@ public sealed record Refresh : SaveInstruction #if MODIBUFF_SYSTEM_TEXT_JSON [System.Text.Json.Serialization.JsonConstructor] #endif - public Refresh(RefreshType refreshType) : base(refreshType, Id) + public Refresh(RefreshType refreshType) : base(Id) { RefreshType = refreshType; } @@ -299,13 +272,23 @@ public sealed record Stack : SaveInstruction { public const int Id = Refresh.Id + 1; + public readonly WhenStackEffect WhenStackEffect; + public readonly int MaxStacks; + public readonly int EveryXStacks; + public readonly float SingleStackTime; + public readonly float IndependentStackTime; + #if MODIBUFF_SYSTEM_TEXT_JSON [System.Text.Json.Serialization.JsonConstructor] #endif - public Stack(WhenStackEffect when, int maxStacks, int everyXStacks, - float singleStackTime, float independentStackTime) - : base((when, maxStacks, everyXStacks, singleStackTime, independentStackTime), Id) + public Stack(WhenStackEffect whenStackEffect, int maxStacks, int everyXStacks, float singleStackTime, + float independentStackTime) : base(Id) { + WhenStackEffect = whenStackEffect; + MaxStacks = maxStacks; + EveryXStacks = everyXStacks; + SingleStackTime = singleStackTime; + IndependentStackTime = independentStackTime; } } @@ -313,12 +296,12 @@ public sealed record Dispel : SaveInstruction { public const int Id = Stack.Id + 1; + public readonly DispelType Type; + #if MODIBUFF_SYSTEM_TEXT_JSON [System.Text.Json.Serialization.JsonConstructor] #endif - public Dispel(DispelType type) : base(type, Id) - { - } + public Dispel(DispelType type) : base(Id) => Type = type; } public sealed record Tag : SaveInstruction @@ -332,11 +315,16 @@ public enum Type Set, } + public readonly Type InstructionTagType; + public readonly TagType TagType; + #if MODIBUFF_SYSTEM_TEXT_JSON [System.Text.Json.Serialization.JsonConstructor] #endif - public Tag(Type type, TagType tagType) : base((type, tagType), Id) + public Tag(Type instructionTagType, TagType tagType) : base(Id) { + InstructionTagType = instructionTagType; + TagType = tagType; } } @@ -344,15 +332,18 @@ public sealed record CallbackUnit : SaveInstruction { public const int Id = Tag.Id + 1; + public readonly int CallbackUnitType; + #if MODIBUFF_SYSTEM_TEXT_JSON [System.Text.Json.Serialization.JsonConstructor] #endif - public CallbackUnit(int callbackUnit) : base(callbackUnit, Id) + public CallbackUnit(int callbackUnitType) : base(Id) { + CallbackUnitType = callbackUnitType; } } - public record Effect : SaveInstruction + public sealed record Effect : SaveInstruction { public const int Id = CallbackUnit.Id + 1; @@ -363,8 +354,7 @@ public record Effect : SaveInstruction #if MODIBUFF_SYSTEM_TEXT_JSON [System.Text.Json.Serialization.JsonConstructor] #endif - public Effect(int effectId, object saveData, EffectOn effectOn) - : base((effectId, saveData, effectOn), Id) + public Effect(int effectId, object saveData, EffectOn effectOn) : base(Id) { EffectId = effectId; SaveData = saveData; @@ -376,12 +366,16 @@ public sealed record ModifierAction : SaveInstruction { public const int Id = Effect.Id + 1; + public readonly ModiBuff.Core.ModifierAction ModifierActionFlags; + public readonly EffectOn EffectOn; + #if MODIBUFF_SYSTEM_TEXT_JSON [System.Text.Json.Serialization.JsonConstructor] #endif - public ModifierAction(ModiBuff.Core.ModifierAction modifierAction, EffectOn effectOn) - : base((modifierAction, effectOn), Id) + public ModifierAction(ModiBuff.Core.ModifierAction modifierActionFlags, EffectOn effectOn) : base(Id) { + ModifierActionFlags = modifierActionFlags; + EffectOn = effectOn; } } } diff --git a/ModiBuff/ModiBuff/Core/SerializationExtensions.cs b/ModiBuff/ModiBuff/Core/SerializationExtensions.cs index a756eac9..c38ecebb 100644 --- a/ModiBuff/ModiBuff/Core/SerializationExtensions.cs +++ b/ModiBuff/ModiBuff/Core/SerializationExtensions.cs @@ -62,11 +62,6 @@ public static T GetDataFromJsonObject(this object fromLoad) return TryGetDataFromJsonObject(fromLoad, out T data) ? data : default; } - public static object GetDataFromJsonObject(this object fromLoad, Type type) - { - return fromLoad is not System.Text.Json.JsonElement jsonElement ? null : jsonElement.ToValue(type); - } - private static readonly List valuesHolder = new List(); public static object[] GetValues(this System.Text.Json.JsonElement element, params Type[] types) @@ -95,7 +90,7 @@ public static object[] GetValues(this System.Text.Json.JsonElement element, para return values; } - private static object ToValue(this System.Text.Json.JsonElement element, Type type) + public static object ToValue(this System.Text.Json.JsonElement element, Type type) { if (type == typeof(int)) return element.GetInt32(); From 73b6370b69eb3afef4697e61b939808e4fa946d2 Mon Sep 17 00:00:00 2001 From: chillu Date: Sat, 3 Aug 2024 18:31:09 +0200 Subject: [PATCH 3/4] fix: revert old polymorphism serialization fix --- .../ModiBuff.Extensions.Serialization.Json/SaveController.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ModiBuff/ModiBuff.Extensions.Serialization.Json/SaveController.cs b/ModiBuff/ModiBuff.Extensions.Serialization.Json/SaveController.cs index aad54fa6..9c1aa6c7 100644 --- a/ModiBuff/ModiBuff.Extensions.Serialization.Json/SaveController.cs +++ b/ModiBuff/ModiBuff.Extensions.Serialization.Json/SaveController.cs @@ -22,8 +22,8 @@ public SaveController(string fileName) }; } - public string Save(T obj) => JsonSerializer.Serialize(obj, obj.GetType(), _options); - public string Save(GameState.SaveData obj) => JsonSerializer.Serialize(obj, obj.GetType(), _options); + public string Save(T obj) => JsonSerializer.Serialize(obj, _options); + public string Save(GameState.SaveData obj) => JsonSerializer.Serialize(obj, _options); public void SaveToPath(string json, string fileName) => File.WriteAllText(System.IO.Path.Combine(Path, fileName), json); From b7ff71fa22a01852f483ed172403c7a8881b489b Mon Sep 17 00:00:00 2001 From: chillu Date: Sat, 3 Aug 2024 18:37:46 +0200 Subject: [PATCH 4/4] fix: merge fixes --- ModiBuff/ModiBuff.Tests/ModiBuff.Tests.csproj | 1 - .../Core/Modifier/Creation/Recipe/ModifierRecipeSaveLoad.cs | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/ModiBuff/ModiBuff.Tests/ModiBuff.Tests.csproj b/ModiBuff/ModiBuff.Tests/ModiBuff.Tests.csproj index a146d609..5869f344 100644 --- a/ModiBuff/ModiBuff.Tests/ModiBuff.Tests.csproj +++ b/ModiBuff/ModiBuff.Tests/ModiBuff.Tests.csproj @@ -7,7 +7,6 @@ Library net6.0 false - MODIBUFF_SYSTEM_TEXT_JSON diff --git a/ModiBuff/ModiBuff/Core/Modifier/Creation/Recipe/ModifierRecipeSaveLoad.cs b/ModiBuff/ModiBuff/Core/Modifier/Creation/Recipe/ModifierRecipeSaveLoad.cs index a88e5e70..c220f237 100644 --- a/ModiBuff/ModiBuff/Core/Modifier/Creation/Recipe/ModifierRecipeSaveLoad.cs +++ b/ModiBuff/ModiBuff/Core/Modifier/Creation/Recipe/ModifierRecipeSaveLoad.cs @@ -155,6 +155,7 @@ public void LoadState(SaveData saveData) #endif } +#if MODIBUFF_SYSTEM_TEXT_JSON [System.Text.Json.Serialization.JsonPolymorphic] [System.Text.Json.Serialization.JsonDerivedType(typeof(Initialize), Initialize.Id)] [System.Text.Json.Serialization.JsonDerivedType(typeof(Interval), Interval.Id)] @@ -167,6 +168,7 @@ public void LoadState(SaveData saveData) [System.Text.Json.Serialization.JsonDerivedType(typeof(CallbackUnit), CallbackUnit.Id)] [System.Text.Json.Serialization.JsonDerivedType(typeof(Effect), Effect.Id)] [System.Text.Json.Serialization.JsonDerivedType(typeof(ModifierAction), ModifierAction.Id)] +#endif public record SaveInstruction { public readonly int InstructionId;