2019-06-28 11:37:53 +00:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
2019-01-24 08:43:03 +00:00
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
2018-04-13 09:19:50 +00:00
|
|
|
|
2018-03-27 09:57:20 +00:00
|
|
|
using System;
|
2018-07-06 07:39:27 +00:00
|
|
|
using System.Threading.Tasks;
|
2018-03-27 09:57:20 +00:00
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Logging;
|
|
|
|
using osu.Game;
|
|
|
|
using osu.Game.Overlays;
|
|
|
|
using osu.Game.Overlays.Notifications;
|
2023-05-23 09:06:04 +00:00
|
|
|
using osu.Game.Screens.Play;
|
2024-07-04 22:00:45 +00:00
|
|
|
using Velopack;
|
2024-07-04 21:28:49 +00:00
|
|
|
using Velopack.Sources;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
2018-07-06 07:39:27 +00:00
|
|
|
namespace osu.Desktop.Updater
|
2018-03-27 09:57:20 +00:00
|
|
|
{
|
2024-08-31 14:03:10 +00:00
|
|
|
public partial class VelopackUpdateManager : Game.Updater.UpdateManager
|
2018-03-27 09:57:20 +00:00
|
|
|
{
|
2024-07-04 22:00:45 +00:00
|
|
|
private readonly UpdateManager updateManager;
|
2022-08-02 14:23:54 +00:00
|
|
|
private INotificationOverlay notificationOverlay = null!;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
2022-09-12 05:52:11 +00:00
|
|
|
[Resolved]
|
|
|
|
private OsuGameBase game { get; set; } = null!;
|
|
|
|
|
2023-05-23 09:06:04 +00:00
|
|
|
[Resolved]
|
|
|
|
private ILocalUserPlayInfo? localUserInfo { get; set; }
|
|
|
|
|
2024-10-01 10:55:46 +00:00
|
|
|
private bool isInGameplay => localUserInfo?.PlayingState.Value != LocalUserPlayingState.NotPlaying;
|
2024-10-01 08:53:25 +00:00
|
|
|
|
2024-09-03 10:11:34 +00:00
|
|
|
private UpdateInfo? pendingUpdate;
|
|
|
|
|
2024-08-31 14:03:10 +00:00
|
|
|
public VelopackUpdateManager()
|
2024-07-04 21:28:49 +00:00
|
|
|
{
|
2024-09-02 05:23:05 +00:00
|
|
|
updateManager = new UpdateManager(new GithubSource(@"https://github.com/ppy/osu", null, false), new UpdateOptions
|
2024-07-04 22:00:45 +00:00
|
|
|
{
|
2024-09-03 10:11:34 +00:00
|
|
|
AllowVersionDowngrade = true,
|
2024-07-04 22:00:45 +00:00
|
|
|
});
|
2024-07-04 21:28:49 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 09:57:20 +00:00
|
|
|
[BackgroundDependencyLoader]
|
2022-04-18 21:18:10 +00:00
|
|
|
private void load(INotificationOverlay notifications)
|
2018-03-27 09:57:20 +00:00
|
|
|
{
|
2022-04-18 21:18:10 +00:00
|
|
|
notificationOverlay = notifications;
|
2018-03-27 09:57:20 +00:00
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
2021-03-08 03:57:16 +00:00
|
|
|
protected override async Task<bool> PerformUpdateCheck() => await checkForUpdateAsync().ConfigureAwait(false);
|
2020-05-07 06:07:22 +00:00
|
|
|
|
2024-10-07 10:48:57 +00:00
|
|
|
private async Task<bool> checkForUpdateAsync()
|
2018-03-27 09:57:20 +00:00
|
|
|
{
|
2024-09-08 14:07:17 +00: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 09:19:50 +00:00
|
|
|
|
2018-03-27 09:57:20 +00:00
|
|
|
try
|
|
|
|
{
|
2023-05-23 09:06:04 +00:00
|
|
|
// Avoid any kind of update checking while gameplay is running.
|
2024-10-01 08:53:25 +00:00
|
|
|
if (isInGameplay)
|
2024-09-08 14:07:17 +00:00
|
|
|
{
|
|
|
|
scheduleRecheck = true;
|
2024-10-07 10:35:09 +00:00
|
|
|
return true;
|
2024-09-08 14:07:17 +00:00
|
|
|
}
|
2023-05-23 09:06:04 +00:00
|
|
|
|
2024-09-04 10:41:40 +00:00
|
|
|
// 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).
|
2024-09-03 10:11:34 +00:00
|
|
|
if (pendingUpdate != null)
|
2020-10-06 04:18:42 +00:00
|
|
|
{
|
2024-10-07 08:04:52 +00:00
|
|
|
// If there is an update pending restart, show the notification to restart again.
|
|
|
|
notificationOverlay.Post(new UpdateApplicationCompleteNotification
|
2020-10-06 04:18:42 +00:00
|
|
|
{
|
2024-10-07 08:04:52 +00:00
|
|
|
Activated = () =>
|
2022-09-12 05:52:11 +00:00
|
|
|
{
|
2024-10-07 08:04:52 +00:00
|
|
|
Task.Run(restartToApplyUpdate);
|
|
|
|
return true;
|
|
|
|
}
|
2024-07-04 21:28:49 +00:00
|
|
|
});
|
2024-09-08 14:07:17 +00:00
|
|
|
|
2024-07-04 21:28:49 +00:00
|
|
|
return true;
|
2020-10-06 04:18:42 +00:00
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
2024-09-03 10:11:34 +00:00
|
|
|
pendingUpdate = await updateManager.CheckForUpdatesAsync().ConfigureAwait(false);
|
|
|
|
|
2024-09-08 14:07:17 +00:00
|
|
|
// No update is available. We'll check again later.
|
2024-09-03 10:11:34 +00:00
|
|
|
if (pendingUpdate == null)
|
2024-09-08 14:07:17 +00:00
|
|
|
{
|
|
|
|
scheduleRecheck = true;
|
2024-09-03 10:11:34 +00:00
|
|
|
return false;
|
2024-09-08 14:07:17 +00:00
|
|
|
}
|
2024-09-03 10:11:34 +00:00
|
|
|
|
2024-09-08 14:07:17 +00:00
|
|
|
// An update is found, let's notify the user and start downloading it.
|
2024-10-07 10:48:57 +00:00
|
|
|
UpdateProgressNotification notification = new UpdateProgressNotification
|
2018-03-27 09:57:20 +00:00
|
|
|
{
|
2024-10-07 10:48:57 +00:00
|
|
|
CompletionClickAction = () =>
|
2022-09-12 05:52:11 +00:00
|
|
|
{
|
2024-10-07 10:48:57 +00:00
|
|
|
Task.Run(restartToApplyUpdate);
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
};
|
2018-04-13 09:19:50 +00:00
|
|
|
|
2024-10-07 10:48:57 +00:00
|
|
|
runOutsideOfGameplay(() => notificationOverlay.Post(notification));
|
2022-09-12 05:52:11 +00:00
|
|
|
notification.StartDownload();
|
2018-04-13 09:19:50 +00:00
|
|
|
|
2018-03-27 09:57:20 +00:00
|
|
|
try
|
|
|
|
{
|
2024-09-03 10:11:34 +00:00
|
|
|
await updateManager.DownloadUpdatesAsync(pendingUpdate, p => notification.Progress = p / 100f).ConfigureAwait(false);
|
2024-10-01 08:53:25 +00:00
|
|
|
runOutsideOfGameplay(() => notification.State = ProgressNotificationState.Completed);
|
2018-03-27 09:57:20 +00:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2024-07-04 21:45:34 +00:00
|
|
|
// In the case of an error, a separate notification will be displayed.
|
2024-09-08 16:58:09 +00:00
|
|
|
scheduleRecheck = true;
|
2024-07-04 21:45:34 +00:00
|
|
|
notification.FailDownload();
|
|
|
|
Logger.Error(e, @"update failed!");
|
2018-03-27 09:57:20 +00:00
|
|
|
}
|
|
|
|
}
|
2024-07-04 21:28:49 +00:00
|
|
|
catch (Exception e)
|
2018-03-27 09:57:20 +00:00
|
|
|
{
|
|
|
|
// we'll ignore this and retry later. can be triggered by no internet connection or thread abortion.
|
2021-07-12 05:55:09 +00:00
|
|
|
scheduleRecheck = true;
|
2024-09-03 10:16:10 +00:00
|
|
|
Logger.Log($@"update check failed ({e.Message})");
|
2018-03-27 09:57:20 +00:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
2019-06-28 11:37:53 +00:00
|
|
|
if (scheduleRecheck)
|
2018-03-27 09:57:20 +00:00
|
|
|
{
|
2021-07-02 05:43:48 +00:00
|
|
|
Scheduler.AddDelayed(() => Task.Run(async () => await checkForUpdateAsync().ConfigureAwait(false)), 60000 * 30);
|
2018-03-27 09:57:20 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-06 04:00:02 +00:00
|
|
|
|
|
|
|
return true;
|
2018-03-27 09:57:20 +00:00
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
2024-10-01 08:53:25 +00:00
|
|
|
private void runOutsideOfGameplay(Action action)
|
|
|
|
{
|
|
|
|
if (isInGameplay)
|
|
|
|
{
|
|
|
|
Scheduler.AddDelayed(() => runOutsideOfGameplay(action), 1000);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
action();
|
|
|
|
}
|
|
|
|
|
2024-09-25 16:11:23 +00:00
|
|
|
private async Task restartToApplyUpdate()
|
2020-10-06 04:18:42 +00:00
|
|
|
{
|
2024-09-25 16:11:23 +00:00
|
|
|
await updateManager.WaitExitThenApplyUpdatesAsync(pendingUpdate?.TargetFullRelease).ConfigureAwait(false);
|
2024-06-26 19:25:41 +00:00
|
|
|
Schedule(() => game.AttemptExit());
|
2018-11-19 12:29:29 +00:00
|
|
|
}
|
2018-03-27 09:57:20 +00:00
|
|
|
}
|
|
|
|
}
|