diff --git a/src/IntegratedModManager.Core/Online/CharacterCatalogClient.cs b/src/IntegratedModManager.Core/Online/CharacterCatalogClient.cs
index 914cbb7..79c82de 100644
--- a/src/IntegratedModManager.Core/Online/CharacterCatalogClient.cs
+++ b/src/IntegratedModManager.Core/Online/CharacterCatalogClient.cs
@@ -25,6 +25,35 @@ public static class CharacterCatalogClient
public const string HoYoWikiApiBaseUrl = "https://sg-wiki-api-static.hoyolab.com/hoyowiki/wapi";
public const string EndfieldOperatorCatalogUrl = "https://endfield.hypergryph.com/operator";
+ ///
+ /// The catalog behind a repository's wiki link, else behind its game's built-in wiki: a repository
+ /// set up by hand may have the game's category but no wiki link, and would otherwise get no catalog.
+ ///
+ public static WikiCatalogDescriptor? Describe(string? wikiUrl, string? gameWikiUrl) => Describe(wikiUrl) ?? Describe(gameWikiUrl);
+
+ ///
+ /// Runs one catalog request with its own time limit, and once more after a timeout or a network error.
+ /// A stalled request would otherwise hold the catalog for HttpClient's 100 seconds and then give up.
+ ///
+ public static async Task WithRetryAsync(Func> request, TimeSpan timeout, Action? retrying = null,
+ CancellationToken cancellationToken = default)
+ {
+ for (int attempt = 1; ; attempt++)
+ {
+ using var limit = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
+ limit.CancelAfter(timeout);
+ try
+ {
+ return await request(limit.Token);
+ }
+ catch (Exception ex) when (attempt < 2 && !cancellationToken.IsCancellationRequested
+ && ex is HttpRequestException or OperationCanceledException)
+ {
+ retrying?.Invoke(ex);
+ }
+ }
+ }
+
/// The catalog behind a repository's wiki link, or null for a wiki without one.
public static WikiCatalogDescriptor? Describe(string? wikiUrl)
{
diff --git a/src/WinUI3/MainWindow.OnlineCharacters.cs b/src/WinUI3/MainWindow.OnlineCharacters.cs
index e071d7f..93f3d31 100644
--- a/src/WinUI3/MainWindow.OnlineCharacters.cs
+++ b/src/WinUI3/MainWindow.OnlineCharacters.cs
@@ -707,8 +707,17 @@ private async Task WriteOnlineCharacterCatalogCacheAsync(
}
}
+ // Each catalog request gets this long, then one more try; see CharacterCatalogClient.WithRetryAsync.
+ private static readonly TimeSpan CharacterCatalogRequestTimeout = TimeSpan.FromSeconds(20);
+
+ // The repository's own wiki link, else its game's built-in one (found by category or game name).
private static WikiCatalogDescriptor? GetWikiCatalogDescriptor(WorkspaceRepository repository)
- => CharacterCatalogClient.Describe(repository.WikiUrl);
+ => CharacterCatalogClient.Describe(repository.WikiUrl,
+ BuiltInRepositoryPresets.FirstOrDefault(preset => IsRepositoryForPreset(repository, preset))?.WikiUrl);
+
+ private Task RequestCharacterCatalogAsync(string operation, Func> request)
+ => CharacterCatalogClient.WithRetryAsync(request, CharacterCatalogRequestTimeout,
+ ex => LogApplicationIssue(operation + ", trying again", ex));
private async Task> FetchWikiCharacterCatalogAsync(WorkspaceRepository repository)
{
@@ -718,16 +727,19 @@ private async Task> FetchWikiCharacterCatalogAsync(W
}
return GetWikiCatalogDescriptor(repository) is { } descriptor
- ? await CharacterCatalogClient.FetchWikiCharactersAsync(_httpClient, descriptor, repository.WikiUrl)
+ ? await RequestCharacterCatalogAsync("Load wiki character catalog",
+ token => CharacterCatalogClient.FetchWikiCharactersAsync(_httpClient, descriptor, repository.WikiUrl, token))
: [];
}
private Task> FetchEndfieldCharacterCatalogAsync(WorkspaceRepository repository)
- => CharacterCatalogClient.FetchEndfieldOperatorsAsync(_httpClient, EndfieldCharacters,
- string.IsNullOrWhiteSpace(repository.WikiUrl) ? EndfieldOfficialWikiUrl : repository.WikiUrl);
+ => RequestCharacterCatalogAsync("Load Endfield operator catalog",
+ token => CharacterCatalogClient.FetchEndfieldOperatorsAsync(_httpClient, EndfieldCharacters,
+ string.IsNullOrWhiteSpace(repository.WikiUrl) ? EndfieldOfficialWikiUrl : repository.WikiUrl, token));
private Task> FetchGameBananaCharacterCatalogAsync(string categoryId)
- => CharacterCatalogClient.FetchGameBananaCharactersAsync(_httpClient, categoryId);
+ => RequestCharacterCatalogAsync("Load GameBanana character categories",
+ token => CharacterCatalogClient.FetchGameBananaCharactersAsync(_httpClient, categoryId, token));
private async Task ApplyOfficialHoYoWikiEnglishNamesAsync(
WorkspaceRepository repository,
@@ -744,13 +756,16 @@ private async Task ApplyOfficialHoYoWikiEnglishNamesAsync(
try
{
// Fetch sequentially to keep the monthly Wiki refresh gentle and separate from mod loading.
- List chineseCharacters = await CharacterCatalogClient.FetchHoYoWikiNamesAsync(_httpClient, descriptor, "zh-cn");
- List englishCharacters = await CharacterCatalogClient.FetchHoYoWikiNamesAsync(_httpClient, descriptor, "en-us");
+ List chineseCharacters = await RequestCharacterCatalogAsync("Load HoYoWiki names",
+ token => CharacterCatalogClient.FetchHoYoWikiNamesAsync(_httpClient, descriptor, "zh-cn", token));
+ List englishCharacters = await RequestCharacterCatalogAsync("Load HoYoWiki names",
+ token => CharacterCatalogClient.FetchHoYoWikiNamesAsync(_httpClient, descriptor, "en-us", token));
CharacterCatalogClient.ApplyEnglishNames(characters, chineseCharacters, englishCharacters);
}
- catch
+ catch (Exception ex)
{
// The existing Chinese catalog and translation fallback remain usable when HoYoWiki is unavailable.
+ LogApplicationIssue("Load HoYoWiki names", ex);
}
}
@@ -879,9 +894,10 @@ private async Task EnsureOnlineCharacterCatalogAsync(WorkspaceRepository reposit
gameBananaCharacters = await FetchGameBananaCharacterCatalogAsync(
GetEffectiveOnlineCategoryId(repository));
}
- catch
+ catch (Exception ex)
{
// Wiki names remain usable if the optional category mapping request fails.
+ LogApplicationIssue("Load GameBanana character categories", ex);
}
List mergedCharacters = OnlineCharacterCatalogMatcher.Merge(
@@ -893,9 +909,10 @@ private async Task EnsureOnlineCharacterCatalogAsync(WorkspaceRepository reposit
ApplyOnlineCharacterCatalog(catalogKey, mergedCharacters);
}
}
- catch
+ catch (Exception ex)
{
- // The current page still supplies a usable partial character list.
+ // The current page still supplies a usable partial character list; the log says why the catalog did not load.
+ LogApplicationIssue("Load online character catalog", ex);
}
finally
{
diff --git a/tests/Core.Tests/Online/CharacterCatalogClientTests.cs b/tests/Core.Tests/Online/CharacterCatalogClientTests.cs
index af6aca8..0d105f5 100644
--- a/tests/Core.Tests/Online/CharacterCatalogClientTests.cs
+++ b/tests/Core.Tests/Online/CharacterCatalogClientTests.cs
@@ -43,6 +43,69 @@ public void Describe_RecognisesTheMiHoYoWikis(string url, string appSn, int chan
[InlineData("https://wiki.skland.com/endfield")]
public void Describe_ReturnsNullForOtherLinks(string? url) => Assert.Null(CharacterCatalogClient.Describe(url));
+ [Theory]
+ [InlineData(null, "ys_obc")]
+ [InlineData("", "ys_obc")]
+ [InlineData("https://example.com/my-notes", "ys_obc")]
+ [InlineData("https://bbs.mihoyo.com/sr/wiki/channel/map/17/18", "sr_wiki")]
+ public void Describe_FallsBackToTheGameWikiOnlyWithoutAWikiOfItsOwn(string? repositoryWiki, string expected)
+ => Assert.Equal(expected, CharacterCatalogClient.Describe(repositoryWiki, "https://baike.mihoyo.com/ys/obc/channel/map/189/25")!.AppSn);
+
+ [Fact]
+ public void Describe_HasNoCatalogWithoutEitherWiki() => Assert.Null(CharacterCatalogClient.Describe(null, null));
+
+ [Fact]
+ public async Task WithRetry_TriesAgainAfterAStalledRequest()
+ {
+ int attempts = 0;
+ var retried = new List();
+
+ int result = await CharacterCatalogClient.WithRetryAsync(async token =>
+ {
+ if (++attempts == 1) await Task.Delay(Timeout.Infinite, token);
+ return 42;
+ }, TimeSpan.FromMilliseconds(50), retried.Add);
+
+ Assert.Equal((42, 2), (result, attempts));
+ Assert.IsAssignableFrom(Assert.Single(retried));
+ }
+
+ [Fact]
+ public async Task WithRetry_TriesAgainAfterANetworkErrorAndThenGivesUp()
+ {
+ int attempts = 0;
+
+ await Assert.ThrowsAsync(() => CharacterCatalogClient.WithRetryAsync(_ =>
+ {
+ attempts++;
+ throw new HttpRequestException("offline");
+ }, TimeSpan.FromSeconds(5)));
+
+ Assert.Equal(2, attempts);
+ }
+
+ [Fact]
+ public async Task WithRetry_DoesNotRetryWhenTheCallerCancelsOrOnOtherErrors()
+ {
+ using var cancelled = new CancellationTokenSource();
+ cancelled.Cancel();
+ int attempts = 0;
+
+ await Assert.ThrowsAnyAsync(() => CharacterCatalogClient.WithRetryAsync(token =>
+ {
+ attempts++;
+ token.ThrowIfCancellationRequested();
+ return Task.FromResult(1);
+ }, TimeSpan.FromSeconds(5), cancellationToken: cancelled.Token));
+ await Assert.ThrowsAsync(() => CharacterCatalogClient.WithRetryAsync(_ =>
+ {
+ attempts++;
+ throw new System.Text.Json.JsonException("bad data");
+ }, TimeSpan.FromSeconds(5)));
+
+ Assert.Equal(2, attempts);
+ }
+
[Fact]
public async Task FetchWikiCharacters_ReadsTheCharacterChannelInOrder()
{