-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSophonPatchAsset.Update.cs
More file actions
778 lines (694 loc) · 31.5 KB
/
SophonPatchAsset.Update.cs
File metadata and controls
778 lines (694 loc) · 31.5 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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
using Hi3Helper.Sophon.Helper;
using Hi3Helper.Sophon.Structs;
using SharpHDiffPatch.Core;
using System;
using System.Buffers;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
// ReSharper disable IdentifierTypo
// ReSharper disable CommentTypo
// ReSharper disable AccessToModifiedClosure
#nullable enable
namespace Hi3Helper.Sophon
{
public partial class SophonPatchAsset
{
private static ReadOnlySpan<byte> HDiffPatchMagic => "HDIFF"u8;
private const string BlankFileMd5Hash = "d41d8cd98f00b204e9800998ecf8427e";
private const string BlankFileDiffExt = ".diff_ref";
public async Task ApplyPatchUpdateAsync(HttpClient client,
string inputDir,
string patchOutputDir,
bool removeOldAssets = true,
Action<long>? downloadReadDelegate = null,
Action<long>? diskWriteDelegate = null,
SophonDownloadSpeedLimiter? downloadSpeedLimiter = null,
CancellationToken token = default)
{
int retry = 5;
StartOver:
bool isRemove = SophonPatchMethod.Remove == PatchMethod;
bool isCopyOver = SophonPatchMethod.CopyOver == PatchMethod;
bool isPatchHDiff = SophonPatchMethod.Patch == PatchMethod;
string sourceFileNameToCheck = PatchMethod switch
{
SophonPatchMethod.Remove => OriginalFilePath,
SophonPatchMethod.DownloadOver => TargetFilePath,
SophonPatchMethod.Patch => OriginalFilePath,
SophonPatchMethod.CopyOver => TargetFilePath,
_ => throw new InvalidOperationException($"Unsupported patch method: {PatchMethod}")
};
string sourceFilePathToCheck = Path.Combine(inputDir, sourceFileNameToCheck);
if (isRemove)
{
if (!removeOldAssets)
{
return;
}
FileInfo removableAssetFileInfo = sourceFilePathToCheck.CreateFileInfo();
PerformPatchAssetRemove(removableAssetFileInfo);
return;
}
if (PatchMethod is SophonPatchMethod.DownloadOver or
SophonPatchMethod.CopyOver or
SophonPatchMethod.Patch &&
await IsFilePatched(inputDir, token))
{
diskWriteDelegate?.Invoke(TargetFileSize);
return;
}
if (!isCopyOver)
{
string sourceFileHashString = PatchMethod switch
{
SophonPatchMethod.Remove => OriginalFileHash,
SophonPatchMethod.DownloadOver => TargetFileHash,
SophonPatchMethod.Patch => OriginalFileHash,
_ => throw new InvalidOperationException($"Unsupported patch method: {PatchMethod}")
};
long sourceFileSizeToCheck = PatchMethod switch
{
SophonPatchMethod.Remove => OriginalFileSize,
SophonPatchMethod.DownloadOver => TargetFileSize,
SophonPatchMethod.Patch => OriginalFileSize,
_ => throw new InvalidOperationException($"Unsupported patch method: {PatchMethod}")
};
SophonChunk sourceFileToCheckAsChunk = new()
{
ChunkHashDecompressed = Extension.HexToBytes(sourceFileHashString.AsSpan()),
ChunkName = sourceFileNameToCheck,
ChunkOffset = 0,
ChunkOldOffset = 0,
ChunkSize = sourceFileSizeToCheck,
ChunkSizeDecompressed = sourceFileSizeToCheck
};
FileInfo sourceFileInfoToCheck = sourceFilePathToCheck.CreateFileInfo();
bool isSourceFileExist = sourceFileInfoToCheck.Exists;
// Check for the original file existence and length
bool isNeedCompleteDownload = !(sourceFileInfoToCheck is { Exists: true } &&
sourceFileInfoToCheck.Length == sourceFileSizeToCheck);
FileStream? sourceFileStreamToCheck = null;
try
{
// If the length check is passed, try check the hash of the file and compare it
if (!isNeedCompleteDownload)
{
// Open the stream, read it and check for the original file hash
sourceFileStreamToCheck = sourceFileInfoToCheck
.Open(new FileStreamOptions
{
Mode = isSourceFileExist ? FileMode.Open : FileMode.Create,
Access = FileAccess.Read,
Share = FileShare.Read, // Lock to only share read access
// Use RandomAccess due to repeated access from other asset activity, so the OS
// can perform better read optimization if seeking is rapidly happening.
Options = FileOptions.RandomAccess,
BufferSize = sourceFileToCheckAsChunk.ChunkSizeDecompressed.GetFileStreamBufferSize()
});
isNeedCompleteDownload = !(sourceFileToCheckAsChunk.ChunkHashDecompressed.Length != 8 ?
await sourceFileToCheckAsChunk.CheckChunkMd5HashAsync(sourceFileStreamToCheck,
true,
token) :
await sourceFileToCheckAsChunk.CheckChunkXxh64HashAsync(sourceFileStreamToCheck,
sourceFileToCheckAsChunk.ChunkHashDecompressed,
true,
token));
// If it needs a complete download due to unmatched original file hash, then remove the file
if (isNeedCompleteDownload)
{
#if NET6_0_OR_GREATER
await sourceFileStreamToCheck.DisposeAsync();
#else
sourceFileStreamToCheck.Dispose();
#endif
PerformPatchAssetRemove(sourceFileInfoToCheck);
}
else
{
if (!isPatchHDiff)
{
diskWriteDelegate?.Invoke(TargetFileSize);
return;
}
}
}
// If the original file needs a download, then perform a DownloadOver patch method.
if (isNeedCompleteDownload)
{
PatchMethod = SophonPatchMethod.DownloadOver;
}
}
finally
{
#if NET6_0_OR_GREATER
if (sourceFileStreamToCheck != null)
{
await sourceFileStreamToCheck.DisposeAsync();
}
#else
sourceFileStreamToCheck?.Dispose();
#endif
}
}
Task<bool> writeDelegateTask = PatchMethod switch
{
SophonPatchMethod.DownloadOver => PerformPatchDownloadOver(client,
inputDir,
downloadReadDelegate,
diskWriteDelegate,
downloadSpeedLimiter,
token),
SophonPatchMethod.CopyOver => PerformPatchCopyOver(inputDir,
patchOutputDir,
diskWriteDelegate,
token),
SophonPatchMethod.Patch => PerformPatchHDiff(inputDir,
patchOutputDir,
diskWriteDelegate,
token),
_ => throw new InvalidOperationException($"Invalid operation while performing patch: {PatchMethod}")
};
try
{
bool isRedirect = !await writeDelegateTask;
if (isRedirect)
{
goto StartOver;
}
}
catch (OperationCanceledException) when (token.IsCancellationRequested)
{
throw;
}
catch (Exception ex)
{
if (retry < 0)
{
throw;
}
this.PushLogError("An error has occurred while performing patch with method:" +
$" {PatchMethod} for asset: {TargetFilePath}. Retry attempt left: {retry}\r\n" +
$"Source file: {OriginalFilePath}\r\nPatch file: {PatchNameSource}\r\nException: {ex}");
// Set patch method to DownloadOver and start over
PatchMethod = SophonPatchMethod.DownloadOver;
--retry;
goto StartOver;
}
}
private async Task<bool> PerformPatchDownloadOver(HttpClient client,
string inputDir,
Action<long>? downloadReadDelegate,
Action<long>? diskWriteDelegate,
SophonDownloadSpeedLimiter? downloadSpeedLimiter,
CancellationToken token)
{
bool isSuccess = false;
long writtenToDisk = 0;
long downloadedRead = 0;
// Get the FileInfo and Path instance of the target file.
string targetFilePath = Path.Combine(inputDir, TargetFilePath);
FileInfo targetFileInfo = targetFilePath.CreateFileInfo();
string targetFilePathTemp = targetFilePath + ".temp";
FileInfo targetFileInfoTemp = targetFilePathTemp.CreateFileInfo();
// If the target temporary file has already exists, try to unassign read-only before creating new stream.
if (targetFileInfoTemp.Exists)
{
targetFileInfoTemp.IsReadOnly = false;
}
// Create target temporary file stream.
targetFileInfoTemp.Directory?.Create();
FileStream targetFileStreamTemp = targetFileInfoTemp.Open(new FileStreamOptions
{
Mode = FileMode.Create,
Access = FileAccess.Write,
Share = FileShare.Write,
BufferSize = TargetFileSize.GetFileStreamBufferSize()
});
targetFileInfoTemp.Refresh();
try
{
// Download the file using Main Downloader
await MainAssetInfo
.WriteToStreamAsync(client,
targetFileStreamTemp,
writeInfoDelegate: x =>
{
diskWriteDelegate?.Invoke(x);
Interlocked.Add(ref writtenToDisk, x);
},
downloadInfoDelegate: (read, _) =>
{
downloadReadDelegate?.Invoke(read);
Interlocked.Add(ref downloadedRead, read);
},
token: token);
isSuccess = true;
return true;
}
catch
{
diskWriteDelegate?.Invoke(-writtenToDisk);
downloadReadDelegate?.Invoke(-downloadedRead);
throw;
}
finally
{
// Dispose the target temporary file stream and try to remove old target file (if exist)
#if NET6_0_OR_GREATER
await targetFileStreamTemp.DisposeAsync();
#else
targetFileStreamTemp.Dispose();
#endif
targetFileInfoTemp.Refresh();
if (isSuccess)
{
if (targetFileInfo.Exists)
{
targetFileInfo.IsReadOnly = false;
targetFileInfo.Refresh();
targetFileInfo.Delete();
}
// Then rename the temporary file to the actual file name
targetFileInfoTemp.MoveTo(targetFilePath);
}
else
{
if (targetFileInfoTemp.Exists)
{
targetFileInfoTemp.IsReadOnly = false;
targetFileInfoTemp.Delete();
}
}
}
}
private void PerformPatchAssetRemove(FileInfo originalFileInfo)
{
try
{
if (!originalFileInfo.Exists)
{
return;
}
originalFileInfo.IsReadOnly = false;
originalFileInfo.Refresh();
originalFileInfo.Delete();
this.PushLogDebug("[Method: Remove] Removing asset file:" +
$" {OriginalFilePath} is completed!");
}
catch (Exception ex)
{
this.PushLogError("An error has occurred while deleting old asset:" +
$" {originalFileInfo.FullName} | {ex}");
}
}
private async Task<bool> PerformPatchCopyOver(string inputDir,
string patchOutputDir,
Action<long>? diskWriteDelegate,
CancellationToken token)
{
bool isSuccess = false;
long writtenToDisk = 0;
PatchTargetProperty patchTargetProperty = PatchTargetProperty
.Create(patchOutputDir,
PatchChunkLength,
inputDir,
TargetFilePath,
TargetFileSize,
this,
true);
bool isUseCopyToStrategy =
#if NET6_0_OR_GREATER
PatchChunkLength <= 1 << 20
#else
false
#endif
;
string logMessage = $"[Method: CopyOver][Strategy: {(isUseCopyToStrategy ? "DirectCopyTo" : "BufferedCopy")}]" +
$" Writing target file: {TargetFilePath} with offset: {PatchOffset:x8}" +
$" and length: {PatchChunkLength:x8} from {PatchNameSource} is completed!";
try
{
if (patchTargetProperty.TargetFileTempStream == null)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(patchTargetProperty.TargetFileTempStream,
#else
throw new ArgumentNullException(
#endif
nameof(patchTargetProperty.TargetFileTempStream));
}
if (patchTargetProperty.PatchChunkStream == null)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(patchTargetProperty.PatchChunkStream,
#else
throw new ArgumentNullException(
#endif
nameof(patchTargetProperty.PatchChunkStream));
}
if (IsChunkActuallyHDiff(patchTargetProperty.PatchChunkStream))
{
patchTargetProperty.PatchChunkStream.Position = 0;
string originalFilePath = Path.Combine(inputDir, TargetFilePath + BlankFileDiffExt);
FileInfo fileInfo = new FileInfo(originalFilePath);
if (fileInfo.Exists)
{
fileInfo.IsReadOnly = false;
}
fileInfo.Directory?.Create();
#if NET6_0_OR_GREATER
await fileInfo.Open(FileMode.Create, FileAccess.Write, FileShare.Write).DisposeAsync();
#else
fileInfo.Open(FileMode.Create, FileAccess.Write, FileShare.Write).Dispose();
#endif
OriginalFilePath ??= originalFilePath;
OriginalFileHash ??= BlankFileMd5Hash;
OriginalFileSize = 0;
PatchMethod = SophonPatchMethod.Patch;
// Remove if there's a left-over patch file caused by post issue files
FileInfo leftFile = new FileInfo(Path.Combine(inputDir, TargetFilePath));
if (!leftFile.Exists)
{
return false;
}
bool isDeleteLeftFile;
#if NET6_0_OR_GREATER
await
#endif
using (FileStream leftFileStream = leftFile.Open(new FileStreamOptions
{
Mode = FileMode.Open,
Access = FileAccess.Read,
Share = FileShare.Read,
Options = FileOptions.RandomAccess,
BufferSize = leftFile.Length.GetFileStreamBufferSize()
}))
{
isDeleteLeftFile = IsChunkActuallyHDiff(leftFileStream);
}
if (isDeleteLeftFile)
{
leftFile.IsReadOnly = false;
leftFile.Delete();
}
return false;
}
#if NET6_0_OR_GREATER
if (isUseCopyToStrategy)
{
await patchTargetProperty.PatchChunkStream
.CopyToAsync(patchTargetProperty.TargetFileTempStream,
PatchChunkLength.GetFileStreamBufferSize(),
token);
diskWriteDelegate?.Invoke(PatchChunkLength);
Interlocked.Add(ref writtenToDisk, PatchChunkLength);
isSuccess = true;
return false;
}
#endif
byte[] buffer = ArrayPool<byte>.Shared.Rent(SophonAsset.BufferSize);
try
{
int read;
while ((read = await patchTargetProperty.PatchChunkStream.ReadAsync(
#if NET6_0_OR_GREATER
buffer, token
#else
buffer, 0, buffer.Length, token
#endif
)) > 0)
{
#if NET6_0_OR_GREATER
patchTargetProperty.TargetFileTempStream.Write(buffer.AsSpan(0, read));
#else
patchTargetProperty.TargetFileTempStream.Write(buffer, 0, read);
#endif
diskWriteDelegate?.Invoke(read);
Interlocked.Add(ref writtenToDisk, read);
}
isSuccess = true;
return true;
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
}
}
catch
{
diskWriteDelegate?.Invoke(-writtenToDisk);
throw;
}
finally
{
this.PushLogDebug(logMessage);
if (isSuccess)
patchTargetProperty.Dispose();
else
patchTargetProperty.DisposeAndDeleteTemp();
}
}
private async Task<bool> PerformPatchHDiff(string inputDir,
string patchOutputDir,
Action<long>? diskWriteDelegate,
CancellationToken token)
{
bool isSuccess = false;
long writtenToDisk = 0;
PatchTargetProperty patchTargetProperty = PatchTargetProperty
.Create(patchOutputDir,
PatchChunkLength,
inputDir,
TargetFilePath,
TargetFileSize,
this,
false);
string logMessage = $"[Method: PatchHDiff] Writing target file: {TargetFilePath}" +
$" with offset: {PatchOffset:x8} and length: {PatchChunkLength:x8}" +
$" from {PatchNameSource} is completed!";
FileInfo patchPath = patchTargetProperty.PatchFilePath;
string targetTempPath = patchTargetProperty.TargetFileTempInfo.FullName;
try
{
await Task.Factory
.StartNew(Impl,
token,
token)
.ConfigureAwait(false);
this.PushLogDebug(logMessage);
isSuccess = true;
}
finally
{
if (isSuccess)
patchTargetProperty.Dispose();
else
patchTargetProperty.DisposeAndDeleteTemp();
}
return true;
void Impl(object? ctx)
{
HDiffPatch patcher = new HDiffPatch();
string inputPath = Path.Combine(inputDir, OriginalFilePath);
try
{
patcher.Initialize(CreateChunkStream);
patcher.Patch(inputPath,
targetTempPath,
true,
x =>
{
diskWriteDelegate?.Invoke(x);
Interlocked.Add(ref writtenToDisk, x);
},
(CancellationToken)ctx!,
false,
true);
}
catch
{
diskWriteDelegate?.Invoke(-writtenToDisk);
throw;
}
finally
{
if (inputPath.EndsWith(BlankFileDiffExt, StringComparison.OrdinalIgnoreCase))
{
FileInfo fileInfo = new FileInfo(inputPath);
if (fileInfo.Exists)
{
fileInfo.IsReadOnly = false;
fileInfo.Delete();
}
}
}
}
ChunkStream CreateChunkStream()
{
FileStream fileStream = patchPath.Open(new FileStreamOptions
{
Mode = FileMode.Open,
Access = FileAccess.Read,
Share = FileShare.Read,
Options = FileOptions.SequentialScan
});
ChunkStream chunkStream = new ChunkStream(fileStream,
PatchOffset,
PatchOffset + PatchChunkLength,
true);
return chunkStream;
}
}
private async Task<bool> IsFilePatched(string inputPath, CancellationToken token)
{
string targetFilePath = Path.Combine(inputPath, TargetFilePath);
FileInfo targetFileInfo = targetFilePath.CreateFileInfo();
if (!targetFileInfo.Exists)
{
return false;
}
if (TargetFileSize != targetFileInfo.Length)
{
return false;
}
SophonChunk checkByHashChunk = new SophonChunk
{
ChunkHashDecompressed = Extension.HexToBytes(TargetFileHash.AsSpan()),
ChunkName = TargetFilePath,
ChunkSize = TargetFileSize,
ChunkSizeDecompressed = TargetFileSize,
ChunkOffset = 0,
ChunkOldOffset = 0,
};
#if NET6_0_OR_GREATER
await
#endif
using FileStream targetFileStream = targetFileInfo.Open(new FileStreamOptions
{
Mode = FileMode.Open,
Access = FileAccess.Read,
Share = FileShare.Read,
BufferSize = TargetFileSize.GetFileStreamBufferSize()
});
bool isHashMatched = checkByHashChunk.ChunkHashDecompressed.Length == 8 ?
await checkByHashChunk.CheckChunkXxh64HashAsync(targetFileStream,
checkByHashChunk.ChunkHashDecompressed,
true,
token) :
await checkByHashChunk.CheckChunkMd5HashAsync(targetFileStream,
true,
token);
return isHashMatched;
}
private static bool IsChunkActuallyHDiff(Stream chunkStream)
{
#if NET7_0_OR_GREATER
Span<byte> magicBuffer = stackalloc byte[HDiffPatchMagic.Length];
_ = chunkStream.ReadAtLeast(magicBuffer, magicBuffer.Length, false);
#else
byte[] magicBuffer = new byte[HDiffPatchMagic.Length];
_ = chunkStream.Read(magicBuffer, 0, magicBuffer.Length);
#endif
chunkStream.Position = 0;
return HDiffPatchMagic.SequenceEqual(magicBuffer);
}
}
internal class PatchTargetProperty : IDisposable
{
private FileInfo TargetFileInfo { get; }
public FileInfo TargetFileTempInfo { get; }
public FileStream? TargetFileTempStream { get; }
public FileInfo PatchFilePath { get; }
private FileStream? PatchFileStream { get; }
public ChunkStream? PatchChunkStream { get; }
private PatchTargetProperty(string patchOutputDir,
long patchChunkSize,
string inputDir,
string targetFilePath,
long targetFileSize,
SophonPatchAsset asset,
bool createStream)
{
PatchFilePath = asset.GetLegacyOrHoyoPlayPatchChunkPath(patchOutputDir);
targetFilePath = Path.Combine(inputDir, targetFilePath);
string targetFileTempPath = targetFilePath + ".temp";
TargetFileInfo = targetFilePath.CreateFileInfo();
TargetFileTempInfo = targetFileTempPath.CreateFileInfo();
TargetFileTempInfo.Directory?.Create();
if (TargetFileTempInfo.Exists)
{
TargetFileTempInfo.IsReadOnly = false;
TargetFileTempInfo.Refresh();
}
if (!PatchFilePath.Exists)
{
throw new FileNotFoundException($"Required patch file: {PatchFilePath} is not found!");
}
if (!createStream)
{
return;
}
long patchChunkStart = asset.PatchOffset;
long patchChunkEnd = patchChunkStart + asset.PatchChunkLength;
TargetFileTempStream = TargetFileTempInfo.Open(new FileStreamOptions
{
Mode = FileMode.Create,
Access = FileAccess.Write,
Share = FileShare.Write,
BufferSize = targetFileSize.GetFileStreamBufferSize()
});
PatchFileStream = PatchFilePath.Open(new FileStreamOptions
{
Mode = FileMode.Open,
Access = FileAccess.Read,
Share = FileShare.Read,
Options = FileOptions.RandomAccess,
BufferSize = patchChunkSize.GetFileStreamBufferSize()
});
PatchChunkStream = new ChunkStream(PatchFileStream, patchChunkStart, patchChunkEnd);
}
public static PatchTargetProperty Create(string patchOutputDir,
long patchChunkSize,
string inputDir,
string targetFilePath,
long targetFileSize,
SophonPatchAsset asset,
bool createTempStream)
=> new(patchOutputDir,
patchChunkSize,
inputDir,
targetFilePath,
targetFileSize,
asset,
createTempStream);
public void Flush()
{
PatchChunkStream?.Dispose();
PatchFileStream?.Dispose();
TargetFileTempStream?.Dispose();
TargetFileTempInfo.Refresh();
}
public void DisposeAndDeleteTemp()
{
Flush();
if (!TargetFileTempInfo.Exists)
{
return;
}
TargetFileTempInfo.IsReadOnly = false;
TargetFileTempInfo.Delete();
}
public void Dispose()
{
Flush();
if (TargetFileInfo.Exists)
{
TargetFileInfo.IsReadOnly = false;
TargetFileInfo.Delete();
}
TargetFileTempInfo.MoveTo(TargetFileInfo.FullName);
}
}
}