132 lines
4.8 KiB
C#
132 lines
4.8 KiB
C#
global using System;
|
|
using System.Collections.Concurrent;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text.Json;
|
|
using DTLib.Demystifier;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.Logging;
|
|
using ParadoxSaveParser.Lib;
|
|
|
|
namespace ParadoxSaveParser.WebAPI;
|
|
|
|
public enum SaveFileProcessingStatus
|
|
{
|
|
Initialized, Uploading, Parsing, SavingResults, Done, Error
|
|
}
|
|
|
|
public enum Game
|
|
{
|
|
Unknown, EU4
|
|
}
|
|
|
|
public class SaveFileMetadata
|
|
{
|
|
public required string id { get; init; }
|
|
public required Game game { get; init; }
|
|
public required SaveFileProcessingStatus status { get; set; }
|
|
|
|
public string? errorMesage { get; set; }
|
|
|
|
private static readonly JsonSerializerOptions _jsonOptions = new() { WriteIndented = true };
|
|
public void SaveToFile()
|
|
{
|
|
using var metaFile = File.Open(PathHelper.GetMetaFilePath(id), FileMode.CreateNew, FileAccess.Write);
|
|
JsonSerializer.Serialize(metaFile, this, _jsonOptions);
|
|
}
|
|
}
|
|
|
|
public static class PathHelper
|
|
{
|
|
|
|
public const string DATA_DIR = "data";
|
|
public static string SAVES_DIR = Path.Join(DATA_DIR, "saves");
|
|
public static string GetMetaFilePath(string save_id) => Path.Join(SAVES_DIR, save_id + ".meta.json");
|
|
public static string GetEU4SaveFilePath(string save_id) => Path.Join(SAVES_DIR, save_id + ".eu4");
|
|
}
|
|
|
|
public class Program
|
|
{
|
|
private static ConcurrentDictionary<string, SaveFileMetadata> _saveMetadataStorage = new();
|
|
private static WebApplication _app = null!;
|
|
|
|
public static void Main(string[] args)
|
|
{
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
_app = builder.Build();
|
|
|
|
foreach (var metaFilePath in Directory.GetFiles(PathHelper.SAVES_DIR, "*.meta.json", SearchOption.TopDirectoryOnly))
|
|
{
|
|
using var metaFile = File.Open(metaFilePath, FileMode.Open, FileAccess.Read);
|
|
var meta = JsonSerializer.Deserialize<SaveFileMetadata>(metaFile) ?? throw new NullReferenceException(metaFilePath);
|
|
if (meta.status != SaveFileProcessingStatus.Done)
|
|
{
|
|
_app.Logger.Log(LogLevel.Warning, "metadata file '{metaFilePath}' status has invalid status {status}", metaFilePath, meta.status);
|
|
}
|
|
|
|
if(!_saveMetadataStorage.TryAdd(meta.id, meta))
|
|
throw new Exception("Guid collision!");
|
|
}
|
|
|
|
_app.UseHttpsRedirection();
|
|
_app.MapPost("/parse/eu4", (HttpContext httpContext) =>
|
|
{
|
|
var remoteFile = httpContext.Request.Form.Files.FirstOrDefault();
|
|
if(remoteFile is null || !remoteFile.FileName.EndsWith(".eu4"))
|
|
throw new Exception($"Invalid file format: {remoteFile?.FileName}");
|
|
|
|
string saveId = Guid.NewGuid().ToString();
|
|
string metaFilePath = PathHelper.GetMetaFilePath(saveId);
|
|
if (File.Exists(metaFilePath))
|
|
{
|
|
httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
|
|
throw new Exception($"Guid collision! file {metaFilePath} already exists.");
|
|
}
|
|
|
|
var meta = new SaveFileMetadata
|
|
{
|
|
id = saveId,
|
|
game = Game.EU4,
|
|
status = SaveFileProcessingStatus.Initialized,
|
|
};
|
|
if (!_saveMetadataStorage.TryAdd(saveId, meta))
|
|
{
|
|
throw new Exception($"Guid collision! Can't create metadata with id {saveId}");
|
|
}
|
|
|
|
BeginEU4SaveParse(remoteFile, meta);
|
|
return meta;
|
|
});
|
|
|
|
_app.Run();
|
|
}
|
|
|
|
private static async void BeginEU4SaveParse(IFormFile remoteFile, SaveFileMetadata meta)
|
|
{
|
|
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 remoteStream.CopyToAsync(saveFile);
|
|
}
|
|
|
|
meta.status = SaveFileProcessingStatus.Parsing;
|
|
saveFile.Seek(0, SeekOrigin.Begin);
|
|
var parser = new ParserEU4(saveFile);
|
|
//var result = parser.Parse();
|
|
meta.status = SaveFileProcessingStatus.SavingResults;
|
|
// save
|
|
meta.status = SaveFileProcessingStatus.Done;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
meta.status = SaveFileProcessingStatus.Error;
|
|
string errorMesage = ex.ToStringDemystified();
|
|
_app.Logger.Log(LogLevel.Error, "EU4SaveParse Error: {errorMesage}", errorMesage);
|
|
}
|
|
}
|
|
} |