Files
minecraft-modpack-updater/ModpackUpdater/Form1.cs
2024-06-18 10:56:32 +02:00

248 lines
7.8 KiB
C#

using ModpackUpdater.Manager;
using ModpackUpdater.Model;
using ModpackUpdater.My.Resources;
using Telerik.WinControls;
using Telerik.WinControls.UI;
namespace ModpackUpdater;
public partial class Form1
{
private ModpackConfig updateConfig = new();
private bool currentUpdating = false;
private UpdateCheckResult lastUpdateCheckResult = null;
public Form1()
{
InitializeComponent();
Text = $"{Text} (v{Application.ProductVersion})";
}
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 = default;
if (!IsMinecaftProfileLoaded() || !MinecraftProfileChecker.CheckProfile(GetMinecraftProfilePath()))
{
SetStatus(LangRes.StatusText_MinecraftProfileWarning, MySymbols.icons8_general_warning_sign_16px);
CheckStatusRet = false;
}
else if (!IsUpdateConfigLoaded())
{
SetStatus(LangRes.StatusText_MinecraftProfileWarning, MySymbols.icons8_general_warning_sign_16px);
CheckStatusRet = false;
}
else
{
CheckStatusRet = true;
}
return CheckStatusRet;
}
private void SetStatus(string statusText, Image image)
{
RadLabel_Status.Text = statusText;
RadLabel_Status.Image = image;
}
private void ClearStatus()
{
RadLabel_Status.Text = "-";
RadLabel_Status.Image = null;
}
private void LoadMinecraftProfile(string folderPath)
{
RadTextBoxControl_MinecraftProfileFolder.Text = folderPath;
if (IsUpdateConfigLoaded())
RadButton_CheckForUpdates.PerformClick();
else
{
ClearStatus();
}
}
private void LoadUpdateConfigFile(string filePath)
{
RadTextBoxControl_ModpackConfig.Text = filePath;
try
{
if (IsUpdateConfigLoaded())
updateConfig = (ModpackConfig)ModpackConfig.LoadFromUrl(filePath);
}
catch (Exception ex)
{
RadTextBoxControl_ModpackConfig.Text = string.Empty;
}
if (IsMinecaftProfileLoaded())
RadButton_CheckForUpdates.PerformClick();
else
{
ClearStatus();
}
}
private async Task ExecuteUpdate(bool doInstall)
{
var updater = new ModpackInstaller(updateConfig, GetMinecraftProfilePath());
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, MySymbols.icons8_update_16px);
try
{
lastUpdateCheckResult = await updater.Check(!AppConfig.Instance.AllowRemoveLocalFiles);
}
catch (Exception ex)
{
SetStatus(LangRes.StatusText_ErrorWhileUpdateCheckOrUpdate, MySymbols.icons8_delete_16px);
}
finally
{
}
}
if (lastUpdateCheckResult is null || lastUpdateCheckResult.HasError)
SetStatus(LangRes.StatusText_ErrorWhileUpdateCheckOrUpdate, MySymbols.icons8_delete_16px);
else if (lastUpdateCheckResult.HasUpdates)
{
if (doInstall)
{
SetStatus(LangRes.StatusText_Installing, MySymbols.icons8_software_installer_16px);
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, MySymbols.icons8_checkmark_16px);
}
else
{
SetStatus(LangRes.StatusText_ErrorWhileUpdateCheckOrUpdate, MySymbols.icons8_delete_16px);
}
}
catch
{
SetStatus(LangRes.StatusText_ErrorWhileUpdateCheckOrUpdate, MySymbols.icons8_delete_16px);
}
finally
{
currentUpdating = false;
}
}
else
{
SetStatus(LangRes.StatusText_UpdateAvailable, MySymbols.icons8_software_installer_16px);
}
}
else
{
SetStatus(LangRes.StatusTest_EverythingOk, MySymbols.icons8_checkmark_16px);
}
}
private void Update_InstallProgessUpdated(UpdateCheckResult result, int processedSyncs)
{
int actionCount = result.Actions.Count;
SetStatus(Math.Round(processedSyncs / (double)actionCount * 100d, 1) + "%", MySymbols.icons8_software_installer_16px);
}
private void Updated_CheckingProgresssUpdated(int toCheck, int processed)
{
SetStatus(Math.Round(processed / (double)toCheck * 100d, 1) + "%", MySymbols.icons8_update_16px);
}
private void ButtonX_SearchMinecraftProfile_Click(object sender, EventArgs e)
{
var ofd = new RadOpenFolderDialog();
if (ofd.ShowDialog(this) == DialogResult.OK)
LoadMinecraftProfile(ofd.FileName);
}
private void ButtonX_SearchUpdateConfig_Click(object sender, EventArgs e)
{
var ofd = new RadOpenFileDialog() { Filter = FiledialogFilters.JSON_Display + "|" + FiledialogFilters.JSON_Filter };
if (ofd.ShowDialog(this) == DialogResult.OK)
LoadUpdateConfigFile(ofd.FileName);
}
private void RadButton_PasteModpackConfig_Click(object sender, EventArgs e)
{
string text = Clipboard.GetText();
if (text.StartsWith("http"))
LoadUpdateConfigFile(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;
AppConfig.Instance.LastConfigFilePath = RadTextBoxControl_ModpackConfig.Text;
AppConfig.Instance.SaveConfig();
}
private void Form1_Load(object sender, EventArgs e)
{
if (Directory.Exists(AppConfig.Instance.LastMinecraftProfilePath))
LoadMinecraftProfile(AppConfig.Instance.LastMinecraftProfilePath);
LoadUpdateConfigFile(AppConfig.Instance.LastConfigFilePath);
}
private async void Form1_Shown(object sender, EventArgs e)
{
var updater = new AppUpdater();
if (await updater.Check() && RadMessageBox.Show(LangRes.MsgBox_UpdateAvailable, LangRes.MsgBox_UpdateAvailable_Title, MessageBoxButtons.YesNo, RadMessageIcon.Info) == DialogResult.Yes)
{
SetStatus(LangRes.StatusText_InstallingAppUpdate, MySymbols.icons8_software_installer_16px);
Enabled = false;
await updater.Install();
Application.Restart();
}
}
}