diff --git a/ModpackUpdater.Manager/Extensions.cs b/ModpackUpdater.Manager/Extensions.cs index 6b4b7d3..61e6d00 100644 --- a/ModpackUpdater.Manager/Extensions.cs +++ b/ModpackUpdater.Manager/Extensions.cs @@ -2,7 +2,6 @@ namespace ModpackUpdater.Manager; - public static class Extensions { public static bool IsSide(this Side @this, Side side) @@ -30,7 +29,7 @@ public static class Extensions foreach (FileInfo @file in dir.GetFiles()) { string targetFilePath = Path.Combine(destinationDir, @file.Name); - @file.CopyTo(targetFilePath); + @file.CopyTo(targetFilePath, true); } // If recursive and copying subdirectories, recursively call this method @@ -43,5 +42,4 @@ public static class Extensions } } } - } \ No newline at end of file diff --git a/ModpackUpdater.Manager/ModpackInstaller.cs b/ModpackUpdater.Manager/ModpackInstaller.cs index 50b3157..a03a35f 100644 --- a/ModpackUpdater.Manager/ModpackInstaller.cs +++ b/ModpackUpdater.Manager/ModpackInstaller.cs @@ -42,6 +42,7 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf InstallInfos installInfos = null; UpdateInfos updateInfos = null; var result = new UpdateCheckResult(); + var exists = modpackInfo.Exists; if (updateConfig.Maintenance && !options.IgnoreMaintenance) { @@ -55,7 +56,7 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf return result; } - if (!modpackInfo.Exists) + if (!exists) { installInfos = await DownloadInstallInfos(); @@ -66,6 +67,7 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf { result.Actions.AddRange(installInfos.Actions); result.LatestVersion = installInfos.Version; + result.CurrentVersion = installInfos.Version; } } @@ -73,7 +75,7 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf result.HasError = true; } - if (options.AllowUpdaterAfterInstall) + if (exists || options.AllowUpdaterAfterInstall) { updateInfos = await DownloadUpdateInfos(); @@ -81,11 +83,13 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf { var updatesOrderes = updateInfos.Updates.OrderByDescending(n => n.Version); result.LatestVersion = updatesOrderes.First().Version; - result.CurrentVersion = modpackInfo.Version; + if (exists) + result.CurrentVersion = modpackInfo.Version; result.IsInstalled = true; var checkingVersionIndex = 0; var checkingVersion = updatesOrderes.ElementAtOrDefault(checkingVersionIndex); + var actionsZeroIndex = result.Actions.Count; // Ensure we insert update actions behind install actions while (checkingVersion is not null && checkingVersion.Version > result.CurrentVersion) { @@ -97,7 +101,7 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf actionsToAdd.Add(action); } - result.Actions.InsertRange(0, actionsToAdd); + result.Actions.InsertRange(actionsZeroIndex, actionsToAdd); checkingVersionIndex += 1; checkingVersion = updatesOrderes.ElementAtOrDefault(checkingVersionIndex); diff --git a/ModpackUpdater.Model/ModpackInfo.cs b/ModpackUpdater.Model/ModpackInfo.cs index 832f2a7..4dd660d 100644 --- a/ModpackUpdater.Model/ModpackInfo.cs +++ b/ModpackUpdater.Model/ModpackInfo.cs @@ -16,7 +16,7 @@ public class ModpackInfo [JsonIgnore] public string LocaLPath { get; private set; } [JsonIgnore] - public bool Exists => Directory.Exists(LocaLPath); + public bool Exists => File.Exists(GetFilePath(LocaLPath)); public void Save() { diff --git a/ModpackUpdater/Form1.cs b/ModpackUpdater/Form1.cs index 6a3e2f8..9cbc9c3 100644 --- a/ModpackUpdater/Form1.cs +++ b/ModpackUpdater/Form1.cs @@ -22,6 +22,8 @@ public partial class Form1 if (!string.IsNullOrWhiteSpace(updateOptions.ProfileFolder)) LoadMinecraftProfile(updateOptions.ProfileFolder); + else if (!string.IsNullOrWhiteSpace(AppConfig.Instance.LastMinecraftProfilePath)) + LoadMinecraftProfile(AppConfig.Instance.LastMinecraftProfilePath); } public Form1() @@ -38,44 +40,49 @@ public partial class Form1 RadButton_PasteModpackConfig.SvgImage = AppSymbolFactory.Instance.GetSvgImage(AppSymbols.paste, SvgImageSize.Small); } - private bool IsMinecaftProfileLoaded() + private void LoadMinecraftProfile(string folderPath) { - return !string.IsNullOrEmpty(GetMinecraftProfilePath()); - } + RadTextBoxControl_MinecraftProfileFolder.Text = folderPath; + AppConfig.Instance.LastMinecraftProfilePath = folderPath; - private string GetMinecraftProfilePath() - { - return RadTextBoxControl_MinecraftProfileFolder.Text.Trim(); - } + if (string.IsNullOrWhiteSpace(folderPath)) + return; - private bool IsUpdateConfigLoaded() - { - return !string.IsNullOrEmpty(GetUpdateconfigPath()); - } - - private string GetUpdateconfigPath() - { - return RadTextBoxControl_ModpackConfig.Text.Trim(); - } - - private bool CheckStatus() - { - bool CheckStatusRet; - - if (!IsMinecaftProfileLoaded()) + try { - SetStatus(LangRes.StatusText_MinecraftProfileWarning, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.general_warning_sign, SvgImageSize.Small)); - CheckStatusRet = false; + modpackInfo = ModpackInfo.TryLoad(folderPath); + RadTextBoxControl_ModpackConfig.Text = modpackInfo.ExtrasKey; + radTextBoxControl_InstallKey.Text = modpackInfo.ExtrasKey; } - else if (!IsUpdateConfigLoaded()) + catch { - SetStatus(LangRes.StatusText_MinecraftProfileWarning, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.general_warning_sign, SvgImageSize.Small)); - CheckStatusRet = false; } - else - CheckStatusRet = true; - return CheckStatusRet; + CheckStatusAndUpdate(); + } + + private void LoadUpdateConfigFile(string filePath) + { + RadTextBoxControl_ModpackConfig.Text = filePath; + + if (string.IsNullOrWhiteSpace(filePath)) + return; + + try + { + updateConfig = ModpackConfig.LoadFromUrl(filePath); + } + catch (Exception) + { + } + + CheckStatusAndUpdate(); + } + + private void LoadInstallKey(string installKey) + { + radTextBoxControl_InstallKey.Text = modpackInfo.ExtrasKey = installKey; + CheckStatusAndUpdate(); } private void SetStatus(string statusText, RadSvgImage image) @@ -90,58 +97,47 @@ public partial class Form1 RadLabel_Status.SvgImage = null; } - private void LoadMinecraftProfile(string folderPath) + private void CheckStatusAndUpdate() { - RadTextBoxControl_MinecraftProfileFolder.Text = folderPath; - - if (IsMinecaftProfileLoaded()) - { - try - { - modpackInfo = ModpackInfo.TryLoad(folderPath); - radTextBoxControl_InstallKey.Text = modpackInfo.ExtrasKey; - } - catch - { - RadTextBoxControl_MinecraftProfileFolder.Text = string.Empty; - } - - if (string.IsNullOrWhiteSpace(RadTextBoxControl_ModpackConfig.Text) && !string.IsNullOrWhiteSpace(modpackInfo?.ConfigUrl)) - LoadUpdateConfigFile(modpackInfo.ConfigUrl); - else if (IsUpdateConfigLoaded()) - RadButton_CheckForUpdates.PerformClick(); - } - else - ClearStatus(); + if (CheckStatus()) + RadButton_CheckForUpdates.PerformClick(); } - private void LoadUpdateConfigFile(string filePath) + private bool CheckStatus() { - static ModpackConfig loadConfig(string filePath) + if (modpackInfo == null || string.IsNullOrWhiteSpace(RadTextBoxControl_MinecraftProfileFolder.Text) /*|| modpackInfo.Valid*/) { - try - { - return ModpackConfig.LoadFromUrl(filePath); - } - catch (Exception) - { - } - return null; + SetStatus(LangRes.StatusText_MinecraftProfileWarning, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.general_warning_sign, SvgImageSize.Small)); + RadButton_PasteModpackConfig.Enabled = false; + radButton_PasteInstallKey.Enabled = false; + RadButton_CheckForUpdates.Enabled = false; + RadButton_Install.Enabled = false; + return false; + } + else if (updateConfig == null || string.IsNullOrWhiteSpace(RadTextBoxControl_ModpackConfig.Text)) + { + SetStatus(LangRes.StatusText_ConfigIncompleteOrNotLoaded, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.general_warning_sign, SvgImageSize.Small)); + RadButton_PasteModpackConfig.Enabled = true; + radButton_PasteInstallKey.Enabled = false; + RadButton_CheckForUpdates.Enabled = false; + RadButton_Install.Enabled = false; + return false; + } + else if (updateConfig.Maintenance && !updateOptions.IgnoreMaintenance) + { + SetStatus(LangRes.StatusText_Maintenance, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.services, SvgImageSize.Small)); + RadButton_PasteModpackConfig.Enabled = true; + radButton_PasteInstallKey.Enabled = true; + RadButton_CheckForUpdates.Enabled = false; + RadButton_Install.Enabled = false; + return false; } - RadTextBoxControl_ModpackConfig.Text = filePath; - - if (!string.IsNullOrWhiteSpace(filePath) && loadConfig(filePath) is ModpackConfig modpackConfig) - { - if (updateConfig.Maintenance && !updateOptions.IgnoreMaintenance) - SetStatus(LangRes.StatusText_Maintenance, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.services, SvgImageSize.Small)); - else - RadButton_CheckForUpdates.PerformClick(); - } - else - { - ClearStatus(); - } + RadButton_PasteModpackConfig.Enabled = true; + radButton_PasteInstallKey.Enabled = true; + RadButton_CheckForUpdates.Enabled = true; + RadButton_Install.Enabled = false; + return true; } private async Task ExecuteUpdate(bool doInstall) @@ -150,63 +146,93 @@ public partial class Form1 updater.InstallProgessUpdated += Update_InstallProgessUpdated; updater.CheckingProgressUpdated += Updated_CheckingProgresssUpdated; + void error() + { + SetStatus(LangRes.StatusText_ErrorWhileUpdateCheckOrUpdate, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.close, SvgImageSize.Small)); + currentUpdating = false; + } + void installing() + { + SetStatus(LangRes.StatusText_Installing, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.software_installer, SvgImageSize.Small)); + currentUpdating = true; + } + void updatesAvailable() + { + SetStatus(LangRes.StatusText_UpdateAvailable, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.software_installer, SvgImageSize.Small)); + } + void everythingOk() + { + SetStatus(LangRes.StatusTest_EverythingOk, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.done, SvgImageSize.Small)); + currentUpdating = false; + } + // Check only if not pressed "install", not really needed otherwise. if (lastUpdateCheckResult is null || !doInstall) { SetStatus(LangRes.StatusText_CheckingForUpdates, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.update_done, SvgImageSize.Small)); // Check for extras once again - modpackInfo.ExtrasKey = radTextBoxControl_InstallKey.Text.Trim(); updateOptions.IncludeExtras = AppFeatures.AllowExtras.IsEnabled(new AllowExtrasFeatureContext(modpackInfo, updateConfig)); try { lastUpdateCheckResult = await updater.Check(updateOptions); } - catch + catch(Exception) { - SetStatus(LangRes.StatusText_ErrorWhileUpdateCheckOrUpdate, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.close, SvgImageSize.Small)); + error(); + if (Debugger.IsAttached) + throw; } finally { } } + // Error while update check if (lastUpdateCheckResult is null || lastUpdateCheckResult.HasError) - SetStatus(LangRes.StatusText_ErrorWhileUpdateCheckOrUpdate, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.close, SvgImageSize.Small)); - else if (lastUpdateCheckResult.HasUpdates) { - if (doInstall) - { - SetStatus(LangRes.StatusText_Installing, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.software_installer, SvgImageSize.Small)); - currentUpdating = true; - - try - { - if (await updater.Install(lastUpdateCheckResult) == true) - { - lastUpdateCheckResult = null; // Reset last update check, a new one would be needed now. - SetStatus(LangRes.StatusTest_EverythingOk, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.done, SvgImageSize.Small)); - } - else - SetStatus(LangRes.StatusText_ErrorWhileUpdateCheckOrUpdate, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.close, SvgImageSize.Small)); - } - catch (Exception) - { - SetStatus(LangRes.StatusText_ErrorWhileUpdateCheckOrUpdate, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.close, SvgImageSize.Small)); - if (Debugger.IsAttached) - throw; - } - finally - { - currentUpdating = false; - } - } - else - SetStatus(LangRes.StatusText_UpdateAvailable, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.software_installer, SvgImageSize.Small)); + error(); + return; + } + + // No updates available + if (!lastUpdateCheckResult.HasUpdates) + { + everythingOk(); + return; + } + + // Updates available (but don't install) + if (!doInstall) + { + updatesAvailable(); + return; + } + + // Install updates + installing(); + currentUpdating = true; + try + { + // Install + if (await updater.Install(lastUpdateCheckResult) == false) + { + error(); + return; + } + + // Success + lastUpdateCheckResult = null; // Reset last update check, a new one would be needed now. + everythingOk(); + } + catch (Exception) + { + // Error + error(); + if (Debugger.IsAttached) + throw; } - else - SetStatus(LangRes.StatusTest_EverythingOk, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.done, SvgImageSize.Small)); } private void Update_InstallProgessUpdated(UpdateCheckResult result, int processedSyncs) @@ -238,7 +264,7 @@ public partial class Form1 { if (modpackInfo == null) return; - radTextBoxControl_InstallKey.Text = modpackInfo.ExtrasKey = Clipboard.GetText(); + LoadInstallKey(Clipboard.GetText()); } private void RadButton_RefreshConfig_Click(object sender, EventArgs e) @@ -249,8 +275,7 @@ public partial class Form1 private async void ButtonX_CheckForUpdates_Click(object sender, EventArgs e) { ClearStatus(); - if (CheckStatus()) - await ExecuteUpdate(false); + await ExecuteUpdate(false); } private async void ButtonX_StartUpdate_Click(object sender, EventArgs e) @@ -258,8 +283,7 @@ public partial class Form1 if (!currentUpdating) { ClearStatus(); - if (CheckStatus()) - await ExecuteUpdate(true); + await ExecuteUpdate(true); } } diff --git a/ModpackUpdater/Program.cs b/ModpackUpdater/Program.cs index f4694de..0ab084d 100644 --- a/ModpackUpdater/Program.cs +++ b/ModpackUpdater/Program.cs @@ -37,7 +37,7 @@ public static class Program if (ThemeResolutionService.LoadPackageResource("ModpackUpdater.CustomThemes.Office2019DarkBluePurple.tssp")) ThemeResolutionService.ApplicationThemeName = "Office2019DarkBluePurple"; - Application.Run(new Form1(updateOptions)); + Application.Run(new Form1(updateOptions)); } private static string GetSettingsPath(int? settingsVersion = 2)