forked from Cookies_Public/AquaDX
更新了配置文件格式,原有的配置文件将被自动无缝迁移,详情请见新的配置文件中的注释(例外:`SlideJudgeTweak` 不再默认启用) 旧配置文件将被重命名备份,如果更新到此版本遇到 Bug 请联系我们 Updated configuration file schema. The old config file will be migrated automatically and seamlessly. See the comments in the new configuration file for details. (Except for `SlideJudgeTweak` is no longer enabled by default) Your old configuration file will be renamed as a backup. If you encounter any bug with this version, please contact us.
59 lines
1.7 KiB
C#
59 lines
1.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using AquaMai.Config.Interfaces;
|
|
|
|
namespace AquaMai.Config.Migration;
|
|
|
|
public class ConfigMigrationManager : IConfigMigrationManager
|
|
{
|
|
public static readonly ConfigMigrationManager Instance = new();
|
|
|
|
private readonly Dictionary<string, IConfigMigration> migrationMap =
|
|
new List<IConfigMigration>
|
|
{
|
|
new ConfigMigration_V1_0_V2_0()
|
|
}.ToDictionary(m => m.FromVersion);
|
|
|
|
public readonly string latestVersion;
|
|
|
|
private ConfigMigrationManager()
|
|
{
|
|
latestVersion = migrationMap.Values
|
|
.Select(m => m.ToVersion)
|
|
.OrderByDescending(version =>
|
|
{
|
|
var versionParts = version.Split('.').Select(int.Parse).ToArray();
|
|
return versionParts[0] * 100000 + versionParts[1];
|
|
})
|
|
.First();
|
|
}
|
|
|
|
public IConfigView Migrate(IConfigView config)
|
|
{
|
|
var currentVersion = GetVersion(config);
|
|
while (migrationMap.ContainsKey(currentVersion))
|
|
{
|
|
var migration = migrationMap[currentVersion];
|
|
Utility.Log($"Migrating config from v{migration.FromVersion} to v{migration.ToVersion}");
|
|
config = migration.Migrate(config);
|
|
currentVersion = migration.ToVersion;
|
|
}
|
|
if (currentVersion != latestVersion)
|
|
{
|
|
throw new ArgumentException($"Could not migrate the config from v{currentVersion} to v{latestVersion}");
|
|
}
|
|
return config;
|
|
}
|
|
|
|
public string GetVersion(IConfigView config)
|
|
{
|
|
if (config.TryGetValue<string>("Version", out var version))
|
|
{
|
|
return version;
|
|
}
|
|
// Assume v1.0 if not found
|
|
return "1.0";
|
|
}
|
|
}
|