Files
ParadoxSaveParser/ParadoxSaveParser.WebAPI/Config.cs
T

61 lines
2.4 KiB
C#

using System.Text.Json.Serialization;
using ZstdSharp;
namespace ParadoxSaveParser.WebAPI;
public class Config
{
[JsonIgnore]
public const int ActualVersion = 1;
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 SaveUploadConfig SaveUpload { get; set; } = new();
/// level used to compress parsed save data
public int ZstdCompressionLevel { get; set; } = 10;
public class SaveUploadConfig
{
/// max size of game data entry after decompression, in MiB
public int MaxSizePlainTextMegabytes { get; set; } = 200;
/// max size of uploaded .tar.zst, in MiB
public int MaxSizeCompressedMegabytes { get; set; } = 60;
// long, because int overflows at 2048 MiB
[JsonIgnore]
public long MaxSizePlainTextBytes => MaxSizePlainTextMegabytes * 1024L * 1024L;
[JsonIgnore]
public long MaxSizeCompressedBytes => MaxSizeCompressedMegabytes * 1024L * 1024L;
}
public static JsonSerializerOptions JsonOptions = new()
{
WriteIndented = false,
MaxDepth = 1024,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
};
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)
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})");
// fail on start instead of on first parsed save
if (cfg.ZstdCompressionLevel < Compressor.MinCompressionLevel
|| cfg.ZstdCompressionLevel > Compressor.MaxCompressionLevel)
throw new Exception($"{nameof(ZstdCompressionLevel)} must be in range "
+ $"[{Compressor.MinCompressionLevel}, {Compressor.MaxCompressionLevel}]");
return cfg;
}
public override string ToString() => JsonSerializer.Serialize(this, JsonOptions);
public void Save(IOPath configPath)
{
File.WriteAllText(configPath, ToString());
}
}