1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-17 02:53:01 +08:00
osu-lazer/osu.Desktop/Updater/VelopackUpdateManager.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

141 lines
5.0 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-04-13 17:19:50 +08:00
2018-03-27 17:57:20 +08:00
using System;
2018-07-06 15:39:27 +08:00
using System.Threading.Tasks;
2018-03-27 17:57:20 +08:00
using osu.Framework.Allocation;
using osu.Framework.Logging;
using osu.Game;
using osu.Game.Overlays;
using osu.Game.Overlays.Notifications;
using osu.Game.Screens.Play;
using Velopack;
2024-07-05 05:28:49 +08:00
using Velopack.Sources;
2018-04-13 17:19:50 +08:00
2018-07-06 15:39:27 +08:00
namespace osu.Desktop.Updater
2018-03-27 17:57:20 +08:00
{
2024-08-31 22:03:10 +08:00
public partial class VelopackUpdateManager : Game.Updater.UpdateManager
2018-03-27 17:57:20 +08:00
{
private readonly UpdateManager updateManager;
private INotificationOverlay notificationOverlay = null!;
2018-04-13 17:19:50 +08:00
[Resolved]
private OsuGameBase game { get; set; } = null!;
[Resolved]
private ILocalUserPlayInfo? localUserInfo { get; set; }
private UpdateInfo? pendingUpdate;
2024-08-31 22:03:10 +08:00
public VelopackUpdateManager()
2024-07-05 05:28:49 +08:00
{
updateManager = new UpdateManager(new GithubSource(@"https://github.com/ppy/osu", null, false), new UpdateOptions
{
AllowVersionDowngrade = true,
});
2024-07-05 05:28:49 +08:00
}
2018-03-27 17:57:20 +08:00
[BackgroundDependencyLoader]
private void load(INotificationOverlay notifications)
2018-03-27 17:57:20 +08:00
{
notificationOverlay = notifications;
2018-03-27 17:57:20 +08:00
}
2018-04-13 17:19:50 +08:00
protected override async Task<bool> PerformUpdateCheck() => await checkForUpdateAsync().ConfigureAwait(false);
2020-05-07 14:07:22 +08:00
2024-06-27 03:25:41 +08:00
private async Task<bool> checkForUpdateAsync(UpdateProgressNotification? notification = null)
2018-03-27 17:57:20 +08:00
{
// whether to check again in 30 minutes. generally only if there's an error or no update was found (yet).
bool scheduleRecheck = false;
2018-04-13 17:19:50 +08:00
2018-03-27 17:57:20 +08:00
try
{
// Avoid any kind of update checking while gameplay is running.
if (localUserInfo?.IsPlaying.Value == true)
{
scheduleRecheck = true;
return false;
}
// TODO: we should probably be checking if there's a more recent update, rather than shortcutting here.
// Velopack does support this scenario (see https://github.com/ppy/osu/pull/28743#discussion_r1743495975).
if (pendingUpdate != null)
{
2024-07-05 05:28:49 +08:00
// If there is an update pending restart, show the notification to restart again.
notificationOverlay.Post(new UpdateApplicationCompleteNotification
{
2024-07-05 05:28:49 +08:00
Activated = () =>
{
Task.Run(restartToApplyUpdate);
2024-07-05 05:28:49 +08:00
return true;
}
});
2024-07-05 05:28:49 +08:00
return true;
}
2018-04-13 17:19:50 +08:00
pendingUpdate = await updateManager.CheckForUpdatesAsync().ConfigureAwait(false);
// No update is available. We'll check again later.
if (pendingUpdate == null)
{
scheduleRecheck = true;
return false;
}
// An update is found, let's notify the user and start downloading it.
2018-03-27 17:57:20 +08:00
if (notification == null)
{
notification = new UpdateProgressNotification
{
CompletionClickAction = () =>
{
Task.Run(restartToApplyUpdate);
return true;
},
};
2018-03-27 17:57:20 +08:00
Schedule(() => notificationOverlay.Post(notification));
}
2018-04-13 17:19:50 +08:00
notification.StartDownload();
2018-04-13 17:19:50 +08:00
2018-03-27 17:57:20 +08:00
try
{
await updateManager.DownloadUpdatesAsync(pendingUpdate, p => notification.Progress = p / 100f).ConfigureAwait(false);
2018-04-13 17:19:50 +08:00
2018-03-27 17:57:20 +08:00
notification.State = ProgressNotificationState.Completed;
}
catch (Exception e)
{
2024-07-05 05:45:34 +08:00
// In the case of an error, a separate notification will be displayed.
scheduleRecheck = true;
2024-07-05 05:45:34 +08:00
notification.FailDownload();
Logger.Error(e, @"update failed!");
2018-03-27 17:57:20 +08:00
}
}
2024-07-05 05:28:49 +08:00
catch (Exception e)
2018-03-27 17:57:20 +08:00
{
// we'll ignore this and retry later. can be triggered by no internet connection or thread abortion.
scheduleRecheck = true;
Logger.Log($@"update check failed ({e.Message})");
2018-03-27 17:57:20 +08:00
}
finally
{
if (scheduleRecheck)
2018-03-27 17:57:20 +08:00
{
Scheduler.AddDelayed(() => Task.Run(async () => await checkForUpdateAsync().ConfigureAwait(false)), 60000 * 30);
2018-03-27 17:57:20 +08:00
}
}
return true;
2018-03-27 17:57:20 +08:00
}
2018-04-13 17:19:50 +08:00
private async Task restartToApplyUpdate()
{
await updateManager.WaitExitThenApplyUpdatesAsync(pendingUpdate?.TargetFullRelease).ConfigureAwait(false);
2024-06-27 03:25:41 +08:00
Schedule(() => game.AttemptExit());
}
2018-03-27 17:57:20 +08:00
}
}