diff --git a/src/Snap.Hutao/Snap.Hutao/Control/Text/DescriptionTextBlock.cs b/src/Snap.Hutao/Snap.Hutao/Control/Text/DescriptionTextBlock.cs index a60ea1de..897c014f 100644 --- a/src/Snap.Hutao/Snap.Hutao/Control/Text/DescriptionTextBlock.cs +++ b/src/Snap.Hutao/Snap.Hutao/Control/Text/DescriptionTextBlock.cs @@ -8,7 +8,7 @@ using Microsoft.UI.Xaml.Media; using Snap.Hutao.Control.Extension; using Snap.Hutao.Control.Media; using Snap.Hutao.Control.Theme; -using System.Diagnostics; +using Snap.Hutao.Metadata; using Windows.Foundation; using Windows.UI; @@ -55,7 +55,7 @@ internal sealed partial class DescriptionTextBlock : ContentControl private static void OnDescriptionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { TextBlock textBlock = (TextBlock)((DescriptionTextBlock)d).Content; - ReadOnlySpan description = MetadataSpecialNames.Handle((string)e.NewValue); + ReadOnlySpan description = SpecialNameHandler.Handle((string)e.NewValue); try { diff --git a/src/Snap.Hutao/Snap.Hutao/Control/Text/MetadataSpecialNames.cs b/src/Snap.Hutao/Snap.Hutao/Control/Text/MetadataSpecialNames.cs deleted file mode 100644 index 64bb7b39..00000000 --- a/src/Snap.Hutao/Snap.Hutao/Control/Text/MetadataSpecialNames.cs +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright (c) DGP Studio. All rights reserved. -// Licensed under the MIT license. - -using System.Text; -using System.Text.RegularExpressions; - -namespace Snap.Hutao.Control.Text; - -internal static partial class MetadataSpecialNames -{ - public static string Handle(string input) - { - if (input.AsSpan()[0] is not '#') - { - return input; - } - - StringBuilder resultBuilder = new(input); - resultBuilder - .Replace("{MATEAVATAR#SEXPRO[INFO_MALE_PRONOUN_BOYD|INFO_FEMALE_PRONOUN_GIRLD]}", SH.ControlTextMetadataSpecialNameMetaAvatarSexProD) - .Replace("{PLAYERAVATAR#SEXPRO[INFO_MALE_PRONOUN_HE|INFO_FEMALE_PRONOUN_SHE]}", SH.ControlTextMetadataSpecialNamePlayerAvatarSexPro) - .Replace("{REALNAME[ID(1)]}", SH.ControlTextMetadataSpecialNameRealNameId1); - - input = resultBuilder.ToString(); - - // {M#.}{F#.} - input = MaleFemaleRegex().Replace(input, SH.ControlTextMetadataSpecialNameMaleFemale); - - return input[1..]; - } - - [GeneratedRegex("\\{M#(.*?)\\}\\{F#(.*?)\\}")] - private static partial Regex MaleFemaleRegex(); -} \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Model/Metadata/Converter/DescriptionsParametersDescriptor.cs b/src/Snap.Hutao/Snap.Hutao/Model/Metadata/Converter/DescriptionsParametersDescriptor.cs index 6aa30a3d..58003b35 100644 --- a/src/Snap.Hutao/Snap.Hutao/Model/Metadata/Converter/DescriptionsParametersDescriptor.cs +++ b/src/Snap.Hutao/Snap.Hutao/Model/Metadata/Converter/DescriptionsParametersDescriptor.cs @@ -2,7 +2,9 @@ // Licensed under the MIT license. using Snap.Hutao.Control; +using Snap.Hutao.Control.Text; using Snap.Hutao.Core.ExceptionService; +using Snap.Hutao.Metadata; using Snap.Hutao.Model.Metadata.Avatar; using Snap.Hutao.Model.Primitive; using System.Globalization; @@ -50,8 +52,20 @@ internal sealed partial class DescriptionsParametersDescriptor : ValueConverter< { if (desc.AsSpan().TrySplitIntoTwo('|', out ReadOnlySpan description, out ReadOnlySpan format)) { - string resultFormatted = ParamRegex().Replace(format.ToString(), match => ReplaceParamInMatch(match, paramList)); - results.Add(new ParameterDescription { Description = description.ToString(), Parameter = resultFormatted }); + if (description[0] is not '#') + { + // Fast path + string resultFormatted = ParamRegex().Replace(format.ToString(), match => ReplaceParamInMatch(match, paramList)); + results.Add(new ParameterDescription { Description = description.ToString(), Parameter = resultFormatted }); + } + else + { + string descriptionString = SpecialNameHandler.Handle(description.ToString()); + string formatString = SpecialNameHandler.Handle(format.ToString()); + + string resultFormatted = ParamRegex().Replace(formatString, match => ReplaceParamInMatch(match, paramList)); + results.Add(new ParameterDescription { Description = descriptionString, Parameter = resultFormatted }); + } } else { diff --git a/src/Snap.Hutao/Snap.Hutao/Model/Metadata/SpecialNameHandler.cs b/src/Snap.Hutao/Snap.Hutao/Model/Metadata/SpecialNameHandler.cs new file mode 100644 index 00000000..31d0f076 --- /dev/null +++ b/src/Snap.Hutao/Snap.Hutao/Model/Metadata/SpecialNameHandler.cs @@ -0,0 +1,47 @@ +// Copyright (c) DGP Studio. All rights reserved. +// Licensed under the MIT license. + +using System.Text; +using System.Text.RegularExpressions; + +namespace Snap.Hutao.Metadata; + +internal static partial class SpecialNameHandler +{ + // Use this regex to query special names in metadata + // "#(?!.*(?:F#|M#|NON_BREAK_SPACE|REALNAME\[ID\(1\)(\|HOSTONLY\(true\)|)\]|\{LAYOUT_MOBILE#.+?\}\{LAYOUT_PC#.+?\}\{LAYOUT_PS#.+?\})).* + public static string Handle(string input) + { + if (input.AsSpan()[0] is not '#') + { + return input; + } + + StringBuilder resultBuilder = new(input); + resultBuilder + .Replace("{MATEAVATAR#SEXPRO[INFO_MALE_PRONOUN_BOYFIRST|INFO_FEMALE_PRONOUN_GIRLFIRST]}", SH.MetadataSpecialNameMetaAvatarSexProInfoPronounBoyGirlFirst) + .Replace("{MATEAVATAR#SEXPRO[INFO_MALE_PRONOUN_BOYD|INFO_FEMALE_PRONOUN_GIRLD]}", SH.MetadataSpecialNameMetaAvatarSexProInfoPronounBoyGirlD) + .Replace("{PLAYERAVATAR#SEXPRO[INFO_MALE_PRONOUN_HE|INFO_FEMALE_PRONOUN_SHE]}", SH.MetadataSpecialNamePlayerAvatarSexProInfoPronounHeShe) + .Replace("{REALNAME[ID(1)|HOSTONLY(true)]}", SH.MetadataSpecialNameRealNameId1) + .Replace("{REALNAME[ID(1)]}", SH.MetadataSpecialNameRealNameId1) + .Replace("{NICKNAME}", SH.MetadataSpecialNameNickname) + .Replace("{NON_BREAK_SPACE}", "\u00A0"); + + input = resultBuilder.ToString(); + + input = MaleFemaleRegex().Replace(input, "$1/$2"); + input = FemaleMaleRegex().Replace(input, "$1/$2"); + input = LayoutRegex().Replace(input, "$2"); + + return input[1..]; + } + + [GeneratedRegex("\\{M#(.*?)\\}\\{F#(.*?)\\}")] + private static partial Regex MaleFemaleRegex(); + + [GeneratedRegex("\\{F#(.*?)\\}\\{M#(.*?)\\}")] + private static partial Regex FemaleMaleRegex(); + + [GeneratedRegex("\\{LAYOUT_MOBILE#(.+?)\\}\\{LAYOUT_PC#(.+?)\\}\\{LAYOUT_PS#(.+?)\\}")] + private static partial Regex LayoutRegex(); +} \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx index b04ece4a..5cdefd0e 100644 --- a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx +++ b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx @@ -159,18 +159,6 @@ 列表 - - 旅行者(男):$1\r\n旅行者(女):$2 - - - 王子/公主 - - - 他/她 - - - 流浪者 - 数据库已损坏:{0} @@ -210,6 +198,21 @@ 选择账号并启动 + + <color=#1E90FF>王子</color>/<color=#FFB6C1>公主</color> + + + <color=#1E90FF>我</color>/<color=#FFB6C1>我</color> + + + 旅行者 + + + <color=#1E90FF>他</color>/<color=#FFB6C1>她</color> + + + 流浪者 + 精炼 {0}