mirror of
https://github.com/Ottermandias/Glamourer.git
synced 2026-02-02 14:04:38 +01:00
65 lines
2.3 KiB
C#
65 lines
2.3 KiB
C#
using System;
|
|
using System.IO;
|
|
using Glamourer.Automation;
|
|
using Glamourer.Gui;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace Glamourer.Services;
|
|
|
|
public class ConfigMigrationService
|
|
{
|
|
private readonly SaveService _saveService;
|
|
private readonly FixedDesignMigrator _fixedDesignMigrator;
|
|
private readonly BackupService _backupService;
|
|
|
|
private Configuration _config = null!;
|
|
private JObject _data = null!;
|
|
|
|
public ConfigMigrationService(SaveService saveService, FixedDesignMigrator fixedDesignMigrator, BackupService backupService)
|
|
{
|
|
_saveService = saveService;
|
|
_fixedDesignMigrator = fixedDesignMigrator;
|
|
_backupService = backupService;
|
|
}
|
|
|
|
public void Migrate(Configuration config)
|
|
{
|
|
_config = config;
|
|
if (config.Version >= Configuration.Constants.CurrentVersion || !File.Exists(_saveService.FileNames.ConfigFile))
|
|
{
|
|
AddColors(config, false);
|
|
return;
|
|
}
|
|
|
|
_data = JObject.Parse(File.ReadAllText(_saveService.FileNames.ConfigFile));
|
|
MigrateV1To2();
|
|
AddColors(config, true);
|
|
}
|
|
|
|
private void MigrateV1To2()
|
|
{
|
|
if (_config.Version > 1)
|
|
return;
|
|
|
|
_backupService.CreateMigrationBackup("pre_v1_to_v2_migration");
|
|
_fixedDesignMigrator.Migrate(_data["FixedDesigns"]);
|
|
_config.Version = 2;
|
|
var customizationColor = _data["CustomizationColor"]?.ToObject<uint>() ?? ColorId.CustomizationDesign.Data().DefaultColor;
|
|
_config.Colors[ColorId.CustomizationDesign] = customizationColor;
|
|
var stateColor = _data["StateColor"]?.ToObject<uint>() ?? ColorId.StateDesign.Data().DefaultColor;
|
|
_config.Colors[ColorId.StateDesign] = stateColor;
|
|
var equipmentColor = _data["EquipmentColor"]?.ToObject<uint>() ?? ColorId.EquipmentDesign.Data().DefaultColor;
|
|
_config.Colors[ColorId.EquipmentDesign] = equipmentColor;
|
|
}
|
|
|
|
private static void AddColors(Configuration config, bool forceSave)
|
|
{
|
|
var save = false;
|
|
foreach (var color in Enum.GetValues<ColorId>())
|
|
save |= config.Colors.TryAdd(color, color.Data().DefaultColor);
|
|
|
|
if (save || forceSave)
|
|
config.Save();
|
|
Colors.SetColors(config);
|
|
}
|
|
}
|