Files
minecraft-modpack-updater/ModpackUpdater/Form1.cs
Schedel Pascal 20c1e5dc8e install keys
2024-06-21 08:55:46 +02:00

288 lines
10 KiB
C#

using ModpackUpdater.Manager;
using ModpackUpdater.Model;
using ModpackUpdater.My.Resources;
using Pilz.UI.Telerik;
using System.Diagnostics;
using Telerik.WinControls;
using Telerik.WinControls.UI;
namespace ModpackUpdater;
public partial class Form1
{
private ModpackInfo modpackInfo = null;
private ModpackConfig updateConfig = new();
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);
}
public Form1()
{
InitializeComponent();
Text = $"{Text} (v{Application.ProductVersion})";
RadButton_Install.SvgImage = AppSymbolFactory.Instance.GetSvgImage(AppSymbols.software_installer, SvgImageSize.Small);
RadButton_CheckForUpdates.SvgImage = AppSymbolFactory.Instance.GetSvgImage(AppSymbols.update_done, SvgImageSize.Small);
radButton_RefreshConfig.SvgImage = AppSymbolFactory.Instance.GetSvgImage(AppSymbols.refresh, SvgImageSize.Small);
RadButton_SearchMinecraftProfileFolder.SvgImage = AppSymbolFactory.Instance.GetSvgImage(AppSymbols.opened_folder, SvgImageSize.Small);
radButton_PasteInstallKey.SvgImage = AppSymbolFactory.Instance.GetSvgImage(AppSymbols.paste, SvgImageSize.Small);
RadButton_PasteModpackConfig.SvgImage = AppSymbolFactory.Instance.GetSvgImage(AppSymbols.paste, SvgImageSize.Small);
}
private bool IsMinecaftProfileLoaded()
{
return !string.IsNullOrEmpty(GetMinecraftProfilePath());
}
private string GetMinecraftProfilePath()
{
return RadTextBoxControl_MinecraftProfileFolder.Text.Trim();
}
private bool IsUpdateConfigLoaded()
{
return !string.IsNullOrEmpty(GetUpdateconfigPath());
}
private string GetUpdateconfigPath()
{
return RadTextBoxControl_ModpackConfig.Text.Trim();
}
private bool CheckStatus()
{
bool CheckStatusRet;
if (!IsMinecaftProfileLoaded())
{
SetStatus(LangRes.StatusText_MinecraftProfileWarning, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.general_warning_sign, SvgImageSize.Small));
CheckStatusRet = false;
}
else if (!IsUpdateConfigLoaded())
{
SetStatus(LangRes.StatusText_MinecraftProfileWarning, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.general_warning_sign, SvgImageSize.Small));
CheckStatusRet = false;
}
else
CheckStatusRet = true;
return CheckStatusRet;
}
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 LoadMinecraftProfile(string folderPath)
{
RadTextBoxControl_MinecraftProfileFolder.Text = folderPath;
if (IsUpdateConfigLoaded())
{
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();
}
private void LoadUpdateConfigFile(string filePath)
{
static ModpackConfig loadConfig(string filePath)
{
try
{
return ModpackConfig.LoadFromUrl(filePath);
}
catch (Exception)
{
}
return null;
}
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();
}
}
private async Task ExecuteUpdate(bool doInstall)
{
var updater = new ModpackInstaller(updateConfig, modpackInfo);
updater.InstallProgessUpdated += Update_InstallProgessUpdated;
updater.CheckingProgressUpdated += Updated_CheckingProgresssUpdated;
// 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
{
SetStatus(LangRes.StatusText_ErrorWhileUpdateCheckOrUpdate, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.close, SvgImageSize.Small));
}
finally
{
}
}
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));
}
else
SetStatus(LangRes.StatusTest_EverythingOk, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.done, SvgImageSize.Small));
}
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, SvgImageSize.Small));
}
private void Updated_CheckingProgresssUpdated(int toCheck, int processed)
{
SetStatus(Math.Round(processed / (double)toCheck * 100d, 1) + "%", AppSymbolFactory.Instance.GetSvgImage(AppSymbols.update_done, SvgImageSize.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)
{
var text = Clipboard.GetText();
if (text.StartsWith("http"))
LoadUpdateConfigFile(text);
}
private void RadButton_PasteInstallKey_Click(object sender, EventArgs e)
{
if (modpackInfo == null)
return;
radTextBoxControl_InstallKey.Text = modpackInfo.ExtrasKey = 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();
if (CheckStatus())
await ExecuteUpdate(false);
}
private async void ButtonX_StartUpdate_Click(object sender, EventArgs e)
{
if (!currentUpdating)
{
ClearStatus();
if (CheckStatus())
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, SvgImageSize.Small));
Enabled = false;
await updater.Install();
Application.Restart();
}
}
}