1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-24 06:43:03 +08:00
osu-lazer/osu.Desktop/Updater/SquirrelUpdateManager.cs

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

228 lines
7.9 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;
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.Graphics;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
2018-03-27 17:57:20 +08:00
using osu.Framework.Logging;
using osu.Game;
using osu.Game.Graphics;
using osu.Game.Overlays;
using osu.Game.Overlays.Notifications;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
2018-03-27 17:57:20 +08:00
using Squirrel;
using Squirrel.SimpleSplat;
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
{
[SupportedOSPlatform("windows")]
public class SquirrelUpdateManager : osu.Game.Updater.UpdateManager
2018-03-27 17:57:20 +08:00
{
private UpdateManager updateManager;
private INotificationOverlay notificationOverlay;
2018-04-13 17:19:50 +08:00
2019-06-28 20:26:31 +08:00
public Task PrepareUpdateAsync() => UpdateManager.RestartAppWhenExited();
2018-04-13 17:19:50 +08:00
private static readonly Logger logger = Logger.GetLogger("updater");
/// <summary>
/// Whether an update has been downloaded but not yet applied.
/// </summary>
private bool updatePending;
private readonly SquirrelLogger squirrelLogger = new SquirrelLogger();
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-04-13 17:19:50 +08:00
SquirrelLocator.CurrentMutable.Register(() => squirrelLogger, typeof(ILogger));
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
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?
bool scheduleRecheck = true;
2018-04-13 17:19:50 +08:00
const string github_token = null; // TODO: populate.
2018-03-27 17:57:20 +08:00
try
{
updateManager ??= new GithubUpdateManager(@"https://github.com/ppy/osu", false, github_token, @"osulazer");
2018-04-13 17:19:50 +08:00
var info = await updateManager.CheckForUpdate(!useDeltaPatching).ConfigureAwait(false);
2018-03-27 17:57:20 +08:00
if (info.ReleasesToApply.Count == 0)
{
if (updatePending)
{
// the user may have dismissed the completion notice, so show it again.
notificationOverlay.Post(new UpdateCompleteNotification(this));
return true;
}
2020-05-05 09:31:11 +08:00
// no updates available. bail and retry later.
return false;
}
2018-04-13 17:19:50 +08:00
scheduleRecheck = false;
2018-03-27 17:57:20 +08:00
if (notification == null)
{
notification = new UpdateProgressNotification(this) { State = ProgressNotificationState.Active };
Schedule(() => notificationOverlay.Post(notification));
}
2018-04-13 17:19:50 +08:00
2018-03-27 17:57:20 +08:00
notification.Progress = 0;
notification.Text = @"Downloading update...";
2018-04-13 17:19:50 +08:00
2018-03-27 17:57:20 +08:00
try
{
await updateManager.DownloadReleases(info.ReleasesToApply, p => notification.Progress = p / 100f).ConfigureAwait(false);
2018-04-13 17:19:50 +08:00
2018-03-27 17:57:20 +08:00
notification.Progress = 0;
notification.Text = @"Installing update...";
2018-04-13 17:19:50 +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;
updatePending = true;
2018-03-27 17:57:20 +08:00
}
catch (Exception e)
{
if (useDeltaPatching)
{
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.
await checkForUpdateAsync(false, notification).ConfigureAwait(false);
2018-03-27 17:57:20 +08:00
}
else
{
// In the case of an error, a separate notification will be displayed.
2019-06-28 20:26:31 +08:00
notification.State = ProgressNotificationState.Cancelled;
notification.Close();
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.
scheduleRecheck = true;
2018-03-27 17:57:20 +08:00
}
finally
{
if (scheduleRecheck)
2018-03-27 17:57:20 +08:00
{
2020-05-05 09:31:11 +08:00
// check again in 30 minutes.
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
2018-03-27 17:57:20 +08:00
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
updateManager?.Dispose();
}
2018-04-13 17:19:50 +08:00
private class UpdateCompleteNotification : ProgressCompletionNotification
{
[Resolved]
private OsuGame game { get; set; }
public UpdateCompleteNotification(SquirrelUpdateManager updateManager)
{
Text = @"Update ready to install. Click to restart!";
Activated = () =>
{
updateManager.PrepareUpdateAsync()
.ContinueWith(_ => updateManager.Schedule(() => game?.GracefullyExit()));
return true;
};
}
}
2018-03-27 17:57:20 +08:00
private class UpdateProgressNotification : ProgressNotification
{
private readonly SquirrelUpdateManager updateManager;
2018-04-13 17:19:50 +08:00
2018-03-27 17:57:20 +08:00
public UpdateProgressNotification(SquirrelUpdateManager updateManager)
{
this.updateManager = updateManager;
}
2018-04-13 17:19:50 +08:00
protected override Notification CreateCompletionNotification()
2018-03-27 17:57:20 +08:00
{
return new UpdateCompleteNotification(updateManager);
}
2018-04-13 17:19:50 +08:00
2018-03-27 17:57:20 +08:00
[BackgroundDependencyLoader]
private void load(OsuColour colours)
2018-03-27 17:57:20 +08:00
{
IconContent.AddRange(new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = ColourInfo.GradientVertical(colours.YellowDark, colours.Yellow)
},
new SpriteIcon
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
2019-04-02 18:55:24 +08:00
Icon = FontAwesome.Solid.Upload,
2018-03-27 17:57:20 +08:00
Colour = Color4.White,
Size = new Vector2(20),
}
});
}
public override void Close()
{
// cancelling updates is not currently supported by the underlying updater.
// only allow dismissing for now.
switch (State)
{
case ProgressNotificationState.Cancelled:
base.Close();
break;
}
}
2018-03-27 17:57:20 +08:00
}
private class SquirrelLogger : ILogger, IDisposable
{
public Squirrel.SimpleSplat.LogLevel Level { get; set; } = Squirrel.SimpleSplat.LogLevel.Info;
public void Write(string message, Squirrel.SimpleSplat.LogLevel logLevel)
{
if (logLevel < Level)
return;
2019-05-15 13:12:59 +08:00
logger.Add(message);
}
public void Dispose()
{
}
}
2018-03-27 17:57:20 +08:00
}
}