Skip to content

Commit

Permalink
fix #804
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightczx committed Jul 16, 2023
1 parent c5dada3 commit 8631933
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/Snap.Hutao/Snap.Hutao/Core/LifeCycle/Activation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private async Task HandleNormalLaunchActionAsync()
// Increase launch times
LocalSetting.Set(SettingKeys.LaunchTimes, LocalSetting.Get(SettingKeys.LaunchTimes, 0) + 1);

if (LocalSetting.Get(SettingKeys.Major1Minor7Revision0GuideState, (uint)GuideState.None) < (uint)GuideState.Completed)
if (false && LocalSetting.Get(SettingKeys.Major1Minor7Revision0GuideState, (uint)GuideState.None) < (uint)GuideState.Completed)
{
await taskContext.SwitchToMainThreadAsync();
serviceProvider.GetRequiredService<GuideWindow>();
Expand Down
16 changes: 16 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Extension/StringExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,20 @@ public static Uri ToUri(this string value)
{
return new(value);
}

/// <summary>
/// 移除结尾可能存在的字符串
/// </summary>
/// <param name="source">源</param>
/// <param name="value">值</param>
/// <returns>新的字符串</returns>
public static string TrimEnd(this string source, string value)
{
while (source.EndsWith(value))
{
source = source[..^value.Length];
}

return source;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2135,4 +2135,7 @@
<data name="GuideWindowTitle" xml:space="preserve">
<value>欢迎使用胡桃</value>
</data>
<data name="ServiceGachaLogUrlProviderUrlLanguageNotMatchCurrentLocale" xml:space="preserve">
<value>Url 中的语言:{0} 与胡桃的语言:{1} 不匹配,请切换到对应语言重试</value>
</data>
</root>
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

using Snap.Hutao.Service.Metadata;
using Snap.Hutao.View.Dialog;
using Snap.Hutao.Web.Request.QueryString;

namespace Snap.Hutao.Service.GachaLog.QueryProvider;

Expand All @@ -14,6 +16,7 @@ namespace Snap.Hutao.Service.GachaLog.QueryProvider;
internal sealed partial class GachaLogQueryManualInputProvider : IGachaLogQueryProvider
{
private readonly IServiceProvider serviceProvider;
private readonly MetadataOptions metadataOptions;
private readonly ITaskContext taskContext;

/// <inheritdoc/>
Expand All @@ -25,13 +28,26 @@ public async Task<ValueResult<bool, GachaLogQuery>> GetQueryAsync()
// ContentDialog must be created by main thread.
await taskContext.SwitchToMainThreadAsync();
GachaLogUrlDialog dialog = serviceProvider.CreateInstance<GachaLogUrlDialog>();
(bool isOk, string query) = await dialog.GetInputUrlAsync().ConfigureAwait(false);
(bool isOk, string queryString) = await dialog.GetInputUrlAsync().ConfigureAwait(false);

if (isOk)
{
if (query.Contains("&auth_appid=webview_gacha"))
QueryString query = QueryString.Parse(queryString);
string queryLanguageCode = query["lang"];
if (query["auth_appid"] == "webview_gacha")
{
return new(true, new(query));
if (metadataOptions.IsCurrentLocale(queryLanguageCode))
{
return new(true, new(queryString));
}
else
{
string message = string.Format(
SH.ServiceGachaLogUrlProviderUrlLanguageNotMatchCurrentLocale,
queryLanguageCode,
metadataOptions.LocaleName);
return new(false, message);
}
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

using Snap.Hutao.Service.Metadata;
using Snap.Hutao.Service.User;
using Snap.Hutao.ViewModel.User;
using Snap.Hutao.Web.Hoyolab.Hk4e.Event.GachaInfo;
Expand All @@ -18,6 +19,7 @@ namespace Snap.Hutao.Service.GachaLog.QueryProvider;
internal sealed partial class GachaLogQuerySTokenProvider : IGachaLogQueryProvider
{
private readonly BindingClient2 bindingClient2;
private readonly MetadataOptions metadataOptions;
private readonly IUserService userService;

/// <inheritdoc/>
Expand All @@ -38,7 +40,7 @@ public async Task<ValueResult<bool, GachaLogQuery>> GetQueryAsync()

if (authkeyResponse.IsOk())
{
return new(true, new(GachaLogQueryOptions.AsQuery(data, authkeyResponse.Data)));
return new(true, new(GachaLogQueryOptions.AsQuery(data, authkeyResponse.Data, metadataOptions.LanguageCode)));
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

using Snap.Hutao.Core.IO;
using Snap.Hutao.Service.Game;
using Snap.Hutao.Service.Metadata;
using Snap.Hutao.Web.Request.QueryString;
using System.Globalization;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
Expand All @@ -18,6 +21,7 @@ namespace Snap.Hutao.Service.GachaLog.QueryProvider;
internal sealed partial class GachaLogQueryWebCacheProvider : IGachaLogQueryProvider
{
private readonly IGameService gameService;
private readonly MetadataOptions metadataOptions;

/// <inheritdoc/>
public string Name { get => nameof(GachaLogQueryWebCacheProvider); }
Expand Down Expand Up @@ -63,7 +67,20 @@ public async Task<ValueResult<bool, GachaLogQuery>> GetQueryAsync()

if (!string.IsNullOrEmpty(result))
{
return new(true, new(result));
QueryString query = QueryString.Parse(result.TrimEnd("#/log"));
string queryLanguageCode = query["lang"];
if (metadataOptions.IsCurrentLocale(queryLanguageCode))
{
return new(true, new(result));
}
else
{
string message = string.Format(
SH.ServiceGachaLogUrlProviderUrlLanguageNotMatchCurrentLocale,
queryLanguageCode,
metadataOptions.LocaleName);
return new(false, message);
}
}
else
{
Expand Down
135 changes: 102 additions & 33 deletions src/Snap.Hutao/Snap.Hutao/Service/Metadata/MetadataOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Microsoft.Extensions.Options;
using Snap.Hutao.Core;
using System.Collections.Immutable;
using System.Globalization;
using System.IO;

Expand All @@ -15,6 +16,8 @@ namespace Snap.Hutao.Service.Metadata;
[Injection(InjectAs.Singleton)]
internal sealed partial class MetadataOptions : IOptions<MetadataOptions>
{


private readonly AppOptions appOptions;
private readonly HutaoOptions hutaoOptions;

Expand Down Expand Up @@ -61,12 +64,51 @@ public string LocalizedDataFolder
/// </summary>
public string LocaleName
{
get => localeName ??= GetLocaleName();
get => localeName ??= GetLocaleName(appOptions.CurrentCulture);
}

/// <summary>
/// 当前语言代码
/// </summary>
public string LanguageCode
{
get => LocaleNames.LocaleNameLanguageCodeMap[LocaleName];
}

/// <inheritdoc/>
public MetadataOptions Value { get => this; }

/// <summary>
/// 获取语言名称
/// </summary>
/// <param name="cultureInfo">语言信息</param>
/// <returns>元数据语言名称</returns>
public static string GetLocaleName(CultureInfo cultureInfo)
{
while (true)
{
if (LocaleNames.LanguageNameLocaleNameMap.TryGetValue(cultureInfo.Name, out string? localeName))
{
return localeName;
}
else
{
cultureInfo = cultureInfo.Parent;
}
}
}

/// <summary>
/// 检查是否为当前语言名称
/// </summary>
/// <param name="languageCode">语言代码</param>
/// <returns>是否为当前语言名称</returns>
public bool IsCurrentLocale(string languageCode)
{
CultureInfo cultureInfo = CultureInfo.GetCultureInfo(languageCode);
return GetLocaleName(cultureInfo) == LocaleName;
}

/// <summary>
/// 获取本地的本地化元数据文件
/// </summary>
Expand All @@ -90,38 +132,65 @@ public string GetLocalizedRemoteFile(string fileNameWithExtension)
return Web.HutaoEndpoints.HutaoMetadata2File(LocaleName, fileNameWithExtension);
#endif
}
}

private string GetLocaleName()
{
CultureInfo cultureInfo = appOptions.CurrentCulture;
/// <summary>
/// 本地化名称
/// </summary>
internal static class LocaleNames
{
public const string DE = "DE"; // German
public const string EN = "EN"; // English
public const string ES = "ES"; // Spanish
public const string FR = "FR"; // French
public const string ID = "ID"; // Indonesian
public const string IT = "IT"; // Italian
public const string JP = "JP"; // Japanese
public const string KR = "KR"; // Korean
public const string PT = "PT"; // Portuguese
public const string RU = "RU"; // Russian
public const string TH = "TH"; // Thai
public const string TR = "TR"; // Turkish
public const string VI = "VI"; // Vietnamese
public const string CHS = "CHS"; // Chinese (Simplified)
public const string CHT = "CHT"; // Chinese (Traditional)

while (true)
{
if (Equals(cultureInfo, CultureInfo.InvariantCulture))
{
// Fallback to Chinese.
return "CHS";
}
public static readonly ImmutableDictionary<string, string> LanguageNameLocaleNameMap = new Dictionary<string, string>()
{
["de"] = DE,
["en"] = EN,
["es"] = ES,
["fr"] = FR,
["id"] = ID,
["it"] = IT,
["ja"] = JP,
["ko"] = KR,
["pt"] = PT,
["ru"] = RU,
["th"] = TH,
["tr"] = TR,
["vi"] = VI,
["zh-Hans"] = CHS,
["zh-Hant"] = CHT,
[string.Empty] = CHS, // Fallback to Chinese.
}.ToImmutableDictionary();

switch (cultureInfo.Name)
{
case "de": return "DE"; // German
case "en": return "EN"; // English
case "es": return "ES"; // Spanish
case "fr": return "FR"; // French
case "id": return "ID"; // Indonesian
case "it": return "IT"; // Italian
case "ja": return "JP"; // Japanese
case "ko": return "KR"; // Korean
case "pt": return "PT"; // Portuguese
case "ru": return "RU"; // Russian
case "th": return "TH"; // Thai
case "tr": return "TR"; // Turkish
case "vi": return "TR"; // Vietnamese
case "zh-Hans": return "CHS"; // Chinese (Simplified)
case "zh-Hant": return "CHT"; // Chinese (Traditional)
default: cultureInfo = cultureInfo.Parent; break;
}
}
}
}
public static readonly ImmutableDictionary<string, string> LocaleNameLanguageCodeMap = new Dictionary<string, string>()
{
[DE] = "de-de",
[EN] = "en-us",
[ES] = "es-es",
[FR] = "fr-fr",
[ID] = "id-id",
[IT] = "it-it",
[JP] = "ja-jp",
[KR] = "ko-kr",
[PT] = "pt-pt",
[RU] = "ru-ru",
[TH] = "th-th",
[TR] = "tr-tr",
[VI] = "vi-vn",
[CHS] = "zh-cn",
[CHT] = "zh-tw",
}.ToImmutableDictionary();
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task<ValueResult<bool, string>> GetInputUrlAsync()
{
await taskContext.SwitchToMainThreadAsync();
ContentDialogResult result = await ShowAsync();
string url = InputText.Text;
string url = InputText.Text.TrimEnd("#/log");

return new(result == ContentDialogResult.Primary, url);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public GachaLogQueryOptions(in GachaLogQuery query, GachaConfigType type, long e
IsOversea = query.IsOversea;
innerQuery = QueryString.Parse(query.Query);

innerQuery.Set("lang", "zh-cn");
// innerQuery.Set("lang", "zh-cn");
innerQuery.Set("gacha_type", (int)type);
innerQuery.Set("size", Size);

Expand All @@ -68,10 +68,12 @@ public GachaLogQueryOptions(in GachaLogQuery query, GachaConfigType type, long e
/// </summary>
/// <param name="genAuthKeyData">生成信息</param>
/// <param name="gameAuthKey">验证包装</param>
/// <param name="lang">语言</param>
/// <returns>查询</returns>
public static string AsQuery(GenAuthKeyData genAuthKeyData, GameAuthKey gameAuthKey)
public static string AsQuery(GenAuthKeyData genAuthKeyData, GameAuthKey gameAuthKey, string lang)
{
QueryString queryString = new();
queryString.Set("lang", lang);
queryString.Set("auth_appid", genAuthKeyData.AuthAppId);
queryString.Set("authkey", Uri.EscapeDataString(gameAuthKey.AuthKey));
queryString.Set("authkey_ver", gameAuthKey.AuthKeyVersion);
Expand Down

0 comments on commit 8631933

Please sign in to comment.