Compare commits

..

No commits in common. "f3106769d9f4d40d2ffe4ef5535eb24ef0d21b2f" and "e9c7c8f5c16df017b569548bae7c2038ab0d0bb2" have entirely different histories.

5 changed files with 47 additions and 165 deletions

View File

@ -37,73 +37,35 @@ namespace ParadoxSaveParser.Lib;
/// 2 3 | 4 | 5
/// 3 4 | 5 |
/// </code>
public class BufferedEnumerator<T> : IEnumerator<BufferedEnumerator<T>.Node>
public class BufferedEnumerator<T> : IEnumerator<LinkedListNode<T>>
{
public class Node
{
#nullable disable
public Node Previous;
public Node Next;
public T Value;
#nullable enable
}
private readonly int _bufferSize;
private LinkedListNode<T>? _currentNode;
private int _currentNodeIndex = -1;
private readonly IEnumerator<T> _enumerator;
private readonly Node[] _ringBuffer;
private Node? _currentNode;
private int _currentBufferIndex = -1;
private int _lastValueIndex = -1;
private readonly LinkedList<T> _llist = new();
public BufferedEnumerator(IEnumerator<T> enumerator, int bufferSize)
{
_enumerator = enumerator;
_ringBuffer = new Node[bufferSize];
_bufferSize = 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()
{
if (_currentBufferIndex == -1)
{
InitBuffer();
int beforeMidpoint = _ringBuffer.Length / 2 - 1;
for (int i = 0; i <= beforeMidpoint && _enumerator.MoveNext(); i++)
{
_ringBuffer[i].Value = _enumerator.Current;
}
}
if (_currentNodeIndex >= _bufferSize / 2)
_llist.RemoveFirst();
_currentBufferIndex = (_currentBufferIndex + 1) % _ringBuffer.Length;
if (_enumerator.MoveNext())
while (_llist.Count < _bufferSize && _enumerator.MoveNext())
{
int midpoint = (_currentBufferIndex + _ringBuffer.Length / 2) % _ringBuffer.Length;
_ringBuffer[midpoint].Value = _enumerator.Current;
_lastValueIndex = midpoint;
_llist.AddLast(_enumerator.Current);
}
if(_currentBufferIndex == (_lastValueIndex + 1) % _ringBuffer.Length)
if (_llist.Count == 0)
return false;
_currentNode = _ringBuffer[_currentBufferIndex];
return true;
_currentNodeIndex++;
_currentNode = _currentNode is null ? _llist.First : _currentNode.Next;
return _currentNode is not null;
}
public void Reset()
@ -111,7 +73,7 @@ public class BufferedEnumerator<T> : IEnumerator<BufferedEnumerator<T>.Node>
throw new NotImplementedException();
}
public Node Current => _currentNode!;
public LinkedListNode<T> Current => _currentNode!;
object IEnumerator.Current => Current;

View File

@ -4,8 +4,5 @@
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="9.0.3" />
</ItemGroup>
</Project>

View File

@ -2,7 +2,6 @@
global using System.Collections.Generic;
global using System.IO;
global using System.Text;
using Microsoft.Extensions.ObjectPool;
namespace ParadoxSaveParser.Lib;
@ -11,13 +10,9 @@ namespace ParadoxSaveParser.Lib;
/// </summary>
public class SaveParserEU4
{
protected readonly Stream _saveFile;
private readonly BufferedEnumerator<Token> _tokens;
private readonly ObjectPool<StringBuilder> _stringBuilderPool;
protected Stream _saveFile;
private ISearchExpression? _searchExprCurrent;
public int SBPoolGetCount = 0;
public int SBPoolReturnCount = 0;
private readonly BufferedEnumerator<Token> _tokens;
/// <param name="savefile">
/// Uncompressed stream of <c>gamestate</c> file which can be extracted from save archive
@ -28,16 +23,9 @@ public class SaveParserEU4
/// </param>
public SaveParserEU4(Stream savefile, ISearchExpression? query)
{
_tokens = new BufferedEnumerator<Token>(LexTextSave(), 5);
_saveFile = savefile;
_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()
@ -49,8 +37,7 @@ public class SaveParserEU4
if (headStr != expectedHeader)
throw new Exception($"Invalid gamestate header. Expected '{expectedHeader}', got '{headStr}'.");
StringBuilder strb = _stringBuilderPool.Get();
SBPoolGetCount++;
StringBuilder str = new();
int line = 2;
int column = 0;
bool isQuoteOpen = false;
@ -59,8 +46,7 @@ public class SaveParserEU4
{
type = TokenType.Invalid,
column = -1,
line = -1,
value = null,
line = -1
};
bool TryCompleteStringToken()
@ -69,18 +55,17 @@ public class SaveParserEU4
return false;
// strings in quotes may be empty
if (!isStrInQuotes && (strb.Length <= 0 || strb[0] == '#'))
if (!isStrInQuotes && (str.Length <= 0 || str[0] == '#'))
return false;
strToken = new Token
{
type = TokenType.StringOrNumber,
column = (short)(column - strb.Length),
column = (short)(column - str.Length),
line = line,
value = strb,
value = str.ToString()
};
strb = _stringBuilderPool.Get();
SBPoolGetCount++;
str.Clear();
isStrInQuotes = false;
return true;
}
@ -94,8 +79,6 @@ public class SaveParserEU4
case -1:
if (TryCompleteStringToken())
yield return strToken;
_stringBuilderPool.Return(strb);
SBPoolReturnCount++;
yield break;
case '\"':
isQuoteOpen = !isQuoteOpen;
@ -144,13 +127,10 @@ public class SaveParserEU4
// Skip control characters, which are invisible and causing frontend bugs.
// I dont know why there are so many of them in strings.
if (c >= 0x20)
strb.Append((char)c);
str.Append((char)c);
break;
}
}
_stringBuilderPool.Return(strb);
SBPoolReturnCount++;
}
@ -161,16 +141,15 @@ public class SaveParserEU4
switch (tok.type)
{
case TokenType.StringOrNumber:
string tokStr = tok.value!.ToString();
_stringBuilderPool.Return(tok.value);
SBPoolReturnCount++;
if (tokStr[0] != '-' && !char.IsDigit(tokStr[0]))
return tokStr;
if (tokStr.Contains('.') && double.TryParse(tokStr, out double d))
if (string.IsNullOrEmpty(tok.value))
return string.Empty;
if (tok.value[0] != '-' && !char.IsDigit(tok.value[0]))
return tok.value;
if (tok.value.Contains('.') && double.TryParse(tok.value, out double d))
return d;
if (long.TryParse(tokStr, out long l))
if (long.TryParse(tok.value, out long l))
return l;
return tokStr;
return tok.value;
case TokenType.BracketOpen:
object obj = ParseListOrDict();
return obj;
@ -187,22 +166,13 @@ public class SaveParserEU4
private bool SkipValue()
{
var tok = _tokens.Current.Value;
switch (tok.type)
if (tok.type == TokenType.BracketOpen)
{
case TokenType.BracketOpen:
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);
SkipObject();
return true;
}
return tok.type != TokenType.BracketClose;
}
// skips all tokens inside curly braces block
@ -215,11 +185,6 @@ public class SaveParserEU4
bracketBalance++;
else if (tok.type == TokenType.BracketClose)
bracketBalance--;
else if (tok.type == TokenType.StringOrNumber)
{
_stringBuilderPool.Return(tok.value!);
SBPoolReturnCount++;
}
}
}
@ -277,7 +242,7 @@ public class SaveParserEU4
if (tok.type != TokenType.StringOrNumber)
throw new UnexpectedTokenException(tok);
var keySB = tok.value!;
string key = tok.value!;
// next token should be `=` or `{`
if (!_tokens.MoveNext())
@ -298,11 +263,9 @@ public class SaveParserEU4
ISearchExpression? searchExprNext = null;
if (_searchExprCurrent != null
&& !_searchExprCurrent.DoesMatch(new SearchArgs(localIndex, keySB), out searchExprNext))
&& !_searchExprCurrent.DoesMatch(new SearchArgs(key, localIndex), out searchExprNext))
{
SkipValue();
_stringBuilderPool.Return(keySB);
SBPoolReturnCount++;
continue;
}
@ -313,13 +276,10 @@ public class SaveParserEU4
throw new UnexpectedTokenException(_tokens.Current.Value);
_searchExprCurrent = searExpressionPrevious;
string keyStr = keySB.ToString();
_stringBuilderPool.Return(keySB);
SBPoolReturnCount++;
if (!dict.TryGetValue(keyStr, out var list))
if (!dict.TryGetValue(key, out var list))
{
list = new List<object>();
dict.Add(keyStr, list);
dict.Add(key, list);
}
list.Add(value);
@ -348,7 +308,7 @@ public class SaveParserEU4
public required TokenType type;
public required short column;
public required int line;
public StringBuilder? value;
public string? value;
public override string ToString()
{
@ -359,9 +319,7 @@ public class SaveParserEU4
s = "INVALID_TOKEN";
break;
case TokenType.StringOrNumber:
if (value == null || value.Length == 0)
s = "NULL";
else s = value.ToString();
s = value ?? "NULL";
break;
case TokenType.Equals:
s = "=";

View File

@ -1,25 +1,6 @@
namespace ParadoxSaveParser.Lib;
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 record SearchArgs(string key, int localIndex);
public interface ISearchExpression
{
@ -128,7 +109,7 @@ public static class SearchExpressionCompiler
{
public bool DoesMatch(SearchArgs args, out ISearchExpression? nextSearchExpression)
{
if (args.LocalIndex == index)
if (args.localIndex == index)
{
nextSearchExpression = next;
return true;
@ -143,7 +124,7 @@ public static class SearchExpressionCompiler
{
public bool DoesMatch(SearchArgs args, out ISearchExpression? nextSearchExpression)
{
if ((args.KeySB != null && args.KeySB.Equals(key)) || args.KeyStr == key)
if (args.key == key)
{
nextSearchExpression = next;
return true;

View File

@ -12,7 +12,6 @@ global using Directory = DTLib.Filesystem.Directory;
global using File = DTLib.Filesystem.File;
global using Path = DTLib.Filesystem.Path;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.IO;
using System.Text.Encodings.Web;
using DTLib.Dtsod;
@ -55,21 +54,6 @@ public static partial class Program
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
if (!File.Exists(_configPath))
{