From b5a1c031af0b84e9111134663cd93dca4af13dfc Mon Sep 17 00:00:00 2001 From: SillyStar-Github <90102598+SillyStar-Github@users.noreply.github.com> Date: Wed, 21 Jan 2026 16:57:36 -0500 Subject: [PATCH] some of the final code fixes, 3.3.1 --- MelonLoader/AssetStore/StringStore.cs | 120 +++++++++--------- .../BaseTextObjects/InGameText_Patch.cs | 33 ++++- .../AdvantureEvolutionMenu_Patch.cs | 69 +++++++++- .../GameObjects/AdvantureMenu_Patch.cs | 36 +++--- .../GameObjects/MinorObjects/Zombie_Patch.cs | 15 +-- .../GameObjects/NoticePauseMenu_Patch.cs | 17 ++- .../Patches/Managers/CanvasUpUI_Patch.cs | 4 +- ...PvZ Fusion Translator [MelonLoader].csproj | 2 +- 8 files changed, 204 insertions(+), 92 deletions(-) diff --git a/MelonLoader/AssetStore/StringStore.cs b/MelonLoader/AssetStore/StringStore.cs index 9ea3ff5..b37d343 100644 --- a/MelonLoader/AssetStore/StringStore.cs +++ b/MelonLoader/AssetStore/StringStore.cs @@ -124,61 +124,6 @@ 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)) @@ -266,12 +211,73 @@ namespace PvZ_Fusion_Translator.AssetStore if (isLog) Log.LogDebug($"Text '{originalText}' not translated"); + if(Utils.CheckForUntranslatedText(originalText)) + { + FileLoader.DumpUntranslatedStrings(originalText); + } return originalText; } - public static bool TestRegex(string originalText, string pattern) + public static string TranslateColorText(string originalText, bool isLog = false) + { + string text = TranslateColorSegments(originalText); + 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 TranslateColorSegments(string input) { - return Regex.IsMatch(originalText, pattern); + string result = ""; + string pattern = @"]+>[\s\S]*?<\/color>|(?:<(?!color=)|[^<])+"; + string smallPattern = @"(]+>)(.*?)()"; + MatchCollection matches = Regex.Matches(input, pattern); + + for (int i = 0; i < matches.Count; i++) + { + Match match = matches[i]; + if (Regex.Match(match.Value, smallPattern, RegexOptions.Singleline).Success) + { + result += TranslateColorSegment(match.Value); + } + else + { + result += DoTranslateText(match.Value); + } + } + + return result; + } + + public static string TranslateColorSegment(string input) + { + string result = input; + string pattern = @"(]+>)(.*?)()"; + Match match = Regex.Match(input, pattern, RegexOptions.Singleline); + string translatedText = DoTranslateText(match.Groups[2].Value); + if(Utils.CheckForUntranslatedText(translatedText)) + { + FileLoader.DumpUntranslatedStrings(translatedText); + } + result = match.Groups[1].Value + translatedText + match.Groups[3].Value; + + return result; + } + public static bool TestRegex(string originalText, string pattern, bool singleLine = false) + { + RegexOptions options = (singleLine) ? RegexOptions.None : RegexOptions.Singleline; + return Regex.IsMatch(originalText, pattern, options); } private static TextAlignmentOptions TextAnchorToTMPAlignment(TextAnchor anchor) diff --git a/MelonLoader/Patches/BaseTextObjects/InGameText_Patch.cs b/MelonLoader/Patches/BaseTextObjects/InGameText_Patch.cs index 035dc17..46e55f0 100644 --- a/MelonLoader/Patches/BaseTextObjects/InGameText_Patch.cs +++ b/MelonLoader/Patches/BaseTextObjects/InGameText_Patch.cs @@ -4,7 +4,8 @@ using Il2CppTMPro; using PvZ_Fusion_Translator.AssetStore; using PvZ_Fusion_Translator.Patches.Managers; using System.Text.RegularExpressions; -using UnityEngine.TextCore.Text; +using System.Xml.Linq; +using UnityEngine; namespace PvZ_Fusion_Translator.Patches.BaseTextObjects { @@ -17,17 +18,45 @@ namespace PvZ_Fusion_Translator.Patches.BaseTextObjects { TMP_FontAsset fontAsset = FontStore.LoadTMPFont(Utils.Language.ToString()); - foreach (TextMeshProUGUI txt in __instance.textMeshes) + foreach (TextMeshProUGUI txt in __instance.GetComponentsInChildren()) { string travelMatch = TravelMgr_Patch.MatchTravelBuff(txt.text); + string originalText = ""; + + if (txt.gameObject.name.Contains("shadow")) + { + continue; + } if (travelMatch != "") { txt.text = travelMatch; } + else if(Regex.Match(txt.text, @"(]*>.*?)", RegexOptions.Singleline).Success) + { + txt.text = StringStore.TranslateColorText(txt.text, true); + if (txt.gameObject.name.Contains("main")) + { + originalText = txt.text; + Transform shadowText = txt.transform.parent.Find("Text_shadow"); + if (shadowText != null) + { + shadowText.GetComponent().text = Utils.RemoveColorTags(originalText); + } + } + } else { txt.text = StringStore.TranslateText(txt.text, true); + if (txt.gameObject.name.Contains("main")) + { + originalText = txt.text; + Transform shadowText = txt.transform.parent.Find("Text_shadow"); + if (shadowText != null) + { + shadowText.GetComponent().text = Utils.RemoveColorTags(originalText); + } + } } txt.font = fontAsset; } diff --git a/MelonLoader/Patches/GameObjects/AdvantureEvolutionMenu_Patch.cs b/MelonLoader/Patches/GameObjects/AdvantureEvolutionMenu_Patch.cs index e07e77c..30399e7 100644 --- a/MelonLoader/Patches/GameObjects/AdvantureEvolutionMenu_Patch.cs +++ b/MelonLoader/Patches/GameObjects/AdvantureEvolutionMenu_Patch.cs @@ -2,12 +2,79 @@ using Il2Cpp; using Il2CppTMPro; using PvZ_Fusion_Translator.AssetStore; +using UnityEngine; namespace PvZ_Fusion_Translator.Patches.GameObjects { [HarmonyPatch(typeof(AdvantureEvolutionMenu))] public static class AdvantureEvolutionMenu_Patch { - + [HarmonyPatch(nameof(AdvantureMenu.Awake))] + [HarmonyPostfix] + public static void Awake(AdvantureEvolutionMenu __instance) + { + foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren()) + { + text.text = StringStore.TranslateColorText(text.text); + } + + Transform baseWindow = __instance.transform.Find("BaseWindow"); + Transform advancedWindow = __instance.transform.Find("AdvancedWindow"); + + if(baseWindow != null) + { + foreach(TextMeshProUGUI text in baseWindow.GetComponentsInChildren()) + { + if(text.gameObject.name.Contains("Name")) + { + text.text = Utils.GetPlantNameFromAlmanac(__instance.thePlantType); + } + } + } + + if (advancedWindow != null) + { + foreach (TextMeshProUGUI text in advancedWindow.GetComponentsInChildren()) + { + if (text.gameObject.name.Contains("Name")) + { + text.text = Utils.GetPlantNameFromAlmanac(__instance.thePlantType); + } + } + } + } + + [HarmonyPatch(nameof(AdvantureEvolutionMenu.UpdateText))] + [HarmonyPostfix] + public static void UpdateText(AdvantureEvolutionMenu __instance) + { + foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren()) + { + FileLoader.DumpUntranslatedStrings(text.text); + text.text = StringStore.TranslateColorText(text.text); + } + } + + [HarmonyPatch(nameof(AdvantureEvolutionMenu.RollEvolution))] + [HarmonyPostfix] + public static void RollEvolution(AdvantureEvolutionMenu __instance) + { + foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren()) + { + FileLoader.DumpUntranslatedStrings(text.text); + text.text = StringStore.TranslateColorText(text.text); + } + } + + [HarmonyPatch(nameof(AdvantureEvolutionMenu.RollEvolutionGood))] + [HarmonyPostfix] + public static void RollEvolutionGood(AdvantureEvolutionMenu __instance) + { + foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren()) + { + FileLoader.DumpUntranslatedStrings(text.text); + text.text = StringStore.TranslateColorText(text.text); + } + } } } diff --git a/MelonLoader/Patches/GameObjects/AdvantureMenu_Patch.cs b/MelonLoader/Patches/GameObjects/AdvantureMenu_Patch.cs index 0da395b..e956db4 100644 --- a/MelonLoader/Patches/GameObjects/AdvantureMenu_Patch.cs +++ b/MelonLoader/Patches/GameObjects/AdvantureMenu_Patch.cs @@ -12,48 +12,48 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects [HarmonyPostfix] private static void Start(AdvantureMenu __instance) { - __instance.difficulty.text = Utils.RemoveSizeTags(StringStore.TranslateText(__instance.difficulty.text)); - foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren()) - { - text.text = StringStore.TranslateText(text.text); - } + TranslateAdvantureMenu(__instance); } [HarmonyPatch(nameof(AdvantureMenu.ChangeDifficulty))] [HarmonyPostfix] private static void ChangeDifficulty(AdvantureMenu __instance) { - __instance.difficulty.text = Utils.RemoveSizeTags(StringStore.TranslateText(__instance.difficulty.text)); - foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren()) - { - text.text = StringStore.TranslateText(text.text); - } + TranslateAdvantureMenu(__instance); } [HarmonyPatch(nameof(AdvantureMenu.GetChapterDifficulty))] [HarmonyPostfix] private static void GetChapterDifficulty(AdvantureMenu __instance) { - foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren()) - { - text.text = StringStore.TranslateText(text.text); - } + TranslateAdvantureMenu(__instance); } [HarmonyPatch(nameof(AdvantureMenu.UpdateChapterDifficulty))] [HarmonyPostfix] private static void UpdateChapterDifficulty(AdvantureMenu __instance) { - foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren()) - { - text.text = StringStore.TranslateText(text.text); - } + TranslateAdvantureMenu(__instance); } [HarmonyPatch(nameof(AdvantureMenu.UpdateStarCount))] [HarmonyPostfix] private static void UpdateStarCount(AdvantureMenu __instance) { + TranslateAdvantureMenu(__instance); + } + + public static void TranslateAdvantureMenu(AdvantureMenu __instance) + { + __instance.difficulty.text = Utils.RemoveSizeTags(StringStore.TranslateText(__instance.difficulty.text)); + __instance.chapterDifficulty.text = StringStore.TranslateText(__instance.chapterDifficulty.text); + __instance.starCount.text = StringStore.TranslateText(__instance.starCount.text); + + foreach (TextMeshProUGUI text in __instance.title) + { + text.text = StringStore.TranslateText(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 3966670..a243e6a 100644 --- a/MelonLoader/Patches/GameObjects/MinorObjects/Zombie_Patch.cs +++ b/MelonLoader/Patches/GameObjects/MinorObjects/Zombie_Patch.cs @@ -57,15 +57,14 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.MinorObjects { string origText = textTMP.text; string translatedText = origText; - string pattern = "([^\\s]+)阶\n([^\\s]+)"; + string pattern = "(\\d+)阶\n([\\s\\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); + var match = Regex.Match(translatedText, pattern, RegexOptions.Singleline); + string tier = match.Groups[1].Value; + string healthText = TranslateHPText(match.Groups[2].Value, __instance); string fStr = StringStore.translationStringRegex[pattern]; - translatedText = string.Format(fStr, [match.Groups[1].Value, halfText]); + translatedText = string.Format(fStr, [tier, healthText]); } else { @@ -105,8 +104,7 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.MinorObjects for(int i = 0; i < HPStrings.Count; i++) { var hp = HPStrings.ElementAt(i); - Regex regex = new Regex(hp.Key); - if(regex.IsMatch(originalText)) + if(Regex.Match(originalText, hp.Key, RegexOptions.Singleline).Success) { fIndex = i; found = true; @@ -160,6 +158,7 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.MinorObjects { string fStr = HPStrings.ElementAt(fIndex).Value; string DPS = originalText.Split("\n")[1].Replace("DPS:", ""); + DPS = StringStore.TranslateText(DPS); switch (fIndex) { diff --git a/MelonLoader/Patches/GameObjects/NoticePauseMenu_Patch.cs b/MelonLoader/Patches/GameObjects/NoticePauseMenu_Patch.cs index cdfd61e..b434320 100644 --- a/MelonLoader/Patches/GameObjects/NoticePauseMenu_Patch.cs +++ b/MelonLoader/Patches/GameObjects/NoticePauseMenu_Patch.cs @@ -39,12 +39,23 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects string changelogText = File.ReadAllText(changelogDir); GameObject contentObject = __instance.transform.FindChild("Scroll View/Viewport/Content").gameObject; - - TextMeshProUGUI contentText = contentObject.GetComponent(); + + TextMeshProUGUI contentText = contentObject.GetComponent(); contentText.text = changelogText; + contentText.margin = new Vector4(6, 2, 12, 0); + + contentText.enableWordWrapping = true; + contentText.overflowMode = TextOverflowModes.ScrollRect; + + Canvas.ForceUpdateCanvases(); + contentText.ForceMeshUpdate(); + + //float textHeight = contentText.preferredHeight; + //RectTransform contentRect = contentObject.GetComponent(); + //contentRect.sizeDelta = new Vector2(contentRect.sizeDelta.x, textHeight); ContentSizeFitter sizeFitter = contentObject.AddComponent(); sizeFitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize; } - } + } } \ No newline at end of file diff --git a/MelonLoader/Patches/Managers/CanvasUpUI_Patch.cs b/MelonLoader/Patches/Managers/CanvasUpUI_Patch.cs index 6f840c5..6efb242 100644 --- a/MelonLoader/Patches/Managers/CanvasUpUI_Patch.cs +++ b/MelonLoader/Patches/Managers/CanvasUpUI_Patch.cs @@ -12,13 +12,13 @@ namespace PvZ_Fusion_Translator.Patches.Managers { [HarmonyPatch(nameof(CanvasUpUI.Start))] [HarmonyPostfix] - public static void Start(IZBottomMenu __instance) + public static void Start(CanvasUpUI __instance) { TMP_FontAsset fontAsset = FontStore.LoadTMPFont(Utils.Language.ToString()); foreach(TextMeshProUGUI text in __instance.GetComponentsInChildren()) { - string originalKey = StringStore.translationString.FirstOrDefault(x => x.Value.Replace("\n", " ") == text.text).Key; + string originalKey = StringStore.translationString.FirstOrDefault(x => (x.Value == text.text || x.Value.Replace("\n", "") == text.text)).Key; text.text = (StringStore.translationString.ContainsKey(originalKey + "_MG")) ? StringStore.TranslateText(originalKey + "_MG") : StringStore.TranslateText(text.text); if(text.gameObject.name.Contains("LevelName")) { diff --git a/MelonLoader/PvZ Fusion Translator [MelonLoader].csproj b/MelonLoader/PvZ Fusion Translator [MelonLoader].csproj index e2eb414..4d83fe5 100644 --- a/MelonLoader/PvZ Fusion Translator [MelonLoader].csproj +++ b/MelonLoader/PvZ Fusion Translator [MelonLoader].csproj @@ -13,7 +13,7 @@ latest enable Debug;DebugEnglish;ReleaseMulti;ReleaseEnglish;DebugTesting;ReleaseOther;DebugObfuscate;ReleaseObfuscate - C:\Users\rainb\Desktop\PC_PVZ-Fusion_3.3.0_Part1 + C:\Users\rainb\Desktop\PC_PVZ-Fusion_3.3.1 AnyCPU;ARM64;x64