using ModpackUpdater.Manager; using ModpackUpdater.Model; using ModpackUpdater.My.Resources; using Pilz.UI.Symbols; using Pilz.UI.Telerik; using System.Diagnostics; using System.Reflection; using Telerik.WinControls; using Telerik.WinControls.UI; namespace ModpackUpdater; public partial class Form1 { private ModpackInfo modpackInfo = new(); private ModpackConfig updateConfig = new(); private ModpackFeatures features; private bool currentUpdating = false; private UpdateCheckResult lastUpdateCheckResult = null; private readonly UpdateCheckOptionsAdv updateOptions; public Form1(UpdateCheckOptionsAdv updateOptions) : this() { this.updateOptions = updateOptions; if (!string.IsNullOrWhiteSpace(updateOptions.ProfileFolder)) LoadMinecraftProfile(updateOptions.ProfileFolder); else if (!string.IsNullOrWhiteSpace(AppConfig.Instance.LastMinecraftProfilePath)) LoadMinecraftProfile(AppConfig.Instance.LastMinecraftProfilePath); } public Form1() { InitializeComponent(); Text = $"{Text} (v{Assembly.GetExecutingAssembly().GetName().Version})"; RadButton_Install.SvgImage = AppSymbolFactory.Instance.GetSvgImage(AppSymbols.software_installer, SymbolSize.Small); RadButton_CheckForUpdates.SvgImage = AppSymbolFactory.Instance.GetSvgImage(AppSymbols.update_done, SymbolSize.Small); radButton_RefreshConfig.SvgImage = AppSymbolFactory.Instance.GetSvgImage(AppSymbols.refresh, SymbolSize.Small); RadButton_SearchMinecraftProfileFolder.SvgImage = AppSymbolFactory.Instance.GetSvgImage(AppSymbols.opened_folder, SymbolSize.Small); radButton_PasteInstallKey.SvgImage = AppSymbolFactory.Instance.GetSvgImage(AppSymbols.paste, SymbolSize.Small); RadButton_PasteModpackConfig.SvgImage = AppSymbolFactory.Instance.GetSvgImage(AppSymbols.paste, SymbolSize.Small); } private void LoadMinecraftProfile(string folderPath) { RadTextBoxControl_MinecraftProfileFolder.Text = folderPath; AppConfig.Instance.LastMinecraftProfilePath = folderPath; CheckStatusAndUpdate(loadedProfile: true); } private void LoadUpdateConfigFile(string filePath) { RadTextBoxControl_ModpackConfig.Text = filePath; CheckStatusAndUpdate(); } private void LoadInstallKey(string installKey) { radTextBoxControl_InstallKey.Text = modpackInfo.ExtrasKey = installKey; CheckStatusAndUpdate(); } private void SetStatus(string statusText, RadSvgImage image) { RadLabel_Status.Text = statusText; RadLabel_Status.SvgImage = image; } private void ClearStatus() { RadLabel_Status.Text = "-"; RadLabel_Status.SvgImage = null; } private void CheckStatusAndUpdate(bool loadedProfile = false) { if (CheckStatus(loadedProfile)) RadButton_CheckForUpdates.PerformClick(); } private bool CheckStatus(bool loadedProfile) { try { modpackInfo = ModpackInfo.TryLoad(RadTextBoxControl_MinecraftProfileFolder.Text.Trim()); if (loadedProfile) { RadTextBoxControl_ModpackConfig.Text = modpackInfo.ConfigUrl; radTextBoxControl_InstallKey.Text = modpackInfo.ExtrasKey; } } catch { } try { updateConfig = ModpackConfig.LoadFromUrl(RadTextBoxControl_ModpackConfig.Text); } catch (Exception) { } if (modpackInfo != null) features = new(updateConfig); if (modpackInfo == null || string.IsNullOrWhiteSpace(RadTextBoxControl_MinecraftProfileFolder.Text) /*|| modpackInfo.Valid*/) { SetStatus(LangRes.StatusText_MinecraftProfileWarning, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.general_warning_sign, SymbolSize.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, SymbolSize.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, SymbolSize.Small)); RadButton_PasteModpackConfig.Enabled = true; radButton_PasteInstallKey.Enabled = true; RadButton_CheckForUpdates.Enabled = false; RadButton_Install.Enabled = false; return false; } RadButton_PasteModpackConfig.Enabled = true; radButton_PasteInstallKey.Enabled = true; RadButton_CheckForUpdates.Enabled = true; RadButton_Install.Enabled = true; return true; } private async Task ExecuteUpdate(bool doInstall) { var updater = new ModpackInstaller(updateConfig, modpackInfo); updater.InstallProgessUpdated += Update_InstallProgessUpdated; updater.CheckingProgressUpdated += Updated_CheckingProgresssUpdated; void error() { SetStatus(LangRes.StatusText_ErrorWhileUpdateCheckOrUpdate, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.close, SymbolSize.Small)); currentUpdating = false; } void installing() { SetStatus(LangRes.StatusText_Installing, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.software_installer, SymbolSize.Small)); currentUpdating = true; } void updatesAvailable() { SetStatus(LangRes.StatusText_UpdateAvailable, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.software_installer, SymbolSize.Small)); } void everythingOk() { SetStatus(LangRes.StatusTest_EverythingOk, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.done, SymbolSize.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, SymbolSize.Small)); // Check for extras once again updateOptions.IncludeExtras = features.IsEnabled(ModpackFeatures.FeatureAllowExtas, new AllowExtrasFeatureContext(modpackInfo)); try { lastUpdateCheckResult = await updater.Check(updateOptions); } catch(Exception) { error(); if (Debugger.IsAttached) throw; } finally { } } // Error while update check if (lastUpdateCheckResult is null || lastUpdateCheckResult.HasError) { 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; } } private void Update_InstallProgessUpdated(UpdateCheckResult result, int processedSyncs) { int actionCount = result.Actions.Count; SetStatus(Math.Round(processedSyncs / (double)actionCount * 100d, 1) + "%", AppSymbolFactory.Instance.GetSvgImage(AppSymbols.software_installer, SymbolSize.Small)); } private void Updated_CheckingProgresssUpdated(int toCheck, int processed) { SetStatus(Math.Round(processed / (double)toCheck * 100d, 1) + "%", AppSymbolFactory.Instance.GetSvgImage(AppSymbols.update_done, SymbolSize.Small)); } private void ButtonX_SearchMinecraftProfile_Click(object sender, EventArgs e) { var ofd = new RadOpenFolderDialog(); if (ofd.ShowDialog(this) == DialogResult.OK) LoadMinecraftProfile(ofd.FileName); } private void RadButton_PasteModpackConfig_Click(object sender, EventArgs e) { LoadUpdateConfigFile(Clipboard.GetText()); } private void RadButton_PasteInstallKey_Click(object sender, EventArgs e) { LoadInstallKey(Clipboard.GetText()); } private void RadButton_RefreshConfig_Click(object sender, EventArgs e) { LoadUpdateConfigFile(RadTextBoxControl_ModpackConfig.Text); } private async void ButtonX_CheckForUpdates_Click(object sender, EventArgs e) { ClearStatus(); await ExecuteUpdate(false); } private async void ButtonX_StartUpdate_Click(object sender, EventArgs e) { if (!currentUpdating) { ClearStatus(); await ExecuteUpdate(true); } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { AppConfig.Instance.LastMinecraftProfilePath = RadTextBoxControl_MinecraftProfileFolder.Text; } private void Form1_Load(object sender, EventArgs e) { if (Directory.Exists(AppConfig.Instance.LastMinecraftProfilePath)) LoadMinecraftProfile(AppConfig.Instance.LastMinecraftProfilePath); } private async void Form1_Shown(object sender, EventArgs e) { var updater = new AppUpdater(); if (!updateOptions.NoUpdate && await updater.Check() && RadMessageBox.Show(LangRes.MsgBox_UpdateAvailable, LangRes.MsgBox_UpdateAvailable_Title, MessageBoxButtons.YesNo, RadMessageIcon.Info) == DialogResult.Yes) { SetStatus(LangRes.StatusText_InstallingAppUpdate, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.software_installer, SymbolSize.Small)); Enabled = false; await updater.Install(); Application.Restart(); } } }