replaced dtsod with json
This commit is contained in:
@@ -4,8 +4,13 @@ global using DTLib.Demystifier;
|
|||||||
global using DTLib.Filesystem;
|
global using DTLib.Filesystem;
|
||||||
global using Directory = DTLib.Filesystem.Directory;
|
global using Directory = DTLib.Filesystem.Directory;
|
||||||
global using File = DTLib.Filesystem.File;
|
global using File = DTLib.Filesystem.File;
|
||||||
using ParadoxSaveParser.CLI;
|
|
||||||
|
|
||||||
|
namespace ParadoxSaveParser.CLI;
|
||||||
|
|
||||||
|
internal class Program
|
||||||
|
{
|
||||||
|
public static int Main(string[] args)
|
||||||
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Mode mode = Mode.Unset;
|
Mode mode = Mode.Unset;
|
||||||
@@ -64,7 +69,12 @@ catch (LaunchArgumentParser.ExitAfterHelpException)
|
|||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
ColoredConsole.WriteLine(ex.ToStringDemystified(), ConsoleColor.Red);
|
ColoredConsole.WriteLine(ex.ToStringDemystified(), ConsoleColor.Red);
|
||||||
Console.ResetColor();
|
return 1;
|
||||||
Environment.Exit(1);
|
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
Console.ResetColor();
|
Console.ResetColor();
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,21 +1,27 @@
|
|||||||
using DTLib.Dtsod;
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace ParadoxSaveParser.WebAPI;
|
namespace ParadoxSaveParser.WebAPI;
|
||||||
|
|
||||||
public class Config
|
public class Config
|
||||||
{
|
{
|
||||||
|
[JsonIgnore]
|
||||||
public const int ActualVersion = 1;
|
public const int ActualVersion = 1;
|
||||||
public string BaseUrl = "http://127.0.0.1:5226/";
|
public int Version { get; set; } = ActualVersion;
|
||||||
|
public string BaseUrl { get; set; } = "http://127.0.0.1:5226/";
|
||||||
|
public int UploadLifetimeDays { get; set; } = 30;
|
||||||
|
public string DbPath { get; set; } = "database.sqlite";
|
||||||
|
|
||||||
public int Version = ActualVersion;
|
public static JsonSerializerOptions JsonOptions = new()
|
||||||
|
|
||||||
public static Config FromDtsod(DtsodV23 d)
|
|
||||||
{
|
{
|
||||||
var cfg = new Config
|
WriteIndented = false,
|
||||||
{
|
MaxDepth = 1024,
|
||||||
Version = d["version"],
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
||||||
BaseUrl = d["baseUrl"]
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
public static Config Load(IOPath configPath)
|
||||||
|
{
|
||||||
|
var cfg = JsonSerializer.Deserialize<Config>(File.ReadAllText(configPath))
|
||||||
|
?? throw new Exception("Could not parse config file");
|
||||||
if (cfg.Version < ActualVersion)
|
if (cfg.Version < ActualVersion)
|
||||||
throw new Exception($"config is obsolete (config v{cfg.Version} < program v{ActualVersion})");
|
throw new Exception($"config is obsolete (config v{cfg.Version} < program v{ActualVersion})");
|
||||||
if (cfg.Version > ActualVersion)
|
if (cfg.Version > ActualVersion)
|
||||||
@@ -23,12 +29,10 @@ public class Config
|
|||||||
return cfg;
|
return cfg;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DtsodV23 ToDtsod()
|
public override string ToString() => JsonSerializer.Serialize(this, JsonOptions);
|
||||||
=> new()
|
|
||||||
{
|
|
||||||
{ "version", Version },
|
|
||||||
{ "baseUrl", BaseUrl }
|
|
||||||
};
|
|
||||||
|
|
||||||
public override string ToString() => ToDtsod().ToString();
|
public void Save(IOPath configPath)
|
||||||
|
{
|
||||||
|
File.WriteAllText(configPath, ToString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -12,7 +12,6 @@ global using Directory = DTLib.Filesystem.Directory;
|
|||||||
global using File = DTLib.Filesystem.File;
|
global using File = DTLib.Filesystem.File;
|
||||||
global using Path = DTLib.Filesystem.Path;
|
global using Path = DTLib.Filesystem.Path;
|
||||||
using DTLib.Console;
|
using DTLib.Console;
|
||||||
using DTLib.Dtsod;
|
|
||||||
using DTLib.Web;
|
using DTLib.Web;
|
||||||
using DTLib.Web.Routes;
|
using DTLib.Web.Routes;
|
||||||
using ParadoxSaveParser.WebAPI.BackgroundTasks;
|
using ParadoxSaveParser.WebAPI.BackgroundTasks;
|
||||||
@@ -29,7 +28,7 @@ public static class Program
|
|||||||
internal static DatabaseConnector DB = null!;
|
internal static DatabaseConnector DB = null!;
|
||||||
|
|
||||||
|
|
||||||
public static async Task Main(string[] args)
|
public static async Task<int> Main(string[] args)
|
||||||
{
|
{
|
||||||
Console.InputEncoding = Encoding.UTF8;
|
Console.InputEncoding = Encoding.UTF8;
|
||||||
Console.OutputEncoding = Encoding.UTF8;
|
Console.OutputEncoding = Encoding.UTF8;
|
||||||
@@ -43,7 +42,8 @@ public static class Program
|
|||||||
new LaunchArgument(["d", "debug"],
|
new LaunchArgument(["d", "debug"],
|
||||||
"enables debug log output to console",
|
"enables debug log output to console",
|
||||||
() => IsDebug = true)
|
() => IsDebug = true)
|
||||||
).AllowNoArguments().ParseAndHandle(args);
|
)
|
||||||
|
.AllowNoArguments().ParseAndHandle(args);
|
||||||
|
|
||||||
var loggerRoot = new CompositeLogger(
|
var loggerRoot = new CompositeLogger(
|
||||||
new ConsoleLogger
|
new ConsoleLogger
|
||||||
@@ -68,23 +68,22 @@ public static class Program
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
// config
|
// config
|
||||||
IOPath configPath = "./config.dtsod";
|
IOPath configPath = "config.json";
|
||||||
Config config;
|
Config config;
|
||||||
if (File.Exists(configPath))
|
if (File.Exists(configPath))
|
||||||
{
|
{
|
||||||
config = Config.FromDtsod(new DtsodV23(File.ReadAllText(configPath)));
|
config = Config.Load(configPath);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
loggerMain.LogWarn("config file not found.");
|
loggerMain.LogWarn($"Config file not found. Creating default config at {configPath}.");
|
||||||
config = new();
|
config = new Config();
|
||||||
File.WriteAllText(configPath, config.ToString());
|
config.Save(configPath);
|
||||||
loggerMain.LogWarn($"created default at {configPath}.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PathHelper.CreateProgramDirectories();
|
PathHelper.CreateProgramDirectories();
|
||||||
PathHelper.CleanTempDirectory();
|
PathHelper.CleanTempDirectory();
|
||||||
DB = new("database.sqlite", 30, loggerRoot);
|
DB = new DatabaseConnector(config.DbPath, config.UploadLifetimeDays, loggerRoot);
|
||||||
await DB.InitializeAsync();
|
await DB.InitializeAsync();
|
||||||
await DB.DeleteInvalidData();
|
await DB.DeleteInvalidData();
|
||||||
|
|
||||||
@@ -108,6 +107,10 @@ public static class Program
|
|||||||
var app = new WebApp(config.BaseUrl, loggerRoot, router, mainCancel.Token);
|
var app = new WebApp(config.BaseUrl, loggerRoot, router, mainCancel.Token);
|
||||||
await app.Run();
|
await app.Run();
|
||||||
}
|
}
|
||||||
|
catch (LaunchArgumentParser.ExitAfterHelpException)
|
||||||
|
{
|
||||||
|
// this exception is throwed after -h argument to close the program
|
||||||
|
}
|
||||||
catch (OperationCanceledException ex)
|
catch (OperationCanceledException ex)
|
||||||
{
|
{
|
||||||
loggerMain.LogWarn($"catched OperationCanceledException from {ex.Source}");
|
loggerMain.LogWarn($"catched OperationCanceledException from {ex.Source}");
|
||||||
@@ -115,6 +118,12 @@ public static class Program
|
|||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
loggerMain.LogError(ex.ToStringDemystified());
|
loggerMain.LogError(ex.ToStringDemystified());
|
||||||
}
|
return 1;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
Console.ResetColor();
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user