221 lines
9.3 KiB
C#
221 lines
9.3 KiB
C#
global using System;
|
|
global using System.Collections.Generic;
|
|
global using System.Text;
|
|
global using System.Text.Json;
|
|
global using System.Threading;
|
|
global using System.Threading.Tasks;
|
|
global using DTLib.Demystifier;
|
|
global using DTLib.Filesystem;
|
|
global using DTLib.Logging;
|
|
global using ParadoxSaveParser.Lib;
|
|
global using Directory = DTLib.Filesystem.Directory;
|
|
global using File = DTLib.Filesystem.File;
|
|
global using Path = DTLib.Filesystem.Path;
|
|
using System.Collections.Concurrent;
|
|
using System.IO;
|
|
using System.IO.Compression;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text.Encodings.Web;
|
|
using DTLib.Dtsod;
|
|
using DTLib.Extensions;
|
|
using DTLib.Web;
|
|
using DTLib.Web.Routes;
|
|
|
|
namespace ParadoxSaveParser.WebAPI;
|
|
|
|
public class Program
|
|
{
|
|
private static readonly IOPath _configPath = "./config.dtsod";
|
|
private static Config _config = new();
|
|
|
|
private static readonly ILogger _loggerRoot = new CompositeLogger(
|
|
new ConsoleLogger(),
|
|
new FileLogger("logs", "ParadoxSaveParser.WebAPI"));
|
|
|
|
private static readonly CancellationTokenSource _mainCancel = new();
|
|
private static ConcurrentDictionary<string, SaveFileMetadata> _saveMetadataStorage = new();
|
|
|
|
private static JsonSerializerOptions _saveSerializerOptions = new()
|
|
{
|
|
WriteIndented = false,
|
|
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
|
MaxDepth = 1024,
|
|
};
|
|
|
|
|
|
public static void Main(string[] args)
|
|
{
|
|
Console.InputEncoding = Encoding.UTF8;
|
|
Console.OutputEncoding = Encoding.UTF8;
|
|
Console.CursorVisible = false;
|
|
ContextLogger logger = new ContextLogger(nameof(Main), _loggerRoot);
|
|
Console.CancelKeyPress += (_, e) =>
|
|
{
|
|
e.Cancel = true;
|
|
logger.LogInfo("Ctrl+C Pressed");
|
|
_mainCancel.Cancel();
|
|
};
|
|
|
|
try
|
|
{
|
|
// config
|
|
if (!File.Exists(_configPath))
|
|
{
|
|
logger.LogWarn("config file not found.");
|
|
File.WriteAllText(_configPath, _config.ToString());
|
|
logger.LogWarn($"created default at {_configPath}.");
|
|
}
|
|
else _config = Config.FromDtsod(new DtsodV23(File.ReadAllText(_configPath)));
|
|
|
|
PrepareLocalFiles();
|
|
|
|
// http server
|
|
var router = new SimpleRouter(_loggerRoot);
|
|
router.DefaultRoute = new ServeFilesRouteHandler("public");
|
|
router.MapRoute("/getSaveStatus", HttpMethod.GET, GetSaveStatusHandler);
|
|
router.MapRoute("/uploadSave/eu4", HttpMethod.POST, UploadSaveHandler);
|
|
router.MapRoute("/parseSave/eu4", HttpMethod.POST, ParseSaveEU4Handler);
|
|
|
|
var app = new WebApp(_config.BaseUrl, _loggerRoot, router, _mainCancel.Token);
|
|
app.Run().GetAwaiter().GetResult();
|
|
}
|
|
catch (OperationCanceledException ex)
|
|
{
|
|
logger.LogWarn($"catched OperationCanceledException from {ex.Source}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError(ex.ToStringDemystified());
|
|
}
|
|
}
|
|
|
|
public static void PrepareLocalFiles()
|
|
{
|
|
Directory.Create(PathHelper.DATA_DIR);
|
|
Directory.Create(PathHelper.SAVES_DIR);
|
|
foreach (var metaFilePath in System.IO.Directory.GetFiles(
|
|
PathHelper.SAVES_DIR.Str, "*.meta.json",
|
|
SearchOption.TopDirectoryOnly))
|
|
{
|
|
using var metaFile = File.OpenRead(metaFilePath);
|
|
var meta = JsonSerializer.Deserialize<SaveFileMetadata>(metaFile) ??
|
|
throw new NullReferenceException(metaFilePath);
|
|
if (meta.status != SaveFileProcessingStatus.Done)
|
|
{
|
|
_loggerRoot.LogWarn(nameof(PrepareLocalFiles), $"metadata file '{metaFilePath}' status has invalid status {meta.status}");
|
|
}
|
|
|
|
if(!_saveMetadataStorage.TryAdd(meta.id, meta))
|
|
throw new Exception("Guid collision!");
|
|
}
|
|
}
|
|
|
|
// ReSharper disable once NotAccessedPositionalProperty.Global
|
|
public record ErrorMessage(string errorMessage);
|
|
|
|
private static async Task<HttpStatusCode>ReturnResponse(HttpListenerContext ctx, HttpStatusCode statusCode, object response)
|
|
{
|
|
await JsonSerializer.SerializeAsync(ctx.Response.OutputStream, response, response.GetType(),
|
|
JsonSerializerOptions.Default, _mainCancel.Token);
|
|
ctx.Response.StatusCode = (int)statusCode;
|
|
return statusCode;
|
|
}
|
|
|
|
private static async Task<HttpStatusCode>ReturnResponse(HttpListenerContext ctx, HttpStatusCode statusCode, string response)
|
|
{
|
|
await ctx.Response.OutputStream.WriteAsync(response.ToBytes(), _mainCancel.Token);
|
|
ctx.Response.StatusCode = (int)statusCode;
|
|
return statusCode;
|
|
}
|
|
|
|
private static async Task<HttpStatusCode>UploadSaveHandler(HttpListenerContext ctx)
|
|
{
|
|
string? contentType = ctx.Request.Headers.GetValues("Content-Type")?.FirstOrDefault();
|
|
if (contentType != "application/octet-stream")
|
|
return await ReturnResponse(ctx, HttpStatusCode.BadRequest,
|
|
new ErrorMessage($"Invalid request Content-Type: '{contentType}'"));
|
|
|
|
string saveId = Guid.NewGuid().ToString();
|
|
IOPath metaFilePath = PathHelper.GetMetaFilePath(saveId);
|
|
if (File.Exists(metaFilePath))
|
|
return await ReturnResponse(ctx, HttpStatusCode.InternalServerError,
|
|
new ErrorMessage($"Guid collision! file' {metaFilePath}' already exists."));
|
|
|
|
var meta = new SaveFileMetadata { id = saveId, game = Game.EU4, status = SaveFileProcessingStatus.Initialized, };
|
|
if (!_saveMetadataStorage.TryAdd(saveId, meta))
|
|
return await ReturnResponse(ctx, HttpStatusCode.InternalServerError,
|
|
new ErrorMessage($"Guid collision! Can't create metadata with id {saveId}"));
|
|
|
|
meta.status = SaveFileProcessingStatus.Uploading;
|
|
IOPath saveFilePath = PathHelper.GetSaveFilePath(meta.id);
|
|
await using var saveFile = File.OpenWrite(saveFilePath);
|
|
await using var remoteStream = ctx.Request.InputStream;
|
|
await remoteStream.CopyToAsync(saveFile, _mainCancel.Token);
|
|
meta.status = SaveFileProcessingStatus.Uploaded;
|
|
|
|
return await ReturnResponse(ctx, HttpStatusCode.OK, saveId);
|
|
}
|
|
|
|
private static (SaveFileMetadata? meta, ErrorMessage? errorMesage) GetMetaFromRequestId(HttpListenerContext ctx, string requestParamName)
|
|
{
|
|
var ids = ctx.Request.QueryString.GetValues(requestParamName);
|
|
string? id = ids?.FirstOrDefault();
|
|
if (string.IsNullOrEmpty(id))
|
|
return (null, new ErrorMessage($"No request parameter '{requestParamName}' provided"));
|
|
|
|
if (!_saveMetadataStorage.TryGetValue(id, out var meta))
|
|
return (null,new ErrorMessage($"Save with {id} not found"));
|
|
|
|
return (meta, null);
|
|
}
|
|
|
|
private static async Task<HttpStatusCode>GetSaveStatusHandler(HttpListenerContext ctx)
|
|
{
|
|
var (meta, errorMessage) = GetMetaFromRequestId(ctx, "id");
|
|
if(errorMessage is not null)
|
|
return await ReturnResponse(ctx, HttpStatusCode.InternalServerError, errorMessage);
|
|
|
|
return await ReturnResponse(ctx, HttpStatusCode.OK, meta!);
|
|
}
|
|
|
|
private static async Task<HttpStatusCode>ParseSaveEU4Handler(HttpListenerContext ctx)
|
|
{
|
|
var (meta, errorMessage) = GetMetaFromRequestId(ctx, "id");
|
|
if(errorMessage is not null)
|
|
return await ReturnResponse(ctx, HttpStatusCode.InternalServerError, errorMessage);
|
|
|
|
try
|
|
{
|
|
using var zipArchive = ZipFile.Open(PathHelper.GetSaveFilePath(meta!.id).Str, ZipArchiveMode.Read);
|
|
var zipEntry = zipArchive.Entries.FirstOrDefault(e => e.Name == "gamestate");
|
|
if (zipEntry is null)
|
|
return await ReturnResponse(ctx, HttpStatusCode.BadRequest,
|
|
new ErrorMessage("Invalid save format: no 'gamestate' file found"));
|
|
|
|
string extractedGamestatePath = PathHelper.GetSaveFilePath(meta.id) + ".gamestate";
|
|
zipEntry.ExtractToFile(extractedGamestatePath, true);
|
|
var gamestateStream = File.OpenRead(extractedGamestatePath);
|
|
|
|
meta.status = SaveFileProcessingStatus.Parsing;
|
|
var parser = new Parser(gamestateStream);
|
|
var result = parser.Parse();
|
|
|
|
meta.status = SaveFileProcessingStatus.SavingResults;
|
|
IOPath resultFilePath = PathHelper.GetParsedSaveFilePath(meta.id);
|
|
await using var resultFile = File.OpenWrite(resultFilePath);
|
|
await JsonSerializer.SerializeAsync(resultFile, result, _saveSerializerOptions, _mainCancel.Token);
|
|
meta.status = SaveFileProcessingStatus.Done;
|
|
meta.SaveToFile();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
string errorMesage = ex.ToStringDemystified();
|
|
_loggerRoot.LogWarn(nameof(ParseSaveEU4Handler), errorMesage);
|
|
return await ReturnResponse(ctx, HttpStatusCode.BadRequest,
|
|
new ErrorMessage(errorMesage));
|
|
}
|
|
|
|
return await ReturnResponse(ctx, HttpStatusCode.OK, meta);
|
|
}
|
|
} |