diff --git a/osu.Desktop/Updater/SquirrelUpdateManager.cs b/osu.Desktop/Updater/SquirrelUpdateManager.cs index dd50b05c75..05c8e835ac 100644 --- a/osu.Desktop/Updater/SquirrelUpdateManager.cs +++ b/osu.Desktop/Updater/SquirrelUpdateManager.cs @@ -30,18 +30,16 @@ public class SquirrelUpdateManager : osu.Game.Updater.UpdateManager private static readonly Logger logger = Logger.GetLogger("updater"); [BackgroundDependencyLoader] - private void load(NotificationOverlay notification, OsuGameBase game) + private void load(NotificationOverlay notification) { notificationOverlay = notification; - if (game.IsDeployedBuild) - { - Splat.Locator.CurrentMutable.Register(() => new SquirrelLogger(), typeof(Splat.ILogger)); - Schedule(() => Task.Run(() => checkForUpdateAsync())); - } + Splat.Locator.CurrentMutable.Register(() => new SquirrelLogger(), typeof(Splat.ILogger)); } - private async void checkForUpdateAsync(bool useDeltaPatching = true, UpdateProgressNotification notification = null) + protected override async Task PerformUpdateCheck() => await checkForUpdateAsync(); + + private async Task checkForUpdateAsync(bool useDeltaPatching = true, UpdateProgressNotification notification = null) { // should we schedule a retry on completion of this check? bool scheduleRecheck = true; @@ -83,7 +81,7 @@ private async void checkForUpdateAsync(bool useDeltaPatching = true, UpdateProgr // could fail if deltas are unavailable for full update path (https://github.com/Squirrel/Squirrel.Windows/issues/959) // try again without deltas. - checkForUpdateAsync(false, notification); + await checkForUpdateAsync(false, notification); scheduleRecheck = false; } else @@ -102,7 +100,7 @@ private async void checkForUpdateAsync(bool useDeltaPatching = true, UpdateProgr if (scheduleRecheck) { // check again in 30 minutes. - Scheduler.AddDelayed(() => checkForUpdateAsync(), 60000 * 30); + Scheduler.AddDelayed(async () => await checkForUpdateAsync(), 60000 * 30); } } } diff --git a/osu.Game/OsuGame.cs b/osu.Game/OsuGame.cs index 7c5e4b8d94..b0d7b14d34 100644 --- a/osu.Game/OsuGame.cs +++ b/osu.Game/OsuGame.cs @@ -622,6 +622,9 @@ protected override void LoadComplete() loadComponentSingleFile(screenshotManager, Add); + // dependency on notification overlay, dependent by settings overlay + loadComponentSingleFile(CreateUpdateManager(), Add, true); + // overlay elements loadComponentSingleFile(beatmapListing = new BeatmapListingOverlay(), overlayContent.Add, true); loadComponentSingleFile(dashboard = new DashboardOverlay(), overlayContent.Add, true); @@ -654,7 +657,6 @@ protected override void LoadComplete() chatOverlay.State.ValueChanged += state => channelManager.HighPollRate.Value = state.NewValue == Visibility.Visible; Add(externalLinkOpener = new ExternalLinkOpener()); - Add(CreateUpdateManager()); // dependency on notification overlay // side overlays which cancel each other. var singleDisplaySideOverlays = new OverlayContainer[] { Settings, notifications }; diff --git a/osu.Game/Overlays/Settings/Sections/General/UpdateSettings.cs b/osu.Game/Overlays/Settings/Sections/General/UpdateSettings.cs index 95a1868392..9c7d0b0be4 100644 --- a/osu.Game/Overlays/Settings/Sections/General/UpdateSettings.cs +++ b/osu.Game/Overlays/Settings/Sections/General/UpdateSettings.cs @@ -1,19 +1,26 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. +using System.Threading.Tasks; using osu.Framework; using osu.Framework.Allocation; using osu.Framework.Platform; using osu.Framework.Screens; using osu.Game.Configuration; using osu.Game.Overlays.Settings.Sections.Maintenance; +using osu.Game.Updater; namespace osu.Game.Overlays.Settings.Sections.General { public class UpdateSettings : SettingsSubsection { + [Resolved(CanBeNull = true)] + private UpdateManager updateManager { get; set; } + protected override string Header => "Updates"; + private SettingsButton checkForUpdatesButton; + [BackgroundDependencyLoader(true)] private void load(Storage storage, OsuConfigManager config, OsuGame game) { @@ -23,6 +30,19 @@ private void load(Storage storage, OsuConfigManager config, OsuGame game) Bindable = config.GetBindable(OsuSetting.ReleaseStream), }); + if (updateManager?.CanCheckForUpdate == true) + { + Add(checkForUpdatesButton = new SettingsButton + { + Text = "Check for updates", + Action = () => + { + checkForUpdatesButton.Enabled.Value = false; + Task.Run(updateManager.CheckForUpdateAsync).ContinueWith(t => Schedule(() => checkForUpdatesButton.Enabled.Value = true)); + } + }); + } + if (RuntimeInfo.IsDesktop) { Add(new SettingsButton diff --git a/osu.Game/Updater/SimpleUpdateManager.cs b/osu.Game/Updater/SimpleUpdateManager.cs index 1e8a96444f..ebb9995c66 100644 --- a/osu.Game/Updater/SimpleUpdateManager.cs +++ b/osu.Game/Updater/SimpleUpdateManager.cs @@ -28,12 +28,9 @@ public class SimpleUpdateManager : UpdateManager private void load(OsuGameBase game) { version = game.Version; - - if (game.IsDeployedBuild) - Schedule(() => Task.Run(checkForUpdateAsync)); } - private async void checkForUpdateAsync() + protected override async Task PerformUpdateCheck() { try { diff --git a/osu.Game/Updater/UpdateManager.cs b/osu.Game/Updater/UpdateManager.cs index 28a295215f..61775a26b7 100644 --- a/osu.Game/Updater/UpdateManager.cs +++ b/osu.Game/Updater/UpdateManager.cs @@ -1,6 +1,7 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. +using System.Threading.Tasks; using osu.Framework.Allocation; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Sprites; @@ -16,6 +17,13 @@ namespace osu.Game.Updater /// public class UpdateManager : CompositeDrawable { + /// + /// Whether this UpdateManager should be or is capable of checking for updates. + /// + public bool CanCheckForUpdate => game.IsDeployedBuild && + // only implementations will actually check for updates. + GetType() != typeof(UpdateManager); + [Resolved] private OsuConfigManager config { get; set; } @@ -29,7 +37,10 @@ protected override void LoadComplete() { base.LoadComplete(); + Schedule(() => Task.Run(CheckForUpdateAsync)); + var version = game.Version; + var lastVersion = config.Get(OsuSetting.Version); if (game.IsDeployedBuild && version != lastVersion) @@ -44,6 +55,28 @@ protected override void LoadComplete() config.Set(OsuSetting.Version, version); } + private readonly object updateTaskLock = new object(); + + private Task updateCheckTask; + + public async Task CheckForUpdateAsync() + { + if (!CanCheckForUpdate) + return; + + Task waitTask; + + lock (updateTaskLock) + waitTask = (updateCheckTask ??= PerformUpdateCheck()); + + await waitTask; + + lock (updateTaskLock) + updateCheckTask = null; + } + + protected virtual Task PerformUpdateCheck() => Task.CompletedTask; + private class UpdateCompleteNotification : SimpleNotification { private readonly string version;