From 65b268637714204a7084a06bf3120d301bffb28b Mon Sep 17 00:00:00 2001 From: CptMoore <39010654+CptMoore@users.noreply.github.com> Date: Sun, 8 Dec 2024 22:47:26 +0100 Subject: [PATCH] Play around with more mechdefs. Somehow custom component settings trigger issues. --- ...JSONSerializationUtility_FromJSON_Patch.cs | 24 ++++++++++++---- ModTek/Util/HBSJsonUtils.cs | 28 ++++++++++--------- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/ModTek/Features/Profiler/Patches/JSONSerializationUtility_FromJSON_Patch.cs b/ModTek/Features/Profiler/Patches/JSONSerializationUtility_FromJSON_Patch.cs index d6a82858..ad87d1a5 100644 --- a/ModTek/Features/Profiler/Patches/JSONSerializationUtility_FromJSON_Patch.cs +++ b/ModTek/Features/Profiler/Patches/JSONSerializationUtility_FromJSON_Patch.cs @@ -12,11 +12,23 @@ namespace ModTek.Features.Profiler.Patches; +[HarmonyPatch(typeof(MechDef), nameof(MechDef.RefreshChassis))] +internal static class MechDef_RefreshChassis_Patch +{ + internal static void Prefix(ref bool __runOriginal, MechDef __instance) + { + if (__instance.dataManager == null && __instance.Description == null) + { + __runOriginal = false; + } + } +} + [HarmonyPatch] internal static class JSONSerializationUtility_FromJSON_Patch { private static string basePath; - private static bool testNewton = false; + private static bool testNewton = true; public static bool Prepare() { @@ -29,7 +41,7 @@ public static bool Prepare() } Directory.CreateDirectory(basePath); } - return ModTek.Enabled && ModTek.Config.ProfilerEnabled; + return ModTek.Enabled; // && ModTek.Config.ProfilerEnabled; } [HarmonyTargetMethods] @@ -106,7 +118,7 @@ public static void Prefix(object target, string json, ref MyState __state) { __state = new MyState(); - if (testNewton && target.GetType() == typeof(MechDef)) + if (testNewton) // && target.GetType() == typeof(MechDef) { s_newton.Start(); try @@ -119,7 +131,7 @@ public static void Prefix(object target, string json, ref MyState __state) } catch (Exception ex) { - Log.Main.Error?.Log("Error populating " + target.GetType(), ex); + Log.Main.Error?.Log("Error N PopulateObject SerializeObject" + target.GetType(), ex); } finally { @@ -135,7 +147,7 @@ public static void Postfix(object target, string json, ref MyState __state) { s_stopwatch.AddMeasurement(__state.Tracker.End()); - if (testNewton) + if (testNewton) // && target.GetType() == typeof(MechDef) { try { @@ -145,7 +157,7 @@ public static void Postfix(object target, string json, ref MyState __state) } catch (Exception ex) { - Log.Main.Error?.Log("Error populating " + target.GetType(), ex); + Log.Main.Error?.Log("Error H SerializeObject " + target.GetType(), ex); } } } diff --git a/ModTek/Util/HBSJsonUtils.cs b/ModTek/Util/HBSJsonUtils.cs index c533bf37..9c79a077 100644 --- a/ModTek/Util/HBSJsonUtils.cs +++ b/ModTek/Util/HBSJsonUtils.cs @@ -9,6 +9,7 @@ using HBS.Collections; using HBS.Util; using Newtonsoft.Json; +using Newtonsoft.Json.Converters; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Serialization; @@ -54,17 +55,20 @@ private static string FixHBSJsonCommas(string json) return s_fixMissingCommasInJson.Replace(json, "$1,\n$2"); } + private static readonly JsonSerializerSettings s_jsonSerializerSettings = new() + { + ContractResolver = new FastJsonContractResolver(), + NullValueHandling = NullValueHandling.Ignore, + Converters = [ + new TagSetConverter(), + new StringEnumConverter(), + ], + }; + // might work, only slightly tested internal static string SerializeObject(object target) { - return JsonConvert.SerializeObject( - target, - new JsonSerializerSettings - { - MetadataPropertyHandling = MetadataPropertyHandling.Ignore, - ContractResolver = new FastJsonContractResolver() - } - ); + return JsonConvert.SerializeObject(target, s_jsonSerializerSettings); } // does not work 100%, issues e.g. with MechDef and ChassisRefresh @@ -76,11 +80,7 @@ internal static void PopulateObject(object target, string json) var commentsStripped = JSONSerializationUtility.StripHBSCommentsFromJSON(json); var commasAdded = FixHBSJsonCommas(commentsStripped); var dictionariesFixed = FixStructures(commasAdded); - JsonConvert.PopulateObject(dictionariesFixed, target, new JsonSerializerSettings - { - ContractResolver = new FastJsonContractResolver(), - Converters = [new TagSetConverter()] - }); + JsonConvert.PopulateObject(dictionariesFixed, target, s_jsonSerializerSettings); } private static string FixStructures(string json) @@ -207,6 +207,8 @@ private static bool ExtractKeyValue( private class FastJsonContractResolver : DefaultContractResolver { + + private readonly Dictionary> _serializableMembersCache = new(); protected override List GetSerializableMembers(Type objectType) {