fixed many bugs in parser

This commit is contained in:
Timerix 2025-03-22 18:14:32 +05:00
parent 05972fa40f
commit f69b498caf
4 changed files with 134 additions and 37 deletions

View File

@ -1,8 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>

View File

@ -1,11 +1,14 @@
using System.Text; global using System;
global using System.IO;
global using System.Text;
global using System.Collections.Generic;
namespace ParadoxSaveParser.Lib; namespace ParadoxSaveParser.Lib;
public class Parser public class Parser
{ {
protected Stream _saveFile; protected Stream _saveFile;
private List<Token> _tokens = new(); private List<Token> _tokens = new(4_194_304);
private int _tokenIndex; private int _tokenIndex;
public Parser(Stream savefile) public Parser(Stream savefile)
@ -13,7 +16,7 @@ public class Parser
_saveFile = savefile; _saveFile = savefile;
} }
protected enum TokenType protected enum TokenType : byte
{ {
Invalid, Invalid,
String, String,
@ -25,67 +28,119 @@ public class Parser
protected struct Token protected struct Token
{ {
public TokenType type; public TokenType type;
public short column;
public int line;
public string? value; public string? value;
public override string ToString() public override string ToString()
{ {
string s;
switch (type) switch (type)
{ {
case TokenType.Invalid: case TokenType.Invalid:
return "INVALID_TOKEN"; s = "INVALID_TOKEN";
break;
case TokenType.String: case TokenType.String:
return value ?? "NULL"; s = value ?? "NULL";
break;
case TokenType.Equals: case TokenType.Equals:
return "="; s = "=";
break;
case TokenType.BracketOpen: case TokenType.BracketOpen:
return "{"; s = "{";
break;
case TokenType.BracketClose: case TokenType.BracketClose:
return "}"; s = "}";
break;
default: default:
throw new ArgumentOutOfRangeException(type.ToString()); throw new ArgumentOutOfRangeException(type.ToString());
} }
return $"{line}:{column} '{s}'";
} }
} }
protected void Lex() protected void Lex()
{ {
_tokens.Clear(); _tokens.Clear();
string expectedHeader = "EU4txt";
byte[] headBytes = new byte[expectedHeader.Length];
_saveFile.ReadExactly(headBytes);
string headStr = Encoding.UTF8.GetString(headBytes);
if (headStr != expectedHeader)
throw new Exception($"Invalid gamestate header: '{headStr}'");
StringBuilder str = new(); StringBuilder str = new();
int line = 2;
int column = 0;
bool isQuoteOpen = false;
bool isStrInQuotes = false;
void CompleteStringToken() void CompleteStringToken()
{ {
if (str.Length > 0 && str[0] != '#') if (isQuoteOpen)
return;
// strings in quotes can be empty
if (!isStrInQuotes && (str.Length <= 0 || str[0] == '#'))
return;
_tokens.Add(new Token
{ {
_tokens.Add(new Token { type = TokenType.String, value = str.ToString() }); type = TokenType.String,
column = (short)(column - str.Length),
line = line,
value = str.ToString()
});
str.Clear(); str.Clear();
} isStrInQuotes = false;
} }
while (_saveFile.CanRead) while (_saveFile.CanRead)
{ {
int c = _saveFile.ReadByte(); int c = _saveFile.ReadByte();
column++;
switch (c) switch (c)
{ {
case -1: case -1:
CompleteStringToken(); CompleteStringToken();
return; return;
case '\"':
isQuoteOpen = !isQuoteOpen;
isStrInQuotes = true;
break;
case ' ': case ' ':
case '\t': case '\t':
case '\n':
case '\r': case '\r':
CompleteStringToken(); CompleteStringToken();
break; break;
case '\n':
CompleteStringToken();
line++;
column = 0;
break;
case '=': case '=':
CompleteStringToken(); CompleteStringToken();
_tokens.Add(new Token { type = TokenType.Equals }); _tokens.Add(new Token
{
type = TokenType.Equals,
line = line, column = (short)column
});
break; break;
case '{': case '{':
CompleteStringToken(); CompleteStringToken();
_tokens.Add(new Token { type = TokenType.BracketOpen }); _tokens.Add(new Token
{
type = TokenType.BracketOpen,
line = line, column = (short)column
});
break; break;
case '}': case '}':
CompleteStringToken(); CompleteStringToken();
_tokens.Add(new Token { type = TokenType.BracketClose }); _tokens.Add(new Token
{
type = TokenType.BracketClose,
line = line, column = (short)column
});
break; break;
default: default:
str.Append((char)c); str.Append((char)c);
@ -131,8 +186,7 @@ public class Parser
private List<object> ParseList() private List<object> ParseList()
{ {
List<object> list = new(); List<object> list = new();
Token tok = _tokens[_tokenIndex]; while(true)
while (tok.type != TokenType.BracketClose)
{ {
object? value = ParseValue(); object? value = ParseValue();
if (value == null) if (value == null)
@ -150,16 +204,42 @@ public class Parser
while (_tokenIndex < _tokens.Count) while (_tokenIndex < _tokens.Count)
{ {
Token tok = _tokens[_tokenIndex++]; Token tok = _tokens[_tokenIndex++];
// end of dictionary
if (tok.type == TokenType.BracketClose) if (tok.type == TokenType.BracketClose)
break; break;
// Saves may contain some blocks without key.
// Such blocks are skipped because idk where to put them.
// Example: `technology_group=tech_cannorian{ }
// { } { } { }`
if (tok.type == TokenType.BracketOpen)
{
int bracketBalance = 1;
while (bracketBalance != 0)
{
tok = _tokens[_tokenIndex++];
if (tok.type == TokenType.BracketOpen)
bracketBalance++;
else if (tok.type == TokenType.BracketClose)
bracketBalance--;
}
continue;
}
if(tok.type != TokenType.String) if(tok.type != TokenType.String)
throw new UnexpectedTokenException(tok, _tokenIndex - 1); throw new UnexpectedTokenException(tok, _tokenIndex - 1);
string key = tok.value!; string key = tok.value!;
tok = _tokens[_tokenIndex++]; tok = _tokens[_tokenIndex++];
if(tok.type != TokenType.Equals) if (tok.type == TokenType.BracketOpen)
{
// Saves may contain key-value definition without `=`.
// Example: `map_area_data{` instead of `map_area_data = {`
_tokenIndex--;
}
else if(tok.type != TokenType.Equals)
throw new UnexpectedTokenException(tok, _tokenIndex - 1); throw new UnexpectedTokenException(tok, _tokenIndex - 1);

View File

@ -1,5 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
@ -15,4 +14,19 @@
<PackageReference Include="DTLib.Demystifier" Version="1.1.0" /> <PackageReference Include="DTLib.Demystifier" Version="1.1.0" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Compile Remove="data\**" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Remove="data\**" />
</ItemGroup>
<ItemGroup>
<Content Remove="data\**" />
</ItemGroup>
<ItemGroup>
<None Remove="data\**" />
</ItemGroup>
</Project> </Project>

View File

@ -1,5 +1,7 @@
global using System; global using System;
global using System.IO; global using System.IO;
global using System.Collections.Generic;
global using System.Text;
global using System.Text.Json; global using System.Text.Json;
global using System.Threading.Tasks; global using System.Threading.Tasks;
global using DTLib.Demystifier; global using DTLib.Demystifier;
@ -7,7 +9,7 @@ global using ParadoxSaveParser.Lib;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.IO.Compression; using System.IO.Compression;
using System.Linq; using System.Linq;
using System.Text; using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -19,6 +21,13 @@ public class Program
private static ConcurrentDictionary<string, SaveFileMetadata> _saveMetadataStorage = new(); private static ConcurrentDictionary<string, SaveFileMetadata> _saveMetadataStorage = new();
private static WebApplication _app = null!; private static WebApplication _app = null!;
private static JsonSerializerOptions _saveSerializerOptions = new()
{
WriteIndented = false,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
MaxDepth = 1024,
};
public static void Main(string[] args) public static void Main(string[] args)
{ {
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@ -116,31 +125,25 @@ public class Program
try try
{ {
if(meta.status != SaveFileProcessingStatus.Uploaded) if (meta.status != SaveFileProcessingStatus.Uploaded)
throw new Exception($"Invalid save processing status: {meta.status}"); throw new Exception($"Invalid save processing status: {meta.status}");
using var zipArchive = ZipFile.Open(PathHelper.GetSaveFilePath(meta.id), ZipArchiveMode.Read); using var zipArchive = ZipFile.Open(PathHelper.GetSaveFilePath(meta.id), ZipArchiveMode.Read);
var zipEntry = zipArchive.Entries.FirstOrDefault(e => e.Name == "gamestate"); var zipEntry = zipArchive.Entries.FirstOrDefault(e => e.Name == "gamestate");
if(zipEntry is null) if (zipEntry is null)
throw new Exception("Invalid save format: no gamestate file found"); throw new Exception("Invalid save format: no gamestate file found");
string extractedGamestatePath = PathHelper.GetSaveFilePath(meta.id) + ".gamestate"; string extractedGamestatePath = PathHelper.GetSaveFilePath(meta.id) + ".gamestate";
zipEntry.ExtractToFile(extractedGamestatePath); zipEntry.ExtractToFile(extractedGamestatePath);
var gamestateStream = File.Open(extractedGamestatePath, FileMode.Open, FileAccess.Read); var gamestateStream = File.Open(extractedGamestatePath, FileMode.Open, FileAccess.Read);
meta.status = SaveFileProcessingStatus.Parsing; meta.status = SaveFileProcessingStatus.Parsing;
string expectedHeader = "EU4txt";
byte[] headBytes = new byte[expectedHeader.Length];
gamestateStream.ReadExactly(headBytes);
string headStr = Encoding.UTF8.GetString(headBytes);
if(headStr != expectedHeader)
throw new Exception($"Invalid gamestate header: '{headStr}'");
var parser = new Parser(gamestateStream); var parser = new Parser(gamestateStream);
var result = parser.Parse(); var result = parser.Parse();
meta.status = SaveFileProcessingStatus.SavingResults; meta.status = SaveFileProcessingStatus.SavingResults;
string resultFilePath = PathHelper.GetParsedSaveFilePath(meta.id); string resultFilePath = PathHelper.GetParsedSaveFilePath(meta.id);
await using var resultFile = File.Open(resultFilePath, FileMode.CreateNew, FileAccess.Write); await using var resultFile = File.Open(resultFilePath, FileMode.CreateNew, FileAccess.Write);
await JsonSerializer.SerializeAsync(resultFile, result); await JsonSerializer.SerializeAsync(resultFile, result, _saveSerializerOptions);
meta.status = SaveFileProcessingStatus.Done; meta.status = SaveFileProcessingStatus.Done;
meta.SaveToFile(); meta.SaveToFile();
} }
@ -153,6 +156,7 @@ public class Program
_app.Logger.Log(LogLevel.Error, "ParseSaveEU4 Error: {errorMesage}", errorMesage); _app.Logger.Log(LogLevel.Error, "ParseSaveEU4 Error: {errorMesage}", errorMesage);
} }
GC.Collect();
await httpContext.Response.WriteAsJsonAsync(meta); await httpContext.Response.WriteAsJsonAsync(meta);
} }
} }