appupdater
This commit is contained in:
59
ModpackUpdater.Apps/AppUpdates.cs
Normal file
59
ModpackUpdater.Apps/AppUpdates.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using Avalonia.Controls;
|
||||
using MsBox.Avalonia;
|
||||
using MsBox.Avalonia.Enums;
|
||||
using Pilz;
|
||||
using Pilz.Extensions;
|
||||
using Pilz.Runtime;
|
||||
using Pilz.Updating.Client;
|
||||
|
||||
namespace ModpackUpdater.Apps;
|
||||
|
||||
public class AppUpdates(string updateUrl, Window mainWindow)
|
||||
{
|
||||
public event EventHandler? OnDownloadProgramUpdate;
|
||||
|
||||
public async Task UpdateApp()
|
||||
{
|
||||
#if !DISABLE_UPDATE
|
||||
try
|
||||
{
|
||||
await UpdateAppCore();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await MessageBoxManager.GetMessageBoxStandard(MsgBoxLangRes.UpdateAvailable_Title, string.Format(MsgBoxLangRes.UpdateAvailable, ex.Message), ButtonEnum.YesNo, MsBox.Avalonia.Enums.Icon.Info).ShowWindowAsync();
|
||||
mainWindow.IsEnabled = true;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
private async Task UpdateAppCore()
|
||||
{
|
||||
if (Debugger.IsAttached)
|
||||
return;
|
||||
|
||||
var myAppPath = EnvironmentEx.ProcessPath!;
|
||||
var updater = new UpdateClient(updateUrl, Assembly.GetEntryAssembly()!.GetAppVersion(), AppChannel.Stable)
|
||||
{
|
||||
Distro = RuntimeInformationsEx.GetRuntimeIdentifier(),
|
||||
};
|
||||
|
||||
if (await updater.CheckForUpdate() is {} packageToInstall
|
||||
&& await MessageBoxManager.GetMessageBoxStandard(MsgBoxLangRes.UpdateAvailable_Title, MsgBoxLangRes.UpdateAvailable, ButtonEnum.YesNo, MsBox.Avalonia.Enums.Icon.Info).ShowWindowDialogAsync(mainWindow) == ButtonResult.Yes)
|
||||
{
|
||||
OnDownloadProgramUpdate?.Invoke(this, EventArgs.Empty);
|
||||
mainWindow.IsEnabled = false;
|
||||
if (await updater.DownloadPackageAsync(packageToInstall)
|
||||
&& await updater.InstallPackageAsync(packageToInstall, myAppPath))
|
||||
{
|
||||
mainWindow.IsVisible = false;
|
||||
await Process.Start(myAppPath).WaitForExitAsync();
|
||||
Environment.Exit(0);
|
||||
return;
|
||||
}
|
||||
mainWindow.IsEnabled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user