Compare commits
2 Commits
e9c7c8f5c1
...
f3106769d9
| Author | SHA1 | Date | |
|---|---|---|---|
| f3106769d9 | |||
| b0fefbf667 |
@ -37,35 +37,73 @@ namespace ParadoxSaveParser.Lib;
|
|||||||
/// 2 3 | 4 | 5
|
/// 2 3 | 4 | 5
|
||||||
/// 3 4 | 5 |
|
/// 3 4 | 5 |
|
||||||
/// </code>
|
/// </code>
|
||||||
public class BufferedEnumerator<T> : IEnumerator<LinkedListNode<T>>
|
public class BufferedEnumerator<T> : IEnumerator<BufferedEnumerator<T>.Node>
|
||||||
{
|
{
|
||||||
private readonly int _bufferSize;
|
public class Node
|
||||||
private LinkedListNode<T>? _currentNode;
|
{
|
||||||
private int _currentNodeIndex = -1;
|
#nullable disable
|
||||||
|
public Node Previous;
|
||||||
|
public Node Next;
|
||||||
|
public T Value;
|
||||||
|
#nullable enable
|
||||||
|
}
|
||||||
|
|
||||||
private readonly IEnumerator<T> _enumerator;
|
private readonly IEnumerator<T> _enumerator;
|
||||||
private readonly LinkedList<T> _llist = new();
|
private readonly Node[] _ringBuffer;
|
||||||
|
private Node? _currentNode;
|
||||||
|
private int _currentBufferIndex = -1;
|
||||||
|
private int _lastValueIndex = -1;
|
||||||
|
|
||||||
public BufferedEnumerator(IEnumerator<T> enumerator, int bufferSize)
|
public BufferedEnumerator(IEnumerator<T> enumerator, int bufferSize)
|
||||||
{
|
{
|
||||||
_enumerator = enumerator;
|
_enumerator = enumerator;
|
||||||
_bufferSize = bufferSize;
|
_ringBuffer = new Node[bufferSize];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void InitBuffer()
|
||||||
|
{
|
||||||
|
_ringBuffer[0] = new Node
|
||||||
|
{
|
||||||
|
Value = default!
|
||||||
|
};
|
||||||
|
for (int i = 1; i < _ringBuffer.Length; i++)
|
||||||
|
{
|
||||||
|
_ringBuffer[i] = new Node
|
||||||
|
{
|
||||||
|
Previous = _ringBuffer[i - 1],
|
||||||
|
Value = default!,
|
||||||
|
};
|
||||||
|
_ringBuffer[i - 1].Next = _ringBuffer[i];
|
||||||
|
}
|
||||||
|
_ringBuffer[^1].Next = _ringBuffer[0];
|
||||||
|
_ringBuffer[0].Previous = _ringBuffer[^1];
|
||||||
|
}
|
||||||
|
|
||||||
public bool MoveNext()
|
public bool MoveNext()
|
||||||
{
|
{
|
||||||
if (_currentNodeIndex >= _bufferSize / 2)
|
if (_currentBufferIndex == -1)
|
||||||
_llist.RemoveFirst();
|
|
||||||
|
|
||||||
while (_llist.Count < _bufferSize && _enumerator.MoveNext())
|
|
||||||
{
|
{
|
||||||
_llist.AddLast(_enumerator.Current);
|
InitBuffer();
|
||||||
|
|
||||||
|
int beforeMidpoint = _ringBuffer.Length / 2 - 1;
|
||||||
|
for (int i = 0; i <= beforeMidpoint && _enumerator.MoveNext(); i++)
|
||||||
|
{
|
||||||
|
_ringBuffer[i].Value = _enumerator.Current;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (_llist.Count == 0)
|
|
||||||
|
_currentBufferIndex = (_currentBufferIndex + 1) % _ringBuffer.Length;
|
||||||
|
if (_enumerator.MoveNext())
|
||||||
|
{
|
||||||
|
int midpoint = (_currentBufferIndex + _ringBuffer.Length / 2) % _ringBuffer.Length;
|
||||||
|
_ringBuffer[midpoint].Value = _enumerator.Current;
|
||||||
|
_lastValueIndex = midpoint;
|
||||||
|
}
|
||||||
|
if(_currentBufferIndex == (_lastValueIndex + 1) % _ringBuffer.Length)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
_currentNodeIndex++;
|
_currentNode = _ringBuffer[_currentBufferIndex];
|
||||||
_currentNode = _currentNode is null ? _llist.First : _currentNode.Next;
|
return true;
|
||||||
return _currentNode is not null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Reset()
|
public void Reset()
|
||||||
@ -73,7 +111,7 @@ public class BufferedEnumerator<T> : IEnumerator<LinkedListNode<T>>
|
|||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public LinkedListNode<T> Current => _currentNode!;
|
public Node Current => _currentNode!;
|
||||||
|
|
||||||
object IEnumerator.Current => Current;
|
object IEnumerator.Current => Current;
|
||||||
|
|
||||||
|
|||||||
@ -4,5 +4,8 @@
|
|||||||
<ImplicitUsings>disable</ImplicitUsings>
|
<ImplicitUsings>disable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="9.0.3" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
global using System.Collections.Generic;
|
global using System.Collections.Generic;
|
||||||
global using System.IO;
|
global using System.IO;
|
||||||
global using System.Text;
|
global using System.Text;
|
||||||
|
using Microsoft.Extensions.ObjectPool;
|
||||||
|
|
||||||
namespace ParadoxSaveParser.Lib;
|
namespace ParadoxSaveParser.Lib;
|
||||||
|
|
||||||
@ -10,9 +11,13 @@ namespace ParadoxSaveParser.Lib;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class SaveParserEU4
|
public class SaveParserEU4
|
||||||
{
|
{
|
||||||
protected Stream _saveFile;
|
protected readonly Stream _saveFile;
|
||||||
private ISearchExpression? _searchExprCurrent;
|
|
||||||
private readonly BufferedEnumerator<Token> _tokens;
|
private readonly BufferedEnumerator<Token> _tokens;
|
||||||
|
private readonly ObjectPool<StringBuilder> _stringBuilderPool;
|
||||||
|
private ISearchExpression? _searchExprCurrent;
|
||||||
|
|
||||||
|
public int SBPoolGetCount = 0;
|
||||||
|
public int SBPoolReturnCount = 0;
|
||||||
|
|
||||||
/// <param name="savefile">
|
/// <param name="savefile">
|
||||||
/// Uncompressed stream of <c>gamestate</c> file which can be extracted from save archive
|
/// Uncompressed stream of <c>gamestate</c> file which can be extracted from save archive
|
||||||
@ -23,9 +28,16 @@ public class SaveParserEU4
|
|||||||
/// </param>
|
/// </param>
|
||||||
public SaveParserEU4(Stream savefile, ISearchExpression? query)
|
public SaveParserEU4(Stream savefile, ISearchExpression? query)
|
||||||
{
|
{
|
||||||
_tokens = new BufferedEnumerator<Token>(LexTextSave(), 5);
|
|
||||||
_saveFile = savefile;
|
_saveFile = savefile;
|
||||||
_searchExprCurrent = query;
|
_searchExprCurrent = query;
|
||||||
|
const int tokenBufSize = 5;
|
||||||
|
_tokens = new BufferedEnumerator<Token>(LexTextSave(), tokenBufSize);
|
||||||
|
_stringBuilderPool = new DefaultObjectPool<StringBuilder>(
|
||||||
|
new StringBuilderPooledObjectPolicy
|
||||||
|
{
|
||||||
|
InitialCapacity = tokenBufSize * 13,
|
||||||
|
MaximumRetainedCapacity = tokenBufSize * 13,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
protected IEnumerator<Token> LexTextSave()
|
protected IEnumerator<Token> LexTextSave()
|
||||||
@ -37,7 +49,8 @@ public class SaveParserEU4
|
|||||||
if (headStr != expectedHeader)
|
if (headStr != expectedHeader)
|
||||||
throw new Exception($"Invalid gamestate header. Expected '{expectedHeader}', got '{headStr}'.");
|
throw new Exception($"Invalid gamestate header. Expected '{expectedHeader}', got '{headStr}'.");
|
||||||
|
|
||||||
StringBuilder str = new();
|
StringBuilder strb = _stringBuilderPool.Get();
|
||||||
|
SBPoolGetCount++;
|
||||||
int line = 2;
|
int line = 2;
|
||||||
int column = 0;
|
int column = 0;
|
||||||
bool isQuoteOpen = false;
|
bool isQuoteOpen = false;
|
||||||
@ -46,7 +59,8 @@ public class SaveParserEU4
|
|||||||
{
|
{
|
||||||
type = TokenType.Invalid,
|
type = TokenType.Invalid,
|
||||||
column = -1,
|
column = -1,
|
||||||
line = -1
|
line = -1,
|
||||||
|
value = null,
|
||||||
};
|
};
|
||||||
|
|
||||||
bool TryCompleteStringToken()
|
bool TryCompleteStringToken()
|
||||||
@ -55,17 +69,18 @@ public class SaveParserEU4
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
// strings in quotes may be empty
|
// strings in quotes may be empty
|
||||||
if (!isStrInQuotes && (str.Length <= 0 || str[0] == '#'))
|
if (!isStrInQuotes && (strb.Length <= 0 || strb[0] == '#'))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
strToken = new Token
|
strToken = new Token
|
||||||
{
|
{
|
||||||
type = TokenType.StringOrNumber,
|
type = TokenType.StringOrNumber,
|
||||||
column = (short)(column - str.Length),
|
column = (short)(column - strb.Length),
|
||||||
line = line,
|
line = line,
|
||||||
value = str.ToString()
|
value = strb,
|
||||||
};
|
};
|
||||||
str.Clear();
|
strb = _stringBuilderPool.Get();
|
||||||
|
SBPoolGetCount++;
|
||||||
isStrInQuotes = false;
|
isStrInQuotes = false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -79,6 +94,8 @@ public class SaveParserEU4
|
|||||||
case -1:
|
case -1:
|
||||||
if (TryCompleteStringToken())
|
if (TryCompleteStringToken())
|
||||||
yield return strToken;
|
yield return strToken;
|
||||||
|
_stringBuilderPool.Return(strb);
|
||||||
|
SBPoolReturnCount++;
|
||||||
yield break;
|
yield break;
|
||||||
case '\"':
|
case '\"':
|
||||||
isQuoteOpen = !isQuoteOpen;
|
isQuoteOpen = !isQuoteOpen;
|
||||||
@ -127,10 +144,13 @@ public class SaveParserEU4
|
|||||||
// 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);
|
strb.Append((char)c);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_stringBuilderPool.Return(strb);
|
||||||
|
SBPoolReturnCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -141,15 +161,16 @@ public class SaveParserEU4
|
|||||||
switch (tok.type)
|
switch (tok.type)
|
||||||
{
|
{
|
||||||
case TokenType.StringOrNumber:
|
case TokenType.StringOrNumber:
|
||||||
if (string.IsNullOrEmpty(tok.value))
|
string tokStr = tok.value!.ToString();
|
||||||
return string.Empty;
|
_stringBuilderPool.Return(tok.value);
|
||||||
if (tok.value[0] != '-' && !char.IsDigit(tok.value[0]))
|
SBPoolReturnCount++;
|
||||||
return tok.value;
|
if (tokStr[0] != '-' && !char.IsDigit(tokStr[0]))
|
||||||
if (tok.value.Contains('.') && double.TryParse(tok.value, out double d))
|
return tokStr;
|
||||||
|
if (tokStr.Contains('.') && double.TryParse(tokStr, out double d))
|
||||||
return d;
|
return d;
|
||||||
if (long.TryParse(tok.value, out long l))
|
if (long.TryParse(tokStr, out long l))
|
||||||
return l;
|
return l;
|
||||||
return tok.value;
|
return tokStr;
|
||||||
case TokenType.BracketOpen:
|
case TokenType.BracketOpen:
|
||||||
object obj = ParseListOrDict();
|
object obj = ParseListOrDict();
|
||||||
return obj;
|
return obj;
|
||||||
@ -166,13 +187,22 @@ public class SaveParserEU4
|
|||||||
private bool SkipValue()
|
private bool SkipValue()
|
||||||
{
|
{
|
||||||
var tok = _tokens.Current.Value;
|
var tok = _tokens.Current.Value;
|
||||||
if (tok.type == TokenType.BracketOpen)
|
switch (tok.type)
|
||||||
{
|
{
|
||||||
SkipObject();
|
case TokenType.BracketOpen:
|
||||||
return true;
|
SkipObject();
|
||||||
|
return true;
|
||||||
|
case TokenType.StringOrNumber:
|
||||||
|
_stringBuilderPool.Return(tok.value!);
|
||||||
|
SBPoolReturnCount++;
|
||||||
|
return true;
|
||||||
|
case TokenType.Equals:
|
||||||
|
return true;
|
||||||
|
case TokenType.BracketClose:
|
||||||
|
return false;
|
||||||
|
default:
|
||||||
|
throw new UnexpectedTokenException(tok);
|
||||||
}
|
}
|
||||||
|
|
||||||
return tok.type != TokenType.BracketClose;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// skips all tokens inside curly braces block
|
// skips all tokens inside curly braces block
|
||||||
@ -185,6 +215,11 @@ public class SaveParserEU4
|
|||||||
bracketBalance++;
|
bracketBalance++;
|
||||||
else if (tok.type == TokenType.BracketClose)
|
else if (tok.type == TokenType.BracketClose)
|
||||||
bracketBalance--;
|
bracketBalance--;
|
||||||
|
else if (tok.type == TokenType.StringOrNumber)
|
||||||
|
{
|
||||||
|
_stringBuilderPool.Return(tok.value!);
|
||||||
|
SBPoolReturnCount++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -242,7 +277,7 @@ public class SaveParserEU4
|
|||||||
if (tok.type != TokenType.StringOrNumber)
|
if (tok.type != TokenType.StringOrNumber)
|
||||||
throw new UnexpectedTokenException(tok);
|
throw new UnexpectedTokenException(tok);
|
||||||
|
|
||||||
string key = tok.value!;
|
var keySB = tok.value!;
|
||||||
|
|
||||||
// next token should be `=` or `{`
|
// next token should be `=` or `{`
|
||||||
if (!_tokens.MoveNext())
|
if (!_tokens.MoveNext())
|
||||||
@ -263,9 +298,11 @@ public class SaveParserEU4
|
|||||||
|
|
||||||
ISearchExpression? searchExprNext = null;
|
ISearchExpression? searchExprNext = null;
|
||||||
if (_searchExprCurrent != null
|
if (_searchExprCurrent != null
|
||||||
&& !_searchExprCurrent.DoesMatch(new SearchArgs(key, localIndex), out searchExprNext))
|
&& !_searchExprCurrent.DoesMatch(new SearchArgs(localIndex, keySB), out searchExprNext))
|
||||||
{
|
{
|
||||||
SkipValue();
|
SkipValue();
|
||||||
|
_stringBuilderPool.Return(keySB);
|
||||||
|
SBPoolReturnCount++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -276,10 +313,13 @@ public class SaveParserEU4
|
|||||||
throw new UnexpectedTokenException(_tokens.Current.Value);
|
throw new UnexpectedTokenException(_tokens.Current.Value);
|
||||||
_searchExprCurrent = searExpressionPrevious;
|
_searchExprCurrent = searExpressionPrevious;
|
||||||
|
|
||||||
if (!dict.TryGetValue(key, out var list))
|
string keyStr = keySB.ToString();
|
||||||
|
_stringBuilderPool.Return(keySB);
|
||||||
|
SBPoolReturnCount++;
|
||||||
|
if (!dict.TryGetValue(keyStr, out var list))
|
||||||
{
|
{
|
||||||
list = new List<object>();
|
list = new List<object>();
|
||||||
dict.Add(key, list);
|
dict.Add(keyStr, list);
|
||||||
}
|
}
|
||||||
|
|
||||||
list.Add(value);
|
list.Add(value);
|
||||||
@ -308,7 +348,7 @@ public class SaveParserEU4
|
|||||||
public required TokenType type;
|
public required TokenType type;
|
||||||
public required short column;
|
public required short column;
|
||||||
public required int line;
|
public required int line;
|
||||||
public string? value;
|
public StringBuilder? value;
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
@ -319,7 +359,9 @@ public class SaveParserEU4
|
|||||||
s = "INVALID_TOKEN";
|
s = "INVALID_TOKEN";
|
||||||
break;
|
break;
|
||||||
case TokenType.StringOrNumber:
|
case TokenType.StringOrNumber:
|
||||||
s = value ?? "NULL";
|
if (value == null || value.Length == 0)
|
||||||
|
s = "NULL";
|
||||||
|
else s = value.ToString();
|
||||||
break;
|
break;
|
||||||
case TokenType.Equals:
|
case TokenType.Equals:
|
||||||
s = "=";
|
s = "=";
|
||||||
|
|||||||
@ -1,6 +1,25 @@
|
|||||||
namespace ParadoxSaveParser.Lib;
|
namespace ParadoxSaveParser.Lib;
|
||||||
|
|
||||||
public record SearchArgs(string key, int localIndex);
|
public record SearchArgs
|
||||||
|
{
|
||||||
|
public readonly string KeyStr;
|
||||||
|
public readonly StringBuilder? KeySB;
|
||||||
|
public readonly int LocalIndex;
|
||||||
|
|
||||||
|
public SearchArgs(int localIndex, string keyStr)
|
||||||
|
{
|
||||||
|
KeyStr = keyStr;
|
||||||
|
KeySB = null;
|
||||||
|
LocalIndex = localIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SearchArgs(int localIndex, StringBuilder keySb)
|
||||||
|
{
|
||||||
|
KeyStr = string.Empty;
|
||||||
|
KeySB = keySb;
|
||||||
|
LocalIndex = localIndex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public interface ISearchExpression
|
public interface ISearchExpression
|
||||||
{
|
{
|
||||||
@ -109,7 +128,7 @@ public static class SearchExpressionCompiler
|
|||||||
{
|
{
|
||||||
public bool DoesMatch(SearchArgs args, out ISearchExpression? nextSearchExpression)
|
public bool DoesMatch(SearchArgs args, out ISearchExpression? nextSearchExpression)
|
||||||
{
|
{
|
||||||
if (args.localIndex == index)
|
if (args.LocalIndex == index)
|
||||||
{
|
{
|
||||||
nextSearchExpression = next;
|
nextSearchExpression = next;
|
||||||
return true;
|
return true;
|
||||||
@ -124,7 +143,7 @@ public static class SearchExpressionCompiler
|
|||||||
{
|
{
|
||||||
public bool DoesMatch(SearchArgs args, out ISearchExpression? nextSearchExpression)
|
public bool DoesMatch(SearchArgs args, out ISearchExpression? nextSearchExpression)
|
||||||
{
|
{
|
||||||
if (args.key == key)
|
if ((args.KeySB != null && args.KeySB.Equals(key)) || args.KeyStr == key)
|
||||||
{
|
{
|
||||||
nextSearchExpression = next;
|
nextSearchExpression = next;
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@ -12,6 +12,7 @@ global using Directory = DTLib.Filesystem.Directory;
|
|||||||
global using File = DTLib.Filesystem.File;
|
global using File = DTLib.Filesystem.File;
|
||||||
global using Path = DTLib.Filesystem.Path;
|
global using Path = DTLib.Filesystem.Path;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text.Encodings.Web;
|
using System.Text.Encodings.Web;
|
||||||
using DTLib.Dtsod;
|
using DTLib.Dtsod;
|
||||||
@ -54,6 +55,21 @@ public static partial class Program
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
Stopwatch stopwatch = new();
|
||||||
|
using var save = File.OpenRead("data/gamestate");
|
||||||
|
stopwatch.Start();
|
||||||
|
var parser = new SaveParserEU4(save, SearchExpressionCompiler.Compile("saved_event_target"));
|
||||||
|
var result = parser.Parse();
|
||||||
|
stopwatch.Stop();
|
||||||
|
using (var resultFile = File.OpenWrite("data/parsed.json"))
|
||||||
|
{
|
||||||
|
JsonSerializer.Serialize(resultFile, result, _saveSerializerOptions);
|
||||||
|
}
|
||||||
|
Console.WriteLine($"get: {parser.SBPoolGetCount} return: {parser.SBPoolReturnCount} " +
|
||||||
|
$"delta: {parser.SBPoolGetCount - parser.SBPoolReturnCount}");
|
||||||
|
Console.WriteLine(stopwatch.Elapsed);
|
||||||
|
return;
|
||||||
|
|
||||||
// config
|
// config
|
||||||
if (!File.Exists(_configPath))
|
if (!File.Exists(_configPath))
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user