-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathProgram.cs
More file actions
499 lines (414 loc) · 23.7 KB
/
Program.cs
File metadata and controls
499 lines (414 loc) · 23.7 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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
using Hi3Helper.Sophon.Helper;
using Hi3Helper.Sophon.Structs;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
#if NET6_0_OR_GREATER
using System.Net;
#endif
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
using Hi3Helper.Sophon;
using System.Linq;
// ReSharper disable IdentifierTypo
namespace SophonUpdatePreload
{
public class MainApp
{
private static string _cancelMessage = "";
private static bool _isRetry;
private static readonly string[] SizeSuffixes = new string[] { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
private static int UsageHelp()
{
string executableName = Process.GetCurrentProcess().ProcessName + ".exe";
Console.WriteLine($"{executableName} [Preload/Update] [Sophon Build URL From] [Sophon Build URL To] [Matching field name (usually, you can set \"game\" as the value)] [Old Directory Path] [New Directory Path] [OPTIONAL: Amount of threads to be used (Default: {Environment.ProcessorCount})] [OPTIONAL: Amount of max. connection used for Http Client (Default: 128)]");
Console.WriteLine($"{executableName} [PreloadPatch/UpdatePatch] [Sophon Patch URL] [ScatteredFiles URL] [Matching field name (usually, you can set \"game\" as the value)] [Version to update from (For example: \"5.4.0\"] [Old Directory Path] [New Directory Path] [OPTIONAL: Amount of threads to be used (Default: {Environment.ProcessorCount})] [OPTIONAL: Amount of max. connection used for Http Client (Default: 128)]");
return 1;
}
public static async Task<int> Main(params string[] args)
{
int threads = Environment.ProcessorCount;
int maxHttpHandle = 128;
bool isPreloadMode;
_cancelMessage = "[\"C\"] Stop or [\"R\"] Restart";
if (args.Length != 0 && args[0].EndsWith("Patch", StringComparison.OrdinalIgnoreCase))
return await RunPatchMode(args);
if (args.Length < 6)
return UsageHelp();
if (!((isPreloadMode = args[0].Equals("Preload", StringComparison.OrdinalIgnoreCase)) ||
args[0].Equals("Update", StringComparison.OrdinalIgnoreCase) ||
args[0].Equals("PreloadPatch", StringComparison.OrdinalIgnoreCase) ||
args[0].Equals("UpdatePatch", StringComparison.OrdinalIgnoreCase)))
return UsageHelp();
if (args.Length > 6 && int.TryParse(args[6], out threads))
Console.WriteLine($"Thread count has been set to: {threads} for downloading!");
if (args.Length > 7 && int.TryParse(args[7], out maxHttpHandle))
Console.WriteLine($"HTTP Client maximum connection has been set to: {maxHttpHandle} handles!");
string outputDir = args[4];
Logger.LogHandler += Logger_LogHandler;
StartDownload:
using (CancellationTokenSource tokenSource = new CancellationTokenSource())
{
using (HttpClientHandler httpHandler = new HttpClientHandler
{
MaxConnectionsPerServer = maxHttpHandle
})
using (HttpClient httpClient = new HttpClient(httpHandler)
{
#if NET6_0_OR_GREATER
DefaultRequestVersion = HttpVersion.Version30,
DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrLower
#endif
})
{
List<SophonAsset> sophonAssets = new List<SophonAsset>();
string outputNewDir = args[5];
SophonChunkManifestInfoPair manifestPairFrom = await SophonManifest.CreateSophonChunkManifestInfoPair(httpClient, args[1], args[3], tokenSource.Token);
SophonChunkManifestInfoPair manifestPairTo = await SophonManifest.CreateSophonChunkManifestInfoPair(httpClient, args[2], args[3], tokenSource.Token);
await foreach (SophonAsset asset in SophonUpdate.EnumerateUpdateAsync(
httpClient,
manifestPairFrom,
manifestPairTo,
true,
null,
tokenSource.Token))
{
sophonAssets.Add(asset);
}
long currentRead = 0;
Task.Run(() => AppExitKeyTrigger(tokenSource));
ParallelOptions parallelOptions =
new ParallelOptions
{
CancellationToken = tokenSource.Token,
MaxDegreeOfParallelism = threads
};
Stopwatch stopwatch = Stopwatch.StartNew();
try
{
string chunkOutPath = Path.Combine(outputDir, "chunk_collapse");
if (!Directory.Exists(chunkOutPath))
Directory.CreateDirectory(chunkOutPath);
if (!Directory.Exists(outputNewDir))
Directory.CreateDirectory(outputNewDir);
long totalSizeDiff = sophonAssets.GetCalculatedDiffSize(!isPreloadMode);
string totalSizeDiffUnit = SummarizeSizeSimple(totalSizeDiff);
string totalSizeUnit = SummarizeSizeSimple(manifestPairTo.ChunksInfo.TotalSize);
foreach (string fileTemp in Directory.EnumerateFiles(outputDir, "*_tempUpdate", SearchOption.AllDirectories))
{
File.Delete(fileTemp);
}
_isRetry = false;
ActionBlock<Tuple<SophonAsset, HttpClient>> downloadTaskQueues = new(
async ctx =>
{
SophonAsset asset = ctx.Item1;
HttpClient client = ctx.Item2;
if (asset.IsDirectory)
return;
string outputAssetPath = Path.Combine(outputDir, asset.AssetName);
string outputAssetDir = Path.GetDirectoryName(outputAssetPath);
if (!string.IsNullOrEmpty(outputAssetDir))
Directory.CreateDirectory(outputAssetDir);
if (isPreloadMode)
{
await asset.DownloadDiffChunksAsync(
client,
chunkOutPath,
parallelOptions,
read =>
{
Interlocked.Add(ref currentRead, read);
string sizeUnit = SummarizeSizeSimple(currentRead);
string speedUnit = SummarizeSizeSimple(currentRead / stopwatch.Elapsed.TotalSeconds);
Console.Write($"{_cancelMessage} | {sizeUnit}/{totalSizeUnit} ({totalSizeDiffUnit} diff) -> {currentRead} ({speedUnit}/s) \r");
});
}
else
{
await asset.WriteUpdateAsync(
client,
outputDir,
outputNewDir,
chunkOutPath,
false,
parallelOptions,
read =>
{
Interlocked.Add(ref currentRead, read);
string sizeUnit = SummarizeSizeSimple(currentRead);
string speedUnit = SummarizeSizeSimple(currentRead / stopwatch.Elapsed.TotalSeconds);
Console.Write($"{_cancelMessage} | {sizeUnit}/{totalSizeUnit} ({totalSizeDiffUnit} diff) -> {currentRead} ({speedUnit}/s) \r");
},
(_, _) =>
{
Console.WriteLine($"Downloaded: {asset.AssetName}");
});
}
},
new ExecutionDataflowBlockOptions
{
CancellationToken = tokenSource.Token,
MaxDegreeOfParallelism = threads,
MaxMessagesPerTask = threads
});
foreach (SophonAsset asset in sophonAssets)
{
await downloadTaskQueues.SendAsync(new Tuple<SophonAsset, HttpClient>(asset, httpClient), tokenSource.Token);
}
downloadTaskQueues.Complete();
await downloadTaskQueues.Completion;
}
catch (OperationCanceledException)
{
Console.WriteLine("Download has been cancelled!");
}
finally
{
stopwatch.Stop();
}
}
}
if (_isRetry)
goto StartDownload;
return 0;
}
private static string[] ParseMultipleMatchingFields(string matchingFieldSource)
{
if (string.IsNullOrEmpty(matchingFieldSource))
{
throw new ArgumentNullException(nameof(matchingFieldSource), "Matching field cannot be left undefined!");
}
// ReSharper disable once UseIndexFromEndExpression
if (matchingFieldSource[0] == '[' && matchingFieldSource[matchingFieldSource.Length - 1] == ']')
{
return ParseMultipleMatchingFieldsInner(matchingFieldSource);
}
return new[] { matchingFieldSource };
}
private static string[] ParseMultipleMatchingFieldsInner(string matchingFieldSource)
{
matchingFieldSource = matchingFieldSource.TrimStart('[').TrimEnd(']');
return matchingFieldSource.Split(new[] { ',', ';', ' ' }, StringSplitOptions.RemoveEmptyEntries);
}
private static async Task<int> RunPatchMode(string[] args)
{
int threads = Environment.ProcessorCount;
int maxHttpHandle = 128;
if (args.Length > 7 && int.TryParse(args[7], out threads))
Console.WriteLine($"Thread count has been set to: {threads} for downloading!");
if (args.Length > 8 && int.TryParse(args[8], out maxHttpHandle))
Console.WriteLine($"HTTP Client maximum connection has been set to: {maxHttpHandle} handles!");
if (args.Length < 7)
return UsageHelp();
string oldDir = args[5];
string newDir = args[6];
string patchesDir = Path.Combine(oldDir, "chunk_collapse");
string sophonPatchUrl = args[1];
string scatteredFilesUrl = args[2];
string sophonMatchingField = args[3];
string sophonVersionUpdateFrom = args[4];
Logger.LogHandler += Logger_LogHandler;
bool isPreloadMode = args[0].Equals("PreloadPatch", StringComparison.OrdinalIgnoreCase);
StartDownload:
using (CancellationTokenSource tokenSource = new CancellationTokenSource())
using (HttpClientHandler httpHandler = new HttpClientHandler())
{
httpHandler.MaxConnectionsPerServer = maxHttpHandle;
using (HttpClient httpClient = new HttpClient(httpHandler)
{
Timeout = TimeSpan.FromSeconds(5)
})
{
long currentRead = 0;
_ = Task.Run(() => AppExitKeyTrigger(tokenSource));
string[] tags = ParseMultipleMatchingFields(sophonMatchingField);
long totalRemoteCompressedSize = 0;
List<SophonPatchAsset> patchAssetList = new();
foreach (string matchingField in tags)
{
SophonChunkManifestInfoPair patchInfoPair = await SophonPatch.CreateSophonChunkManifestInfoPair(httpClient, sophonPatchUrl, sophonVersionUpdateFrom, matchingField, tokenSource.Token);
if (!patchInfoPair.IsFound)
{
Console.Error.WriteLine($"An error has occurred! -> ({patchInfoPair.ReturnCode}) {patchInfoPair.ReturnMessage}");
return patchInfoPair.ReturnCode;
}
string baseConsoleTitle = $"SophonUpdatePreload - {(isPreloadMode ? "Preloading" : "Updating")}: {sophonVersionUpdateFrom} -> {patchInfoPair.OtherSophonPatchData.TagName} for MatchingFields: [{string.Join(", ", tags)}]";
Console.Title = baseConsoleTitle;
totalRemoteCompressedSize += patchInfoPair.ChunksInfo.TotalCompressedSize;
await foreach (SophonPatchAsset patchAsset in SophonPatch.EnumerateUpdateAsync(httpClient,
patchInfoPair,
sophonVersionUpdateFrom,
scatteredFilesUrl,
null,
tokenSource.Token))
{
patchAssetList.Add(patchAsset);
}
}
long totalAssetSize = patchAssetList
.Where(x => x.PatchMethod != SophonPatchMethod.Remove)
.Sum(x => x.TargetFileSize);
long totalAssetPatchSize = patchAssetList
.Where(x => x.PatchMethod != SophonPatchMethod.Remove && x.PatchMethod != SophonPatchMethod.DownloadOver)
.Sum(x => x.PatchChunkLength);
string totalAssetSizeSizeUnit = SummarizeSizeSimple(totalAssetSize);
string totalAssetPatchSizeUnit = SummarizeSizeSimple(totalAssetPatchSize);
Debug.Assert(totalAssetPatchSize == totalRemoteCompressedSize);
Stopwatch stopwatch = Stopwatch.StartNew();
_isRetry = false;
try
{
ExecutionDataflowBlockOptions dataflowBlockOpt = new ExecutionDataflowBlockOptions
{
CancellationToken = tokenSource.Token,
MaxDegreeOfParallelism = threads,
MaxMessagesPerTask = threads,
BoundedCapacity = threads
};
long currentDownloaded = 0;
long lastDownloaded = 0;
long scLastTick = Environment.TickCount;
long scLastReceivedBytes = 0;
double scLastSpeed = 0;
ActionBlock<ValueTuple<SophonPatchAsset, HttpClient, CancellationToken>> downloadTaskQueues = new(
async ctx =>
{
SophonPatchAsset asset = ctx.Item1;
HttpClient client = ctx.Item2;
CancellationToken token = ctx.Item3;
await asset.DownloadPatchAsync(client,
patchesDir,
true,
downloadRead =>
{
Interlocked.Add(ref currentRead, downloadRead);
string sizeUnit = SummarizeSizeSimple(currentRead);
string speedUnit = SummarizeSizeSimple(CalculateSpeed(downloadRead));
Console.Write($"{_cancelMessage} | {sizeUnit}/{totalAssetPatchSizeUnit} -> {currentRead} (Download: {speedUnit}/s) \r");
},
null,
token);
}, dataflowBlockOpt);
object currentLock = new();
ActionBlock<ValueTuple<SophonPatchAsset, HttpClient, CancellationToken>> patchTaskQueues = new(
async ctx =>
{
SophonPatchAsset asset = ctx.Item1;
HttpClient client = ctx.Item2;
CancellationToken token = ctx.Item3;
await asset.ApplyPatchUpdateAsync(client,
oldDir,
patchesDir,
true,
downloadRead =>
{
lock (currentLock)
{
Interlocked.Add(ref currentDownloaded, downloadRead);
}
},
diskWrite =>
{
Interlocked.Add(ref currentRead, diskWrite);
string sizeUnit = SummarizeSizeSimple(currentRead);
string speedUnit = SummarizeSizeSimple(CalculateSpeed(diskWrite));
lock (currentLock)
{
long downloadRead = currentDownloaded - lastDownloaded;
string speedUnitDownload = SummarizeSizeSimple(CalculateSpeed(downloadRead, ref scLastSpeed, ref scLastReceivedBytes, ref scLastTick));
Console.Write($"{_cancelMessage} | {sizeUnit}/{totalAssetSizeSizeUnit} -> {currentRead} (DiskWrite: {speedUnit}/s) (Download: {speedUnitDownload}/s) \r");
lastDownloaded = currentDownloaded;
}
},
null,
token);
}, dataflowBlockOpt);
foreach (SophonPatchAsset asset in patchAssetList.EnsureOnlyGetDedupPatchAssets())
{
await downloadTaskQueues.SendAsync(new ValueTuple<SophonPatchAsset, HttpClient, CancellationToken>(asset, httpClient, tokenSource.Token), tokenSource.Token);
}
downloadTaskQueues.Complete();
await downloadTaskQueues.Completion;
if (!isPreloadMode)
{
currentRead = 0;
foreach (SophonPatchAsset asset in patchAssetList)
{
await patchTaskQueues.SendAsync(new ValueTuple<SophonPatchAsset, HttpClient, CancellationToken>(asset, httpClient, tokenSource.Token), tokenSource.Token);
}
patchTaskQueues.Complete();
await patchTaskQueues.Completion;
patchAssetList.RemovePatches(patchesDir);
}
}
catch (OperationCanceledException)
{
Console.WriteLine("Download has been cancelled!");
}
finally
{
stopwatch.Stop();
}
}
}
if (_isRetry)
goto StartDownload;
return 0;
}
private const double ScOneSecond = 1000;
private static long _scLastTick = Environment.TickCount;
private static long _scLastReceivedBytes;
private static double _scLastSpeed;
protected static double CalculateSpeed(long receivedBytes) => CalculateSpeed(receivedBytes, ref _scLastSpeed, ref _scLastReceivedBytes, ref _scLastTick);
protected static double CalculateSpeed(long receivedBytes, ref double lastSpeedToUse, ref long lastReceivedBytesToUse, ref long lastTickToUse)
{
long currentTick = Environment.TickCount - lastTickToUse + 1;
long totalReceivedInSecond = Interlocked.Add(ref lastReceivedBytesToUse, receivedBytes);
double speed = totalReceivedInSecond * ScOneSecond / currentTick;
if (!(currentTick > ScOneSecond))
{
return lastSpeedToUse;
}
lastSpeedToUse = speed;
_ = Interlocked.Exchange(ref lastSpeedToUse, speed);
_ = Interlocked.Exchange(ref lastReceivedBytesToUse, 0);
_ = Interlocked.Exchange(ref lastTickToUse, Environment.TickCount);
return lastSpeedToUse;
}
private static string SummarizeSizeSimple(double value, int decimalPlaces = 2)
{
byte mag = (byte)Math.Log(value, 1000);
return $"{Math.Round(value / (1L << (mag * 10)), decimalPlaces)} {SizeSuffixes[mag]}";
}
private static void AppExitKeyTrigger(CancellationTokenSource tokenSource)
{
while (true)
{
ConsoleKeyInfo keyInfo = Console.ReadKey();
switch (keyInfo.Key)
{
case ConsoleKey.C:
_cancelMessage = "Cancelling download...";
tokenSource.Cancel();
return;
case ConsoleKey.R:
_isRetry = true;
_cancelMessage = "Retrying download...";
tokenSource.Cancel();
return;
}
}
}
private static void Logger_LogHandler(object sender, LogStruct e)
{
#if !DEBUG
if (e.LogLevel == LogLevel.Debug) return;
#endif
Console.WriteLine($"[{e.LogLevel}] {e.Message}");
}
}
}