|
11 | 11 | using System; |
12 | 12 | using System.Collections.Generic; |
13 | 13 | using System.IO; |
| 14 | +using System.Linq; |
| 15 | +using System.Net; |
14 | 16 | using System.Net.Http; |
15 | 17 | using System.Security.Cryptography; |
16 | 18 | using System.Text.Json.Serialization; |
@@ -374,6 +376,20 @@ await LoadMetadataFile<StarRailAssetJsonMetadata>(instance, |
374 | 376 | aDirRawRes, |
375 | 377 | token); |
376 | 378 |
|
| 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 | + |
377 | 393 | return new StarRailPersistentRefResult |
378 | 394 | { |
379 | 395 | BaseDirs = baseDirs, |
@@ -598,19 +614,65 @@ public AssetBaseDirs() : this("", "", "", "", "", "", "") |
598 | 614 | public class AssetBaseUrls |
599 | 615 | { |
600 | 616 | public required Dictionary<string, string> GatewayKvp { get; set; } |
601 | | - public required string DesignData { get; set; } |
| 617 | + public required string DesignData { get; init; } |
602 | 618 | public required string Archive { get; set; } |
603 | 619 | public required string Audio { get; set; } |
604 | 620 | public required string AsbBlock { get; set; } |
605 | 621 | public required string AsbBlockPersistent { get; set; } |
606 | | - public required string NativeData { get; set; } |
| 622 | + public required string NativeData { get; init; } |
607 | 623 | public required string Video { get; set; } |
608 | | - public required string RawRes { get; set; } |
| 624 | + public required string RawRes { get; init; } |
609 | 625 |
|
610 | | - public string? CacheLua { get; set; } |
611 | | - public string? CacheIFix { get; set; } |
| 626 | + public string? CacheLua { get; init; } |
| 627 | + public string? CacheIFix { get; init; } |
612 | 628 |
|
613 | 629 | 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 | + } |
614 | 676 | } |
615 | 677 |
|
616 | 678 | public class AssetMetadata |
|
0 commit comments