From 9143397a50d225a022d7bd4fd75f1f7b73d88cb0 Mon Sep 17 00:00:00 2001 From: NathanKell Date: Wed, 24 Aug 2022 00:21:39 -0700 Subject: [PATCH] Fix nested Persistent IConfigNode objects that each call LoadObjectFromConfig or CreateConfigFromObject (#86) --- .../Modding/PersistentIConfigNode.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/KSPCommunityFixes/Modding/PersistentIConfigNode.cs b/KSPCommunityFixes/Modding/PersistentIConfigNode.cs index 1923854..fbdc230 100644 --- a/KSPCommunityFixes/Modding/PersistentIConfigNode.cs +++ b/KSPCommunityFixes/Modding/PersistentIConfigNode.cs @@ -95,6 +95,30 @@ private static void ConfigNode_LoadObjectFromConfig_Postfix(LoadState __state) readLinks = __state.links; } + private static bool ConfigNode_WriteObject_Prefix(object obj, ConfigNode node, int pass) + { + __state = writeLinks; + } + + // and restore it + private static void ConfigNode_CreateConfigFromObject_Postfix(WriteLinkList __state) + { + writeLinks = __state; + } + + // This will fail if nested, so we cache off the old readlinks and remove. + private static void ConfigNode_LoadObjectFromConfig_Prefix(out LoadState __state) + { + __state = new LoadState(removeAfterUse, readLinks); + } + + // and restore them + private static void ConfigNode_LoadObjectFromConfig_Postfix(LoadState __state) + { + removeAfterUse = __state.wasRemove; + readLinks = __state.links; + } + // used by TypeCache since we can't make the below method return a value. public static bool WriteSuccess;