a few updates

This commit is contained in:
SillyStar-Github 2026-01-19 03:59:22 -05:00
parent e2c595a669
commit a89ba825ee
6 changed files with 137 additions and 19 deletions

View file

@ -124,6 +124,61 @@ namespace PvZ_Fusion_Translator.AssetStore
return checkText; return checkText;
} }
public static string TranslateColorText(string originalText, bool isLog = false)
{
string first = ReplaceInsideColorTags(originalText);
string text = ReplaceOutsideColorTags(first);
string checkText;
#if DEBUG
Regex regex = new("\\p{IsCJKUnifiedIdeographs}+");
Match match = regex.Match(text);
if (match.Success)
{
// Log.LogDebug("Untranslated String: " + match.Value);
FileLoader.DumpUntranslatedStrings(text);
}
#endif
checkText = text;
return checkText;
}
public static string ReplaceOutsideColorTags(string input)
{
string pattern = @"(<color[^>]*>.*?</color>)";
var parts = Regex.Split(input, pattern);
for (int i = 0; i < parts.Length; i++)
{
if (i % 2 == 0)
{
parts[i] = DoTranslateText(parts[i], false);
if(Utils.CheckForUntranslatedText(parts[i]))
{
FileLoader.DumpUntranslatedStrings(parts[i]);
}
}
}
return string.Join("", parts);
}
public static string ReplaceInsideColorTags(string input)
{
string text = Regex.Replace(input, @"(<color=[^>]+>)(.*?)(</color>)", match =>
{
string translatedText = DoTranslateText(match.Groups[2].Value, false);
if (Utils.CheckForUntranslatedText(translatedText))
{
FileLoader.DumpUntranslatedStrings(translatedText);
}
return match.Groups[1].Value + DoTranslateText(match.Groups[2].Value, false) + match.Groups[3].Value;
});
return text;
}
public static string TranslateText(string originalText, string pattern, bool isLog = false) public static string TranslateText(string originalText, string pattern, bool isLog = false)
{ {
if (TestRegex(originalText, pattern) && translationStringRegex.ContainsKey(pattern)) if (TestRegex(originalText, pattern) && translationStringRegex.ContainsKey(pattern))
@ -214,7 +269,7 @@ namespace PvZ_Fusion_Translator.AssetStore
return originalText; return originalText;
} }
private static bool TestRegex(string originalText, string pattern) public static bool TestRegex(string originalText, string pattern)
{ {
return Regex.IsMatch(originalText, pattern); return Regex.IsMatch(originalText, pattern);
} }

View file

@ -3,6 +3,7 @@ using Il2Cpp;
using Il2CppTMPro; using Il2CppTMPro;
using PvZ_Fusion_Translator.AssetStore; using PvZ_Fusion_Translator.AssetStore;
using PvZ_Fusion_Translator.Patches.Managers; using PvZ_Fusion_Translator.Patches.Managers;
using System.Text.RegularExpressions;
using UnityEngine.TextCore.Text; using UnityEngine.TextCore.Text;
namespace PvZ_Fusion_Translator.Patches.BaseTextObjects namespace PvZ_Fusion_Translator.Patches.BaseTextObjects
@ -30,6 +31,11 @@ namespace PvZ_Fusion_Translator.Patches.BaseTextObjects
} }
txt.font = fontAsset; txt.font = fontAsset;
} }
if(__instance.TryGetComponent<TextMeshProUGUI>(out TextMeshProUGUI text))
{
text.text = StringStore.TranslateText(text.text);
}
} }
} }
} }

View file

