@@ -108,7 +108,7 @@ await SophonPatch.CreateSophonChunkManifestInfoPair(httpClient,
108108 SophonDownloadSpeedLimiter . CreateInstance ( LauncherConfig . DownloadSpeedLimitCached ) ;
109109
110110 // Get the patch assets to download
111- ( List < SophonPatchAsset > , List < SophonChunkManifestInfoPair > ) patchAssets =
111+ ( List < SophonPatchAsset > AssetList , List < SophonChunkManifestInfoPair > InfoPairs , bool IsAllowRemoveOldFile ) patchAssets =
112112 await GetAlterSophonPatchAssets ( httpClient ,
113113 branchResources . PatchUrl ,
114114 ( isPreloadMode ? branchResources . PreloadUrl : branchResources . MainUrl ) ?? "" ,
@@ -120,8 +120,9 @@ await GetAlterSophonPatchAssets(httpClient,
120120 // Start the patch pipeline
121121 await StartAlterSophonPatch ( httpClient ,
122122 isPreloadMode ,
123- patchAssets . Item1 ,
124- patchAssets . Item2 ,
123+ patchAssets . AssetList ,
124+ patchAssets . InfoPairs ,
125+ patchAssets . IsAllowRemoveOldFile ,
125126 downloadSpeedLimiter ,
126127 maxThread ,
127128 Token . Token ) ;
@@ -135,7 +136,7 @@ protected virtual async Task ConfirmAdditionalPatchDataPackageFiles(SophonChunkM
135136 {
136137 string currentVersion = GameVersion . ToString ( ) ;
137138
138- List < SophonManifestPatchIdentity > otherManifestIdentity = patchManifest . OtherSophonPatchData . ManifestIdentityList
139+ List < SophonManifestPatchIdentity > otherManifestIdentity = patchManifest . OtherSophonPatchData ! . ManifestIdentityList
139140 . Where ( x => ! CommonSophonPackageMatchingFields . Contains ( x . MatchingField , StringComparer . OrdinalIgnoreCase ) )
140141 . ToList ( ) ;
141142
@@ -303,7 +304,7 @@ protected async Task<bool> SpawnAdditionalPackageDownloadDialog(long baseDownloa
303304 return confirmAdditionalTag == ContentDialogResult . Primary ;
304305 }
305306
306- protected virtual async Task < ( List < SophonPatchAsset > , List < SophonChunkManifestInfoPair > ) >
307+ protected virtual async Task < ( List < SophonPatchAsset > AssetList , List < SophonChunkManifestInfoPair > InfoPairs , bool IsAllowRemoveOldFile ) >
307308 GetAlterSophonPatchAssets ( HttpClient httpClient ,
308309 string manifestUrl ,
309310 string downloadOverUrl ,
@@ -315,6 +316,7 @@ protected async Task<bool> SpawnAdditionalPackageDownloadDialog(long baseDownloa
315316 SophonChunkManifestInfoPair ? rootPatchManifest = null ;
316317 SophonChunkManifestInfoPair ? rootMainManifest = null ;
317318 List < ( SophonChunkManifestInfoPair Patch , SophonChunkManifestInfoPair Main , bool IsCommon ) > patchManifestList = [ ] ;
319+ bool isAlowRemoveOldFile = true ;
318320
319321 // Iterate matching fields and get the patch metadata
320322 foreach ( string matchingField in matchingFields )
@@ -338,8 +340,15 @@ protected async Task<bool> SpawnAdditionalPackageDownloadDialog(long baseDownloa
338340 Logger . LogWriteLine ( $ "Getting diff for matching field: { matchingField } ", LogType . Debug , true ) ;
339341
340342 // Get the manifest pair based on the matching field
341- SophonChunkManifestInfoPair patchManifest = rootPatchManifest
342- . GetOtherPatchInfoPair ( matchingField , updateVersionfrom ) ;
343+ if ( ! rootPatchManifest
344+ . TryGetOtherPatchInfoPair ( matchingField , updateVersionfrom , out var patchManifest ) )
345+ {
346+ Logger . LogWriteLine ( $ "[InstallManagerBase::GetAlterSophonPatchAssets] Cannot find past-version patch manifest for matching field: { matchingField } , Skipping!",
347+ LogType . Warning ,
348+ true ) ;
349+ isAlowRemoveOldFile = false ;
350+ continue ;
351+ }
343352
344353 // Get the main manifest pair based on the matching field
345354 SophonChunkManifestInfoPair mainManifest = rootMainManifest
@@ -364,18 +373,38 @@ protected async Task<bool> SpawnAdditionalPackageDownloadDialog(long baseDownloa
364373 {
365374 // Get the asset and add it to the list
366375 await foreach ( SophonPatchAsset patchAsset in SophonPatch
367- . EnumerateUpdateAsync ( httpClient ,
368- manifestPair . Patch ,
369- manifestPair . Main ,
370- updateVersionfrom ,
371- downloadLimiter ,
372- token ) )
376+ . EnumerateUpdateAsync ( httpClient ,
377+ manifestPair . Patch ,
378+ manifestPair . Main ,
379+ updateVersionfrom ,
380+ downloadLimiter ,
381+ token ) )
373382 {
374383 patchAssets . Add ( patchAsset ) ;
375384 }
376385 }
377386
378- return ( patchAssets , patchManifestList . Select ( x => x . Patch ) . ToList ( ) ) ;
387+ // Find the removable assets and compare with the added list.
388+ List < SophonPatchAsset > removableAssets = [ ] ;
389+ foreach ( var manifestPair in patchManifestList )
390+ {
391+ // Get the asset and add it to the list
392+ await foreach ( SophonPatchAsset patchAsset in SophonPatch
393+ . EnumerateRemovableAsync ( httpClient ,
394+ manifestPair . Patch ,
395+ manifestPair . Main ,
396+ updateVersionfrom ,
397+ patchAssets ,
398+ token ) )
399+ {
400+ removableAssets . Add ( patchAsset ) ;
401+ }
402+ }
403+
404+ // Add the removable list to patch assets.
405+ patchAssets . AddRange ( removableAssets ) ;
406+
407+ return ( patchAssets , patchManifestList . Select ( x => x . Patch ) . ToList ( ) , isAlowRemoveOldFile ) ;
379408 }
380409
381410 protected virtual async Task < List < string > > GetAlterSophonPatchVOMatchingFields ( CancellationToken token )
@@ -403,13 +432,14 @@ protected virtual async Task<List<string>> GetAlterSophonPatchVOMatchingFields(C
403432 return voAudioMatchingFields ;
404433 }
405434
406- protected virtual async Task StartAlterSophonPatch ( HttpClient httpClient ,
407- bool isPreloadMode ,
408- List < SophonPatchAsset > patchAssets ,
435+ protected virtual async Task StartAlterSophonPatch ( HttpClient httpClient ,
436+ bool isPreloadMode ,
437+ List < SophonPatchAsset > patchAssets ,
409438 List < SophonChunkManifestInfoPair > patchManifestInfoPairs ,
410- SophonDownloadSpeedLimiter downloadLimiter ,
411- int threadNum ,
412- CancellationToken token )
439+ bool isAllowRemoveOldFile ,
440+ SophonDownloadSpeedLimiter downloadLimiter ,
441+ int threadNum ,
442+ CancellationToken token )
413443 {
414444 Dictionary < string , int > downloadedPatchHashSet = new ( ) ;
415445 Lock dictionaryLock = new ( ) ;
@@ -435,7 +465,7 @@ protected virtual async Task StartAlterSophonPatch(HttpClient httpClient,
435465 parallelOptions . MaxDegreeOfParallelism = Environment . ProcessorCount ;
436466 }
437467
438- string patchOutputDir = _gameSophonChunkDir ;
468+ string patchOutputDir = Path . Combine ( GamePath , "ldiff" ) ;
439469
440470 // Get download sizes
441471 long downloadSizeTotalAssetRemote = patchAssets . Where ( x => x . PatchMethod != SophonPatchMethod . Remove ) . Sum ( x => x . TargetFileSize ) ;
@@ -555,7 +585,7 @@ async ValueTask ImplPatchUpdate(Tuple<SophonPatchAsset, Dictionary<string, int>>
555585 await patchAsset . ApplyPatchUpdateAsync ( httpClient ,
556586 GamePath ,
557587 patchOutputDir ,
558- true ,
588+ isAllowRemoveOldFile ,
559589 read =>
560590 {
561591 UpdateSophonFileDownloadProgress ( 0 , read ) ;
0 commit comments