379 lines
12 KiB
C#
379 lines
12 KiB
C#
using System.Diagnostics;
|
|
using System.Reflection;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Interactivity;
|
|
using Avalonia.Media;
|
|
using Avalonia.Platform.Storage;
|
|
using Avalonia.Threading;
|
|
using ModpackUpdater.Apps.Client.Gui.LangRes;
|
|
using ModpackUpdater.Manager;
|
|
using Pilz.Extensions;
|
|
using Pilz.Extensions.Collections;
|
|
using Pilz.UI.Symbols;
|
|
|
|
namespace ModpackUpdater.Apps.Client.Gui;
|
|
|
|
public partial class MainForm : Window
|
|
{
|
|
private readonly MenuFlyout menuFlyoutSearchProfileFolder = new();
|
|
private readonly UpdateCheckOptions updateOptions = new();
|
|
private ModpackInfo modpackInfo = new();
|
|
private ModpackConfig updateConfig = new();
|
|
private ModpackFeatures? features;
|
|
private UpdateCheckResult? lastUpdateCheckResult;
|
|
private bool currentUpdating;
|
|
private bool loadingData;
|
|
|
|
public MainForm()
|
|
{
|
|
InitializeComponent();
|
|
|
|
Title = $"{Title} (v{Assembly.GetExecutingAssembly().GetAppVersion().ToShortString()})";
|
|
ButtonSearchProfileFolder.Flyout = menuFlyoutSearchProfileFolder;
|
|
|
|
ButtonSearchProfileFolder.ImageSource = AppGlobals.Symbols.GetImageSource(AppSymbols.opened_folder);
|
|
ButtonCheckForUpdates.ImageSource = AppGlobals.Symbols.GetImageSource(AppSymbols.update_done);
|
|
ButtonInstall.ImageSource = AppGlobals.Symbols.GetImageSource(AppSymbols.software_installer);
|
|
MenuItemRepair.Icon = AppGlobals.Symbols.GetImage(AppSymbols.wrench, SymbolSize.Small);
|
|
|
|
ClearStatus();
|
|
LoadProfileToUi();
|
|
}
|
|
|
|
#region Features
|
|
|
|
private void SetStatus(string statusText, IImage? image)
|
|
{
|
|
TextStatus.Text = statusText;
|
|
ImageStatus.Source = image;
|
|
}
|
|
|
|
private void ClearStatus()
|
|
{
|
|
TextStatus.Text = "-";
|
|
ImageStatus.Source = null;
|
|
}
|
|
private void LoadRecentFilesToUi()
|
|
{
|
|
menuFlyoutSearchProfileFolder.Items.Clear();
|
|
|
|
if (AppConfig.Instance.RecentMinecraftProfilePaths.Count == 0)
|
|
{
|
|
menuFlyoutSearchProfileFolder.Items.Add(new TextBlock
|
|
{
|
|
Text = GeneralLangRes.NoRecentProfilesAvailable,
|
|
FontStyle = FontStyle.Italic,
|
|
});
|
|
return;
|
|
}
|
|
|
|
AppConfig.Instance.RecentMinecraftProfilePaths.ForEach(path =>
|
|
{
|
|
if (File.Exists(path))
|
|
return;
|
|
|
|
var item = new MenuItem
|
|
{
|
|
Header = path.Length > 50 ? $"...{path[^50..]}" : path,
|
|
DataContext = path,
|
|
};
|
|
item.Click += MenuItemRecentMinecraftProfilePathItem_Click;
|
|
menuFlyoutSearchProfileFolder.Items.Add(item);
|
|
});
|
|
}
|
|
|
|
private void StoreRecentMinecraftProfilePath(string? path)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(path))
|
|
return;
|
|
|
|
AppConfig.Instance.RecentMinecraftProfilePaths.Remove(path);
|
|
AppConfig.Instance.RecentMinecraftProfilePaths.Insert(0, path);
|
|
AppConfig.Instance.RecentMinecraftProfilePaths.Skip(10).ForEach(n => AppConfig.Instance.RecentMinecraftProfilePaths.Remove(n));
|
|
}
|
|
|
|
private void LoadProfileToUi()
|
|
{
|
|
loadingData = true;
|
|
|
|
TextBoxMinecraftProfileFolder.Text = modpackInfo.LocalPath ?? AppConfig.Instance.RecentMinecraftProfilePaths.FirstOrDefault() ?? TextBoxMinecraftProfileFolder.Text;
|
|
TextBoxModpackConfig.Text = modpackInfo.ConfigUrl ?? TextBoxModpackConfig.Text;
|
|
TextBoxInstallKey.Text = modpackInfo.ExtrasKey ?? TextBoxInstallKey.Text;
|
|
|
|
Dispatcher.UIThread.Post(() => loadingData = false, DispatcherPriority.Background);
|
|
}
|
|
|
|
private void LoadOptionsToUi()
|
|
{
|
|
//foreach (var set in )
|
|
//{
|
|
// // ...
|
|
//}
|
|
}
|
|
|
|
private async void CheckStatusAndUpdate(bool loadProfileToUi)
|
|
{
|
|
if (!CheckStatus(loadProfileToUi))
|
|
return;
|
|
|
|
await ExecuteUpdate(false, false);
|
|
|
|
StoreRecentMinecraftProfilePath(modpackInfo.LocalPath);
|
|
LoadRecentFilesToUi();
|
|
}
|
|
|
|
private bool CheckStatus(bool loadProfileToUi)
|
|
{
|
|
try
|
|
{
|
|
modpackInfo = ModpackInfo.TryLoad(TextBoxMinecraftProfileFolder.Text?.Trim());
|
|
}
|
|
catch
|
|
{
|
|
// Ignore
|
|
}
|
|
|
|
if (loadProfileToUi)
|
|
LoadProfileToUi();
|
|
|
|
try
|
|
{
|
|
updateConfig = ModpackConfig.LoadFromUrl(TextBoxModpackConfig.Text);
|
|
}
|
|
catch
|
|
{
|
|
// Ignore
|
|
}
|
|
|
|
features = new(updateConfig);
|
|
modpackInfo.ExtrasKey = TextBoxInstallKey.Text?.Trim();
|
|
if (!features.IsInvalid() && !string.IsNullOrWhiteSpace(TextBoxInstallKey.Text) && !AllowExtras())
|
|
{
|
|
SetStatus(GeneralLangRes.InstallationKeyNotValid, AppGlobals.Symbols.GetImageSource(AppSymbols.general_warning_sign));
|
|
return false;
|
|
}
|
|
|
|
LabelInstallKey.IsVisible = TextBoxInstallKey.IsVisible = !string.IsNullOrWhiteSpace(updateConfig.UnleashApiUrl);
|
|
|
|
if (string.IsNullOrWhiteSpace(TextBoxMinecraftProfileFolder.Text) /*|| modpackInfo.Valid*/)
|
|
{
|
|
SetStatus(GeneralLangRes.MinecraftProfileFolderSeemsInvalid, AppGlobals.Symbols.GetImageSource(AppSymbols.general_warning_sign));
|
|
ButtonCheckForUpdates.IsEnabled = false;
|
|
ButtonInstall.IsEnabled = false;
|
|
return false;
|
|
}
|
|
else if (string.IsNullOrWhiteSpace(TextBoxModpackConfig.Text))
|
|
{
|
|
SetStatus(GeneralLangRes.ConfigIncompleteOrNotLoaded, AppGlobals.Symbols.GetImageSource(AppSymbols.general_warning_sign));
|
|
ButtonCheckForUpdates.IsEnabled = false;
|
|
ButtonInstall.IsEnabled = false;
|
|
return false;
|
|
}
|
|
else if (updateConfig.Maintenance && !updateOptions.IgnoreMaintenance)
|
|
{
|
|
SetStatus(GeneralLangRes.UpdateServerInMaintenance, AppGlobals.Symbols.GetImageSource(AppSymbols.services));
|
|
ButtonCheckForUpdates.IsEnabled = false;
|
|
ButtonInstall.IsEnabled = false;
|
|
return false;
|
|
}
|
|
LoadOptionsToUi();
|
|
ButtonCheckForUpdates.IsEnabled = true;
|
|
ButtonInstall.IsEnabled = true;
|
|
return true;
|
|
}
|
|
|
|
private async Task ExecuteUpdate(bool doInstall, bool repair)
|
|
{
|
|
// Ensure set extras key
|
|
modpackInfo.ExtrasKey = TextBoxInstallKey.Text?.Trim();
|
|
|
|
var updater = new ModpackInstaller(updateConfig, modpackInfo);
|
|
updater.InstallProgessUpdated += Update_InstallProgessUpdated;
|
|
updater.CheckingProgressUpdated += Updated_CheckingProgresssUpdated;
|
|
|
|
void error()
|
|
{
|
|
SetStatus(GeneralLangRes.ErrorOnUpdateCheckOrUpdating, AppGlobals.Symbols.GetImageSource(AppSymbols.close));
|
|
currentUpdating = false;
|
|
}
|
|
void installing()
|
|
{
|
|
SetStatus(GeneralLangRes.Installing, AppGlobals.Symbols.GetImageSource(AppSymbols.software_installer));
|
|
currentUpdating = true;
|
|
}
|
|
void updatesAvailable()
|
|
{
|
|
SetStatus(GeneralLangRes.AnUpdateIsAvailable, AppGlobals.Symbols.GetImageSource(AppSymbols.software_installer));
|
|
}
|
|
void everythingOk()
|
|
{
|
|
SetStatus(GeneralLangRes.EverythingIsRightAndUpToDate, AppGlobals.Symbols.GetImageSource(AppSymbols.done));
|
|
currentUpdating = false;
|
|
}
|
|
|
|
// Check only if not pressed "install", not really needed otherwise.
|
|
if (lastUpdateCheckResult is null || !doInstall || repair)
|
|
{
|
|
SetStatus(GeneralLangRes.CheckingForUpdates, AppGlobals.Symbols.GetImageSource(AppSymbols.update_done));
|
|
|
|
// Check for extras once again
|
|
updateOptions.IncludeExtras = AllowExtras();
|
|
|
|
// Force re-install on repair
|
|
updateOptions.IgnoreInstalledVersion = repair;
|
|
|
|
try
|
|
{
|
|
lastUpdateCheckResult = await updater.Check(updateOptions);
|
|
}
|
|
catch
|
|
{
|
|
error();
|
|
if (Debugger.IsAttached)
|
|
throw;
|
|
}
|
|
}
|
|
|
|
// Error while update check
|
|
if (lastUpdateCheckResult is null || lastUpdateCheckResult.HasError)
|
|
{
|
|
error();
|
|
return;
|
|
}
|
|
|
|
// Load options
|
|
// lastUpdateCheckResult.OptionsAvailable...
|
|
// lastUpdateCheckResult.OptionsEnabled...
|
|
|
|
// 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 bool AllowExtras()
|
|
{
|
|
return features != null && features.IsEnabled(ModpackFeatures.FeatureAllowExtas, new AllowExtrasFeatureContext(modpackInfo));
|
|
}
|
|
|
|
private void Updated_CheckingProgresssUpdated(int toCheck, int processed)
|
|
{
|
|
SetStatus(Math.Round(processed / (double)toCheck * 100d, 1) + "%", AppGlobals.Symbols.GetImageSource(AppSymbols.update_done));
|
|
}
|
|
|
|
private void Update_InstallProgessUpdated(UpdateCheckResult result, int processedSyncs)
|
|
{
|
|
var actionCount = result.Actions.Count;
|
|
SetStatus(Math.Round(processedSyncs / (double)actionCount * 100d, 1) + "%", AppGlobals.Symbols.GetImageSource(AppSymbols.software_installer));
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Gui
|
|
|
|
private async void MainForm_Loaded(object? sender, RoutedEventArgs e)
|
|
{
|
|
var updates = new AppUpdates("client", this);
|
|
updates.OnDownloadProgramUpdate += (_, _) => SetStatus(GeneralLangRes.DownloadProgramUpdate, AppGlobals.Symbols.GetImageSource(AppSymbols.software_installer));
|
|
await updates.UpdateApp();
|
|
ClearStatus();
|
|
LoadRecentFilesToUi();
|
|
CheckStatusAndUpdate(true);
|
|
}
|
|
|
|
private void TextBoxMinecraftProfileFolder_TextChanged(object? o, TextChangedEventArgs args)
|
|
{
|
|
if (!loadingData)
|
|
CheckStatusAndUpdate(true);
|
|
}
|
|
|
|
private void TextBoxModpackConfig_TextChanged(object? o, RoutedEventArgs args)
|
|
{
|
|
if (!loadingData)
|
|
CheckStatusAndUpdate(false);
|
|
}
|
|
|
|
private void TextBoxInstallKey_TextChanged(object? o, RoutedEventArgs args)
|
|
{
|
|
if (!loadingData)
|
|
CheckStatusAndUpdate(false);
|
|
}
|
|
|
|
private async void ButtonSearchProfileFolder_Click(object? sender, RoutedEventArgs e)
|
|
{
|
|
var filePaths = await StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions
|
|
{
|
|
Title = GeneralLangRes.SelectMinecraftProfileFolder,
|
|
SuggestedStartLocation = await StorageProvider.TryGetFolderFromPathAsync(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)),
|
|
AllowMultiple = false,
|
|
});
|
|
|
|
if (filePaths.Count >= 1)
|
|
TextBoxMinecraftProfileFolder.Text = filePaths[0].Path.AbsolutePath;
|
|
}
|
|
|
|
private async void ButtonCheckForUpdates_Click(object? sender, RoutedEventArgs e)
|
|
{
|
|
ClearStatus();
|
|
await ExecuteUpdate(false, false);
|
|
}
|
|
|
|
private async void ButtonInstall_Click(object? sender, RoutedEventArgs e)
|
|
{
|
|
if (currentUpdating)
|
|
return;
|
|
|
|
ClearStatus();
|
|
await ExecuteUpdate(true, false);
|
|
}
|
|
|
|
private async void MenuItemRepair_Click(object? sender, RoutedEventArgs e)
|
|
{
|
|
if (currentUpdating)
|
|
return;
|
|
|
|
ClearStatus();
|
|
await ExecuteUpdate(true, true);
|
|
}
|
|
|
|
private void MenuItemRecentMinecraftProfilePathItem_Click(object? sender, RoutedEventArgs e)
|
|
{
|
|
if (sender is MenuItem item && item.DataContext is string path)
|
|
TextBoxMinecraftProfileFolder.Text = path;
|
|
}
|
|
|
|
#endregion
|
|
} |