From 74d09c51a0067a8e69f840b5664893bd3a0f0780 Mon Sep 17 00:00:00 2001 From: Timerix Date: Thu, 10 Apr 2025 16:12:48 +0500 Subject: [PATCH] Parser now creates lists only when it is necessary --- ParadoxSaveParser.CLI/Modes/Search.cs | 2 +- .../ParsedValueJsonContext.cs | 2 +- ParadoxSaveParser.Lib/SaveParserEU4.cs | 37 ++++++++++++------- .../SaveDataFilters/ISaveDataFilter.cs | 2 +- .../SaveDataFilters/SaveFilterEU4.cs | 11 +++--- 5 files changed, 31 insertions(+), 23 deletions(-) diff --git a/ParadoxSaveParser.CLI/Modes/Search.cs b/ParadoxSaveParser.CLI/Modes/Search.cs index 993a10d..b4c89cd 100644 --- a/ParadoxSaveParser.CLI/Modes/Search.cs +++ b/ParadoxSaveParser.CLI/Modes/Search.cs @@ -38,7 +38,7 @@ internal static partial class Modes var parser = new SaveParserEU4(inputStream, searchExpression); var parsedValue = parser.Parse(); JsonSerializer.Serialize(outputStream, parsedValue, - ParsedValueJsonContext.Default.DictionaryStringListObject); + ParsedValueJsonContext.Default.DictionaryStringObject); outputStream.WriteByte((byte)'\n'); } finally diff --git a/ParadoxSaveParser.Lib/ParsedValueJsonContext.cs b/ParadoxSaveParser.Lib/ParsedValueJsonContext.cs index 6ba94c2..5fff6fa 100644 --- a/ParadoxSaveParser.Lib/ParsedValueJsonContext.cs +++ b/ParadoxSaveParser.Lib/ParsedValueJsonContext.cs @@ -3,7 +3,7 @@ namespace ParadoxSaveParser.Lib; [JsonSourceGenerationOptions(MaxDepth = 1024, WriteIndented = true)] -[JsonSerializable(typeof(Dictionary>))] +[JsonSerializable(typeof(Dictionary))] [JsonSerializable(typeof(List))] [JsonSerializable(typeof(string))] [JsonSerializable(typeof(long))] diff --git a/ParadoxSaveParser.Lib/SaveParserEU4.cs b/ParadoxSaveParser.Lib/SaveParserEU4.cs index 899f67d..8cee521 100644 --- a/ParadoxSaveParser.Lib/SaveParserEU4.cs +++ b/ParadoxSaveParser.Lib/SaveParserEU4.cs @@ -216,7 +216,7 @@ public class SaveParserEU4 } private static bool IsEmptyCollection(object value) - => value is Dictionary> { Count: 0 } or List { Count: 0 }; + => value is Dictionary { Count: 0 } or List { Count: 0 }; // doesn't move next private object ParseListOrDict() @@ -264,9 +264,9 @@ public class SaveParserEU4 } // moves next - private Dictionary> ParseDict() + private Dictionary ParseDict() { - Dictionary> dict = new(); + Dictionary dict = new(); // root is a dict without closing bracket, so this method must check _tokenIndex < _tokens.Count for (int localIndex = 0; _tokens.MoveNext(); localIndex++) @@ -327,23 +327,32 @@ public class SaveParserEU4 string keyStr = keySB.ToString(); _stringBuilderPool.Return(keySB); - if (!dict.TryGetValue(keyStr, out var list)) - { - list = new List(); - dict.Add(keyStr, list); - } - - // do dot add empty collections into list - if (IsEmptyCollection(value)) - continue; - list.Add(value); + // Paradox save format has another way of defining list: + // a = 1 + // a = 2 + // It means `a = { 1 2 }` + if (dict.TryGetValue(keyStr, out var firstValue)) + { + // Do dot add empty collections into list. + // `key:{}` is okay, but i don't want to see `key:[{},{},{},{},{},{}]` + if (IsEmptyCollection(value)) + continue; + + if (firstValue is List existingList) + existingList.Add(value); + else dict[keyStr] = new List { firstValue, value }; + } + else + { + dict.Add(keyStr, value); + } } return dict; } - public Dictionary> Parse() + public Dictionary Parse() { var root = ParseDict(); return root; diff --git a/ParadoxSaveParser.WebAPI/SaveDataFilters/ISaveDataFilter.cs b/ParadoxSaveParser.WebAPI/SaveDataFilters/ISaveDataFilter.cs index a54838d..8aed66c 100644 --- a/ParadoxSaveParser.WebAPI/SaveDataFilters/ISaveDataFilter.cs +++ b/ParadoxSaveParser.WebAPI/SaveDataFilters/ISaveDataFilter.cs @@ -5,5 +5,5 @@ public interface ISaveDataFilter public string SearchString { get; } public ISearchExpression SearchExpression { get; } - public void Apply(Dictionary> data); + public void Apply(Dictionary data); } \ No newline at end of file diff --git a/ParadoxSaveParser.WebAPI/SaveDataFilters/SaveFilterEU4.cs b/ParadoxSaveParser.WebAPI/SaveDataFilters/SaveFilterEU4.cs index 04ad3ca..1188c37 100644 --- a/ParadoxSaveParser.WebAPI/SaveDataFilters/SaveFilterEU4.cs +++ b/ParadoxSaveParser.WebAPI/SaveDataFilters/SaveFilterEU4.cs @@ -1,5 +1,5 @@ using System.Linq; -using ParsedDict = System.Collections.Generic.Dictionary>; +using ParsedDict = System.Collections.Generic.Dictionary; namespace ParadoxSaveParser.WebAPI.SaveDataFilters; @@ -38,12 +38,11 @@ public class SaveDataFilterEU4 : ISaveDataFilter public void Apply(ParsedDict data) { - var countries = (ParsedDict)data["countries"][0]; + var countries = (ParsedDict)data["countries"]; var countries_filtered = countries.Where(pair - => pair.Value.Count > 0 - && ((ParsedDict)pair.Value[0]).TryGetValue("raw_development", out var raw_development) - && (double)raw_development[0] > 0 + => ((ParsedDict)pair.Value).TryGetValue("raw_development", out var raw_development) + && (double)raw_development > 0 ).ToDictionary(); - data["countries"][0] = countries_filtered; + data["countries"] = countries_filtered; } } \ No newline at end of file