Skip to content

Commit 374cdba

Browse files
committed
[HSR Game Repair] Fix missing URL on Audio and Video assets
1 parent 4fe65fc commit 374cdba

2 files changed

Lines changed: 74 additions & 11 deletions

File tree

CollapseLauncher/Classes/RepairManagement/StarRailV2/StarRailPersistentRefResult.GetPersistentFiles.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,13 @@ private static void AddAdditionalAssets<T>(
293293

294294
FilePropertiesRemote file = new()
295295
{
296-
RN = url,
297-
N = relPathInPersistent,
298-
S = asset.FileSize,
299-
CRCArray = asset.MD5Checksum,
300-
FT = StarRailRepairV2.DetermineFileTypeFromExtension(asset.Filename ?? ""),
301-
IsHasHashMark = isHashMarked
296+
RN = url,
297+
N = relPathInPersistent,
298+
S = asset.FileSize,
299+
CRCArray = asset.MD5Checksum,
300+
FT = StarRailRepairV2.DetermineFileTypeFromExtension(asset.Filename ?? ""),
301+
IsHasHashMark = isHashMarked,
302+
AssociatedObject = asset
302303
};
303304
fileDic.TryAdd(relPathInPersistent, file);
304305
fileList.Add(file);

CollapseLauncher/Classes/RepairManagement/StarRailV2/StarRailPersistentRefResult.cs

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
using System;
1313
using System.Collections.Generic;
1414
using System.IO;
15+
using System.Linq;
16+
using System.Net;
1517
using System.Net.Http;
1618
using System.Security.Cryptography;
1719
using System.Text.Json.Serialization;
@@ -375,6 +377,20 @@ await LoadMetadataFile<StarRailAssetJsonMetadata>(instance,
375377
aDirRawRes,
376378
token);
377379

380+
// Perform URL test & swap for Audio and Video
381+
await baseUrl.TestAndSwapUrlAsync(client,
382+
mainUrlAsbAlt.CombineURLFromString("AudioBlock"),
383+
() => metadataAudioV?.DataList.FirstOrDefault(x => x.Filename?.EndsWith(".pck", StringComparison.OrdinalIgnoreCase) ?? false),
384+
context => context.Audio,
385+
(context, replace) => context.Audio = replace,
386+
token);
387+
await baseUrl.TestAndSwapUrlAsync(client,
388+
mainUrlAsbAlt.CombineURLFromString("Video"),
389+
() => metadataVideoV?.DataList.FirstOrDefault(x => x.Filename?.EndsWith(".usm", StringComparison.OrdinalIgnoreCase) ?? false),
390+
context => context.Video,
391+
(context, replace) => context.Video = replace,
392+
token);
393+
378394
return new StarRailPersistentRefResult
379395
{
380396
BaseDirs = baseDirs,
@@ -599,19 +615,65 @@ public AssetBaseDirs() : this("", "", "", "", "", "", "")
599615
public class AssetBaseUrls
600616
{
601617
public required Dictionary<string, string> GatewayKvp { get; set; }
602-
public required string DesignData { get; set; }
618+
public required string DesignData { get; init; }
603619
public required string Archive { get; set; }
604620
public required string Audio { get; set; }
605621
public required string AsbBlock { get; set; }
606622
public required string AsbBlockPersistent { get; set; }
607-
public required string NativeData { get; set; }
623+
public required string NativeData { get; init; }
608624
public required string Video { get; set; }
609-
public required string RawRes { get; set; }
625+
public required string RawRes { get; init; }
610626

611-
public string? CacheLua { get; set; }
612-
public string? CacheIFix { get; set; }
627+
public string? CacheLua { get; init; }
628+
public string? CacheIFix { get; init; }
613629

614630
public void SwapAsbPersistentUrl() => (AsbBlock, AsbBlockPersistent) = (AsbBlockPersistent, AsbBlock);
631+
632+
public async Task TestAndSwapUrlAsync(
633+
HttpClient client,
634+
string urlBaseAlt,
635+
Func<StarRailAssetGenericFileInfo?> testAssetSelector,
636+
Func<AssetBaseUrls, string> propertySelector,
637+
Action<AssetBaseUrls, string> propertyModifier,
638+
CancellationToken token)
639+
{
640+
if (testAssetSelector() is not {} firstAssetToTest ||
641+
firstAssetToTest.Filename == null)
642+
{
643+
return;
644+
}
645+
646+
bool isRetry = false;
647+
string rootUrlToTest = propertySelector(this);
648+
649+
Test:
650+
string testUrl = rootUrlToTest.CombineURLFromString(firstAssetToTest.Filename);
651+
UrlStatus testUrlStatus = await client.GetCachedUrlStatus(testUrl, token);
652+
653+
switch (testUrlStatus.IsSuccessStatusCode)
654+
{
655+
// If status is success and retry is requested, then swap the Audio property
656+
case true when isRetry:
657+
propertyModifier(this, urlBaseAlt);
658+
return;
659+
// If status still fails and retry is requested, throw.
660+
case false when isRetry:
661+
throw new
662+
HttpRequestException($"Both Base URLs: {Audio} or {urlBaseAlt} doesn't contain a correct location of the audio files.",
663+
null,
664+
testUrlStatus.StatusCode);
665+
}
666+
667+
// If status is 404 (Not Found), start swapping the URL
668+
if (testUrlStatus is { IsSuccessStatusCode: false, StatusCode: HttpStatusCode.NotFound } && !isRetry)
669+
{
670+
isRetry = true;
671+
rootUrlToTest = urlBaseAlt;
672+
goto Test;
673+
}
674+
675+
// If status is already successful, do nothing.
676+
}
615677
}
616678

617679
public class AssetMetadata

0 commit comments

Comments
 (0)