2019-06-28 19:37:53 +08:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
2019-01-24 16:43:03 +08:00
|
|
|
// 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;
|
2022-03-02 16:11:24 +08:00
|
|
|
using System.Runtime.Versioning;
|
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 Squirrel;
|
2022-03-02 16:11:24 +08:00
|
|
|
using Squirrel.SimpleSplat;
|
2022-09-12 13:52:11 +08:00
|
|
|
using LogLevel = Squirrel.SimpleSplat.LogLevel;
|
|
|
|
using UpdateManager = osu.Game.Updater.UpdateManager;
|
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
|
|
|
{
|
2022-03-02 16:11:24 +08:00
|
|
|
[SupportedOSPlatform("windows")]
|
2022-09-12 13:52:11 +08:00
|
|
|
public partial class SquirrelUpdateManager : UpdateManager
|
2018-03-27 17:57:20 +08:00
|
|
|
{
|
2022-09-12 13:52:11 +08:00
|
|
|
private Squirrel.UpdateManager? updateManager;
|
2022-08-02 22:23:54 +08:00
|
|
|
private INotificationOverlay notificationOverlay = null!;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2022-09-12 13:52:11 +08:00
|
|
|
public Task PrepareUpdateAsync() => Squirrel.UpdateManager.RestartAppWhenExited();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2019-07-29 17:14:06 +08:00
|
|
|
private static readonly Logger logger = Logger.GetLogger("updater");
|
|
|
|
|
2020-10-06 12:18:42 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Whether an update has been downloaded but not yet applied.
|
|
|
|
/// </summary>
|
|
|
|
private bool updatePending;
|
|
|
|
|
2022-03-02 16:11:24 +08:00
|
|
|
private readonly SquirrelLogger squirrelLogger = new SquirrelLogger();
|
|
|
|
|
2022-09-12 13:52:11 +08:00
|
|
|
[Resolved]
|
|
|
|
private OsuGameBase game { get; set; } = null!;
|
|
|
|
|
2018-03-27 17:57:20 +08:00
|
|
|
[BackgroundDependencyLoader]
|
2022-04-19 05:18:10 +08:00
|
|
|
private void load(INotificationOverlay notifications)
|
2018-03-27 17:57:20 +08:00
|
|
|
{
|
2022-04-19 05:18:10 +08:00
|
|
|
notificationOverlay = notifications;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2022-03-02 16:11:24 +08:00
|
|
|
SquirrelLocator.CurrentMutable.Register(() => squirrelLogger, typeof(ILogger));
|
2018-03-27 17:57:20 +08:00
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2021-03-08 11:57:16 +08:00
|
|
|
protected override async Task<bool> PerformUpdateCheck() => await checkForUpdateAsync().ConfigureAwait(false);
|
2020-05-07 14:07:22 +08:00
|
|
|
|
2022-08-02 22:23:54 +08:00
|
|
|
private async Task<bool> checkForUpdateAsync(bool useDeltaPatching = true, UpdateProgressNotification? notification = null)
|
2018-03-27 17:57:20 +08:00
|
|
|
{
|
2020-05-05 09:31:11 +08:00
|
|
|
// should we schedule a retry on completion of this check?
|
2019-06-28 19:37:53 +08:00
|
|
|
bool scheduleRecheck = true;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2022-08-02 22:23:54 +08:00
|
|
|
const string? github_token = null; // TODO: populate.
|
2022-03-02 16:11:24 +08:00
|
|
|
|
2018-03-27 17:57:20 +08:00
|
|
|
try
|
|
|
|
{
|
2022-03-02 16:11:24 +08:00
|
|
|
updateManager ??= new GithubUpdateManager(@"https://github.com/ppy/osu", false, github_token, @"osulazer");
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2021-03-08 11:57:16 +08:00
|
|
|
var info = await updateManager.CheckForUpdate(!useDeltaPatching).ConfigureAwait(false);
|
2020-10-06 12:18:42 +08:00
|
|
|
|
2018-03-27 17:57:20 +08:00
|
|
|
if (info.ReleasesToApply.Count == 0)
|
2020-10-06 12:18:42 +08:00
|
|
|
{
|
|
|
|
if (updatePending)
|
|
|
|
{
|
|
|
|
// the user may have dismissed the completion notice, so show it again.
|
2022-09-12 13:52:11 +08:00
|
|
|
notificationOverlay.Post(new UpdateApplicationCompleteNotification
|
|
|
|
{
|
|
|
|
Activated = () =>
|
|
|
|
{
|
|
|
|
restartToApplyUpdate();
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
});
|
2020-10-06 12:18:42 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-05 09:31:11 +08:00
|
|
|
// no updates available. bail and retry later.
|
2020-10-06 12:00:02 +08:00
|
|
|
return false;
|
2020-10-06 12:18:42 +08:00
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2021-07-09 17:23:27 +08:00
|
|
|
scheduleRecheck = false;
|
|
|
|
|
2018-03-27 17:57:20 +08:00
|
|
|
if (notification == null)
|
|
|
|
{
|
2022-09-12 13:52:11 +08:00
|
|
|
notification = new UpdateProgressNotification
|
|
|
|
{
|
|
|
|
CompletionClickAction = restartToApplyUpdate,
|
|
|
|
};
|
|
|
|
|
2018-03-27 17:57:20 +08:00
|
|
|
Schedule(() => notificationOverlay.Post(notification));
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2022-09-12 13:52:11 +08:00
|
|
|
notification.StartDownload();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2018-03-27 17:57:20 +08:00
|
|
|
try
|
|
|
|
{
|
2021-03-08 11:57:16 +08:00
|
|
|
await updateManager.DownloadReleases(info.ReleasesToApply, p => notification.Progress = p / 100f).ConfigureAwait(false);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2022-09-12 13:52:11 +08:00
|
|
|
notification.StartInstall();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2021-03-08 11:57:16 +08:00
|
|
|
await updateManager.ApplyReleases(info, 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;
|
2020-10-06 12:18:42 +08:00
|
|
|
updatePending = true;
|
2018-03-27 17:57:20 +08:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
if (useDeltaPatching)
|
|
|
|
{
|
2019-07-29 17:14:06 +08:00
|
|
|
logger.Add(@"delta patching failed; will attempt full download!");
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2020-05-05 09:31:11 +08:00
|
|
|
// could fail if deltas are unavailable for full update path (https://github.com/Squirrel/Squirrel.Windows/issues/959)
|
|
|
|
// try again without deltas.
|
2021-03-08 11:57:16 +08:00
|
|
|
await checkForUpdateAsync(false, notification).ConfigureAwait(false);
|
2018-03-27 17:57:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-11-24 18:05:52 +08:00
|
|
|
// In the case of an error, a separate notification will be displayed.
|
2022-09-12 13:52:11 +08:00
|
|
|
notification.FailDownload();
|
2018-03-27 17:57:20 +08:00
|
|
|
Logger.Error(e, @"update failed!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
// we'll ignore this and retry later. can be triggered by no internet connection or thread abortion.
|
2021-07-12 13:55:09 +08:00
|
|
|
scheduleRecheck = true;
|
2018-03-27 17:57:20 +08:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
2019-06-28 19:37:53 +08:00
|
|
|
if (scheduleRecheck)
|
2018-03-27 17:57:20 +08:00
|
|
|
{
|
2020-05-05 09:31:11 +08:00
|
|
|
// check again in 30 minutes.
|
2021-07-02 13:43:48 +08:00
|
|
|
Scheduler.AddDelayed(() => Task.Run(async () => await checkForUpdateAsync().ConfigureAwait(false)), 60000 * 30);
|
2018-03-27 17:57:20 +08:00
|
|
|
}
|
|
|
|
}
|
2020-10-06 12:00:02 +08:00
|
|
|
|
|
|
|
return true;
|
2018-03-27 17:57:20 +08:00
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2022-09-12 13:52:11 +08:00
|
|
|
private bool restartToApplyUpdate()
|
2020-10-06 12:18:42 +08:00
|
|
|
{
|
2022-09-12 13:52:11 +08:00
|
|
|
PrepareUpdateAsync()
|
|
|
|
.ContinueWith(_ => Schedule(() => game.AttemptExit()));
|
|
|
|
return true;
|
2020-10-06 12:18:42 +08:00
|
|
|
}
|
|
|
|
|
2022-09-12 13:52:11 +08:00
|
|
|
protected override void Dispose(bool isDisposing)
|
2018-03-27 17:57:20 +08:00
|
|
|
{
|
2022-09-12 13:52:11 +08:00
|
|
|
base.Dispose(isDisposing);
|
|
|
|
updateManager?.Dispose();
|
2018-03-27 17:57:20 +08:00
|
|
|
}
|
2018-11-19 20:29:29 +08:00
|
|
|
|
2022-03-02 16:11:24 +08:00
|
|
|
private class SquirrelLogger : ILogger, IDisposable
|
2018-11-19 20:29:29 +08:00
|
|
|
{
|
2022-09-12 13:52:11 +08:00
|
|
|
public LogLevel Level { get; set; } = LogLevel.Info;
|
2018-11-19 20:29:29 +08:00
|
|
|
|
2022-09-12 13:52:11 +08:00
|
|
|
public void Write(string message, LogLevel logLevel)
|
2018-11-19 20:29:29 +08:00
|
|
|
{
|
|
|
|
if (logLevel < Level)
|
|
|
|
return;
|
|
|
|
|
2019-05-15 13:12:59 +08:00
|
|
|
logger.Add(message);
|
2018-11-19 20:29:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2018-03-27 17:57:20 +08:00
|
|
|
}
|
|
|
|
}
|