Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/IntegratedModManager.Core/Online/CharacterCatalogClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/// <summary>
/// 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.
/// </summary>
public static WikiCatalogDescriptor? Describe(string? wikiUrl, string? gameWikiUrl) => Describe(wikiUrl) ?? Describe(gameWikiUrl);

/// <summary>
/// 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.
/// </summary>
public static async Task<T> WithRetryAsync<T>(Func<CancellationToken, Task<T>> request, TimeSpan timeout, Action<Exception>? 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);
}
}
}

/// <summary>The catalog behind a repository's wiki link, or null for a wiki without one.</summary>
public static WikiCatalogDescriptor? Describe(string? wikiUrl)
{
Expand Down
39 changes: 28 additions & 11 deletions src/WinUI3/MainWindow.OnlineCharacters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> RequestCharacterCatalogAsync<T>(string operation, Func<CancellationToken, Task<T>> request)
=> CharacterCatalogClient.WithRetryAsync(request, CharacterCatalogRequestTimeout,
ex => LogApplicationIssue(operation + ", trying again", ex));

private async Task<List<OnlineCharacterOption>> FetchWikiCharacterCatalogAsync(WorkspaceRepository repository)
{
Expand All @@ -718,16 +727,19 @@ private async Task<List<OnlineCharacterOption>> 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<List<OnlineCharacterOption>> 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<List<OnlineCharacterOption>> FetchGameBananaCharacterCatalogAsync(string categoryId)
=> CharacterCatalogClient.FetchGameBananaCharactersAsync(_httpClient, categoryId);
=> RequestCharacterCatalogAsync("Load GameBanana character categories",
token => CharacterCatalogClient.FetchGameBananaCharactersAsync(_httpClient, categoryId, token));

private async Task ApplyOfficialHoYoWikiEnglishNamesAsync(
WorkspaceRepository repository,
Expand All @@ -744,13 +756,16 @@ private async Task ApplyOfficialHoYoWikiEnglishNamesAsync(
try
{
// Fetch sequentially to keep the monthly Wiki refresh gentle and separate from mod loading.
List<HoYoWikiLocalizedCharacter> chineseCharacters = await CharacterCatalogClient.FetchHoYoWikiNamesAsync(_httpClient, descriptor, "zh-cn");
List<HoYoWikiLocalizedCharacter> englishCharacters = await CharacterCatalogClient.FetchHoYoWikiNamesAsync(_httpClient, descriptor, "en-us");
List<HoYoWikiLocalizedCharacter> chineseCharacters = await RequestCharacterCatalogAsync("Load HoYoWiki names",
token => CharacterCatalogClient.FetchHoYoWikiNamesAsync(_httpClient, descriptor, "zh-cn", token));
List<HoYoWikiLocalizedCharacter> 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);
}
}

Expand Down Expand Up @@ -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<OnlineCharacterOption> mergedCharacters = OnlineCharacterCatalogMatcher.Merge(
Expand All @@ -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
{
Expand Down
63 changes: 63 additions & 0 deletions tests/Core.Tests/Online/CharacterCatalogClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Exception>();

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<OperationCanceledException>(Assert.Single(retried));
}

[Fact]
public async Task WithRetry_TriesAgainAfterANetworkErrorAndThenGivesUp()
{
int attempts = 0;

await Assert.ThrowsAsync<HttpRequestException>(() => CharacterCatalogClient.WithRetryAsync<int>(_ =>
{
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<OperationCanceledException>(() => CharacterCatalogClient.WithRetryAsync<int>(token =>
{
attempts++;
token.ThrowIfCancellationRequested();
return Task.FromResult(1);
}, TimeSpan.FromSeconds(5), cancellationToken: cancelled.Token));
await Assert.ThrowsAsync<System.Text.Json.JsonException>(() => CharacterCatalogClient.WithRetryAsync<int>(_ =>
{
attempts++;
throw new System.Text.Json.JsonException("bad data");
}, TimeSpan.FromSeconds(5)));

Assert.Equal(2, attempts);
}

[Fact]
public async Task FetchWikiCharacters_ReadsTheCharacterChannelInOrder()
{
Expand Down
Loading