Refactor VeloUpdateManager

This commit is contained in:
smallketchup82 2024-07-04 17:28:49 -04:00
parent 36a3765ee4
commit fae8f5f81b
No known key found for this signature in database
GPG Key ID: 7345B7C561243F1E
1 changed files with 23 additions and 26 deletions

View File

@ -10,12 +10,13 @@
using osu.Game.Overlays.Notifications; using osu.Game.Overlays.Notifications;
using osu.Game.Screens.Play; using osu.Game.Screens.Play;
using osu.Game.Updater; using osu.Game.Updater;
using Velopack.Sources;
namespace osu.Desktop.Updater namespace osu.Desktop.Updater
{ {
public partial class VeloUpdateManager : UpdateManager public partial class VeloUpdateManager : UpdateManager
{ {
private Velopack.UpdateManager? updateManager; private readonly Velopack.UpdateManager updateManager;
private INotificationOverlay notificationOverlay = null!; private INotificationOverlay notificationOverlay = null!;
[Resolved] [Resolved]
@ -24,6 +25,12 @@ public partial class VeloUpdateManager : UpdateManager
[Resolved] [Resolved]
private ILocalUserPlayInfo? localUserInfo { get; set; } private ILocalUserPlayInfo? localUserInfo { get; set; }
public VeloUpdateManager()
{
const string? github_token = null; // TODO: populate.
updateManager = new Velopack.UpdateManager(new GithubSource(@"https://github.com/ppy/osu", github_token, false));
}
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(INotificationOverlay notifications) private void load(INotificationOverlay notifications)
{ {
@ -37,36 +44,30 @@ private async Task<bool> checkForUpdateAsync(UpdateProgressNotification? notific
// should we schedule a retry on completion of this check? // should we schedule a retry on completion of this check?
bool scheduleRecheck = true; bool scheduleRecheck = true;
const string? github_token = null; // TODO: populate.
try try
{ {
// Avoid any kind of update checking while gameplay is running. // Avoid any kind of update checking while gameplay is running.
if (localUserInfo?.IsPlaying.Value == true) if (localUserInfo?.IsPlaying.Value == true)
return false; return false;
updateManager ??= new Velopack.UpdateManager(new Velopack.Sources.GithubSource(@"https://github.com/ppy/osu", github_token, false));
var info = await updateManager.CheckForUpdatesAsync().ConfigureAwait(false); var info = await updateManager.CheckForUpdatesAsync().ConfigureAwait(false);
// Handle no updates available.
if (info == null) if (info == null)
{ {
// If there is an update pending restart, show the notification again. // If there's no updates pending restart, bail and retry later.
if (updateManager.IsUpdatePendingRestart) if (!updateManager.IsUpdatePendingRestart) return false;
{
notificationOverlay.Post(new UpdateApplicationCompleteNotification
{
Activated = () =>
{
restartToApplyUpdate();
return true;
}
});
return true;
}
// Otherwise there's no updates available. Bail and retry later. // If there is an update pending restart, show the notification to restart again.
return false; notificationOverlay.Post(new UpdateApplicationCompleteNotification
{
Activated = () =>
{
restartToApplyUpdate();
return true;
}
});
return true;
} }
scheduleRecheck = false; scheduleRecheck = false;
@ -87,8 +88,6 @@ private async Task<bool> checkForUpdateAsync(UpdateProgressNotification? notific
{ {
await updateManager.DownloadUpdatesAsync(info, p => notification.Progress = p / 100f).ConfigureAwait(false); await updateManager.DownloadUpdatesAsync(info, p => notification.Progress = p / 100f).ConfigureAwait(false);
notification.StartInstall();
notification.State = ProgressNotificationState.Completed; notification.State = ProgressNotificationState.Completed;
} }
catch (Exception e) catch (Exception e)
@ -98,10 +97,11 @@ private async Task<bool> checkForUpdateAsync(UpdateProgressNotification? notific
Logger.Error(e, @"update failed!"); Logger.Error(e, @"update failed!");
} }
} }
catch (Exception) catch (Exception e)
{ {
// we'll ignore this and retry later. can be triggered by no internet connection or thread abortion. // we'll ignore this and retry later. can be triggered by no internet connection or thread abortion.
scheduleRecheck = true; scheduleRecheck = true;
Logger.Error(e, @"update check failed!");
} }
finally finally
{ {
@ -117,9 +117,6 @@ private async Task<bool> checkForUpdateAsync(UpdateProgressNotification? notific
private bool restartToApplyUpdate() private bool restartToApplyUpdate()
{ {
if (updateManager == null)
return false;
updateManager.WaitExitThenApplyUpdates(null); updateManager.WaitExitThenApplyUpdates(null);
Schedule(() => game.AttemptExit()); Schedule(() => game.AttemptExit());
return true; return true;