From 2785ebc7a06c6a0ef005a9cedf48cc8e2f5230c7 Mon Sep 17 00:00:00 2001 From: gotmachine <24925209+gotmachine@users.noreply.github.com> Date: Sun, 16 Apr 2023 01:31:16 +0200 Subject: [PATCH] Fixed issue #98 : ConfigNodePerf patch ignoring the last key/value pair when parsing a node-less, values only input --- .../Performance/ConfigNodePerf.cs | 58 +++++++++++++------ 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/KSPCommunityFixes/Performance/ConfigNodePerf.cs b/KSPCommunityFixes/Performance/ConfigNodePerf.cs index 596e825..ff26146 100644 --- a/KSPCommunityFixes/Performance/ConfigNodePerf.cs +++ b/KSPCommunityFixes/Performance/ConfigNodePerf.cs @@ -1,15 +1,15 @@ //#define DEBUG_CONFIGNODE_PERF -//#define COMPARE_LOAD_RESULTS +//#define COMPARE_PARSE_RESULTS //#define CONFIGNODE_PERF_TEST using HarmonyLib; +using KSP.Localization; using System; +using System.Collections; using System.Collections.Generic; -using UnityEngine; -using static ConfigNode; using System.IO; -using KSP.Localization; using System.Text; -using System.Collections; +using UnityEngine; +using static ConfigNode; namespace KSPCommunityFixes.Performance { @@ -227,6 +227,14 @@ private static unsafe bool ConfigNode_Parse_Prefix(string s, ref ConfigNode __re { __result = ParseConfigNode(pszInput, len); } + +#if COMPARE_PARSE_RESULTS + ConfigNode stockNode = RecurseFormat(PreFormatConfig(s.Split('\n', '\r'))); + if (!AreNodesEqual(__result, stockNode, true)) + { + Debug.LogError($"[KSPCommunityFixes] ConfigNodePerf : mismatch in parsed node !\nSTOCK NODE\n{stockNode}\nKSPCF NODE\n{__result}"); + } +#endif } catch (Exception e) { @@ -289,15 +297,6 @@ private static bool ConfigNode_Load_Prefix(string fileFullName, bool bypassLocal _skipOtherPatches = false; var oldTime = sw.ElapsedMilliseconds; Debug.Log($"Ours: {_ourTime} (read {_readTime}), old: {oldTime + oldRead} (read {oldRead})"); -#if COMPARE_LOAD_RESULTS - if (!AreNodesEqual(__result, old, true)) - { - Debug.LogError("@@@@ Mismatch in data!"); - // to step through again - //AreNodesEqual(__result, old); - __result = old; - } -#endif #endif _doClean = oldClean; return false; @@ -745,13 +744,28 @@ private static unsafe ConfigNode ReadFile(string path) _readTime = sw.ElapsedMilliseconds; #endif + ConfigNode result; if (numChars == 0) - return new ConfigNode("root"); + { + result = new ConfigNode("root"); + } + else + { + fixed (char* pBase = chars) + { + result = ParseConfigNode(pBase, numChars); + } + } - fixed (char* pBase = chars) +#if COMPARE_PARSE_RESULTS + string input = new string(chars, 0, numChars); + ConfigNode stockNode = RecurseFormat(PreFormatConfig(input.Split('\n', '\r'))); + if (!AreNodesEqual(result, stockNode, true)) { - return ParseConfigNode(pBase, numChars); + Debug.LogError($"[KSPCommunityFixes] ConfigNodePerf : mismatch in parsed node !\nSTOCK NODE\n{stockNode}\nKSPCF NODE\n{result}"); } +#endif + return result; } public static unsafe ConfigNode ParseConfigNode(char* pBase, int numChars) @@ -977,11 +991,19 @@ public static unsafe ConfigNode ParseConfigNode(char* pBase, int numChars) } } } + + if (_nodeStack.Count == 1 && mode == ParseMode.SkipToValue || mode == ParseMode.ReadValue) + { + int len = lastNonWS - start + 1; + string val = len > 0 ? new string(pBase, start, len) : string.Empty; + _nodeStack.Peek()._values.values.Add(new Value(savedName, val)); + } + _nodeStack.Clear(); return node; } -#if DEBUG_CONFIGNODE_PERF || CONFIGNODE_PERF_TEST +#if DEBUG_CONFIGNODE_PERF || CONFIGNODE_PERF_TEST || COMPARE_PARSE_RESULTS private static string OldCleanupInput(string value) { value = value.Replace("\n", "");