2019-09-24 09:03:01 +00:00
|
|
|
// 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.
|
|
|
|
|
2022-06-17 07:37:17 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2020-06-07 22:39:33 +00:00
|
|
|
using System.Threading.Tasks;
|
2019-09-24 09:03:01 +00:00
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
|
|
|
using osu.Game.Configuration;
|
|
|
|
using osu.Game.Graphics;
|
|
|
|
using osu.Game.Overlays;
|
|
|
|
using osu.Game.Overlays.Notifications;
|
|
|
|
|
|
|
|
namespace osu.Game.Updater
|
|
|
|
{
|
2019-12-20 04:50:57 +00:00
|
|
|
/// <summary>
|
|
|
|
/// An update manager which only shows notifications after an update completes.
|
|
|
|
/// </summary>
|
|
|
|
public class UpdateManager : CompositeDrawable
|
2019-09-24 09:03:01 +00:00
|
|
|
{
|
2020-05-08 00:50:58 +00:00
|
|
|
/// <summary>
|
2020-06-07 22:39:33 +00:00
|
|
|
/// Whether this UpdateManager should be or is capable of checking for updates.
|
2020-05-08 00:50:58 +00:00
|
|
|
/// </summary>
|
2020-06-12 10:24:50 +00:00
|
|
|
public bool CanCheckForUpdate => game.IsDeployedBuild &&
|
|
|
|
// only implementations will actually check for updates.
|
|
|
|
GetType() != typeof(UpdateManager);
|
2020-06-07 22:39:33 +00:00
|
|
|
|
2019-09-24 09:03:01 +00:00
|
|
|
[Resolved]
|
|
|
|
private OsuConfigManager config { get; set; }
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private OsuGameBase game { get; set; }
|
|
|
|
|
|
|
|
[Resolved]
|
2022-04-18 10:59:57 +00:00
|
|
|
protected INotificationOverlay Notifications { get; private set; }
|
2019-09-24 09:03:01 +00:00
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
2020-06-07 22:39:33 +00:00
|
|
|
Schedule(() => Task.Run(CheckForUpdateAsync));
|
|
|
|
|
2021-10-27 04:04:41 +00:00
|
|
|
string version = game.Version;
|
2020-06-12 10:20:45 +00:00
|
|
|
|
2021-10-27 04:04:41 +00:00
|
|
|
string lastVersion = config.Get<string>(OsuSetting.Version);
|
2020-06-12 10:32:32 +00:00
|
|
|
|
2020-06-12 10:20:45 +00:00
|
|
|
if (game.IsDeployedBuild && version != lastVersion)
|
|
|
|
{
|
|
|
|
// only show a notification if we've previously saved a version to the config file (ie. not the first run).
|
|
|
|
if (!string.IsNullOrEmpty(lastVersion))
|
|
|
|
Notifications.Post(new UpdateCompleteNotification(version));
|
|
|
|
}
|
|
|
|
|
2020-06-07 22:39:33 +00:00
|
|
|
// debug / local compilations will reset to a non-release string.
|
|
|
|
// can be useful to check when an install has transitioned between release and otherwise (see OsuConfigManager's migrations).
|
2021-03-17 07:10:16 +00:00
|
|
|
config.SetValue(OsuSetting.Version, version);
|
2020-06-07 22:39:33 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 09:29:21 +00:00
|
|
|
private readonly object updateTaskLock = new object();
|
|
|
|
|
2020-10-06 04:00:02 +00:00
|
|
|
private Task<bool> updateCheckTask;
|
2020-06-12 09:29:21 +00:00
|
|
|
|
2020-10-06 04:00:02 +00:00
|
|
|
public async Task<bool> CheckForUpdateAsync()
|
2020-06-07 22:39:33 +00:00
|
|
|
{
|
2020-10-07 09:40:09 +00:00
|
|
|
if (!CanCheckForUpdate)
|
|
|
|
return false;
|
|
|
|
|
2020-10-06 04:00:02 +00:00
|
|
|
Task<bool> waitTask;
|
2020-06-15 13:19:02 +00:00
|
|
|
|
2020-06-12 09:29:21 +00:00
|
|
|
lock (updateTaskLock)
|
2020-06-15 13:19:02 +00:00
|
|
|
waitTask = (updateCheckTask ??= PerformUpdateCheck());
|
2020-06-12 09:29:21 +00:00
|
|
|
|
2021-03-08 03:57:16 +00:00
|
|
|
bool hasUpdates = await waitTask.ConfigureAwait(false);
|
2020-06-12 09:29:21 +00:00
|
|
|
|
|
|
|
lock (updateTaskLock)
|
|
|
|
updateCheckTask = null;
|
2020-10-06 04:00:02 +00:00
|
|
|
|
|
|
|
return hasUpdates;
|
2020-06-07 22:39:33 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 04:00:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Performs an asynchronous check for application updates.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>Whether any update is waiting. May return true if an error occured (there is potentially an update available).</returns>
|
|
|
|
protected virtual Task<bool> PerformUpdateCheck() => Task.FromResult(false);
|
2020-05-07 06:07:22 +00:00
|
|
|
|
2019-09-24 09:03:01 +00:00
|
|
|
private class UpdateCompleteNotification : SimpleNotification
|
|
|
|
{
|
|
|
|
private readonly string version;
|
|
|
|
|
|
|
|
public UpdateCompleteNotification(string version)
|
|
|
|
{
|
|
|
|
this.version = version;
|
2021-07-04 03:39:50 +00:00
|
|
|
Text = $"You are now running osu! {version}.\nClick to see what's new!";
|
2019-09-24 09:03:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2022-04-18 10:59:57 +00:00
|
|
|
private void load(OsuColour colours, ChangelogOverlay changelog, INotificationOverlay notificationOverlay)
|
2019-09-24 09:03:01 +00:00
|
|
|
{
|
|
|
|
Icon = FontAwesome.Solid.CheckSquare;
|
2021-12-10 05:04:31 +00:00
|
|
|
IconBackground.Colour = colours.BlueDark;
|
2019-09-24 09:03:01 +00:00
|
|
|
|
|
|
|
Activated = delegate
|
|
|
|
{
|
|
|
|
notificationOverlay.Hide();
|
|
|
|
changelog.ShowBuild(OsuGameBase.CLIENT_STREAM_NAME, version);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|