@ -12,41 +12,52 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects
[HarmonyPostfix] [HarmonyPostfix]
private static void Start(AdvantureMenu __instance) private static void Start(AdvantureMenu __instance)
{ {
foreach (TextMeshProUGUI titleText in __instance.GetComponentsInChildren<TextMeshProUGUI>()) __instance.difficulty.text = Utils.RemoveSizeTags(StringStore.TranslateText(__instance.difficulty.text));
foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren<TextMeshProUGUI>())
{ {
titleText.text = StringStore.TranslateText(titleText.text); text.text = StringStore.TranslateText(text.text);
} }
__instance.difficulty.text = StringStore.TranslateText(__instance.difficulty.text);
__instance.chapterDifficulty.text = StringStore.TranslateText(__instance.chapterDifficulty.text, pattern: "章节难度:([^\\s]+)");
__instance.starCount.text = StringStore.TranslateText(__instance.starCount.text);
} }
[HarmonyPatch(nameof(AdvantureMenu.ChangeDifficulty))] [HarmonyPatch(nameof(AdvantureMenu.ChangeDifficulty))]
[HarmonyPostfix] [HarmonyPostfix]
private static void ChangeDifficulty(AdvantureMenu __instance) private static void ChangeDifficulty(AdvantureMenu __instance)
{ {
__instance.difficulty.text = StringStore.TranslateText(__instance.difficulty.text); __instance.difficulty.text = Utils.RemoveSizeTags(StringStore.TranslateText(__instance.difficulty.text));
foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren<TextMeshProUGUI>())
{
text.text = StringStore.TranslateText(text.text);
}
} }
[HarmonyPatch(nameof(AdvantureMenu.GetChapterDifficulty))] [HarmonyPatch(nameof(AdvantureMenu.GetChapterDifficulty))]
[HarmonyPostfix] [HarmonyPostfix]
private static void GetChapterDifficulty(AdvantureMenu __instance) private static void GetChapterDifficulty(AdvantureMenu __instance)
{ {
__instance.chapterDifficulty.text = StringStore.TranslateText(__instance.chapterDifficulty.text, pattern: "章节难度:([^\\s]+)"); foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren<TextMeshProUGUI>())
{
text.text = StringStore.TranslateText(text.text);
}
} }
[HarmonyPatch(nameof(AdvantureMenu.UpdateChapterDifficulty))] [HarmonyPatch(nameof(AdvantureMenu.UpdateChapterDifficulty))]
[HarmonyPostfix] [HarmonyPostfix]
private static void UpdateChapterDifficulty(AdvantureMenu __instance) private static void UpdateChapterDifficulty(AdvantureMenu __instance)
{ {
__instance.chapterDifficulty.text = StringStore.TranslateText(__instance.chapterDifficulty.text, pattern: "章节难度:([^\\s]+)"); foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren<TextMeshProUGUI>())
{
text.text = StringStore.TranslateText(text.text);
}
} }
[HarmonyPatch(nameof(AdvantureMenu.UpdateStarCount))] [HarmonyPatch(nameof(AdvantureMenu.UpdateStarCount))]
[HarmonyPostfix] [HarmonyPostfix]
private static void UpdateStarCount(AdvantureMenu __instance) private static void UpdateStarCount(AdvantureMenu __instance)
{ {
__instance.starCount.text = StringStore.TranslateText(__instance.starCount.text); foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren<TextMeshProUGUI>())
{
text.text = StringStore.TranslateText(text.text);
}
} }
} }
} }

View file

@ -8,24 +8,49 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects
[HarmonyPatch(typeof(AdvantureMissionMenu))] [HarmonyPatch(typeof(AdvantureMissionMenu))]
public static class AdvantureMissionMenu_Patch public static class AdvantureMissionMenu_Patch
{ {
[HarmonyPatch(nameof(AdvantureMissionMenu.Awake))]
[HarmonyPostfix]
private static void Awake(AdvantureMissionMenu __instance)
{
foreach (TextMeshProUGUI text in __instance.contant.GetComponentsInChildren<TextMeshProUGUI>())
{
text.text = StringStore.TranslateColorText(text.text);
}
//foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren<TextMeshProUGUI>())
//{
// text.text = StringStore.TranslateText(text.text);
//}
}
[HarmonyPatch(nameof(AdvantureMissionMenu.InitText))] [HarmonyPatch(nameof(AdvantureMissionMenu.InitText))]
[HarmonyPostfix] [HarmonyPostfix]
private static void InitText(AdvantureMissionMenu __instance) private static void InitText(AdvantureMissionMenu __instance)
{ {
foreach(TextMeshProUGUI text in __instance.GetComponentsInChildren<TextMeshProUGUI>()) foreach (TextMeshProUGUI text in __instance.contant.GetComponentsInChildren<TextMeshProUGUI>())
{ {
text.text = StringStore.TranslateText(text.text); text.text = StringStore.TranslateColorText(text.text);
} }
//foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren<TextMeshProUGUI>())
//{
// text.text = StringStore.TranslateText(text.text);
//}
} }
[HarmonyPatch(nameof(AdvantureMissionMenu.SwitchAward))] [HarmonyPatch(nameof(AdvantureMissionMenu.SwitchAward))]
[HarmonyPostfix] [HarmonyPostfix]
private static void SwitchAward(AdvantureMissionMenu __instance) private static void SwitchAward(AdvantureMissionMenu __instance)
{ {
foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren<TextMeshProUGUI>()) foreach(TextMeshProUGUI text in __instance.contant.GetComponentsInChildren<TextMeshProUGUI>())
{ {
text.text = StringStore.TranslateText(text.text); text.text = StringStore.TranslateColorText(text.text);
} }
//foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren<TextMeshProUGUI>())
//{
// text.text = StringStore.TranslateText(text.text);
//}
} }
} }
} }

