Skip to content

Commit 1c079a1

Browse files
committed
[HSR Game Repair] Fix missing URL on Audio and Video assets
1 parent 3d82d5d commit 1c079a1

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
@@ -294,12 +294,13 @@ private static void AddAdditionalAssets<T>(
294294

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

CollapseLauncher/Classes/RepairManagement/StarRailV2/StarRailPersistentRefResult.cs

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using System;
1212
using System.Collections.Generic;
1313
using System.IO;
14+
using System.Linq;
15+
using System.Net;
1416
using System.Net.Http;
1517
using System.Security.Cryptography;
1618
using System.Text.Json.Serialization;
@@ -374,6 +376,20 @@ await LoadMetadataFile<StarRailAssetJsonMetadata>(instance,
374376
aDirRawRes,
375377
token);
376378

379+
// Perform URL test & swap for Audio and Video
380+
await baseUrl.TestAndSwapUrlAsync(client,
381+
mainUrlAsbAlt.CombineURLFromString("AudioBlock"),
382+
() => metadataAudioV?.DataList.FirstOrDefault(x => x.Filename?.EndsWith(".pck", StringComparison.OrdinalIgnoreCase) ?? false),
383+
context => context.Audio,
384+
(context, replace) => context.Audio = replace,
385+
token);
386+
await baseUrl.TestAndSwapUrlAsync(client,
387+
mainUrlAsbAlt.CombineURLFromString("Video"),
388+
() => metadataVideoV?.DataList.FirstOrDefault(x => x.Filename?.EndsWith(".usm", StringComparison.OrdinalIgnoreCase) ?? false),
389+
context => context.Video,
390+
(context, replace) => context.Video = replace,
391+
token);
392+
377393
return new StarRailPersistentRefResult
378394
{
379395
BaseDirs = baseDirs,
@@ -598,19 +614,65 @@ public AssetBaseDirs() : this("", "", "", "", "", "", "")
598614
public class AssetBaseUrls
599615
{
600616
public required Dictionary<string, string> GatewayKvp { get; set; }
601-
public required string DesignData { get; set; }
617+
public required string DesignData { get; init; }
602618
public required string Archive { get; set; }
603619
public required string Audio { get; set; }
604620
public required string AsbBlock { get; set; }
605621
public required string AsbBlockPersistent { get; set; }
606-
public required string NativeData { get; set; }
622+
public required string NativeData { get; init; }
607623
public required string Video { get; set; }
608-
public required string RawRes { get; set; }
624+
public required string RawRes { get; init; }
609625

610-
public string? CacheLua { get; set; }
611-
public string? CacheIFix { get; set; }
626+
public string? CacheLua { get; init; }
627+
public string? CacheIFix { get; init; }
612628

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

616678
public class AssetMetadata

0 commit comments

Comments
 (0)