From 895e9f88a39610f2080acdbd72f178768b540093 Mon Sep 17 00:00:00 2001 From: chillu Date: Thu, 27 Jun 2024 19:23:59 +0200 Subject: [PATCH] feat: unsupported callback serialization --- .../SaveController.cs | 7 ++++++- .../Creation/Recipe/ModifierRecipeSaveLoad.cs | 7 +++++++ .../Creation/Recipe/SpecialInstructionEffects.cs | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/ModiBuff/ModiBuff.Extensions.Serialization.Json/SaveController.cs b/ModiBuff/ModiBuff.Extensions.Serialization.Json/SaveController.cs index ec29525..9c1aa6c 100644 --- a/ModiBuff/ModiBuff.Extensions.Serialization.Json/SaveController.cs +++ b/ModiBuff/ModiBuff.Extensions.Serialization.Json/SaveController.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Text.Json; +using System.Text.Json.Serialization; using ModiBuff.Core.Units; namespace ModiBuff.Extensions.Serialization.Json @@ -14,7 +15,11 @@ public SaveController(string fileName) { Path = Environment.CurrentDirectory; - _options = new JsonSerializerOptions { WriteIndented = true, IncludeFields = true }; + _options = new JsonSerializerOptions + { + WriteIndented = true, IncludeFields = true, + //DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault + }; } public string Save(T obj) => JsonSerializer.Serialize(obj, _options); diff --git a/ModiBuff/ModiBuff/Core/Modifier/Creation/Recipe/ModifierRecipeSaveLoad.cs b/ModiBuff/ModiBuff/Core/Modifier/Creation/Recipe/ModifierRecipeSaveLoad.cs index 48a086f..e2bec46 100644 --- a/ModiBuff/ModiBuff/Core/Modifier/Creation/Recipe/ModifierRecipeSaveLoad.cs +++ b/ModiBuff/ModiBuff/Core/Modifier/Creation/Recipe/ModifierRecipeSaveLoad.cs @@ -10,8 +10,15 @@ public SaveData SaveState() { if (_unsavableEffects.Count > 0) { + var unsupportedInstructionEffects = _unsavableEffects + .Where(SpecialInstructionEffects.IsUnsupportedEffectType).ToArray(); var nonSpecialInstructionEffects = _unsavableEffects .Where(e => !SpecialInstructionEffects.IsSpecialInstructionEffect(e)).ToArray(); + + if (unsupportedInstructionEffects.Length > 0) + Logger.LogError("[ModiBuff] Saving recipe with unsupported effects: " + + string.Join(", ", unsupportedInstructionEffects.Select(e => e.Name))); + if (nonSpecialInstructionEffects.Length > 0) Logger.LogWarning("[ModiBuff] Saving recipe with unsavable effects, please implement " + $"{nameof(ISaveableRecipeEffect)} for the following effects: " + diff --git a/ModiBuff/ModiBuff/Core/Modifier/Creation/Recipe/SpecialInstructionEffects.cs b/ModiBuff/ModiBuff/Core/Modifier/Creation/Recipe/SpecialInstructionEffects.cs index 4ffdd9a..c01ec78 100644 --- a/ModiBuff/ModiBuff/Core/Modifier/Creation/Recipe/SpecialInstructionEffects.cs +++ b/ModiBuff/ModiBuff/Core/Modifier/Creation/Recipe/SpecialInstructionEffects.cs @@ -10,6 +10,7 @@ namespace ModiBuff.Core public static class SpecialInstructionEffects //TODO Rename { private static readonly HashSet effectTypes; + private static readonly HashSet unsupportedEffectTypes; static SpecialInstructionEffects() { @@ -20,6 +21,15 @@ static SpecialInstructionEffects() typeof(DispelRegisterEffect), typeof(CallbackUnitRegisterEffect<>), }; + + unsupportedEffectTypes = new HashSet(new TypeEqualityComparer()) + { + typeof(CallbackRegisterEffect<>), + typeof(CallbackStateSaveRegisterEffect<,>), + typeof(CallbackEffectRegisterEffect<>), + typeof(CallbackStateEffectRegisterEffect<,>), + typeof(CallbackEffectRegisterEffectUnits<>), + }; } public static bool AddSpecialInstructionEffect(Type effectType) => effectTypes.Add(effectType); @@ -28,6 +38,10 @@ static SpecialInstructionEffects() public static bool IsSpecialInstructionEffect() where T : IEffect => IsSpecialInstructionEffect(typeof(T)); public static bool IsSpecialInstructionEffect(IEffect effect) => IsSpecialInstructionEffect(effect.GetType()); + public static bool IsUnsupportedEffectType(Type effectType) => unsupportedEffectTypes.Contains(effectType); + public static bool IsUnsupportedEffectType() where T : IEffect => IsUnsupportedEffectType(typeof(T)); + public static bool IsUnsupportedEffectType(IEffect effect) => IsUnsupportedEffectType(effect.GetType()); + /// /// Checks that the types are of same type, if both types are generic, only checks the top level type. ///