From ade617e3e45f339c9427edbb9a67593db6b11443 Mon Sep 17 00:00:00 2001 From: SillyStar-Github <90102598+SillyStar-Github@users.noreply.github.com> Date: Thu, 25 Dec 2025 23:59:58 -0500 Subject: [PATCH] 3.2.1 version 1 code (no almanac keyword/formula search) --- MelonLoader/Core.cs | 2 +- .../GameObjects/AlmanacPlantMenu_Patch.cs | 85 +------------------ .../ExploreMakeZombieButton_Patch.cs | 6 +- .../ButtonObjects/UIButton_Patch.cs | 40 +++++++++ .../Patches/GameObjects/EveBuffMenu_Patch.cs | 38 +++++++++ .../GameObjects/EveOptionWindow_Patch.cs | 50 +++++++++++ .../GameObjects/NoticePauseMenu_Patch.cs | 2 +- .../GameObjects/PlantDamageMenu_Patch.cs | 37 ++++++++ .../Patches/Managers/IZBottomMenu_Patch.cs | 36 +++----- ...PvZ Fusion Translator [MelonLoader].csproj | 3 + MelonLoader/Utils.cs | 44 ++++++++++ 11 files changed, 234 insertions(+), 109 deletions(-) create mode 100644 MelonLoader/Patches/GameObjects/EveBuffMenu_Patch.cs create mode 100644 MelonLoader/Patches/GameObjects/EveOptionWindow_Patch.cs create mode 100644 MelonLoader/Patches/GameObjects/PlantDamageMenu_Patch.cs diff --git a/MelonLoader/Core.cs b/MelonLoader/Core.cs index 54cddc8..c9eb3b3 100644 --- a/MelonLoader/Core.cs +++ b/MelonLoader/Core.cs @@ -35,7 +35,7 @@ namespace PvZ_Fusion_Translator AudioStore.Init(); FontStore.Init(); Utils.RegisterPlantIndices(); - + //Utils.RegisterRecipeLinks(); } public override void OnLateInitializeMelon() diff --git a/MelonLoader/Patches/GameObjects/AlmanacPlantMenu_Patch.cs b/MelonLoader/Patches/GameObjects/AlmanacPlantMenu_Patch.cs index 3dbf239..80ab13e 100644 --- a/MelonLoader/Patches/GameObjects/AlmanacPlantMenu_Patch.cs +++ b/MelonLoader/Patches/GameObjects/AlmanacPlantMenu_Patch.cs @@ -7,6 +7,7 @@ using Unity.Collections; using Unity.VisualScripting; using UnityEngine; using UnityEngine.UI; +using static Il2Cpp.AlmanacPlantBank; using static PvZ_Fusion_Translator.FileLoader; using static PvZ_Fusion_Translator.Utils; @@ -50,7 +51,7 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects public static bool SearchChanged(AlmanacPlantMenu __instance, ref TMP_InputField inputField) { string search = inputField.text; - if (search != StringStore.TranslateText("搜索")) + if (search != StringStore.TranslateText("搜索") && search != "") { string currentLanguage = Utils.Language.ToString(); string almanacDir = GetAssetDir(AssetType.Almanac, Utils.Language); @@ -62,30 +63,12 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects TMP_FontAsset fontAsset = FontStore.LoadTMPFont(currentLanguage); - AlmanacPlantBank.PlantData plantData = JsonUtility.FromJson(json); + PlantData plantData = JsonUtility.FromJson(json); Il2CppSystem.Collections.Generic.List searchedPlants = new(); - foreach (AlmanacPlantBank.PlantInfo plantInfo in plantData.plants) + foreach (PlantInfo plantInfo in plantData.plants) { - //List uniqueIngredients = GetIngredients(plantInfo.seedType); - - //// ingredient-based search - - //foreach(int i in uniqueIngredients) - //{ - // string name = Utils.GetPlantNameFromAlmanac((PlantType)i); - - // if(name.ToLower().Contains(search.ToLower())) - // { - // searchedPlants.Add((PlantType)i); - // } - //} - - // keyword-based search - - // name-based search - if (plantInfo.name.ToLower().Contains(search.ToLower())) { searchedPlants.Add((PlantType)plantInfo.seedType); @@ -99,65 +82,5 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects return true; } - - private static List GetIngredients(int theSeedType) - { - List ingredientPairs = new(); - Il2CppSystem.Array mixData = MixData.data.Cast(); - int rows = mixData.GetLength(0); - int columns = mixData.GetLength(1); - - for(int i = 0; i < rows; i++) - { - for(int j = 0; j < columns; j++) - { - if(ContainsFormula(theSeedType)) - { - if (mixData.GetValue(i, j).Unbox() == theSeedType) - { - if (!ingredientPairs.Contains(i)) - { - ingredientPairs.Add(i); - } - - if (!ingredientPairs.Contains(j)) - { - ingredientPairs.Add(j); - } - } - } - else - { - return ingredientPairs; - } - - } - } - - return ingredientPairs; - } - - private static bool ContainsFormula(int theSeedType) - { - bool contains = false; - - Il2CppSystem.Array mixData = MixData.data.Cast(); - int rows = mixData.GetLength(0); - int columns = mixData.GetLength(1); - - for (int i = 0; i < rows; i++) - { - for (int j = 0; j < columns; j++) - { - if (mixData.GetValue(i, j).Unbox() == theSeedType) - { - contains = true; - return contains; - } - } - } - - return contains; - } } } diff --git a/MelonLoader/Patches/GameObjects/ButtonObjects/ExploreMakeZombieButton_Patch.cs b/MelonLoader/Patches/GameObjects/ButtonObjects/ExploreMakeZombieButton_Patch.cs index da7b212..49d638f 100644 --- a/MelonLoader/Patches/GameObjects/ButtonObjects/ExploreMakeZombieButton_Patch.cs +++ b/MelonLoader/Patches/GameObjects/ButtonObjects/ExploreMakeZombieButton_Patch.cs @@ -13,8 +13,8 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.ButtonObjects [HarmonyPatch(nameof(ExploreMakeZombieButton.Start))] public static void Start(ExploreMakeZombieButton __instance) { - //__instance.sceneText.text = Utils.GetZombieNameFromAlmanac(__instance.theZombieType); - //__instance.sceneText.font = FontStore.LoadTMPFont(Utils.Language.ToString()); - } + __instance.sceneText.text = Utils.GetZombieNameFromAlmanac(__instance.theZombieType); + __instance.sceneText.font = FontStore.LoadTMPFont(Utils.Language.ToString()); + } } } \ No newline at end of file diff --git a/MelonLoader/Patches/GameObjects/ButtonObjects/UIButton_Patch.cs b/MelonLoader/Patches/GameObjects/ButtonObjects/UIButton_Patch.cs index cc57702..22dbe7a 100644 --- a/MelonLoader/Patches/GameObjects/ButtonObjects/UIButton_Patch.cs +++ b/MelonLoader/Patches/GameObjects/ButtonObjects/UIButton_Patch.cs @@ -41,5 +41,45 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects } } } + + [HarmonyPatch(nameof(UIButton.OnMouseUpAsButton))] + [HarmonyPostfix] + public static void Postfix_OnMouseUpAsButton(UIButton __instance) + { + try + { + if (__instance.transform.childCount >= 2) + { + if (__instance.name == "EditMode") + { + TMP_FontAsset fontAsset = FontStore.LoadTMPFont(Utils.Language.ToString()); + + TextMeshProUGUI text = __instance.transform.GetChild(0).GetComponent(); + TextMeshProUGUI[] extraTexts = __instance.transform.GetChild(1).GetComponentsInChildren(); + + text.text = StringStore.TranslateText(text.text, false); + text.font = fontAsset; + + foreach (TextMeshProUGUI extraText in extraTexts) + { + extraText.text = StringStore.TranslateText(extraText.text, false); + extraText.font = fontAsset; + } + } + else + { + TextMeshProUGUI text = __instance.transform.GetChild(0).GetComponent(); + TextMeshProUGUI textShadow = __instance.transform.GetChild(1).GetComponent(); + + text.text = StringStore.TranslateText(text.text, false); + textShadow.text = StringStore.TranslateText(textShadow.text, false); + + text.font = FontStore.LoadTMPFont(Utils.Language.ToString()); + textShadow.font = FontStore.LoadTMPFont(Utils.Language.ToString()); + } + } + } + catch (Exception) { } + } } } diff --git a/MelonLoader/Patches/GameObjects/EveBuffMenu_Patch.cs b/MelonLoader/Patches/GameObjects/EveBuffMenu_Patch.cs new file mode 100644 index 0000000..51725ab --- /dev/null +++ b/MelonLoader/Patches/GameObjects/EveBuffMenu_Patch.cs @@ -0,0 +1,38 @@ +using HarmonyLib; +using Il2Cpp; +using Il2CppTMPro; +using PvZ_Fusion_Translator.AssetStore; +using UnityEngine; +using System.Text.RegularExpressions; + +namespace PvZ_Fusion_Translator.Patches.GameObjects +{ + [HarmonyPatch(typeof(EveBuffMenu))] + public static class EveBuffMenu_Patch + { + [HarmonyPatch(nameof(EveBuffMenu.Start))] + [HarmonyPostfix] + private static void Start(EveBuffMenu __instance) + { + foreach (EveOptionWindow window in __instance.windows) + { + EveOptionWindow_Patch.TranslateEveOptionWindow(window); + } + + foreach (TextMeshProUGUI text in __instance.quitButton.GetComponentsInChildren()) + { + text.text = StringStore.TranslateText("合上"); + } + } + + [HarmonyPatch(nameof(EveBuffMenu.InitWindows))] + [HarmonyPostfix] + private static void InitWindows(EveBuffMenu __instance) + { + foreach(EveOptionWindow window in __instance.windows) + { + EveOptionWindow_Patch.TranslateEveOptionWindow(window); + } + } + } +} diff --git a/MelonLoader/Patches/GameObjects/EveOptionWindow_Patch.cs b/MelonLoader/Patches/GameObjects/EveOptionWindow_Patch.cs new file mode 100644 index 0000000..3d0a4c0 --- /dev/null +++ b/MelonLoader/Patches/GameObjects/EveOptionWindow_Patch.cs @@ -0,0 +1,50 @@ +using HarmonyLib; +using Il2Cpp; +using Il2CppTMPro; +using PvZ_Fusion_Translator.AssetStore; +using UnityEngine; +using System.Text.RegularExpressions; + +namespace PvZ_Fusion_Translator.Patches.GameObjects +{ + [HarmonyPatch(typeof(EveOptionWindow))] + public static class EveOptionWindow_Patch + { + [HarmonyPatch(nameof(EveOptionWindow.SetType))] + [HarmonyPostfix] + private static void SetType(EveOptionWindow __instance) + { + TranslateEveOptionWindow(__instance); + } + + [HarmonyPatch(nameof(EveOptionWindow.OnAnimOver))] + [HarmonyPostfix] + private static void OnAnimOver(EveOptionWindow __instance) + { + TranslateEveOptionWindow(__instance); + } + + [HarmonyPatch(nameof(EveOptionWindow.SelectedByRow))] + [HarmonyPostfix] + private static void SelectedByRow(EveOptionWindow __instance) + { + TranslateEveOptionWindow(__instance); + } + + public static void TranslateEveOptionWindow(EveOptionWindow __instance) + { + Transform introduceTransform = __instance.transform.FindChild("Images").FindChild("Introduce"); + TextMeshProUGUI introduceText = introduceTransform.GetComponent(); + TMP_FontAsset fontAsset = FontStore.LoadTMPFont(Utils.Language.ToString()); + + introduceText.text = StringStore.TranslateText(introduceText.text); + introduceText.font = fontAsset; + + foreach (TextMeshProUGUI text in __instance.buttonText) + { + text.text = StringStore.TranslateText(text.text); + text.font = fontAsset; + } + } + } +} diff --git a/MelonLoader/Patches/GameObjects/NoticePauseMenu_Patch.cs b/MelonLoader/Patches/GameObjects/NoticePauseMenu_Patch.cs index 2e73634..cdfd61e 100644 --- a/MelonLoader/Patches/GameObjects/NoticePauseMenu_Patch.cs +++ b/MelonLoader/Patches/GameObjects/NoticePauseMenu_Patch.cs @@ -38,7 +38,7 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects string changelogText = File.ReadAllText(changelogDir); - GameObject contentObject = __instance.transform.FindChild("Scroll View").GetChild(0).GetChild(0).gameObject; //bc normal Find or FindChild didn't work + GameObject contentObject = __instance.transform.FindChild("Scroll View/Viewport/Content").gameObject; TextMeshProUGUI contentText = contentObject.GetComponent(); contentText.text = changelogText; diff --git a/MelonLoader/Patches/GameObjects/PlantDamageMenu_Patch.cs b/MelonLoader/Patches/GameObjects/PlantDamageMenu_Patch.cs new file mode 100644 index 0000000..c5e3bb1 --- /dev/null +++ b/MelonLoader/Patches/GameObjects/PlantDamageMenu_Patch.cs @@ -0,0 +1,37 @@ +using HarmonyLib; +using Il2Cpp; +using Il2CppTMPro; +using PvZ_Fusion_Translator.AssetStore; +using UnityEngine; +using System.Text.RegularExpressions; + +namespace PvZ_Fusion_Translator.Patches.GameObjects +{ + [HarmonyPatch(typeof(PlantDamageMenu))] + public static class PlantDamageMenu_Patch + { + [HarmonyPatch(nameof(PlantDamageMenu.Awake))] + [HarmonyPostfix] + private static void Awake(PlantDamageMenu __instance) + { + TMP_FontAsset fontAsset = FontStore.LoadTMPFont(Utils.Language.ToString()); + foreach(TextMeshProUGUI text in __instance.GetComponentsInChildren()) + { + text.text = StringStore.TranslateText(text.text); + text.font = fontAsset; + } + } + + [HarmonyPatch(nameof(PlantDamageMenu.InitCards))] + [HarmonyPostfix] + private static void InitCards(PlantDamageMenu __instance) + { + TMP_FontAsset fontAsset = FontStore.LoadTMPFont(Utils.Language.ToString()); + foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren()) + { + text.text = StringStore.TranslateText(text.text); + text.font = fontAsset; + } + } + } +} diff --git a/MelonLoader/Patches/Managers/IZBottomMenu_Patch.cs b/MelonLoader/Patches/Managers/IZBottomMenu_Patch.cs index 976806b..8630912 100644 --- a/MelonLoader/Patches/Managers/IZBottomMenu_Patch.cs +++ b/MelonLoader/Patches/Managers/IZBottomMenu_Patch.cs @@ -14,24 +14,19 @@ namespace PvZ_Fusion_Translator.Patches.Managers [HarmonyPostfix] public static void ChangeStringz(IZBottomMenu __instance) { - TextMeshProUGUI[] array = - [ - __instance.transform.GetChild(0).GetComponent(), - __instance.transform.GetChild(0).GetChild(0).GetComponent(), - ]; - for (int i = 0; i < array.Length; i++) + foreach(TextMeshProUGUI text in __instance.levelName) { string fStr = StringStore.translationStringRegex["([^\\s]+),推荐难度:(\\d+)"]; - string[] parts = array[i].text.Split(",推荐难度:"); + string[] parts = text.text.Split(",推荐难度:"); - if(new Regex("([^\\s]+),推荐难度:(\\d+)").IsMatch(array[i].text)) + if(new Regex("([^\\s]+),推荐难度:(\\d+)").IsMatch(text.text)) { string customName = (StringStore.translationString.ContainsKey(parts[0] + "_IZ")) ? parts[0] + "_IZ" : parts[0]; - array[i].text = string.Format(fStr, StringStore.TranslateText(customName), parts[1]); + text.text = string.Format(fStr, StringStore.TranslateText(customName), parts[1]); } - - array[i] = StringStore.TranslateText(array[i]); - array[i].text = array[i].text.Replace("\n", " "); + + text.text = StringStore.TranslateText(text.text); + text.text = text.text.Replace("\n", " "); } } @@ -39,23 +34,18 @@ namespace PvZ_Fusion_Translator.Patches.Managers [HarmonyPostfix] public static void SetLevelName(IZBottomMenu __instance) { - TextMeshProUGUI[] array = - [ - __instance.transform.GetChild(0).GetComponent(), - __instance.transform.GetChild(0).GetChild(0).GetComponent(), - ]; - for (int i = 0; i < array.Length; i++) + foreach(TextMeshProUGUI text in __instance.levelName) { string fStr = StringStore.translationStringRegex["([^\\s]+),推荐难度:(\\d+)"]; - string[] parts = array[i].text.Split(",推荐难度:"); + string[] parts = text.text.Split(",推荐难度:"); - if (new Regex("([^\\s]+),推荐难度:(\\d+)").IsMatch(array[i].text)) + if (new Regex("([^\\s]+),推荐难度:(\\d+)").IsMatch(text.text)) { - array[i].text = string.Format(fStr, StringStore.TranslateText(parts[0]), parts[1]); + text.text = string.Format(fStr, StringStore.TranslateText(parts[0]), parts[1]); } - array[i] = StringStore.TranslateText(array[i]); - array[i].text = array[i].text.Replace("\n", " "); + text.text = StringStore.TranslateText(text.text); + text.text = text.text.Replace("\n", " "); } } } diff --git a/MelonLoader/PvZ Fusion Translator [MelonLoader].csproj b/MelonLoader/PvZ Fusion Translator [MelonLoader].csproj index e2604aa..02a6774 100644 --- a/MelonLoader/PvZ Fusion Translator [MelonLoader].csproj +++ b/MelonLoader/PvZ Fusion Translator [MelonLoader].csproj @@ -406,6 +406,9 @@ + + + diff --git a/MelonLoader/Utils.cs b/MelonLoader/Utils.cs index e44fad9..6bb2e79 100644 --- a/MelonLoader/Utils.cs +++ b/MelonLoader/Utils.cs @@ -7,6 +7,7 @@ using PvZ_Fusion_Translator.AssetStore; using PvZ_Fusion_Translator.Patches.GameObjects.MinorObjects; using System.Diagnostics; using System.Text.RegularExpressions; +using Unity.Burst.Intrinsics; using UnityEngine; using UnityEngine.Experimental.Rendering; using UnityEngine.UI; @@ -299,6 +300,49 @@ namespace PvZ_Fusion_Translator } } + public static Dictionary> recipeLinks = new Dictionary>(); + + public static void RegisterRecipeLinks() + { + recipeLinks.Clear(); + + Il2CppSystem.Array mixData = MixData.data.Cast(); + int rows = mixData.GetLength(0); + int columns = mixData.GetLength(1); + + foreach (PlantType plantType in Enum.GetValues(typeof(PlantType))) + { + int seedType = (int)plantType; + + recipeLinks.Add(seedType, new()); + + if(seedType > 0) + { + foreach (PlantType checkType in Enum.GetValues(typeof(PlantType))) + { + if(checkType > 0) + { + int[] checks = [mixData.GetValue(seedType, (int)checkType).Unbox(), mixData.GetValue((int)checkType, seedType).Unbox()]; + + if (checks[0] != 0) + { + recipeLinks[seedType].Add(checks[0]); + } + + if (checks[1] != 0) + { + recipeLinks[seedType].Add(checks[1]); + } + } + } + } + if (recipeLinks[seedType].Count > 0) + { + Log.LogDebug($"Found {recipeLinks[seedType].Count} fusions for {seedType}!"); + } + } + } + public static bool CheckForUntranslatedText(string text) { Regex regex = new("\\p{IsCJKUnifiedIdeographs}+");