Parser now creates lists only when it is necessary
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
namespace ParadoxSaveParser.Lib;
|
||||
|
||||
[JsonSourceGenerationOptions(MaxDepth = 1024, WriteIndented = true)]
|
||||
[JsonSerializable(typeof(Dictionary<string, List<object>>))]
|
||||
[JsonSerializable(typeof(Dictionary<string, object>))]
|
||||
[JsonSerializable(typeof(List<object>))]
|
||||
[JsonSerializable(typeof(string))]
|
||||
[JsonSerializable(typeof(long))]
|
||||
|
||||
@@ -216,7 +216,7 @@ public class SaveParserEU4
|
||||
}
|
||||
|
||||
private static bool IsEmptyCollection(object value)
|
||||
=> value is Dictionary<string, List<object>> { Count: 0 } or List<object> { Count: 0 };
|
||||
=> value is Dictionary<string, object> { Count: 0 } or List<object> { Count: 0 };
|
||||
|
||||
// doesn't move next
|
||||
private object ParseListOrDict()
|
||||
@@ -264,9 +264,9 @@ public class SaveParserEU4
|
||||
}
|
||||
|
||||
// moves next
|
||||
private Dictionary<string, List<object>> ParseDict()
|
||||
private Dictionary<string, object> ParseDict()
|
||||
{
|
||||
Dictionary<string, List<object>> dict = new();
|
||||
Dictionary<string, object> 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<object>();
|
||||
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<object> existingList)
|
||||
existingList.Add(value);
|
||||
else dict[keyStr] = new List<object> { firstValue, value };
|
||||
}
|
||||
else
|
||||
{
|
||||
dict.Add(keyStr, value);
|
||||
}
|
||||
}
|
||||
|
||||
return dict;
|
||||
}
|
||||
|
||||
public Dictionary<string, List<object>> Parse()
|
||||
public Dictionary<string, object> Parse()
|
||||
{
|
||||
var root = ParseDict();
|
||||
return root;
|
||||
|
||||
Reference in New Issue
Block a user