View file

@ -17,7 +17,7 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.MinorObjects
{ "^HP:(\\d+)/(\\d+)\n一类:(\\d+)/(\\d+)\n二类:(\\d+)/(\\d+)$", "HP: {0}/{1}\nType 1 armor: {2}/{3}\nType 2 armor: {4}/{5}" }, { "^HP:(\\d+)/(\\d+)\n一类:(\\d+)/(\\d+)\n二类:(\\d+)/(\\d+)$", "HP: {0}/{1}\nType 1 armor: {2}/{3}\nType 2 armor: {4}/{5}" },
{ "^HP:(\\d+)/(\\d+)\n一类:(\\d+)/(\\d+)$", "HP: {0}/{1}\nType 1 armor: {2}/{3}" }, { "^HP:(\\d+)/(\\d+)\n一类:(\\d+)/(\\d+)$", "HP: {0}/{1}\nType 1 armor: {2}/{3}" },
{ "^HP:(\\d+)/(\\d+)\n二类:(\\d+)/(\\d+)$", "HP: {0}/{1}\nType 2 armor: {2}/{3}" }, { "^HP:(\\d+)/(\\d+)\n二类:(\\d+)/(\\d+)$", "HP: {0}/{1}\nType 2 armor: {2}/{3}" },
{ "^HP:(\\d+)/(\\d+)$", "HP: {0}/{1}" }, { "^HP:(\\d+)/(\\d+)$", "HP: {0}/{1}" }
}; };
public static void LoadHPStrings() public static void LoadHPStrings()
@ -56,7 +56,21 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.MinorObjects
if (textTMP) if (textTMP)
{ {
string origText = textTMP.text; string origText = textTMP.text;
string translatedText = TranslateHPText(origText, __instance); string translatedText = origText;
string pattern = "([^\\s]+)阶\n([^\\s]+)";
if (StringStore.TestRegex(translatedText, pattern))
{
var regex = new Regex(pattern);
var match = regex.Match(translatedText);
string regularText = match.Groups[2].Value;
string halfText = TranslateHPText(regularText, __instance);
string fStr = StringStore.translationStringRegex[pattern];
translatedText = string.Format(fStr, [match.Groups[1].Value, halfText]);
}
else
{
translatedText = TranslateHPText(origText, __instance);
}
textTMP.text = translatedText == origText ? origText : translatedText; textTMP.text = translatedText == origText ? origText : translatedText;
textTMP.autoSizeTextContainer = false; textTMP.autoSizeTextContainer = false;
if (translatedText != origText) if (translatedText != origText)

View file

@ -346,10 +346,17 @@ namespace PvZ_Fusion_Translator
public static bool CheckForUntranslatedText(string text) public static bool CheckForUntranslatedText(string text)
{ {
Regex regex = new("\\p{IsCJKUnifiedIdeographs}+"); Regex regex = new("\\p{IsCJKUnifiedIdeographs}+");
try
{
Match match = regex.Match(text); Match match = regex.Match(text);
return match.Success; return match.Success;
} }
catch(Exception e)
{
Log.LogError(e);
}
return false;
}
#if MULTI_LANGUAGE #if MULTI_LANGUAGE
internal static void ChangeLanguage(string language) internal static void ChangeLanguage(string language)