implemented SearchExpression (buggy)
This commit is contained in:
@@ -13,9 +13,6 @@ 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;
|
||||
@@ -24,7 +21,7 @@ using DTLib.Web.Routes;
|
||||
|
||||
namespace ParadoxSaveParser.WebAPI;
|
||||
|
||||
public class Program
|
||||
public partial class Program
|
||||
{
|
||||
private static readonly IOPath _configPath = "./config.dtsod";
|
||||
private static Config _config = new();
|
||||
@@ -43,9 +40,64 @@ public class Program
|
||||
MaxDepth = 1024,
|
||||
};
|
||||
|
||||
static void TestSearchExpression(Stream saveStream, TestCase tc)
|
||||
{
|
||||
saveStream.Seek(0, SeekOrigin.Begin);
|
||||
var se = SearchExpression.Parse(tc.q);
|
||||
var parser = new SaveParserEU4(saveStream, se);
|
||||
var rootNode = parser.Parse();
|
||||
string json = JsonSerializer.Serialize(rootNode, _saveSerializerOptions);
|
||||
string pdx = json.Substring(1, json.Length - 2)
|
||||
.Replace(",", " ").Replace("{", "{ ").Replace("}", " }")
|
||||
.Replace("\"", "").Replace("[", "").Replace("]", "").Replace(":", "=");
|
||||
if(pdx == tc.a)
|
||||
{
|
||||
Console.WriteLine($"[OK] q:'{tc.q}' a:'{tc.a}'");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"[Error] q:'{tc.q}' a:'{tc.a}' r:'{pdx}'");
|
||||
}
|
||||
}
|
||||
|
||||
record TestCase(string q, string a);
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
|
||||
using var saveStream = new MemoryStream(
|
||||
"EU4txt a={ b={ c=0 d=1 e=2 } f=3 }".ToBytes(),
|
||||
false);
|
||||
|
||||
TestCase[] testCases = [
|
||||
new("a",
|
||||
"a={ b={ c=0 d=1 e=2 } f=3 }"),
|
||||
|
||||
new("a.*",
|
||||
"a={ b={ c=0 d=1 e=2 } f=3 }"),
|
||||
|
||||
new("a.b",
|
||||
"a={ b={ c=0 d=1 e=2 } }"),
|
||||
|
||||
new("a.[0].c",
|
||||
"a={ b={ c=0 } }"),
|
||||
|
||||
new("a.[1]",
|
||||
"a={ f=3 }"),
|
||||
|
||||
new("a.b.(c|d)",
|
||||
"a={ b={ c=0 d=1 } }"),
|
||||
|
||||
new("a.(b.e|f)",
|
||||
"a={ b={ e=2 } f=3 }"),
|
||||
];
|
||||
|
||||
foreach (var test in testCases)
|
||||
{
|
||||
TestSearchExpression(saveStream, test);
|
||||
}
|
||||
|
||||
/*
|
||||
Console.InputEncoding = Encoding.UTF8;
|
||||
Console.OutputEncoding = Encoding.UTF8;
|
||||
Console.CursorVisible = false;
|
||||
@@ -88,6 +140,7 @@ public class Program
|
||||
{
|
||||
logger.LogError(ex.ToStringDemystified());
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
public static void PrepareLocalFiles()
|
||||
@@ -111,111 +164,4 @@ public class Program
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user