fixed many bugs in parser
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
global using System;
|
||||
global using System.IO;
|
||||
global using System.Collections.Generic;
|
||||
global using System.Text;
|
||||
global using System.Text.Json;
|
||||
global using System.Threading.Tasks;
|
||||
global using DTLib.Demystifier;
|
||||
@@ -7,7 +9,7 @@ global using ParadoxSaveParser.Lib;
|
||||
using System.Collections.Concurrent;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Encodings.Web;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@@ -18,7 +20,14 @@ public class Program
|
||||
{
|
||||
private static ConcurrentDictionary<string, SaveFileMetadata> _saveMetadataStorage = new();
|
||||
private static WebApplication _app = null!;
|
||||
|
||||
|
||||
private static JsonSerializerOptions _saveSerializerOptions = new()
|
||||
{
|
||||
WriteIndented = false,
|
||||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
||||
MaxDepth = 1024,
|
||||
};
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
@@ -116,31 +125,25 @@ public class Program
|
||||
|
||||
try
|
||||
{
|
||||
if(meta.status != SaveFileProcessingStatus.Uploaded)
|
||||
if (meta.status != SaveFileProcessingStatus.Uploaded)
|
||||
throw new Exception($"Invalid save processing status: {meta.status}");
|
||||
|
||||
|
||||
using var zipArchive = ZipFile.Open(PathHelper.GetSaveFilePath(meta.id), ZipArchiveMode.Read);
|
||||
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");
|
||||
string extractedGamestatePath = PathHelper.GetSaveFilePath(meta.id) + ".gamestate";
|
||||
zipEntry.ExtractToFile(extractedGamestatePath);
|
||||
var gamestateStream = File.Open(extractedGamestatePath, FileMode.Open, FileAccess.Read);
|
||||
|
||||
|
||||
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 result = parser.Parse();
|
||||
|
||||
|
||||
meta.status = SaveFileProcessingStatus.SavingResults;
|
||||
string resultFilePath = PathHelper.GetParsedSaveFilePath(meta.id);
|
||||
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.SaveToFile();
|
||||
}
|
||||
@@ -152,7 +155,8 @@ public class Program
|
||||
httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
|
||||
_app.Logger.Log(LogLevel.Error, "ParseSaveEU4 Error: {errorMesage}", errorMesage);
|
||||
}
|
||||
|
||||
|
||||
GC.Collect();
|
||||
await httpContext.Response.WriteAsJsonAsync(meta);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user