Moved from ASP.Net to DTLib.Web, no more bloat!!!
This commit is contained in:
parent
52d5320899
commit
e3b82d7de5
1
.gitignore
vendored
1
.gitignore
vendored
@ -10,6 +10,7 @@
|
|||||||
[Ll]ogs/
|
[Ll]ogs/
|
||||||
[Pp]ublish/
|
[Pp]ublish/
|
||||||
data/
|
data/
|
||||||
|
config.dtsod
|
||||||
|
|
||||||
# IDE files
|
# IDE files
|
||||||
.vs/
|
.vs/
|
||||||
|
|||||||
34
ParadoxSaveParser.WebAPI/Config.cs
Normal file
34
ParadoxSaveParser.WebAPI/Config.cs
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
using DTLib.Dtsod;
|
||||||
|
|
||||||
|
namespace ParadoxSaveParser.WebAPI;
|
||||||
|
|
||||||
|
public class Config
|
||||||
|
{
|
||||||
|
public const int ActualVersion = 1;
|
||||||
|
|
||||||
|
public int Version = ActualVersion;
|
||||||
|
public string BaseUrl = "http://127.0.0.1:8080/";
|
||||||
|
|
||||||
|
public static Config FromDtsod(DtsodV23 d)
|
||||||
|
{
|
||||||
|
var cfg = new Config
|
||||||
|
{
|
||||||
|
Version = d["version"],
|
||||||
|
BaseUrl = d["baseUrl"]
|
||||||
|
};
|
||||||
|
if (cfg.Version < ActualVersion)
|
||||||
|
throw new Exception($"config is obsolete (config v{cfg.Version} < program v{ActualVersion})");
|
||||||
|
if(cfg.Version > ActualVersion)
|
||||||
|
throw new Exception($"program is obsolete (config v{cfg.Version} > program v{ActualVersion})");
|
||||||
|
return cfg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DtsodV23 ToDtsod() =>
|
||||||
|
new()
|
||||||
|
{
|
||||||
|
{ "version", Version },
|
||||||
|
{ "baseUrl", BaseUrl },
|
||||||
|
};
|
||||||
|
|
||||||
|
public override string ToString() => ToDtsod().ToString();
|
||||||
|
}
|
||||||
@ -1,6 +1,8 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<LangVersion>latest</LangVersion>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<ImplicitUsings>disable</ImplicitUsings>
|
<ImplicitUsings>disable</ImplicitUsings>
|
||||||
<InvariantGlobalization>true</InvariantGlobalization>
|
<InvariantGlobalization>true</InvariantGlobalization>
|
||||||
@ -11,7 +13,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="DTLib.Demystifier" Version="1.1.0" />
|
<PackageReference Include="DTLib.Web" Version="1.1.2" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@ -28,5 +30,6 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Remove="data\**" />
|
<None Remove="data\**" />
|
||||||
|
<None Remove="Properties\launchSettings.json" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@ -1,12 +1,10 @@
|
|||||||
using System.IO;
|
namespace ParadoxSaveParser.WebAPI;
|
||||||
|
|
||||||
namespace ParadoxSaveParser.WebAPI;
|
|
||||||
|
|
||||||
public static class PathHelper
|
public static class PathHelper
|
||||||
{
|
{
|
||||||
public const string DATA_DIR = "data";
|
public static readonly IOPath DATA_DIR = "data";
|
||||||
public static string SAVES_DIR = Path.Join(DATA_DIR, "saves");
|
public static readonly IOPath SAVES_DIR = Path.Concat(DATA_DIR, "saves");
|
||||||
public static string GetMetaFilePath(string save_id) => Path.Join(SAVES_DIR, save_id + ".meta.json");
|
public static IOPath GetMetaFilePath(string save_id) => Path.Concat(SAVES_DIR, save_id + ".meta.json");
|
||||||
public static string GetSaveFilePath(string save_id) => Path.Join(SAVES_DIR, save_id + ".eu4");
|
public static IOPath GetSaveFilePath(string save_id) => Path.Concat(SAVES_DIR, save_id + ".eu4");
|
||||||
public static string GetParsedSaveFilePath(string save_id) => Path.Join(SAVES_DIR, save_id + ".parsed.json");
|
public static IOPath GetParsedSaveFilePath(string save_id) => Path.Concat(SAVES_DIR, save_id + ".parsed.json");
|
||||||
}
|
}
|
||||||
@ -1,25 +1,36 @@
|
|||||||
global using System;
|
global using System;
|
||||||
global using System.IO;
|
|
||||||
global using System.Collections.Generic;
|
global using System.Collections.Generic;
|
||||||
global using System.Text;
|
global using System.Text;
|
||||||
global using System.Text.Json;
|
global using System.Text.Json;
|
||||||
|
global using System.Threading;
|
||||||
global using System.Threading.Tasks;
|
global using System.Threading.Tasks;
|
||||||
|
global using DTLib;
|
||||||
global using DTLib.Demystifier;
|
global using DTLib.Demystifier;
|
||||||
|
global using DTLib.Filesystem;
|
||||||
|
global using DTLib.Logging;
|
||||||
global using ParadoxSaveParser.Lib;
|
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.Collections.Concurrent;
|
||||||
|
using System.IO;
|
||||||
using System.IO.Compression;
|
using System.IO.Compression;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
using System.Text.Encodings.Web;
|
using System.Text.Encodings.Web;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using DTLib.Dtsod;
|
||||||
using Microsoft.AspNetCore.Http;
|
using DTLib.Extensions;
|
||||||
using Microsoft.Extensions.Logging;
|
using DTLib.Web;
|
||||||
|
using DTLib.Web.Routes;
|
||||||
|
|
||||||
namespace ParadoxSaveParser.WebAPI;
|
namespace ParadoxSaveParser.WebAPI;
|
||||||
|
|
||||||
public class Program
|
public class Program
|
||||||
{
|
{
|
||||||
|
private static readonly IOPath _configPath = "./config.dtsod";
|
||||||
|
private static Config _config = new();
|
||||||
|
private static readonly ILogger _loggerRoot = new ConsoleLogger();
|
||||||
private static ConcurrentDictionary<string, SaveFileMetadata> _saveMetadataStorage = new();
|
private static ConcurrentDictionary<string, SaveFileMetadata> _saveMetadataStorage = new();
|
||||||
private static WebApplication _app = null!;
|
|
||||||
|
|
||||||
private static JsonSerializerOptions _saveSerializerOptions = new()
|
private static JsonSerializerOptions _saveSerializerOptions = new()
|
||||||
{
|
{
|
||||||
@ -30,31 +41,85 @@ public class Program
|
|||||||
|
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
Console.InputEncoding = Encoding.UTF8;
|
||||||
_app = builder.Build();
|
Console.OutputEncoding = Encoding.UTF8;
|
||||||
|
ContextLogger logger = new ContextLogger(nameof(Main), _loggerRoot);
|
||||||
Directory.CreateDirectory(PathHelper.DATA_DIR);
|
CancellationTokenSource mainCancel = new();
|
||||||
Directory.CreateDirectory(PathHelper.SAVES_DIR);
|
Console.CancelKeyPress += (_, _) =>
|
||||||
foreach (var metaFilePath in Directory.GetFiles(PathHelper.SAVES_DIR, "*.meta.json", SearchOption.TopDirectoryOnly))
|
|
||||||
{
|
{
|
||||||
using var metaFile = File.Open(metaFilePath, FileMode.Open, FileAccess.Read);
|
logger.LogInfo("stopping server...");
|
||||||
|
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("/aaa", async ctx =>
|
||||||
|
{
|
||||||
|
byte[] buffer =
|
||||||
|
"""
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<h1>aaa</h1>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
""".ToBytes();
|
||||||
|
ctx.Response.ContentLength64 = buffer.Length;
|
||||||
|
await ctx.Response.OutputStream.WriteAsync(buffer);
|
||||||
|
return HttpStatusCode.OK;
|
||||||
|
});
|
||||||
|
// router.MapGet("/getSaveStatus", GetSaveStatusHandler);
|
||||||
|
// router.MapPost("/uploadSave/eu4", UploadSaveHandler);
|
||||||
|
// app.MapPost("/parseSave/eu4", 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);
|
var meta = JsonSerializer.Deserialize<SaveFileMetadata>(metaFile) ?? throw new NullReferenceException(metaFilePath);
|
||||||
if (meta.status != SaveFileProcessingStatus.Done)
|
if (meta.status != SaveFileProcessingStatus.Done)
|
||||||
{
|
{
|
||||||
_app.Logger.Log(LogLevel.Warning, "metadata file '{metaFilePath}' status has invalid status {status}", metaFilePath, meta.status);
|
_loggerRoot.LogWarn(nameof(PrepareLocalFiles), $"metadata file '{metaFilePath}' status has invalid status {meta.status}");
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!_saveMetadataStorage.TryAdd(meta.id, meta))
|
if(!_saveMetadataStorage.TryAdd(meta.id, meta))
|
||||||
throw new Exception("Guid collision!");
|
throw new Exception("Guid collision!");
|
||||||
}
|
}
|
||||||
|
|
||||||
_app.UseHttpsRedirection();
|
|
||||||
_app.MapGet("/getSaveStatus", GetSaveStatusHandler);
|
|
||||||
_app.MapPost("/uploadSave/eu4", UploadSaveHandler);
|
|
||||||
_app.MapPost("/parseSave/eu4", ParseSaveEU4Handler);
|
|
||||||
_app.Run();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
private static async Task UploadSaveHandler(HttpContext httpContext)
|
private static async Task UploadSaveHandler(HttpContext httpContext)
|
||||||
{
|
{
|
||||||
var remoteFile = httpContext.Request.Form.Files.FirstOrDefault();
|
var remoteFile = httpContext.Request.Form.Files.FirstOrDefault();
|
||||||
@ -65,7 +130,7 @@ public class Program
|
|||||||
}
|
}
|
||||||
|
|
||||||
string saveId = Guid.NewGuid().ToString();
|
string saveId = Guid.NewGuid().ToString();
|
||||||
string metaFilePath = PathHelper.GetMetaFilePath(saveId);
|
IOPath metaFilePath = PathHelper.GetMetaFilePath(saveId);
|
||||||
if (File.Exists(metaFilePath))
|
if (File.Exists(metaFilePath))
|
||||||
{
|
{
|
||||||
httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
|
httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
|
||||||
@ -79,8 +144,8 @@ public class Program
|
|||||||
}
|
}
|
||||||
|
|
||||||
meta.status = SaveFileProcessingStatus.Uploading;
|
meta.status = SaveFileProcessingStatus.Uploading;
|
||||||
string saveFilePath = PathHelper.GetSaveFilePath(meta.id);
|
IOPath saveFilePath = PathHelper.GetSaveFilePath(meta.id);
|
||||||
await using var saveFile = File.Open(saveFilePath, FileMode.CreateNew, FileAccess.ReadWrite);
|
await using var saveFile = File.OpenWrite(saveFilePath);
|
||||||
await using var remoteStream = remoteFile.OpenReadStream();
|
await using var remoteStream = remoteFile.OpenReadStream();
|
||||||
await remoteStream.CopyToAsync(saveFile);
|
await remoteStream.CopyToAsync(saveFile);
|
||||||
meta.status = SaveFileProcessingStatus.Uploaded;
|
meta.status = SaveFileProcessingStatus.Uploaded;
|
||||||
@ -128,21 +193,21 @@ public class Program
|
|||||||
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).Str, 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.OpenRead(extractedGamestatePath);
|
||||||
|
|
||||||
meta.status = SaveFileProcessingStatus.Parsing;
|
meta.status = SaveFileProcessingStatus.Parsing;
|
||||||
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);
|
IOPath resultFilePath = PathHelper.GetParsedSaveFilePath(meta.id);
|
||||||
await using var resultFile = File.Open(resultFilePath, FileMode.CreateNew, FileAccess.Write);
|
await using var resultFile = File.OpenWrite(resultFilePath);
|
||||||
await JsonSerializer.SerializeAsync(resultFile, result, _saveSerializerOptions);
|
await JsonSerializer.SerializeAsync(resultFile, result, _saveSerializerOptions);
|
||||||
meta.status = SaveFileProcessingStatus.Done;
|
meta.status = SaveFileProcessingStatus.Done;
|
||||||
meta.SaveToFile();
|
meta.SaveToFile();
|
||||||
@ -158,4 +223,5 @@ public class Program
|
|||||||
|
|
||||||
await httpContext.Response.WriteAsJsonAsync(meta);
|
await httpContext.Response.WriteAsJsonAsync(meta);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
@ -1,35 +0,0 @@
|
|||||||
{
|
|
||||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
|
||||||
"iisSettings": {
|
|
||||||
"windowsAuthentication": false,
|
|
||||||
"anonymousAuthentication": true,
|
|
||||||
"iisExpress": {
|
|
||||||
"applicationUrl": "http://localhost:38396",
|
|
||||||
"sslPort": 44312
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"profiles": {
|
|
||||||
"http": {
|
|
||||||
"commandName": "Project",
|
|
||||||
"dotnetRunMessages": true,
|
|
||||||
"applicationUrl": "http://localhost:5226",
|
|
||||||
"environmentVariables": {
|
|
||||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"https": {
|
|
||||||
"commandName": "Project",
|
|
||||||
"dotnetRunMessages": true,
|
|
||||||
"applicationUrl": "https://localhost:7032;http://localhost:5226",
|
|
||||||
"environmentVariables": {
|
|
||||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"IIS Express": {
|
|
||||||
"commandName": "IISExpress",
|
|
||||||
"environmentVariables": {
|
|
||||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,6 +1,4 @@
|
|||||||
using System.IO;
|
using System.Text.Json.Serialization;
|
||||||
using System.Text.Json;
|
|
||||||
using System.Text.Json.Serialization;
|
|
||||||
|
|
||||||
namespace ParadoxSaveParser.WebAPI;
|
namespace ParadoxSaveParser.WebAPI;
|
||||||
|
|
||||||
@ -29,7 +27,7 @@ public class SaveFileMetadata
|
|||||||
private static readonly JsonSerializerOptions _jsonOptions = new() { WriteIndented = true };
|
private static readonly JsonSerializerOptions _jsonOptions = new() { WriteIndented = true };
|
||||||
public void SaveToFile()
|
public void SaveToFile()
|
||||||
{
|
{
|
||||||
using var metaFile = File.Open(PathHelper.GetMetaFilePath(id), FileMode.CreateNew, FileAccess.Write);
|
using var metaFile = File.OpenWrite(PathHelper.GetMetaFilePath(id));
|
||||||
JsonSerializer.Serialize(metaFile, this, _jsonOptions);
|
JsonSerializer.Serialize(metaFile, this, _jsonOptions);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,8 +0,0 @@
|
|||||||
{
|
|
||||||
"Logging": {
|
|
||||||
"LogLevel": {
|
|
||||||
"Default": "Information",
|
|
||||||
"Microsoft.AspNetCore": "Warning"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,9 +0,0 @@
|
|||||||
{
|
|
||||||
"Logging": {
|
|
||||||
"LogLevel": {
|
|
||||||
"Default": "Information",
|
|
||||||
"Microsoft.AspNetCore": "Warning"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"AllowedHosts": "*"
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user