From c97a04c4ce80caabd26afbb308b9ade462386bc3 Mon Sep 17 00:00:00 2001 From: Pilzinsel64 Date: Thu, 20 Jun 2024 15:05:01 +0200 Subject: [PATCH] load modpack info before modpack installer --- .../MinecraftProfileChecker.cs | 11 -------- ModpackUpdater.Manager/ModpackInstaller.cs | 20 +++++--------- ModpackUpdater.Model/ModpackConfig.cs | 7 ++++- ModpackUpdater.Model/ModpackInfo.cs | 26 ++++++++++++++++--- ModpackUpdater/Form1.cs | 21 ++++++++++++--- ModpackUpdater/Program.cs | 12 +++++++-- 6 files changed, 64 insertions(+), 33 deletions(-) delete mode 100644 ModpackUpdater.Manager/MinecraftProfileChecker.cs diff --git a/ModpackUpdater.Manager/MinecraftProfileChecker.cs b/ModpackUpdater.Manager/MinecraftProfileChecker.cs deleted file mode 100644 index e7deed7..0000000 --- a/ModpackUpdater.Manager/MinecraftProfileChecker.cs +++ /dev/null @@ -1,11 +0,0 @@ - -namespace ModpackUpdater.Manager; - -public class MinecraftProfileChecker -{ - public static bool CheckProfile(string folderPath) - { - // Todo: Adds some checks to verify, if the current folder is a minecraft folder. - return true; - } -} \ No newline at end of file diff --git a/ModpackUpdater.Manager/ModpackInstaller.cs b/ModpackUpdater.Manager/ModpackInstaller.cs index bb9332b..20db81e 100644 --- a/ModpackUpdater.Manager/ModpackInstaller.cs +++ b/ModpackUpdater.Manager/ModpackInstaller.cs @@ -3,7 +3,7 @@ using System.IO.Compression; namespace ModpackUpdater.Manager; -public class ModpackInstaller(ModpackConfig updateConfig, string localPath) +public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInfo) { private class LocalZipCacheEntry { @@ -40,7 +40,7 @@ public class ModpackInstaller(ModpackConfig updateConfig, string localPath) public async Task Check(UpdateCheckOptions options) { var result = new UpdateCheckResult(); - var hasConfig = ModpackInfo.HasModpackInfo(localPath); + var hasConfig = modpackInfo.Exists; InstallInfos installInfos = null; UpdateInfos updateInfos = null; @@ -71,7 +71,6 @@ public class ModpackInstaller(ModpackConfig updateConfig, string localPath) if (options.AllowUpdaterAfterInstall) { updateInfos = await DownloadUpdateInfos(); - var modpackInfo = ModpackInfo.HasModpackInfo(localPath) ? ModpackInfo.Load(localPath) : new(); if (updateInfos is not null && updateInfos.Updates.Count != 0) { @@ -108,18 +107,12 @@ public class ModpackInstaller(ModpackConfig updateConfig, string localPath) public async Task Install(UpdateCheckResult checkResult) { - ModpackInfo modpackInfo; int processed = 0; var localZipCache = new List(); - if (ModpackInfo.HasModpackInfo(localPath)) - modpackInfo = ModpackInfo.Load(localPath); - else - modpackInfo = new(); - foreach (InstallAction iaction in checkResult.Actions) { - string destFilePath = Path.Combine(localPath, iaction.DestPath); + string destFilePath = Path.Combine(modpackInfo.LocaLPath, iaction.DestPath); if (iaction is UpdateAction uaction) { @@ -141,7 +134,7 @@ public class ModpackInstaller(ModpackConfig updateConfig, string localPath) break; case UpdateActionType.Copy: { - var srcFilePath = Path.Combine(localPath, uaction.SrcPath); + var srcFilePath = Path.Combine(modpackInfo.LocaLPath, uaction.SrcPath); if (uaction.IsDirectory) { if (Directory.Exists(srcFilePath)) @@ -156,7 +149,7 @@ public class ModpackInstaller(ModpackConfig updateConfig, string localPath) break; case UpdateActionType.Move: { - var srcFilePath = Path.Combine(localPath, uaction.SrcPath); + var srcFilePath = Path.Combine(modpackInfo.LocaLPath, uaction.SrcPath); if (uaction.IsDirectory) { if (Directory.Exists(srcFilePath)) @@ -180,7 +173,8 @@ public class ModpackInstaller(ModpackConfig updateConfig, string localPath) // Save new modpack info modpackInfo.Version = checkResult.LatestVersion; - modpackInfo.Save(localPath); + modpackInfo.ConfigUrl = updateConfig.ConfigUrl; + modpackInfo.Save(); // Delete cached zip files foreach (var task in localZipCache) diff --git a/ModpackUpdater.Model/ModpackConfig.cs b/ModpackUpdater.Model/ModpackConfig.cs index 3e7bdfa..b30e30c 100644 --- a/ModpackUpdater.Model/ModpackConfig.cs +++ b/ModpackUpdater.Model/ModpackConfig.cs @@ -9,9 +9,14 @@ public class ModpackConfig public string UpdateUrl { get; set; } public string InstallUrl { get; set; } + [JsonIgnore] + public string ConfigUrl { get; set; } + public static ModpackConfig LoadFromUrl(string url) { string result = new HttpClient().GetStringAsync(url).Result; - return JsonConvert.DeserializeObject(result); + var config = JsonConvert.DeserializeObject(result); + config.ConfigUrl = url; + return config; } } \ No newline at end of file diff --git a/ModpackUpdater.Model/ModpackInfo.cs b/ModpackUpdater.Model/ModpackInfo.cs index 7aba881..5ca74b1 100644 --- a/ModpackUpdater.Model/ModpackInfo.cs +++ b/ModpackUpdater.Model/ModpackInfo.cs @@ -10,15 +10,35 @@ public class ModpackInfo [JsonConverter(typeof(VersionConverter))] public Version Version { get; set; } + public string ConfigUrl { get; set; } + [JsonIgnore] + public string LocaLPath { get; private set; } + [JsonIgnore] + public bool Exists => Directory.Exists(LocaLPath); + + public void Save() + { + File.WriteAllText(Conversions.ToString(GetFilePath(LocaLPath)), JsonConvert.SerializeObject(this)); + } public void Save(string mcRoot) { - File.WriteAllText(Conversions.ToString(GetFilePath(mcRoot)), JsonConvert.SerializeObject(this)); + LocaLPath = mcRoot; + Save(); + } + + public static ModpackInfo TryLoad(string mcRoot) + { + if (HasModpackInfo(mcRoot)) + return Load(mcRoot); + return new(); } public static ModpackInfo Load(string mcRoot) { - return JsonConvert.DeserializeObject(File.ReadAllText(Conversions.ToString(GetFilePath(mcRoot)))); + var info = JsonConvert.DeserializeObject(File.ReadAllText(GetFilePath(mcRoot))); + info.LocaLPath = mcRoot; + return info; } public static bool HasModpackInfo(string mcRoot) @@ -26,7 +46,7 @@ public class ModpackInfo return File.Exists(Conversions.ToString(GetFilePath(mcRoot))); } - private static object GetFilePath(string mcRoot) + private static string GetFilePath(string mcRoot) { return Path.Combine(mcRoot, FILENAME_MODPACKINFO); } diff --git a/ModpackUpdater/Form1.cs b/ModpackUpdater/Form1.cs index 2877a03..e3edda5 100644 --- a/ModpackUpdater/Form1.cs +++ b/ModpackUpdater/Form1.cs @@ -10,6 +10,7 @@ namespace ModpackUpdater; public partial class Form1 { + private ModpackInfo modpackInfo = null; private ModpackConfig updateConfig = new(); private bool currentUpdating = false; private UpdateCheckResult lastUpdateCheckResult = null; @@ -65,7 +66,7 @@ public partial class Form1 { bool CheckStatusRet; - if (!IsMinecaftProfileLoaded() || !MinecraftProfileChecker.CheckProfile(GetMinecraftProfilePath())) + if (!IsMinecaftProfileLoaded()) { SetStatus(LangRes.StatusText_MinecraftProfileWarning, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.general_warning_sign, SvgImageSize.Small)); CheckStatusRet = false; @@ -98,7 +99,21 @@ public partial class Form1 RadTextBoxControl_MinecraftProfileFolder.Text = folderPath; if (IsUpdateConfigLoaded()) - RadButton_CheckForUpdates.PerformClick(); + { + try + { + modpackInfo = ModpackInfo.TryLoad(folderPath); + } + catch + { + RadTextBoxControl_MinecraftProfileFolder.Text = string.Empty; + } + + if (string.IsNullOrWhiteSpace(RadTextBoxControl_ModpackConfig.Text) && !string.IsNullOrWhiteSpace(modpackInfo?.ConfigUrl)) + LoadUpdateConfigFile(modpackInfo.ConfigUrl); + else + RadButton_CheckForUpdates.PerformClick(); + } else ClearStatus(); } @@ -127,7 +142,7 @@ public partial class Form1 private async Task ExecuteUpdate(bool doInstall) { - var updater = new ModpackInstaller(updateConfig, GetMinecraftProfilePath()); + var updater = new ModpackInstaller(updateConfig, modpackInfo); updater.InstallProgessUpdated += Update_InstallProgessUpdated; updater.CheckingProgressUpdated += Updated_CheckingProgresssUpdated; diff --git a/ModpackUpdater/Program.cs b/ModpackUpdater/Program.cs index 7e0c00b..eb96ee9 100644 --- a/ModpackUpdater/Program.cs +++ b/ModpackUpdater/Program.cs @@ -73,8 +73,9 @@ public static class Program private static void InstallWithoutGui(UpdateCheckOptionsAdv updateOptions, bool silent) { - var config = ModpackConfig.LoadFromUrl(updateOptions.ModpackConfig); - var installer = new ModpackInstaller(config, updateOptions.ProfileFolder); + var info = ModpackInfo.TryLoad(updateOptions.ProfileFolder); + var config = ModpackConfig.LoadFromUrl(CheckModpackConfigUrl(updateOptions.ModpackConfig, info)); + var installer = new ModpackInstaller(config, info); var result = installer.Check(updateOptions).Result; if (!silent && !updateOptions.NoUpdate && new AppUpdater().Check().Result) @@ -89,4 +90,11 @@ public static class Program else if (!silent) Console.WriteLine("No updates available"); } + + private static string CheckModpackConfigUrl(string configUrl, ModpackInfo info) + { + if (string.IsNullOrWhiteSpace(configUrl) && !string.IsNullOrWhiteSpace(info.ConfigUrl)) + return info.ConfigUrl; + return configUrl; + } }