migrate settings

This commit is contained in:
2024-06-18 11:17:57 +02:00
parent 0f3e93bfff
commit 40279ba6b9
4 changed files with 55 additions and 70 deletions

View File

@@ -98,9 +98,7 @@ public class ModpackInstaller(ModpackConfig updateConfig, string localPath)
if (ModpackInfo.HasModpackInfo(localPath)) if (ModpackInfo.HasModpackInfo(localPath))
modpackInfo = ModpackInfo.Load(localPath); modpackInfo = ModpackInfo.Load(localPath);
else else
{ modpackInfo = new();
modpackInfo = new ModpackInfo();
}
foreach (InstallAction iaction in checkResult.Actions) foreach (InstallAction iaction in checkResult.Actions)
{ {

View File

@@ -1,67 +1,22 @@
using Newtonsoft.Json; using Newtonsoft.Json;
using Pilz.Configuration;
namespace ModpackUpdater; namespace ModpackUpdater;
public class AppConfig public class AppConfig : IChildSettings, ISettingsIdentifier
{ {
public static string Identifier => "pilz.appconfig";
public string LastMinecraftProfilePath { get; set; } public string LastMinecraftProfilePath { get; set; }
public string LastConfigFilePath { get; set; } public string LastConfigFilePath { get; set; }
public List<string> KeepLocalFiles { get; set; } = []; public List<string> KeepLocalFiles { get; } = [];
public void Reset() public void Reset()
{ {
LastMinecraftProfilePath = null;
LastConfigFilePath = null;
KeepLocalFiles.Clear(); KeepLocalFiles.Clear();
KeepLocalFiles.Add("OptiFine_1.7.10_HD_U_E7.jar");
} }
private static AppConfig instance = null; public static AppConfig Instance => Program.Settings.Get<AppConfig>();
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<AppConfig>(File.ReadAllText(filePath));
}
} }

View File

@@ -77,9 +77,7 @@ public partial class Form1
if (IsUpdateConfigLoaded()) if (IsUpdateConfigLoaded())
RadButton_CheckForUpdates.PerformClick(); RadButton_CheckForUpdates.PerformClick();
else else
{
ClearStatus(); ClearStatus();
}
} }
private void LoadUpdateConfigFile(string filePath) private void LoadUpdateConfigFile(string filePath)
@@ -98,9 +96,7 @@ public partial class Form1
if (IsMinecaftProfileLoaded()) if (IsMinecaftProfileLoaded())
RadButton_CheckForUpdates.PerformClick(); RadButton_CheckForUpdates.PerformClick();
else else
{
ClearStatus(); ClearStatus();
}
} }
private async Task ExecuteUpdate(bool doInstall) private async Task ExecuteUpdate(bool doInstall)
@@ -144,9 +140,7 @@ public partial class Form1
SetStatus(LangRes.StatusTest_EverythingOk, MySymbols.icons8_checkmark_16px); SetStatus(LangRes.StatusTest_EverythingOk, MySymbols.icons8_checkmark_16px);
} }
else else
{
SetStatus(LangRes.StatusText_ErrorWhileUpdateCheckOrUpdate, MySymbols.icons8_delete_16px); SetStatus(LangRes.StatusText_ErrorWhileUpdateCheckOrUpdate, MySymbols.icons8_delete_16px);
}
} }
catch catch
{ {
@@ -158,14 +152,10 @@ public partial class Form1
} }
} }
else else
{
SetStatus(LangRes.StatusText_UpdateAvailable, MySymbols.icons8_software_installer_16px); SetStatus(LangRes.StatusText_UpdateAvailable, MySymbols.icons8_software_installer_16px);
}
} }
else else
{
SetStatus(LangRes.StatusTest_EverythingOk, MySymbols.icons8_checkmark_16px); SetStatus(LangRes.StatusTest_EverythingOk, MySymbols.icons8_checkmark_16px);
}
} }
private void Update_InstallProgessUpdated(UpdateCheckResult result, int processedSyncs) private void Update_InstallProgessUpdated(UpdateCheckResult result, int processedSyncs)
@@ -223,7 +213,6 @@ public partial class Form1
{ {
AppConfig.Instance.LastMinecraftProfilePath = RadTextBoxControl_MinecraftProfileFolder.Text; AppConfig.Instance.LastMinecraftProfilePath = RadTextBoxControl_MinecraftProfileFolder.Text;
AppConfig.Instance.LastConfigFilePath = RadTextBoxControl_ModpackConfig.Text; AppConfig.Instance.LastConfigFilePath = RadTextBoxControl_ModpackConfig.Text;
AppConfig.Instance.SaveConfig();
} }
private void Form1_Load(object sender, EventArgs e) private void Form1_Load(object sender, EventArgs e)

View File

@@ -1,10 +1,22 @@
using Telerik.WinControls; using Newtonsoft.Json;
using Pilz.Configuration;
using Telerik.WinControls;
namespace ModpackUpdater; 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.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Application.SetCompatibleTextRenderingDefault(false);
@@ -15,4 +27,35 @@ internal static class Program
Application.Run(new Form1()); 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<AppConfig>(File.ReadAllText(settingsPath)) is not AppConfig legacyConfig)
return;
// Migrate
var newConfig = Settings.Get<AppConfig>();
newConfig.LastMinecraftProfilePath = legacyConfig.LastMinecraftProfilePath;
newConfig.LastConfigFilePath = legacyConfig.LastConfigFilePath;
// Ensure save settings
settingsManager.Save();
// Delete legacy config file
File.Delete(settingsPath);
}
} }