code reformat and cleanup

This commit is contained in:
Timerix 2025-04-05 05:59:22 +05:00
parent 758388cda0
commit e9c7c8f5c1
10 changed files with 357 additions and 344 deletions

View File

@ -8,24 +8,24 @@ namespace ParadoxSaveParser.Lib.Tests;
[TestOf(typeof(ISearchExpression))] [TestOf(typeof(ISearchExpression))]
public class SearchExpressionTests public class SearchExpressionTests
{ {
byte[] _smallSaveData;
[SetUp] [SetUp]
public void Setup() public void Setup()
{ {
_smallSaveData = "EU4txt a={ b={ c=0 d=1 e=2 } f=3 }".ToBytes(); _smallSaveData = "EU4txt a={ b={ c=0 d=1 e=2 } f=3 }".ToBytes();
} }
private byte[] _smallSaveData;
private static JsonSerializerOptions _smallSaveSerializerOptions = new()
private static readonly JsonSerializerOptions _smallSaveSerializerOptions = new()
{ {
WriteIndented = false, WriteIndented = false,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
MaxDepth = 1024, MaxDepth = 1024
}; };
internal static string JsonToPdx(string json) => internal static string JsonToPdx(string json)
json.Substring(1, json.Length - 2) => json.Substring(1, json.Length - 2)
.Replace(",", " ").Replace("{", "{ ").Replace("}", " }") .Replace(",", " ").Replace("{", "{ ").Replace("}", " }")
.Replace("\"", "").Replace("[", "").Replace("]", "").Replace(":", "="); .Replace("\"", "").Replace("[", "").Replace("]", "").Replace(":", "=");

View File

@ -39,11 +39,11 @@ namespace ParadoxSaveParser.Lib;
/// </code> /// </code>
public class BufferedEnumerator<T> : IEnumerator<LinkedListNode<T>> public class BufferedEnumerator<T> : IEnumerator<LinkedListNode<T>>
{ {
private IEnumerator<T> _enumerator; private readonly int _bufferSize;
private int _bufferSize;
LinkedList<T> _llist = new();
private LinkedListNode<T>? _currentNode; private LinkedListNode<T>? _currentNode;
private int _currentNodeIndex = -1; private int _currentNodeIndex = -1;
private readonly IEnumerator<T> _enumerator;
private readonly LinkedList<T> _llist = new();
public BufferedEnumerator(IEnumerator<T> enumerator, int bufferSize) public BufferedEnumerator(IEnumerator<T> enumerator, int bufferSize)
{ {

View File

@ -11,12 +11,16 @@ namespace ParadoxSaveParser.Lib;
public class SaveParserEU4 public class SaveParserEU4
{ {
protected Stream _saveFile; protected Stream _saveFile;
private BufferedEnumerator<Token> _tokens;
private ISearchExpression? _searchExprCurrent; private ISearchExpression? _searchExprCurrent;
private readonly BufferedEnumerator<Token> _tokens;
/// <param name="savefile">Uncompressed stream of <c>gamestate</c> file which can be extracted from save archive</param> /// <param name="savefile">
/// <param name="query">Parsing whole save takes 10 seconds on mid pc and takes 1GB of RAM, /// Uncompressed stream of <c>gamestate</c> file which can be extracted from save archive
/// so you should specify what exactly you want to get from save file</param> /// </param>
/// <param name="query">
/// Parsing whole save takes 10 seconds on mid pc and takes 1GB of RAM,
/// so you should specify what exactly you want to get from save file
/// </param>
public SaveParserEU4(Stream savefile, ISearchExpression? query) public SaveParserEU4(Stream savefile, ISearchExpression? query)
{ {
_tokens = new BufferedEnumerator<Token>(LexTextSave(), 5); _tokens = new BufferedEnumerator<Token>(LexTextSave(), 5);
@ -24,57 +28,6 @@ public class SaveParserEU4
_searchExprCurrent = query; _searchExprCurrent = query;
} }
protected enum TokenType : byte
{
Invalid,
StringOrNumber,
Equals,
BracketOpen,
BracketClose,
}
protected struct Token
{
public required TokenType type;
public required short column;
public required int line;
public string? value;
public override string ToString()
{
string s;
switch (type)
{
case TokenType.Invalid:
s = "INVALID_TOKEN";
break;
case TokenType.StringOrNumber:
s = value ?? "NULL";
break;
case TokenType.Equals:
s = "=";
break;
case TokenType.BracketOpen:
s = "{";
break;
case TokenType.BracketClose:
s = "}";
break;
default:
throw new ArgumentOutOfRangeException(type.ToString());
}
return $"{line}:{column} '{s}'";
}
}
protected class UnexpectedTokenException : Exception
{
public UnexpectedTokenException(Token token) :
base($"Unexpected token: {token}")
{}
}
protected IEnumerator<Token> LexTextSave() protected IEnumerator<Token> LexTextSave()
{ {
string expectedHeader = "EU4txt"; string expectedHeader = "EU4txt";
@ -184,7 +137,7 @@ public class SaveParserEU4
// doesn't move next // doesn't move next
private object? ParseValue() private object? ParseValue()
{ {
Token tok = _tokens.Current.Value; var tok = _tokens.Current.Value;
switch (tok.type) switch (tok.type)
{ {
case TokenType.StringOrNumber: case TokenType.StringOrNumber:
@ -192,13 +145,13 @@ public class SaveParserEU4
return string.Empty; return string.Empty;
if (tok.value[0] != '-' && !char.IsDigit(tok.value[0])) if (tok.value[0] != '-' && !char.IsDigit(tok.value[0]))
return tok.value; return tok.value;
if(tok.value.Contains('.') && Double.TryParse(tok.value, out double d)) if (tok.value.Contains('.') && double.TryParse(tok.value, out double d))
return d; return d;
if (Int64.TryParse(tok.value, out long l)) if (long.TryParse(tok.value, out long l))
return l; return l;
return tok.value; return tok.value;
case TokenType.BracketOpen: case TokenType.BracketOpen:
var obj = ParseListOrDict(); object obj = ParseListOrDict();
return obj; return obj;
case TokenType.BracketClose: case TokenType.BracketClose:
return null; return null;
@ -212,7 +165,7 @@ public class SaveParserEU4
/// <returns>true if skipped value, false if current token is closing bracket</returns> /// <returns>true if skipped value, false if current token is closing bracket</returns>
private bool SkipValue() private bool SkipValue()
{ {
Token tok = _tokens.Current.Value; var tok = _tokens.Current.Value;
if (tok.type == TokenType.BracketOpen) if (tok.type == TokenType.BracketOpen)
{ {
SkipObject(); SkipObject();
@ -227,7 +180,7 @@ public class SaveParserEU4
{ {
while (bracketBalance != 0 && _tokens.MoveNext()) while (bracketBalance != 0 && _tokens.MoveNext())
{ {
Token tok = _tokens.Current.Value; var tok = _tokens.Current.Value;
if (tok.type == TokenType.BracketOpen) if (tok.type == TokenType.BracketOpen)
bracketBalance++; bracketBalance++;
else if (tok.type == TokenType.BracketClose) else if (tok.type == TokenType.BracketClose)
@ -259,6 +212,7 @@ public class SaveParserEU4
break; break;
list.Add(value); list.Add(value);
} }
return list; return list;
} }
@ -270,7 +224,7 @@ public class SaveParserEU4
// root is a dict without closing bracket, so this method must check _tokenIndex < _tokens.Count // root is a dict without closing bracket, so this method must check _tokenIndex < _tokens.Count
for (int localIndex = 0; _tokens.MoveNext(); localIndex++) for (int localIndex = 0; _tokens.MoveNext(); localIndex++)
{ {
Token tok = _tokens.Current.Value; var tok = _tokens.Current.Value;
// end of dictionary // end of dictionary
if (tok.type == TokenType.BracketClose) if (tok.type == TokenType.BracketClose)
break; break;
@ -303,7 +257,9 @@ public class SaveParserEU4
// Saves may contain object definition without `=`. // Saves may contain object definition without `=`.
// Example: `map_area_data {` instead of `map_area_data = {` // Example: `map_area_data {` instead of `map_area_data = {`
else if (tok.type != TokenType.BracketOpen) else if (tok.type != TokenType.BracketOpen)
{
throw new UnexpectedTokenException(tok); throw new UnexpectedTokenException(tok);
}
ISearchExpression? searchExprNext = null; ISearchExpression? searchExprNext = null;
if (_searchExprCurrent != null if (_searchExprCurrent != null
@ -320,11 +276,12 @@ public class SaveParserEU4
throw new UnexpectedTokenException(_tokens.Current.Value); throw new UnexpectedTokenException(_tokens.Current.Value);
_searchExprCurrent = searExpressionPrevious; _searchExprCurrent = searExpressionPrevious;
if(!dict.TryGetValue(key, out List<object>? list)) if (!dict.TryGetValue(key, out var list))
{ {
list = new List<object>(); list = new List<object>();
dict.Add(key, list); dict.Add(key, list);
} }
list.Add(value); list.Add(value);
} }
@ -336,4 +293,56 @@ public class SaveParserEU4
var root = ParseDict(); var root = ParseDict();
return root; return root;
} }
protected enum TokenType : byte
{
Invalid,
StringOrNumber,
Equals,
BracketOpen,
BracketClose
}
protected struct Token
{
public required TokenType type;
public required short column;
public required int line;
public string? value;
public override string ToString()
{
string s;
switch (type)
{
case TokenType.Invalid:
s = "INVALID_TOKEN";
break;
case TokenType.StringOrNumber:
s = value ?? "NULL";
break;
case TokenType.Equals:
s = "=";
break;
case TokenType.BracketOpen:
s = "{";
break;
case TokenType.BracketClose:
s = "}";
break;
default:
throw new ArgumentOutOfRangeException(type.ToString());
}
return $"{line}:{column} '{s}'";
}
}
protected class UnexpectedTokenException : Exception
{
public UnexpectedTokenException(Token token) :
base($"Unexpected token: {token}")
{
}
}
} }

View File

@ -9,8 +9,8 @@ public interface ISearchExpression
public static class SearchExpressionCompiler public static class SearchExpressionCompiler
{ {
private static bool CharEqualsAndNotEscaped(char c, ReadOnlySpan<char> chars, int i) => private static bool CharEqualsAndNotEscaped(char c, ReadOnlySpan<char> chars, int i)
chars[i] == c && (i < 1 || chars[i - 1] != '\\') && (i < 2 || chars[i - 2] != '\\'); => chars[i] == c && (i < 1 || chars[i - 1] != '\\') && (i < 2 || chars[i - 2] != '\\');
public static ISearchExpression Compile(ReadOnlySpan<char> query) public static ISearchExpression Compile(ReadOnlySpan<char> query)
{ {
@ -25,9 +25,13 @@ public static class SearchExpressionCompiler
for (int i = supExprBegin; i < query.Length && bracketBalance != 0; i++) for (int i = supExprBegin; i < query.Length && bracketBalance != 0; i++)
{ {
if (CharEqualsAndNotEscaped('(', query, i)) if (CharEqualsAndNotEscaped('(', query, i))
{
bracketBalance++; bracketBalance++;
}
else if (CharEqualsAndNotEscaped(')', query, i)) else if (CharEqualsAndNotEscaped(')', query, i))
{
bracketBalance--; bracketBalance--;
}
else if (bracketBalance == 1 && CharEqualsAndNotEscaped('|', query, i)) else if (bracketBalance == 1 && CharEqualsAndNotEscaped('|', query, i))
{ {
var subPart = query.Slice(supExprBegin, i - supExprBegin); var subPart = query.Slice(supExprBegin, i - supExprBegin);
@ -58,20 +62,16 @@ public static class SearchExpressionCompiler
break; break;
partBeforePointLength++; partBeforePointLength++;
} }
var part = query.Slice(0, partBeforePointLength); var part = query.Slice(0, partBeforePointLength);
ReadOnlySpan<char> remaining = default; ReadOnlySpan<char> remaining = default;
if (partBeforePointLength < query.Length) if (partBeforePointLength < query.Length)
remaining = query.Slice(partBeforePointLength + 1); remaining = query.Slice(partBeforePointLength + 1);
if (part is "*") if (part is "*") return new AnyMatchExpression(remaining.IsEmpty ? null : Compile(remaining));
{
return new AnyMatchExpression(remaining.IsEmpty ? null : Compile(remaining));
}
for (int j = 0; j < part.Length; j++) for (int j = 0; j < part.Length; j++)
{
if (CharEqualsAndNotEscaped('*', part, j)) if (CharEqualsAndNotEscaped('*', part, j))
throw new NotImplementedException("pattern matching other than '*' is not implemented yet"); throw new NotImplementedException("pattern matching other than '*' is not implemented yet");
}
if (part[0] is '[') if (part[0] is '[')
{ {
@ -97,12 +97,8 @@ public static class SearchExpressionCompiler
public bool DoesMatch(SearchArgs args, out ISearchExpression? nextSearchExpression) public bool DoesMatch(SearchArgs args, out ISearchExpression? nextSearchExpression)
{ {
foreach (var e in subExprs) foreach (var e in subExprs)
{
if (e.DoesMatch(args, out nextSearchExpression)) if (e.DoesMatch(args, out nextSearchExpression))
{
return true; return true;
}
}
nextSearchExpression = null; nextSearchExpression = null;
return false; return false;

View File

@ -5,9 +5,9 @@ namespace ParadoxSaveParser.WebAPI;
public class Config public class Config
{ {
public const int ActualVersion = 1; public const int ActualVersion = 1;
public string BaseUrl = "http://127.0.0.1:5226/";
public int Version = ActualVersion; public int Version = ActualVersion;
public string BaseUrl = "http://127.0.0.1:5226/";
public static Config FromDtsod(DtsodV23 d) public static Config FromDtsod(DtsodV23 d)
{ {
@ -23,11 +23,11 @@ public class Config
return cfg; return cfg;
} }
public DtsodV23 ToDtsod() => public DtsodV23 ToDtsod()
new() => new()
{ {
{ "version", Version }, { "version", Version },
{ "baseUrl", BaseUrl }, { "baseUrl", BaseUrl }
}; };
public override string ToString() => ToDtsod().ToString(); public override string ToString() => ToDtsod().ToString();

View File

@ -4,7 +4,10 @@ public static class PathHelper
{ {
public static readonly IOPath DATA_DIR = "data"; public static readonly IOPath DATA_DIR = "data";
public static readonly IOPath SAVES_DIR = Path.Concat(DATA_DIR, "saves"); public static readonly IOPath SAVES_DIR = Path.Concat(DATA_DIR, "saves");
public static IOPath GetMetaFilePath(string save_id) => Path.Concat(SAVES_DIR, save_id + ".meta.json"); public static IOPath GetMetaFilePath(string save_id) => Path.Concat(SAVES_DIR, save_id + ".meta.json");
public static IOPath GetSaveFilePath(string save_id) => Path.Concat(SAVES_DIR, save_id + ".eu4"); public static IOPath GetSaveFilePath(string save_id) => Path.Concat(SAVES_DIR, save_id + ".eu4");
public static IOPath GetParsedSaveFilePath(string save_id) => Path.Concat(SAVES_DIR, save_id + ".parsed.json"); public static IOPath GetParsedSaveFilePath(string save_id) => Path.Concat(SAVES_DIR, save_id + ".parsed.json");
} }

View File

@ -53,6 +53,17 @@ public partial class Program
return error.StatusCode; return error.StatusCode;
} }
internal static ValueOrError<string> GetRequestQueryValue(HttpListenerContext ctx, string paramName)
{
string[]? values = ctx.Request.QueryString.GetValues(paramName);
string? value = values?.FirstOrDefault();
if (string.IsNullOrEmpty(value))
return new ErrorMessage(HttpStatusCode.BadRequest,
$"No request parameter '{paramName}' provided");
return value;
}
public record ErrorMessage public record ErrorMessage
{ {
public ErrorMessage(HttpStatusCode statusCode, string message) public ErrorMessage(HttpStatusCode statusCode, string message)
@ -83,15 +94,4 @@ public partial class Program
public static implicit operator ValueOrError<T>(ErrorMessage e) => new(default, e); public static implicit operator ValueOrError<T>(ErrorMessage e) => new(default, e);
} }
internal static ValueOrError<string> GetRequestQueryValue(HttpListenerContext ctx, string paramName)
{
string[]? values = ctx.Request.QueryString.GetValues(paramName);
string? value = values?.FirstOrDefault();
if (string.IsNullOrEmpty(value))
return new ErrorMessage(HttpStatusCode.BadRequest,
$"No request parameter '{paramName}' provided");
return value;
}
} }

View File

@ -4,16 +4,23 @@ namespace ParadoxSaveParser.WebAPI;
public enum SaveFileProcessingStatus public enum SaveFileProcessingStatus
{ {
Initialized, Uploading, Uploaded, Parsing, SavingResults, Done Initialized,
Uploading,
Uploaded,
Parsing,
SavingResults,
Done
} }
public enum Game public enum Game
{ {
Unknown, EU4 Unknown,
EU4
} }
public class SaveFileMetadata public class SaveFileMetadata
{ {
private static readonly JsonSerializerOptions _jsonOptions = new() { WriteIndented = true };
public required string id { get; init; } public required string id { get; init; }
[JsonConverter(typeof(JsonStringEnumConverter))] [JsonConverter(typeof(JsonStringEnumConverter))]
@ -22,8 +29,6 @@ public class SaveFileMetadata
[JsonConverter(typeof(JsonStringEnumConverter))] [JsonConverter(typeof(JsonStringEnumConverter))]
public required SaveFileProcessingStatus status { get; set; } public required SaveFileProcessingStatus status { get; set; }
private static readonly JsonSerializerOptions _jsonOptions = new() { WriteIndented = true };
public void SaveToFile() public void SaveToFile()
{ {
using var metaFile = File.OpenWrite(PathHelper.GetMetaFilePath(id)); using var metaFile = File.OpenWrite(PathHelper.GetMetaFilePath(id));