code reformat and cleanup
This commit is contained in:
parent
758388cda0
commit
e9c7c8f5c1
@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
|
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
|
||||||
<PackageReference Include="DTLib" Version="1.6.5" />
|
<PackageReference Include="DTLib" Version="1.6.5"/>
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
|
||||||
<PackageReference Include="NUnit" Version="3.14.0"/>
|
<PackageReference Include="NUnit" Version="3.14.0"/>
|
||||||
<PackageReference Include="NUnit.Analyzers" Version="3.9.0"/>
|
<PackageReference Include="NUnit.Analyzers" Version="3.9.0"/>
|
||||||
@ -23,7 +23,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\ParadoxSaveParser.Lib\ParadoxSaveParser.Lib.csproj" />
|
<ProjectReference Include="..\ParadoxSaveParser.Lib\ParadoxSaveParser.Lib.csproj"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@ -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(":", "=");
|
||||||
|
|
||||||
|
|||||||
@ -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)
|
||||||
{
|
{
|
||||||
@ -53,7 +53,7 @@ public class BufferedEnumerator<T> : IEnumerator<LinkedListNode<T>>
|
|||||||
|
|
||||||
public bool MoveNext()
|
public bool MoveNext()
|
||||||
{
|
{
|
||||||
if(_currentNodeIndex >= _bufferSize / 2)
|
if (_currentNodeIndex >= _bufferSize / 2)
|
||||||
_llist.RemoveFirst();
|
_llist.RemoveFirst();
|
||||||
|
|
||||||
while (_llist.Count < _bufferSize && _enumerator.MoveNext())
|
while (_llist.Count < _bufferSize && _enumerator.MoveNext())
|
||||||
|
|||||||
@ -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";
|
||||||
@ -124,7 +77,7 @@ public class SaveParserEU4
|
|||||||
switch (c)
|
switch (c)
|
||||||
{
|
{
|
||||||
case -1:
|
case -1:
|
||||||
if(TryCompleteStringToken())
|
if (TryCompleteStringToken())
|
||||||
yield return strToken;
|
yield return strToken;
|
||||||
yield break;
|
yield break;
|
||||||
case '\"':
|
case '\"':
|
||||||
@ -134,17 +87,17 @@ public class SaveParserEU4
|
|||||||
case ' ':
|
case ' ':
|
||||||
case '\t':
|
case '\t':
|
||||||
case '\r':
|
case '\r':
|
||||||
if(TryCompleteStringToken())
|
if (TryCompleteStringToken())
|
||||||
yield return strToken;
|
yield return strToken;
|
||||||
break;
|
break;
|
||||||
case '\n':
|
case '\n':
|
||||||
if(TryCompleteStringToken())
|
if (TryCompleteStringToken())
|
||||||
yield return strToken;
|
yield return strToken;
|
||||||
line++;
|
line++;
|
||||||
column = 0;
|
column = 0;
|
||||||
break;
|
break;
|
||||||
case '=':
|
case '=':
|
||||||
if(TryCompleteStringToken())
|
if (TryCompleteStringToken())
|
||||||
yield return strToken;
|
yield return strToken;
|
||||||
yield return new Token
|
yield return new Token
|
||||||
{
|
{
|
||||||
@ -153,7 +106,7 @@ public class SaveParserEU4
|
|||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
case '{':
|
case '{':
|
||||||
if(TryCompleteStringToken())
|
if (TryCompleteStringToken())
|
||||||
yield return strToken;
|
yield return strToken;
|
||||||
yield return new Token
|
yield return new Token
|
||||||
{
|
{
|
||||||
@ -162,7 +115,7 @@ public class SaveParserEU4
|
|||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
case '}':
|
case '}':
|
||||||
if(TryCompleteStringToken())
|
if (TryCompleteStringToken())
|
||||||
yield return strToken;
|
yield return strToken;
|
||||||
yield return new Token
|
yield return new Token
|
||||||
{
|
{
|
||||||
@ -173,7 +126,7 @@ public class SaveParserEU4
|
|||||||
default:
|
default:
|
||||||
// Skip control characters, which are invisible and causing frontend bugs.
|
// Skip control characters, which are invisible and causing frontend bugs.
|
||||||
// I dont know why there are so many of them in strings.
|
// I dont know why there are so many of them in strings.
|
||||||
if(c >= 0x20)
|
if (c >= 0x20)
|
||||||
str.Append((char)c);
|
str.Append((char)c);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -184,21 +137,21 @@ 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:
|
||||||
if(string.IsNullOrEmpty(tok.value))
|
if (string.IsNullOrEmpty(tok.value))
|
||||||
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)
|
||||||
@ -250,15 +203,16 @@ public class SaveParserEU4
|
|||||||
private List<object> ParseList()
|
private List<object> ParseList()
|
||||||
{
|
{
|
||||||
List<object> list = new();
|
List<object> list = new();
|
||||||
while(true)
|
while (true)
|
||||||
{
|
{
|
||||||
if(!_tokens.MoveNext())
|
if (!_tokens.MoveNext())
|
||||||
throw new Exception("Unexpected end of file");
|
throw new Exception("Unexpected end of file");
|
||||||
object? value = ParseValue();
|
object? value = ParseValue();
|
||||||
if (value is null)
|
if (value is null)
|
||||||
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;
|
||||||
@ -285,13 +239,13 @@ public class SaveParserEU4
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(tok.type != TokenType.StringOrNumber)
|
if (tok.type != TokenType.StringOrNumber)
|
||||||
throw new UnexpectedTokenException(tok);
|
throw new UnexpectedTokenException(tok);
|
||||||
|
|
||||||
string key = tok.value!;
|
string key = tok.value!;
|
||||||
|
|
||||||
// next token should be `=` or `{`
|
// next token should be `=` or `{`
|
||||||
if(!_tokens.MoveNext())
|
if (!_tokens.MoveNext())
|
||||||
throw new UnexpectedTokenException(tok);
|
throw new UnexpectedTokenException(tok);
|
||||||
tok = _tokens.Current.Value;
|
tok = _tokens.Current.Value;
|
||||||
if (tok.type == TokenType.Equals)
|
if (tok.type == TokenType.Equals)
|
||||||
@ -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}")
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -9,15 +9,15 @@ 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)
|
||||||
{
|
{
|
||||||
if(query.IsEmpty)
|
if (query.IsEmpty)
|
||||||
throw new ArgumentNullException(nameof(query));
|
throw new ArgumentNullException(nameof(query));
|
||||||
|
|
||||||
if(query[0] is '(')
|
if (query[0] is '(')
|
||||||
{
|
{
|
||||||
var subExprs = new List<ISearchExpression>();
|
var subExprs = new List<ISearchExpression>();
|
||||||
int supExprBegin = 1;
|
int supExprBegin = 1;
|
||||||
@ -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);
|
||||||
@ -37,7 +41,7 @@ public static class SearchExpressionCompiler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(query[^1] != ')')
|
if (query[^1] != ')')
|
||||||
throw new NotImplementedException("Expressions after ')' are not supported");
|
throw new NotImplementedException("Expressions after ')' are not supported");
|
||||||
|
|
||||||
if (bracketBalance > 0)
|
if (bracketBalance > 0)
|
||||||
@ -54,24 +58,20 @@ public static class SearchExpressionCompiler
|
|||||||
int partBeforePointLength = 0;
|
int partBeforePointLength = 0;
|
||||||
while (partBeforePointLength < query.Length)
|
while (partBeforePointLength < query.Length)
|
||||||
{
|
{
|
||||||
if(CharEqualsAndNotEscaped('.', query, partBeforePointLength))
|
if (CharEqualsAndNotEscaped('.', query, partBeforePointLength))
|
||||||
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;
|
||||||
|
|||||||
@ -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)
|
||||||
{
|
{
|
||||||
@ -18,16 +18,16 @@ public class Config
|
|||||||
};
|
};
|
||||||
if (cfg.Version < ActualVersion)
|
if (cfg.Version < ActualVersion)
|
||||||
throw new Exception($"config is obsolete (config v{cfg.Version} < program v{ActualVersion})");
|
throw new Exception($"config is obsolete (config v{cfg.Version} < program v{ActualVersion})");
|
||||||
if(cfg.Version > ActualVersion)
|
if (cfg.Version > ActualVersion)
|
||||||
throw new Exception($"program is obsolete (config v{cfg.Version} > program v{ActualVersion})");
|
throw new Exception($"program is obsolete (config v{cfg.Version} > program v{ActualVersion})");
|
||||||
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();
|
||||||
|
|||||||
@ -9,10 +9,10 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\ParadoxSaveParser.Lib\ParadoxSaveParser.Lib.csproj" />
|
<ProjectReference Include="..\ParadoxSaveParser.Lib\ParadoxSaveParser.Lib.csproj"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="DTLib.Web" Version="1.2.2" />
|
<PackageReference Include="DTLib.Web" Version="1.2.2"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@ -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");
|
||||||
}
|
}
|
||||||
@ -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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@ -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));
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user