[+] Add LatestVersion to interface
AquaMai Build / build (push) Has been cancelled Details

pull/91/head
Clansty 2024-11-25 04:04:04 +08:00
parent 734dbfb761
commit 4834363fb5
No known key found for this signature in database
GPG Key ID: 3A6BE8BAF2EDE134
4 changed files with 8 additions and 7 deletions

View File

@ -4,4 +4,5 @@ public interface IConfigMigrationManager
{
public IConfigView Migrate(IConfigView config);
public string GetVersion(IConfigView config);
public string LatestVersion { get; }
}

View File

@ -31,9 +31,9 @@ public class ConfigParser : IConfigParser
public void Parse(IConfig config, IConfigView configView)
{
var configVersion = ConfigMigrationManager.Instance.GetVersion(configView);
if (configVersion != ConfigMigrationManager.Instance.latestVersion)
if (configVersion != ConfigMigrationManager.Instance.LatestVersion)
{
throw new InvalidOperationException($"Config version mismatch: expected {ConfigMigrationManager.Instance.latestVersion}, got {configVersion}");
throw new InvalidOperationException($"Config version mismatch: expected {ConfigMigrationManager.Instance.LatestVersion}, got {configVersion}");
}
Hydrate((Config)config, ((ConfigView)configView).root, "");
}

View File

@ -15,11 +15,11 @@ public class ConfigMigrationManager : IConfigMigrationManager
new ConfigMigration_V1_0_V2_0()
}.ToDictionary(m => m.FromVersion);
public readonly string latestVersion;
public string LatestVersion { get; }
private ConfigMigrationManager()
{
latestVersion = migrationMap.Values
LatestVersion = migrationMap.Values
.Select(m => m.ToVersion)
.OrderByDescending(version =>
{
@ -39,9 +39,9 @@ public class ConfigMigrationManager : IConfigMigrationManager
config = migration.Migrate(config);
currentVersion = migration.ToVersion;
}
if (currentVersion != latestVersion)
if (currentVersion != LatestVersion)
{
throw new ArgumentException($"Could not migrate the config from v{currentVersion} to v{latestVersion}");
throw new ArgumentException($"Could not migrate the config from v{currentVersion} to v{LatestVersion}");
}
return config;
}

View File

@ -46,7 +46,7 @@ public static class ConfigLoader
var configText = File.ReadAllText(ConfigFile);
var configView = new ConfigView(configText);
var configVersion = ConfigMigrationManager.Instance.GetVersion(configView);
if (configVersion != ConfigMigrationManager.Instance.latestVersion)
if (configVersion != ConfigMigrationManager.Instance.LatestVersion)
{
File.WriteAllText(OldConfigFile(configVersion), configText);
configView = (ConfigView)ConfigMigrationManager.Instance.Migrate(configView);