From a89ba825ee55469e2f5e6a3fce00bad3176c0e74 Mon Sep 17 00:00:00 2001
From: SillyStar-Github <90102598+SillyStar-Github@users.noreply.github.com>
Date: Mon, 19 Jan 2026 03:59:22 -0500
Subject: [PATCH] a few updates
---
MelonLoader/AssetStore/StringStore.cs | 57 ++++++++++++++++++-
.../BaseTextObjects/InGameText_Patch.cs | 6 ++
.../GameObjects/AdvantureMenu_Patch.cs | 29 +++++++---
.../GameObjects/AdvantureMissionMenu_Patch.cs | 33 +++++++++--
.../GameObjects/MinorObjects/Zombie_Patch.cs | 18 +++++-
MelonLoader/Utils.cs | 13 ++++-
6 files changed, 137 insertions(+), 19 deletions(-)
diff --git a/MelonLoader/AssetStore/StringStore.cs b/MelonLoader/AssetStore/StringStore.cs
index 24ec52b..9ea3ff5 100644
--- a/MelonLoader/AssetStore/StringStore.cs
+++ b/MelonLoader/AssetStore/StringStore.cs
@@ -124,6 +124,61 @@ namespace PvZ_Fusion_Translator.AssetStore
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 = @"(]*>.*?)";
+ 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, @"(]+>)(.*?)()", 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)
{
if (TestRegex(originalText, pattern) && translationStringRegex.ContainsKey(pattern))
@@ -214,7 +269,7 @@ namespace PvZ_Fusion_Translator.AssetStore
return originalText;
}
- private static bool TestRegex(string originalText, string pattern)
+ public static bool TestRegex(string originalText, string pattern)
{
return Regex.IsMatch(originalText, pattern);
}
diff --git a/MelonLoader/Patches/BaseTextObjects/InGameText_Patch.cs b/MelonLoader/Patches/BaseTextObjects/InGameText_Patch.cs
index e89d4ad..035dc17 100644
--- a/MelonLoader/Patches/BaseTextObjects/InGameText_Patch.cs
+++ b/MelonLoader/Patches/BaseTextObjects/InGameText_Patch.cs
@@ -3,6 +3,7 @@ using Il2Cpp;
using Il2CppTMPro;
using PvZ_Fusion_Translator.AssetStore;
using PvZ_Fusion_Translator.Patches.Managers;
+using System.Text.RegularExpressions;
using UnityEngine.TextCore.Text;
namespace PvZ_Fusion_Translator.Patches.BaseTextObjects
@@ -30,6 +31,11 @@ namespace PvZ_Fusion_Translator.Patches.BaseTextObjects
}
txt.font = fontAsset;
}
+
+ if(__instance.TryGetComponent(out TextMeshProUGUI text))
+ {
+ text.text = StringStore.TranslateText(text.text);
+ }
}
}
}
\ No newline at end of file
diff --git a/MelonLoader/Patches/GameObjects/AdvantureMenu_Patch.cs b/MelonLoader/Patches/GameObjects/AdvantureMenu_Patch.cs
index 62ff70e..0da395b 100644
--- a/MelonLoader/Patches/GameObjects/AdvantureMenu_Patch.cs
+++ b/MelonLoader/Patches/GameObjects/AdvantureMenu_Patch.cs
@@ -12,41 +12,52 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects
[HarmonyPostfix]
private static void Start(AdvantureMenu __instance)
{
- foreach (TextMeshProUGUI titleText in __instance.GetComponentsInChildren())
+ __instance.difficulty.text = Utils.RemoveSizeTags(StringStore.TranslateText(__instance.difficulty.text));
+ foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren())
{
- 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))]
[HarmonyPostfix]
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())
+ {
+ text.text = StringStore.TranslateText(text.text);
+ }
}
[HarmonyPatch(nameof(AdvantureMenu.GetChapterDifficulty))]
[HarmonyPostfix]
private static void GetChapterDifficulty(AdvantureMenu __instance)
{
- __instance.chapterDifficulty.text = StringStore.TranslateText(__instance.chapterDifficulty.text, pattern: "章节难度:([^\\s]+)");
+ foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren())
+ {
+ text.text = StringStore.TranslateText(text.text);
+ }
}
[HarmonyPatch(nameof(AdvantureMenu.UpdateChapterDifficulty))]
[HarmonyPostfix]
private static void UpdateChapterDifficulty(AdvantureMenu __instance)
{
- __instance.chapterDifficulty.text = StringStore.TranslateText(__instance.chapterDifficulty.text, pattern: "章节难度:([^\\s]+)");
+ foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren())
+ {
+ text.text = StringStore.TranslateText(text.text);
+ }
}
[HarmonyPatch(nameof(AdvantureMenu.UpdateStarCount))]
[HarmonyPostfix]
private static void UpdateStarCount(AdvantureMenu __instance)
{
- __instance.starCount.text = StringStore.TranslateText(__instance.starCount.text);
+ foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren())
+ {
+ text.text = StringStore.TranslateText(text.text);
+ }
}
}
}
diff --git a/MelonLoader/Patches/GameObjects/AdvantureMissionMenu_Patch.cs b/MelonLoader/Patches/GameObjects/AdvantureMissionMenu_Patch.cs
index 1f5268e..bb23854 100644
--- a/MelonLoader/Patches/GameObjects/AdvantureMissionMenu_Patch.cs
+++ b/MelonLoader/Patches/GameObjects/AdvantureMissionMenu_Patch.cs
@@ -8,24 +8,49 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects
[HarmonyPatch(typeof(AdvantureMissionMenu))]
public static class AdvantureMissionMenu_Patch
{
+ [HarmonyPatch(nameof(AdvantureMissionMenu.Awake))]
+ [HarmonyPostfix]
+ private static void Awake(AdvantureMissionMenu __instance)
+ {
+ foreach (TextMeshProUGUI text in __instance.contant.GetComponentsInChildren())
+ {
+ text.text = StringStore.TranslateColorText(text.text);
+ }
+
+ //foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren())
+ //{
+ // text.text = StringStore.TranslateText(text.text);
+ //}
+ }
+
[HarmonyPatch(nameof(AdvantureMissionMenu.InitText))]
[HarmonyPostfix]
private static void InitText(AdvantureMissionMenu __instance)
{
- foreach(TextMeshProUGUI text in __instance.GetComponentsInChildren())
+ foreach (TextMeshProUGUI text in __instance.contant.GetComponentsInChildren())
{
- text.text = StringStore.TranslateText(text.text);
+ text.text = StringStore.TranslateColorText(text.text);
}
+
+ //foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren())
+ //{
+ // text.text = StringStore.TranslateText(text.text);
+ //}
}
[HarmonyPatch(nameof(AdvantureMissionMenu.SwitchAward))]
[HarmonyPostfix]
private static void SwitchAward(AdvantureMissionMenu __instance)
{
- foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren())
+ foreach(TextMeshProUGUI text in __instance.contant.GetComponentsInChildren())
{
- text.text = StringStore.TranslateText(text.text);
+ text.text = StringStore.TranslateColorText(text.text);
}
+
+ //foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren())
+ //{
+ // text.text = StringStore.TranslateText(text.text);
+ //}
}
}
}
diff --git a/MelonLoader/Patches/GameObjects/MinorObjects/Zombie_Patch.cs b/MelonLoader/Patches/GameObjects/MinorObjects/Zombie_Patch.cs
index bcdd189..3966670 100644
--- a/MelonLoader/Patches/GameObjects/MinorObjects/Zombie_Patch.cs
+++ b/MelonLoader/Patches/GameObjects/MinorObjects/Zombie_Patch.cs
@@ -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+)$", "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+)$", "HP: {0}/{1}" },
+ { "^HP:(\\d+)/(\\d+)$", "HP: {0}/{1}" }
};
public static void LoadHPStrings()
@@ -56,7 +56,21 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.MinorObjects
if (textTMP)
{
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.autoSizeTextContainer = false;
if (translatedText != origText)
diff --git a/MelonLoader/Utils.cs b/MelonLoader/Utils.cs
index 6bb2e79..01b675e 100644
--- a/MelonLoader/Utils.cs
+++ b/MelonLoader/Utils.cs
@@ -346,9 +346,16 @@ namespace PvZ_Fusion_Translator
public static bool CheckForUntranslatedText(string text)
{
Regex regex = new("\\p{IsCJKUnifiedIdeographs}+");
- Match match = regex.Match(text);
-
- return match.Success;
+ try
+ {
+ Match match = regex.Match(text);
+ return match.Success;
+ }
+ catch(Exception e)
+ {
+ Log.LogError(e);
+ }
+ return false;
}
#if MULTI_LANGUAGE