implemented parser
This commit is contained in:
@@ -5,7 +5,9 @@ global using System.Threading.Tasks;
|
||||
global using DTLib.Demystifier;
|
||||
global using ParadoxSaveParser.Lib;
|
||||
using System.Collections.Concurrent;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@@ -39,30 +41,12 @@ public class Program
|
||||
|
||||
_app.UseHttpsRedirection();
|
||||
_app.MapGet("/getSaveStatus", GetSaveStatusHandler);
|
||||
_app.MapPost("/uploadSave/eu4", UploadSaveHandler);
|
||||
_app.MapPost("/parseSave/eu4", ParseSaveEU4Handler);
|
||||
_app.Run();
|
||||
}
|
||||
|
||||
private static async Task GetSaveStatusHandler(HttpContext httpContext)
|
||||
{
|
||||
httpContext.Request.Query.TryGetValue("id", out var ids);
|
||||
string? id = ids.FirstOrDefault();
|
||||
if (string.IsNullOrEmpty(id))
|
||||
{
|
||||
throw new BadHttpRequestException("No id provided",
|
||||
StatusCodes.Status400BadRequest);
|
||||
}
|
||||
|
||||
if (!_saveMetadataStorage.TryGetValue(id, out var meta))
|
||||
{
|
||||
throw new BadHttpRequestException($"Save with {id} not found",
|
||||
StatusCodes.Status400BadRequest);
|
||||
}
|
||||
|
||||
await httpContext.Response.WriteAsJsonAsync(meta);
|
||||
}
|
||||
|
||||
private static async Task ParseSaveEU4Handler(HttpContext httpContext)
|
||||
private static async Task UploadSaveHandler(HttpContext httpContext)
|
||||
{
|
||||
var remoteFile = httpContext.Request.Form.Files.FirstOrDefault();
|
||||
if (remoteFile is null || !remoteFile.FileName.EndsWith(".eu4"))
|
||||
@@ -84,22 +68,75 @@ public class Program
|
||||
{
|
||||
throw new BadHttpRequestException($"Guid collision! Can't create metadata with id {saveId}", StatusCodes.Status500InternalServerError);
|
||||
}
|
||||
|
||||
meta.status = SaveFileProcessingStatus.Uploading;
|
||||
string saveFilePath = PathHelper.GetSaveFilePath(meta.id);
|
||||
await using var saveFile = File.Open(saveFilePath, FileMode.CreateNew, FileAccess.ReadWrite);
|
||||
await using var remoteStream = remoteFile.OpenReadStream();
|
||||
await remoteStream.CopyToAsync(saveFile);
|
||||
meta.status = SaveFileProcessingStatus.Uploaded;
|
||||
|
||||
await httpContext.Response.WriteAsJsonAsync(meta);
|
||||
}
|
||||
|
||||
private static SaveFileMetadata GetMetaFromRequestId(HttpContext httpContext, string requestParamName)
|
||||
{
|
||||
httpContext.Request.Query.TryGetValue(requestParamName, out var ids);
|
||||
string? id = ids.FirstOrDefault();
|
||||
if (string.IsNullOrEmpty(id))
|
||||
{
|
||||
throw new BadHttpRequestException($"No request parameter '{requestParamName}' provided",
|
||||
StatusCodes.Status400BadRequest);
|
||||
}
|
||||
|
||||
if (!_saveMetadataStorage.TryGetValue(id, out var meta))
|
||||
{
|
||||
throw new BadHttpRequestException($"Save with {id} not found",
|
||||
StatusCodes.Status400BadRequest);
|
||||
}
|
||||
|
||||
return meta;
|
||||
}
|
||||
|
||||
private static async Task GetSaveStatusHandler(HttpContext httpContext)
|
||||
{
|
||||
var meta = GetMetaFromRequestId(httpContext, "id");
|
||||
await httpContext.Response.WriteAsJsonAsync(meta);
|
||||
}
|
||||
|
||||
private static async Task ParseSaveEU4Handler(HttpContext httpContext)
|
||||
{
|
||||
var meta = GetMetaFromRequestId(httpContext, "id");
|
||||
if (meta.status == SaveFileProcessingStatus.Error)
|
||||
{
|
||||
httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
|
||||
await httpContext.Response.WriteAsJsonAsync(meta);
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
meta.status = SaveFileProcessingStatus.Uploading;
|
||||
string saveFilePath = PathHelper.GetEU4SaveFilePath(meta.id);
|
||||
await using var saveFile = File.Open(saveFilePath, FileMode.CreateNew, FileAccess.ReadWrite);
|
||||
await using (var remoteStream = remoteFile.OpenReadStream())
|
||||
{
|
||||
await Task.Delay(50000);
|
||||
await remoteStream.CopyToAsync(saveFile);
|
||||
}
|
||||
|
||||
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)
|
||||
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;
|
||||
saveFile.Seek(0, SeekOrigin.Begin);
|
||||
var parser = new ParserEU4(saveFile);
|
||||
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);
|
||||
@@ -112,7 +149,8 @@ public class Program
|
||||
meta.status = SaveFileProcessingStatus.Error;
|
||||
string errorMesage = ex.ToStringDemystified();
|
||||
meta.errorMesage = errorMesage;
|
||||
_app.Logger.Log(LogLevel.Error, "EU4SaveParse Error: {errorMesage}", errorMesage);
|
||||
httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
|
||||
_app.Logger.Log(LogLevel.Error, "ParseSaveEU4 Error: {errorMesage}", errorMesage);
|
||||
}
|
||||
|
||||
await httpContext.Response.WriteAsJsonAsync(meta);
|
||||
|
||||
Reference in New Issue
Block a user