From 40279ba6b95b69e3d219a9fae59917a74ac4d4d0 Mon Sep 17 00:00:00 2001 From: Pilzinsel64 Date: Tue, 18 Jun 2024 11:17:57 +0200 Subject: [PATCH] migrate settings --- ModpackUpdater.Manager/ModpackInstaller.cs | 4 +- ModpackUpdater/AppConfig.cs | 61 +++------------------- ModpackUpdater/Form1.cs | 11 ---- ModpackUpdater/Program.cs | 49 +++++++++++++++-- 4 files changed, 55 insertions(+), 70 deletions(-) diff --git a/ModpackUpdater.Manager/ModpackInstaller.cs b/ModpackUpdater.Manager/ModpackInstaller.cs index bbacb43..1f32809 100644 --- a/ModpackUpdater.Manager/ModpackInstaller.cs +++ b/ModpackUpdater.Manager/ModpackInstaller.cs @@ -98,9 +98,7 @@ public class ModpackInstaller(ModpackConfig updateConfig, string localPath) if (ModpackInfo.HasModpackInfo(localPath)) modpackInfo = ModpackInfo.Load(localPath); else - { - modpackInfo = new ModpackInfo(); - } + modpackInfo = new(); foreach (InstallAction iaction in checkResult.Actions) { diff --git a/ModpackUpdater/AppConfig.cs b/ModpackUpdater/AppConfig.cs index c2828ed..d4d578f 100644 --- a/ModpackUpdater/AppConfig.cs +++ b/ModpackUpdater/AppConfig.cs @@ -1,67 +1,22 @@ using Newtonsoft.Json; +using Pilz.Configuration; namespace ModpackUpdater; -public class AppConfig +public class AppConfig : IChildSettings, ISettingsIdentifier { + public static string Identifier => "pilz.appconfig"; + public string LastMinecraftProfilePath { get; set; } public string LastConfigFilePath { get; set; } - public List KeepLocalFiles { get; set; } = []; + public List KeepLocalFiles { get; } = []; public void Reset() { + LastMinecraftProfilePath = null; + LastConfigFilePath = null; KeepLocalFiles.Clear(); - KeepLocalFiles.Add("OptiFine_1.7.10_HD_U_E7.jar"); } - private static AppConfig instance = null; - - public static AppConfig Instance - { - get - { - - if (instance is null) - { - if (File.Exists(SettingsPath)) - instance = LoadConfig(SettingsPath); - else - { - instance = new AppConfig(); - instance.Reset(); - } - } - - return instance; - } - } - private static string settingsPath = string.Empty; - - private static string SettingsPath - { - get - { - const string AppDataDirectoryName = "MinecraftModpackUpdater"; - const string SettingsFileName = "Settings.json"; - - if (string.IsNullOrEmpty(settingsPath)) - { - settingsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), AppDataDirectoryName); - Directory.CreateDirectory(settingsPath); - settingsPath = Path.Combine(settingsPath, SettingsFileName); - } - - return settingsPath; - } - } - - public void SaveConfig() - { - File.WriteAllText(SettingsPath, JsonConvert.SerializeObject(this)); - } - - private static AppConfig LoadConfig(string filePath) - { - return JsonConvert.DeserializeObject(File.ReadAllText(filePath)); - } + public static AppConfig Instance => Program.Settings.Get(); } \ No newline at end of file diff --git a/ModpackUpdater/Form1.cs b/ModpackUpdater/Form1.cs index 7870f9f..c01b56e 100644 --- a/ModpackUpdater/Form1.cs +++ b/ModpackUpdater/Form1.cs @@ -77,9 +77,7 @@ public partial class Form1 if (IsUpdateConfigLoaded()) RadButton_CheckForUpdates.PerformClick(); else - { ClearStatus(); - } } private void LoadUpdateConfigFile(string filePath) @@ -98,9 +96,7 @@ public partial class Form1 if (IsMinecaftProfileLoaded()) RadButton_CheckForUpdates.PerformClick(); else - { ClearStatus(); - } } private async Task ExecuteUpdate(bool doInstall) @@ -144,9 +140,7 @@ public partial class Form1 SetStatus(LangRes.StatusTest_EverythingOk, MySymbols.icons8_checkmark_16px); } else - { SetStatus(LangRes.StatusText_ErrorWhileUpdateCheckOrUpdate, MySymbols.icons8_delete_16px); - } } catch { @@ -158,14 +152,10 @@ public partial class Form1 } } else - { SetStatus(LangRes.StatusText_UpdateAvailable, MySymbols.icons8_software_installer_16px); - } } else - { SetStatus(LangRes.StatusTest_EverythingOk, MySymbols.icons8_checkmark_16px); - } } private void Update_InstallProgessUpdated(UpdateCheckResult result, int processedSyncs) @@ -223,7 +213,6 @@ public partial class Form1 { AppConfig.Instance.LastMinecraftProfilePath = RadTextBoxControl_MinecraftProfileFolder.Text; AppConfig.Instance.LastConfigFilePath = RadTextBoxControl_ModpackConfig.Text; - AppConfig.Instance.SaveConfig(); } private void Form1_Load(object sender, EventArgs e) diff --git a/ModpackUpdater/Program.cs b/ModpackUpdater/Program.cs index 3c1e48c..bd5938e 100644 --- a/ModpackUpdater/Program.cs +++ b/ModpackUpdater/Program.cs @@ -1,10 +1,22 @@ -using Telerik.WinControls; +using Newtonsoft.Json; +using Pilz.Configuration; +using Telerik.WinControls; namespace ModpackUpdater; -internal static class Program +public static class Program { - public static void Main(string[] args) + private static readonly SettingsManager settingsManager; + + public static ISettings Settings => settingsManager.Instance; + + static Program() + { + settingsManager = new(GetSettingsPath(2), true); + MigrateLegacySettings(GetSettingsPath(null)); + } + + internal static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); @@ -15,4 +27,35 @@ internal static class Program Application.Run(new Form1()); } + + private static string GetSettingsPath(int? settingsVersion = 2) + { + const string AppDataDirectoryName = "MinecraftModpackUpdater"; + var fileNamePostfix = settingsVersion == null ? string.Empty : $"V{settingsVersion}"; + var SettingsFileName = $"Settings{fileNamePostfix}.json"; + + var settingsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), AppDataDirectoryName); + Directory.CreateDirectory(settingsPath); + settingsPath = Path.Combine(settingsPath, SettingsFileName); + + return settingsPath; + } + + private static void MigrateLegacySettings(string settingsPath) + { + // Try load legacy config file + if (!File.Exists(settingsPath) || JsonConvert.DeserializeObject(File.ReadAllText(settingsPath)) is not AppConfig legacyConfig) + return; + + // Migrate + var newConfig = Settings.Get(); + newConfig.LastMinecraftProfilePath = legacyConfig.LastMinecraftProfilePath; + newConfig.LastConfigFilePath = legacyConfig.LastConfigFilePath; + + // Ensure save settings + settingsManager.Save(); + + // Delete legacy config file + File.Delete(settingsPath); + } }