Parser now creates lists only when it is necessary
This commit is contained in:
parent
08f1d7b0f5
commit
74d09c51a0
@ -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
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -5,5 +5,5 @@ public interface ISaveDataFilter
|
||||
public string SearchString { get; }
|
||||
public ISearchExpression SearchExpression { get; }
|
||||
|
||||
public void Apply(Dictionary<string, List<object>> data);
|
||||
public void Apply(Dictionary<string, object> data);
|
||||
}
|
||||
@ -1,5 +1,5 @@
|
||||
using System.Linq;
|
||||
using ParsedDict = System.Collections.Generic.Dictionary<string, System.Collections.Generic.List<object>>;
|
||||
using ParsedDict = System.Collections.Generic.Dictionary<string, object>;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user