-
Notifications
You must be signed in to change notification settings - Fork 7
/
Updater.cs
414 lines (347 loc) · 15.2 KB
/
Updater.cs
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace TribesLauncherSharp
{
sealed class RemoteObjectManager
{
private static RemoteObjectManager _instance;
public static RemoteObjectManager Instance { get
{
if (_instance is null)
{
_instance = new RemoteObjectManager();
}
return _instance;
}
}
public event EventHandler<DownloadProgressChangedEventArgs> DownloadProgressChanged;
private static readonly string baseUrl = "https://client.update.tamods.org";
private readonly WebClient webClient = new WebClient();
public RemoteObjectManager()
{
webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
}
public string DownloadObjectAsString(string key)
{
return webClient.DownloadString($"{baseUrl}/{key}");
}
public async Task DownloadObjectToFile(string key, string filePath)
{
// Probably could put this elsewhere but good enough
webClient.Proxy = GlobalProxySelection.GetEmptyWebProxy();
await webClient.DownloadFileTaskAsync($"{baseUrl}/{key}", filePath);
}
private void WebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
DownloadProgressChanged?.Invoke(sender, e);
}
}
class Updater
{
private SemaphoreSlim updateSemaphore;
public Config.DebugConfig Debug;
public string ConfigBasePath { get; set; }
public Updater(string configBasePath, Config.DebugConfig debugConfig)
{
ConfigBasePath = configBasePath;
updateSemaphore = new SemaphoreSlim(1, 1);
Debug = debugConfig;
}
public event EventHandler OnUpdateComplete;
public class OnProgressTickEventArgs : EventArgs
{
public double Proportion { get; set; }
public OnProgressTickEventArgs(double proportion)
{
Proportion = proportion;
}
}
public event EventHandler<OnProgressTickEventArgs> OnProgressTick;
public enum UpdatePhase
{
NotUpdating,
Preparing,
Downloading,
Extracting,
Copying,
Finalising
}
public class OnUpdatePhaseChangeEventArgs : EventArgs
{
public UpdatePhase Phase { get; set; }
public OnUpdatePhaseChangeEventArgs(UpdatePhase phase)
{
Phase = phase;
}
}
public event EventHandler<OnUpdatePhaseChangeEventArgs> OnUpdatePhaseChange;
public static bool IsUriAccessible(Uri uri)
{
var result = true;
HttpWebResponse response = null;
var request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "HEAD";
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException)
{
result = false;
}
finally
{
if (response != null) response.Close();
}
return result;
}
private void UpdateInstalledPackageState(List<RemotePackage> justInstalled)
{
var state = InstalledPackageState.Load();
foreach (RemotePackage installed in justInstalled) {
state.MarkInstalled(installed.ToInstalledPackage());
}
state.Save();
}
private async Task InstallPackages(List<RemotePackage> toInstall, string tribesExePath)
{
try
{
BroadcastUpdatePhase(UpdatePhase.Preparing);
if (!tribesExePath.ToLower().EndsWith(".exe"))
{
throw new Exception($"Invalid tribes path {tribesExePath}");
}
// Exe is <basepath>/Binaries/Win32/TribesAscend.exe
// So the directory two levels up is the base path of the install
string tribesBasePath = Path.Combine(Path.GetDirectoryName(tribesExePath), "..", "..");
// Clear temp directory if it exists
if (Directory.Exists("./tmp"))
{
Directory.Delete($"./tmp", true);
}
Directory.CreateDirectory("./tmp");
double progressBarValue = 0;
BroadcastUpdatePhase(UpdatePhase.Downloading);
// Download compressed packages to temp folder
List<string> archives = await DownloadPackages(toInstall, ".", (percentage) =>
{
// Half the progress bar for download, then half for extract / copy
double pct = progressBarValue + 0.5 * percentage;
OnProgressTick?.Invoke(this, new OnProgressTickEventArgs(pct));
});
progressBarValue = 0.5;
BroadcastUpdatePhase(UpdatePhase.Extracting);
// Extract packages and delete the zips
await ExtractArchives(archives, (idx, totalZips) =>
{
// 25% of progress bar for extracts
double pct = progressBarValue + 0.25 * ((double)idx + 1) / totalZips;
OnProgressTick?.Invoke(this, new OnProgressTickEventArgs(pct));
});
progressBarValue = 0.75;
BroadcastUpdatePhase(UpdatePhase.Copying);
// For each package we downloaded, copy its files into the local dir / config dir / Tribes dir
await CopyDownloadedPackages("./tmp", ".", tribesBasePath, (idx, totalPackages) =>
{
// 25% of progress bar for package copy
double pct = progressBarValue + 0.25 * ((double)idx + 1) / totalPackages;
OnProgressTick?.Invoke(this, new OnProgressTickEventArgs(pct));
});
BroadcastUpdatePhase(UpdatePhase.Finalising);
// Save the new installed package manifest
UpdateInstalledPackageState(toInstall);
// Delete temp directory
Directory.Delete($"./tmp", true);
BroadcastUpdatePhase(UpdatePhase.NotUpdating);
// Raise finished event
OnUpdateComplete?.Invoke(this, new EventArgs());
} catch (Exception e)
{
// Reset update phase
BroadcastUpdatePhase(UpdatePhase.NotUpdating);
throw e;
}
}
private string generatePackageDownloadLocation(string localPath, RemotePackage package)
{
string packageFileName = package.ObjectKey.Substring("package/".Length);
return $"{localPath}/tmp/{packageFileName}";
}
private async Task<List<string>> DownloadPackages(List<RemotePackage> packages, string localPath, Action<double> packageDownloadProgressHandler)
{
var packageDownloadDetails = packages
.Select((p, i) =>
{
// Object keys are under package/
return new { Idx = i, ObjectKey = p.ObjectKey, DownloadLocation = generatePackageDownloadLocation(localPath, p) };
});
var downloadedArchives = new List<string>();
int completedPackages = 0;
// Event handler for download progress including progress of each package
EventHandler<DownloadProgressChangedEventArgs> downloadHandler = (object sender, DownloadProgressChangedEventArgs e) =>
{
double baseCompletion = ((double)completedPackages) / packageDownloadDetails.Count();
double nextBaseCompletion = ((double)completedPackages + 1) / packageDownloadDetails.Count();
double completion = baseCompletion + (((double)e.ProgressPercentage) / 100) * (nextBaseCompletion - baseCompletion);
packageDownloadProgressHandler?.Invoke(completion);
};
RemoteObjectManager.Instance.DownloadProgressChanged += downloadHandler;
foreach (var p in packageDownloadDetails)
{
await RemoteObjectManager.Instance.DownloadObjectToFile(p.ObjectKey, p.DownloadLocation);
downloadedArchives.Add(p.DownloadLocation);
completedPackages++;
}
RemoteObjectManager.Instance.DownloadProgressChanged -= downloadHandler;
return downloadedArchives;
}
private async Task ExtractArchives(List<string> toExtract, Action<int, int> onExtractComplete)
{
foreach (var a in toExtract.Select((a, i) => new { Idx = i, Archive = a }))
{
await Task.Run(() =>
{
var dest = $"{Path.GetDirectoryName(a.Archive)}/{Path.GetFileNameWithoutExtension(a.Archive)}";
Directory.CreateDirectory(dest);
ZipFile.ExtractToDirectory(a.Archive, dest);
File.Delete(a.Archive);
});
onExtractComplete(a.Idx, toExtract.Count);
}
}
private void CopyFile(string relativeFilename, string packageRoot, string localRoot, string gameBasePath)
{
string normalisedRelativeFilename = relativeFilename.Replace("/", "\\");
string copyLocation;
if (normalisedRelativeFilename.StartsWith("!CONFIG\\"))
{
copyLocation = $"{ConfigBasePath}\\{normalisedRelativeFilename.Replace("!CONFIG\\", "")}";
} else if (normalisedRelativeFilename.StartsWith("!TRIBESDIR\\"))
{
copyLocation = $"{gameBasePath}\\{normalisedRelativeFilename.Replace("!TRIBESDIR\\", "")}";
} else
{
copyLocation = $"{localRoot}\\{normalisedRelativeFilename}";
}
if (Debug.DisableCopyOnUpdate)
{
Console.WriteLine($"Copy disabled; would write file {normalisedRelativeFilename} to {copyLocation}");
} else
{
// Create directory if required
string dir = new FileInfo(copyLocation).Directory.FullName;
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
// Read-only more like whatever we damn well want
if (File.Exists(copyLocation)) {
File.SetAttributes(copyLocation, FileAttributes.Normal);
}
// Copy the file
File.Copy($"{packageRoot}\\{normalisedRelativeFilename}", copyLocation, true);
}
}
private async Task CopyDownloadedPackages(string tempRoot, string localRoot, string gameBasePath, Action<int, int> onPackageComplete)
{
var packageDirs = Directory.GetDirectories(tempRoot);
foreach (var p in packageDirs.Select((d, i) => new { Idx = i, Directory = d }))
{
var packageRoot = Path.GetFullPath(p.Directory);
// Recursively get every file in the current package
foreach (var file in Directory.EnumerateFiles(p.Directory, "*", SearchOption.AllDirectories))
{
await Task.Run(() =>
{
CopyFile(Path.GetFullPath(file).Remove(0, packageRoot.Length + 1), packageRoot, localRoot, gameBasePath);
});
}
onPackageComplete(p.Idx, packageDirs.Count());
}
}
private void BroadcastUpdatePhase(UpdatePhase phase)
{
OnUpdatePhaseChange?.Invoke(this, new OnUpdatePhaseChangeEventArgs(phase));
}
public async Task PerformUpdate(PackageState packageState, string tribesExePath)
{
// TribesExePath definitely shouldn't be passed in from the UI layer as a param like this
// but I'm retrofitting it and can't be fucked
// Only one update or install may occur at once, if a second is attempted it will do nothing
bool acquiredLock = await updateSemaphore.WaitAsync(0);
if (!acquiredLock) return;
try
{
List<RemotePackage> packages =
packageState.PackagesRequiringUpdate().Select((p) => p.Remote).ToList();
await InstallPackages(packages, tribesExePath);
}
finally
{
updateSemaphore.Release();
}
}
public async Task InstallNewPackage(PackageState packageState, LocalPackage package, string tribesExePath)
{
// Only one update or install may occur at once, if a second is attempted it will do nothing
bool acquiredLock = await updateSemaphore.WaitAsync(0);
if (!acquiredLock) return;
try
{
List<RemotePackage> packages = new List<RemotePackage>();
packages.Add(package.Remote);
// Ensure dependencies are also installed
packages.AddRange(packageState.GetPackageDependenciesForInstall(package).Select((p) => p.Remote));
await InstallPackages(packages, tribesExePath);
}
finally
{
updateSemaphore.Release();
}
}
public bool IsUpdateInProgress() => updateSemaphore.CurrentCount == 0;
#region Ubermenu Specific Handling
public bool ConfigUsesUbermenu()
{
if (!File.Exists($"{ConfigBasePath}/config.lua"))
{
return false;
}
var configLines = File.ReadAllLines($"{ConfigBasePath}/config.lua");
foreach (var line in configLines)
{
if (line.Trim() == "require(\"presets/ubermenu/preset\")" || line.Trim() == "require('presets/ubermenu/preset')")
{
return true;
}
}
return false;
}
public void SetupUbermenuPreset()
{
File.AppendAllLines($"{ConfigBasePath}/config.lua", new string[] {
"",
"require(\"presets/ubermenu/preset\")"
});
}
public void BackupUbermenuConfig()
{
if (!File.Exists($"{ConfigBasePath}/presets/ubermenu/config/config.lua")) return;
File.Copy($"{ConfigBasePath}/presets/ubermenu/config/config.lua", "ubermenu_config_backup.lua", true);
}
public void RestoreUbermenuConfig()
{
if (!File.Exists("ubermenu_config_backup.lua")) return;
File.Copy("ubermenu_config_backup.lua", $"{ConfigBasePath}/presets/ubermenu/config/config.lua", true);
File.Delete("ubermenu_config_backup.lua");
}
#endregion
}
}