-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathUpdateManager.cs
More file actions
255 lines (212 loc) · 8.79 KB
/
Copy pathUpdateManager.cs
File metadata and controls
255 lines (212 loc) · 8.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// 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.
using System;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using osu.Framework;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Logging;
using osu.Game.Configuration;
using osu.Game.Graphics;
using osu.Game.Localisation;
using osu.Game.Online.Multiplayer;
using osu.Game.Overlays;
using osu.Game.Overlays.Notifications;
using osu.Game.Utils;
using osuTK;
namespace osu.Game.Updater
{
/// <summary>
/// An update manager which only shows notifications after an update completes.
/// </summary>
public partial class UpdateManager : CompositeDrawable
{
/// <summary>
/// Whether this UpdateManager should be or is capable of checking for updates.
/// </summary>
public bool CanCheckForUpdate => game.IsDeployedBuild &&
// only implementations will actually check for updates.
GetType() != typeof(UpdateManager);
public virtual ReleaseStream? FixedReleaseStream => null;
[Resolved]
private OsuConfigManager config { get; set; } = null!;
[Resolved]
private OsuGameBase game { get; set; } = null!;
[Resolved]
protected INotificationOverlay Notifications { get; private set; } = null!;
protected IBindable<ReleaseStream> ReleaseStream => releaseStream;
private readonly Bindable<ReleaseStream> releaseStream = new Bindable<ReleaseStream>();
private CancellationTokenSource updateCancellationSource = new CancellationTokenSource();
protected override void LoadComplete()
{
base.LoadComplete();
if (game.IsDeployedBuild)
{
// make sure the release stream setting matches the build which was just run.
if (FixedReleaseStream != null)
config.SetValue(OsuSetting.ReleaseStream, FixedReleaseStream.Value);
// notify the user if they're using a build that is not officially sanctioned.
if (RuntimeInfo.EntryAssembly.GetCustomAttribute<OfficialBuildAttribute>() == null)
Notifications.Post(new SimpleNotification { Text = NotificationsStrings.NotOfficialBuild });
}
else
{
// log that this is not an official build, for if users build their own game without an assembly version.
// this is only logged because a notification would be too spammy in local test builds.
Logger.Log(NotificationsStrings.NotOfficialBuild.ToString());
}
config.BindWith(OsuSetting.ReleaseStream, releaseStream);
releaseStream.BindValueChanged(_ => CheckForUpdate());
CheckForUpdate();
}
/// <summary>
/// Immediately checks for any available update.
/// </summary>
public void CheckForUpdate()
{
CheckForUpdateAsync().FireAndForget();
}
/// <summary>
/// Immediately checks for any available update.
/// </summary>
/// <returns>
/// <c>true</c> if any updates are available, <c>false</c> otherwise.
/// May return true if an error occured (there is potentially an update available).
/// </returns>
public async Task<bool> CheckForUpdateAsync(CancellationToken cancellationToken = default) => await Task.Run(async () =>
{
if (!CanCheckForUpdate)
return false;
var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
// Cancels the last update and closes any existing notifications as stale.
using (var lastCts = Interlocked.Exchange(ref updateCancellationSource, cts))
await lastCts.CancelAsync().ConfigureAwait(false);
try
{
return await PerformUpdateCheck(cts.Token).ConfigureAwait(false);
}
catch (Exception e)
{
Logger.Log($"{nameof(PerformUpdateCheck)} failed ({e.Message})");
return true;
}
}, cancellationToken).ConfigureAwait(false);
/// <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(CancellationToken cancellationToken) => Task.FromResult(false);
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
updateCancellationSource.Cancel();
updateCancellationSource.Dispose();
}
public partial class UpdateDownloadProgressNotification : ProgressNotification
{
private readonly CancellationToken cancellationToken;
public UpdateDownloadProgressNotification(CancellationToken cancellationToken)
{
this.cancellationToken = cancellationToken;
}
[BackgroundDependencyLoader]
private void load()
{
IconContent.AddRange(new Drawable[]
{
new SpriteIcon
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Icon = FontAwesome.Solid.Download,
Size = new Vector2(34),
Colour = OsuColour.Gray(0.2f),
Depth = float.MaxValue,
}
});
}
protected override void Update()
{
base.Update();
if (cancellationToken.IsCancellationRequested)
FailDownload();
}
public void StartDownload()
{
State = ProgressNotificationState.Active;
Progress = 0;
Text = NotificationsStrings.DownloadingUpdate;
}
public void FailDownload()
{
State = ProgressNotificationState.Cancelled;
Close(false);
}
protected override Notification CreateCompletionNotification() => new UpdateReadyNotification(cancellationToken)
{
Activated = () =>
{
if (cancellationToken.IsCancellationRequested)
return true;
return CompletionClickAction?.Invoke() ?? true;
}
};
}
public partial class UpdateReadyNotification : ProgressCompletionNotification
{
private readonly CancellationToken cancellationToken;
public UpdateReadyNotification(CancellationToken cancellationToken)
{
this.cancellationToken = cancellationToken;
Text = NotificationsStrings.UpdateReadyToInstall;
}
protected override void Update()
{
base.Update();
if (cancellationToken.IsCancellationRequested)
Close(false);
}
}
public partial class UpdateAvailableNotification : SimpleNotification
{
private readonly CancellationToken cancellationToken;
public UpdateAvailableNotification(CancellationToken cancellationToken)
{
this.cancellationToken = cancellationToken;
Icon = FontAwesome.Solid.Download;
}
protected override void Update()
{
base.Update();
if (cancellationToken.IsCancellationRequested)
Close(false);
}
}
}
public partial class UpdateCompleteNotification : SimpleNotification
{
private readonly string version;
public UpdateCompleteNotification(string version)
{
this.version = version;
Text = NotificationsStrings.GameVersionAfterUpdate(version);
}
[BackgroundDependencyLoader]
private void load(OsuColour colours, ChangelogOverlay changelog, INotificationOverlay notificationOverlay)
{
Icon = FontAwesome.Solid.CheckSquare;
IconContent.Colour = colours.BlueDark;
Activated = delegate
{
notificationOverlay.Hide();
changelog.ShowBuild(version);
return true;
};
}
}
}