Skip to content

Commit

Permalink
Fixed issue #98 : ConfigNodePerf patch ignoring the last key/value pa…
Browse files Browse the repository at this point in the history
…ir when parsing a node-less, values only input
  • Loading branch information
gotmachine committed Apr 15, 2023
1 parent 041a524 commit 2785ebc
Showing 1 changed file with 40 additions and 18 deletions.
58 changes: 40 additions & 18 deletions KSPCommunityFixes/Performance/ConfigNodePerf.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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", "");
Expand Down

0 comments on commit 2785ebc

Please sign in to comment.