diff --git a/MelonLoader/Core.cs b/MelonLoader/Core.cs index bf447c9..587d633 100644 --- a/MelonLoader/Core.cs +++ b/MelonLoader/Core.cs @@ -34,7 +34,9 @@ namespace PvZ_Fusion_Translator StringStore.Init(); AudioStore.Init(); FontStore.Init(); - } + Utils.RegisterPlantIndices(); + + } public override void OnLateInitializeMelon() { @@ -79,6 +81,11 @@ namespace PvZ_Fusion_Translator { TowerManager_Patch.UpdateText(); } + + if (Input.GetKeyDown(KeyCode.P)) + { + Time.timeScale = 0; + } } public override void OnGUI() diff --git a/MelonLoader/Patches/BaseTextObjects/TextMeshProUGUI_Patch.cs b/MelonLoader/Patches/BaseTextObjects/TextMeshProUGUI_Patch.cs index 5c5d48d..7e2105c 100644 --- a/MelonLoader/Patches/BaseTextObjects/TextMeshProUGUI_Patch.cs +++ b/MelonLoader/Patches/BaseTextObjects/TextMeshProUGUI_Patch.cs @@ -17,12 +17,14 @@ namespace PvZ_Fusion_Translator.Patches.BaseTextObjects { if (!string.IsNullOrEmpty(__instance.text)) { + string originalText = __instance.text; __instance = StringStore.TranslateText(__instance); __instance.autoSizeTextContainer = false; - if (__instance.name.StartsWith("LevelName")) + if (__instance.name.StartsWith("LevelName")) { __instance.text = __instance.text.Replace("\n", " "); + if (__instance.name == "LevelName2" || __instance.name == "LevelName3") { TowerManager_Patch.updatedText.Add(__instance.transform); @@ -44,13 +46,9 @@ namespace PvZ_Fusion_Translator.Patches.BaseTextObjects { if (!string.IsNullOrEmpty(__instance.text)) { - __instance = StringStore.TranslateText(__instance); + __instance = StringStore.TranslateText(__instance); __instance.autoSizeTextContainer = false; - if (__instance.name.StartsWith("LevelName")) - { - __instance.text = __instance.text.Replace("\n", " "); - } if (__instance.transform.parent != null && __instance.transform.parent.name.StartsWith("Window")) { __instance.fontSizeMin = 10; diff --git a/MelonLoader/Patches/GameObjects/ButtonObjects/MainMenu_Btn_Patch.cs b/MelonLoader/Patches/GameObjects/ButtonObjects/MainMenu_Btn_Patch.cs new file mode 100644 index 0000000..ab766a9 --- /dev/null +++ b/MelonLoader/Patches/GameObjects/ButtonObjects/MainMenu_Btn_Patch.cs @@ -0,0 +1,36 @@ +using HarmonyLib; +using Il2Cpp; +using Il2CppTMPro; +using UnityEngine; +using PvZ_Fusion_Translator.AssetStore; + +namespace PvZ_Fusion_Translator.Patches.GameObjects.ButtonObjects +{ + [HarmonyPatch(typeof(MainMenu_Btn))] + public static class MainMenu_Btn_Patch + { + public static List speechBubbles = new List(); + + [HarmonyPatch(nameof(MainMenu_Btn.OnMouseEnter))] + [HarmonyPostfix] + private static void OnMouseEnter(MainMenu_Btn __instance) + { + string textToShow = null; + + if(__instance.transform.parent.name == "recipe") + { + textToShow = "探索模式"; + } + else if (__instance.name == "mysterious") + { + textToShow = "神秘模式"; + } + + if(textToShow != null) + { + InGameText.Instance.ShowText(textToShow, 2.0f); + } + } + } + +} diff --git a/MelonLoader/Patches/GameObjects/ButtonObjects/UIButton_Patch.cs b/MelonLoader/Patches/GameObjects/ButtonObjects/UIButton_Patch.cs index 375793f..4173c95 100644 --- a/MelonLoader/Patches/GameObjects/ButtonObjects/UIButton_Patch.cs +++ b/MelonLoader/Patches/GameObjects/ButtonObjects/UIButton_Patch.cs @@ -27,7 +27,11 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects text.font = FontStore.LoadTMPFont(Utils.Language.ToString()); textShadow.font = FontStore.LoadTMPFont(Utils.Language.ToString()); - text.autoSizeTextContainer = textShadow.autoSizeTextContainer = true; + + if(!text.name.Contains("_EVACUATE")) + { + text.autoSizeTextContainer = textShadow.autoSizeTextContainer = true; + } } else { diff --git a/MelonLoader/Patches/GameObjects/ExploreEnterWindow_Patch.cs b/MelonLoader/Patches/GameObjects/ExploreEnterWindow_Patch.cs index 386f040..648d2e2 100644 --- a/MelonLoader/Patches/GameObjects/ExploreEnterWindow_Patch.cs +++ b/MelonLoader/Patches/GameObjects/ExploreEnterWindow_Patch.cs @@ -16,12 +16,14 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects { PlantType thePlantType = (PlantType)__instance.selfType; - foreach (TextMeshProUGUI txt in __instance.GetComponentsInChildren()) + __instance.nameText.text = Utils.GetPlantNameFromAlmanac(thePlantType); + + if(__instance.nameText.text == "") { - StringStore.TranslateText(txt); + StringStore.TranslateText(__instance.nameText.text); } - __instance.nameText.text = Utils.GetPlantNameFromAlmanac(thePlantType); + __instance.nameText.font = FontStore.LoadTMPFont(Utils.Language.ToString()); } [HarmonyPatch(nameof(ExploreEnterWindow.SetInfo))] @@ -30,12 +32,42 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects { PlantType thePlantType = (PlantType)__instance.selfType; - foreach (TextMeshProUGUI txt in __instance.GetComponentsInChildren()) + __instance.nameText.text = Utils.GetPlantNameFromAlmanac(thePlantType); + + if (__instance.nameText.text == "") { - StringStore.TranslateText(txt); + StringStore.TranslateText(__instance.nameText.text); } + __instance.nameText.font = FontStore.LoadTMPFont(Utils.Language.ToString()); + } + + [HarmonyPatch(nameof(ExploreEnterWindow.SetIcon))] + [HarmonyPostfix] + private static void SetIcon(ExploreEnterWindow __instance) + { + PlantType thePlantType = (PlantType)__instance.selfType; + __instance.nameText.text = Utils.GetPlantNameFromAlmanac(thePlantType); + + if (__instance.nameText.text == "") + { + StringStore.TranslateText(__instance.nameText.text); + } + + __instance.nameText.font = FontStore.LoadTMPFont(Utils.Language.ToString()); + } + + [HarmonyPatch(nameof(ExploreEnterWindow.SetDisable))] + [HarmonyPostfix] + private static void SetDisable(ExploreEnterWindow __instance) + { + if (__instance.disabled) + { + __instance.nameText.text = StringStore.TranslateText("敬请期待"); + } + + __instance.nameText.font = FontStore.LoadTMPFont(Utils.Language.ToString()); } } } diff --git a/MelonLoader/Patches/GameObjects/MinorObjects/TrainingDummy_Patch.cs b/MelonLoader/Patches/GameObjects/MinorObjects/TrainingDummy_Patch.cs index de3d649..f2bb0d5 100644 --- a/MelonLoader/Patches/GameObjects/MinorObjects/TrainingDummy_Patch.cs +++ b/MelonLoader/Patches/GameObjects/MinorObjects/TrainingDummy_Patch.cs @@ -9,8 +9,8 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.MinorObjects [HarmonyPostfix] public static void UpdateHealthText(TrainingDummy __instance) { - Zombie_Patch.TranslateHpUI(__instance.healthText); - Zombie_Patch.TranslateHpUI(__instance.healthTextShadow); + Zombie_Patch.TranslateHpUI_T(__instance.healthText, __instance); + Zombie_Patch.TranslateHpUI_T(__instance.healthTextShadow, __instance); } } } \ No newline at end of file diff --git a/MelonLoader/Patches/GameObjects/MinorObjects/Zombie_Patch.cs b/MelonLoader/Patches/GameObjects/MinorObjects/Zombie_Patch.cs index 43117ba..f1147a2 100644 --- a/MelonLoader/Patches/GameObjects/MinorObjects/Zombie_Patch.cs +++ b/MelonLoader/Patches/GameObjects/MinorObjects/Zombie_Patch.cs @@ -4,6 +4,7 @@ using PvZ_Fusion_Translator.AssetStore; using System.Text.RegularExpressions; using UnityEngine; using Il2CppTMPro; +using System.Reflection.Metadata.Ecma335; namespace PvZ_Fusion_Translator.Patches.GameObjects.MinorObjects { @@ -12,9 +13,10 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.MinorObjects { private static Dictionary HPStrings = new Dictionary() { - { "最后一击:(\\d+)\nDPS:(.+)$", "Last damage: {0}\nDPS: {1}" }, - { "^HP:(\\d+)/(\\d+)\n\\D+(\\d+)/(\\d+)\n\\D+(\\d+)/(\\d+)$", "HP: {0}/{1}\nType 1 armor: {2}/{3}\nType 2 armor: {4}/{5}" }, - { "^HP:(\\d+)/(\\d+)\n\\D+(\\d+)/(\\d+)$", "HP: {0}/{1}\nType 1 armor: {2}/{3}" }, + { "最后一击:(\\d+)\nDPS:([^\\s]+)\n总伤害:(\\d+)", "Last Damage: {0}\nDPS: {1}\nTotal: {2}" }, + { "^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}" }, }; @@ -29,13 +31,9 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.MinorObjects [HarmonyPostfix] public static void Start(Zombie __instance) { - TranslateHpUI(__instance.healthText); - TranslateHpUI(__instance.healthTextShadow); - if (__instance.theFirstArmorHealth == 0 && __instance.theSecondArmorHealth != 0) - { - __instance.healthText.text = __instance.healthText.text.Replace("Type 1", "Type 2"); - __instance.healthTextShadow.text = __instance.healthText.text.Replace("Type 1", "Type 2"); - } + TranslateHpUI(__instance.healthText, __instance); + TranslateHpUI(__instance.healthTextShadow, __instance); + if (__instance.healthText.transform.rotation != Quaternion.Euler(0, 0, 0)) __instance.healthText.transform.rotation = Quaternion.Euler(0f, 0f, 0f); } @@ -46,23 +44,19 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.MinorObjects { if (__instance.theZombieType == ZombieType.TrainingDummy || !Board.Instance.showZombieHealth) return; - TranslateHpUI(__instance.healthText); - TranslateHpUI(__instance.healthTextShadow); - if (__instance.theFirstArmorHealth == 0 && __instance.theSecondArmorHealth != 0) - { - __instance.healthText.text = __instance.healthText.text.Replace("Type 1", "Type 2"); - __instance.healthTextShadow.text = __instance.healthText.text.Replace("Type 1", "Type 2"); - } + TranslateHpUI(__instance.healthText, __instance); + TranslateHpUI(__instance.healthTextShadow, __instance); + if (__instance.healthText.transform.rotation != Quaternion.Euler(0f, 0f, 0f)) __instance.healthText.transform.rotation = Quaternion.Euler(0f, 0f, 0f); } - public static void TranslateHpUI(TextMeshPro textTMP) + public static void TranslateHpUI(TextMeshPro textTMP, Zombie __instance) { if (textTMP) { string origText = textTMP.text; - string translatedText = TranslateHPText(origText); + string translatedText = TranslateHPText(origText, __instance); textTMP.text = translatedText == origText ? origText : translatedText; textTMP.autoSizeTextContainer = false; if (translatedText != origText) @@ -70,30 +64,97 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.MinorObjects } } - public static string TranslateHPText(string originalText) + public static void TranslateHpUI_T(TextMeshPro textTMP, TrainingDummy __instance) { - foreach (var entry in HPStrings) + if (textTMP) { - var regex = new Regex(entry.Key); - if (regex.IsMatch(originalText)) + string origText = textTMP.text; + string translatedText = TranslateHPText_T(origText, __instance); + textTMP.text = translatedText == origText ? origText : translatedText; + textTMP.autoSizeTextContainer = false; + if (translatedText != origText) + textTMP.font = FontStore.LoadTMPFont(Utils.Language.ToString()); + } + } + + public static string TranslateHPText(string originalText, Zombie __instance) + { + string finalText = __instance.healthText.text; + int fIndex = -1; + bool found = false; + + for(int i = 0; i < HPStrings.Count; i++) + { + var hp = HPStrings.ElementAt(i); + Regex regex = new Regex(hp.Key); + if(regex.IsMatch(originalText)) { - var match = regex.Match(originalText); - int groupCount = match.Groups.Count; - - List dynamicParts = new List(); - - for (int i = 1; i < groupCount; i++) - { - string groupValue = match.Groups[i].Value; - string translatedValue = StringStore.translationString.ContainsKey(groupValue) - ? StringStore.translationString[groupValue] - : groupValue; - dynamicParts.Add(translatedValue); - } - return originalText = string.Format(entry.Value, [.. dynamicParts]); + fIndex = i; + found = true; } } - return originalText; + + if(fIndex > -1 && found) + { + string fStr = HPStrings.ElementAt(fIndex).Value; + + switch (fIndex) + { + case 1: + finalText = string.Format(fStr, __instance.theHealth, __instance.theMaxHealth, __instance.theFirstArmorHealth, __instance.theFirstArmorMaxHealth, __instance.theSecondArmorHealth, __instance.theSecondArmorMaxHealth); + break; + case 2: + finalText = string.Format(fStr, __instance.theHealth, __instance.theMaxHealth, __instance.theFirstArmorHealth, __instance.theFirstArmorMaxHealth); + break; + case 3: + finalText = string.Format(fStr, __instance.theHealth, __instance.theMaxHealth, __instance.theSecondArmorHealth, __instance.theSecondArmorMaxHealth); + break; + case 4: + finalText = string.Format(fStr, __instance.theHealth, __instance.theMaxHealth); + break; + default: + break; + } + } + + return finalText; + } + + public static string TranslateHPText_T(string originalText, TrainingDummy __instance) + { + string finalText = __instance.healthText.text; + int fIndex = -1; + bool found = false; + + for (int i = 0; i < HPStrings.Count; i++) + { + var hp = HPStrings.ElementAt(i); + Regex regex = new Regex(hp.Key); + if (regex.IsMatch(originalText)) + { + fIndex = i; + found = true; + } + } + + if (fIndex > -1 && found) + { + string fStr = HPStrings.ElementAt(fIndex).Value; + string DPS = originalText.Split("\n")[1].Replace("DPS:", ""); + + Log.LogInfo(DPS); + + switch (fIndex) + { + case 0: + finalText = string.Format(fStr, __instance.lastDamage, DPS, __instance.totalDamage); + break; + default: + break; + } + } + + return finalText; } } } \ No newline at end of file diff --git a/MelonLoader/Patches/GameObjects/PlantInfoWindow_Patch.cs b/MelonLoader/Patches/GameObjects/PlantInfoWindow_Patch.cs index bbe52fc..c723490 100644 --- a/MelonLoader/Patches/GameObjects/PlantInfoWindow_Patch.cs +++ b/MelonLoader/Patches/GameObjects/PlantInfoWindow_Patch.cs @@ -25,13 +25,15 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects { if(txt.name.Contains("Introduce")) { + txt.text = StringStore.TranslateText(txt.text); + string text = txt.text; string[] lines = text.Split("\n"); lines[0] = Utils.GetPlantNameFromAlmanac(thePlantType); string updatedText = String.Join("\n", lines); - txt.text = StringStore.TranslateText(updatedText); + txt.text = updatedText; } else { diff --git a/MelonLoader/Patches/GameObjects/SeedLibrary_Patch.cs b/MelonLoader/Patches/GameObjects/SeedLibrary_Patch.cs new file mode 100644 index 0000000..230a392 --- /dev/null +++ b/MelonLoader/Patches/GameObjects/SeedLibrary_Patch.cs @@ -0,0 +1,35 @@ +using HarmonyLib; +using Il2Cpp; +using Il2CppTMPro; +using PvZ_Fusion_Translator.AssetStore; +using UnityEngine; + +namespace PvZ_Fusion_Translator.Patches.GameObjects +{ + [HarmonyPatch(typeof(SeedLibrary))] + public static class SeedLibrary_Patch + { + [HarmonyPatch(nameof(SeedLibrary.ChangeSafeMode))] + [HarmonyPostfix] + private static void ChangeSafeMode(SeedLibrary __instance) + { + TMP_FontAsset fontAsset = FontStore.LoadTMPFont(Utils.Language.ToString()); + + foreach (TextMeshProUGUI txt in __instance.GetComponentsInChildren()) + { + txt.text = StringStore.TranslateText(txt.text); + txt.font = fontAsset; + } + + Transform safetyBoxButtonTransform = __instance.transform.FindChild("SafeBox").FindChild("StartSafeMode"); + + if (__instance.safeMode == false && safetyBoxButtonTransform != null) + { + foreach (TextMeshProUGUI txt in safetyBoxButtonTransform.GetComponentsInChildren()) + { + txt.text = StringStore.TranslateText("合上"); + } + } + } + } +} \ No newline at end of file diff --git a/MelonLoader/Patches/GameObjects/TravelLookMenu_Patch.cs b/MelonLoader/Patches/GameObjects/TravelLookMenu_Patch.cs index 7c2b029..683f0ac 100644 --- a/MelonLoader/Patches/GameObjects/TravelLookMenu_Patch.cs +++ b/MelonLoader/Patches/GameObjects/TravelLookMenu_Patch.cs @@ -9,25 +9,37 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects [HarmonyPatch(typeof(TravelLookMenu))] public static class TravelLookMenu_Patch { + public static string savedAssetString = ""; + [HarmonyPatch(nameof(TravelLookMenu.Start))] [HarmonyPostfix] private static void Postfix_Start(TravelLookMenu __instance) { - Transform quitButtonTransform = __instance.transform.Find("Quit"); - if (quitButtonTransform != null) + foreach(TextMeshProUGUI txt in __instance.GetComponentsInChildren()) { - TextMeshProUGUI[] allTexts = quitButtonTransform.GetComponentsInChildren(true); + txt.text = StringStore.TranslateText(txt.text); + txt.font = FontStore.LoadTMPFont(Utils.Language.ToString()); + } - foreach (var textComp in allTexts) - { - if (textComp != null && textComp.text == StringStore.TranslateText("关闭")) - { - textComp.text = StringStore.TranslateText("关闭_close"); + Transform quitButtonTransform = __instance.transform.Find("Quit"); + foreach (TextMeshProUGUI txt in quitButtonTransform.GetComponentsInChildren()) + { + txt.text = StringStore.TranslateText("合上"); + txt.font = FontStore.LoadTMPFont(Utils.Language.ToString()); + } - TMP_FontAsset fontAsset = FontStore.LoadTMPFont(Utils.Language.ToString()); - textComp.font = fontAsset; - } - } + __instance.treasureText.text = StringStore.TranslateText(__instance.treasureText.text); + savedAssetString = __instance.treasureText.text; + } + + [HarmonyPatch(nameof(TravelLookMenu.Update))] + [HarmonyPostfix] + private static void Update(TravelLookMenu __instance) + { + if(savedAssetString != __instance.treasureText.text) + { + __instance.treasureText.text = StringStore.TranslateText(__instance.treasureText.text); + savedAssetString = __instance.treasureText.text; } } } diff --git a/MelonLoader/Patches/GameObjects/TreasureEvacuateMenu_Patch.cs b/MelonLoader/Patches/GameObjects/TreasureEvacuateMenu_Patch.cs new file mode 100644 index 0000000..f32c011 --- /dev/null +++ b/MelonLoader/Patches/GameObjects/TreasureEvacuateMenu_Patch.cs @@ -0,0 +1,30 @@ +using HarmonyLib; +using Il2Cpp; +using Il2CppTMPro; +using PvZ_Fusion_Translator.AssetStore; +using UnityEngine; +using UnityEngine.UI; + +namespace PvZ_Fusion_Translator.Patches.GameObjects +{ + [HarmonyPatch(typeof(TreasureEvacuateMenu))] + public static class TreasureEvacuateMenu_Patch + { + [HarmonyPatch(nameof(TreasureEvacuateMenu.Start))] + [HarmonyPostfix] + private static void Start(TreasureEvacuateMenu __instance) + { + TMP_FontAsset fontAsset = FontStore.LoadTMPFont(Utils.Language.ToString()); + + foreach (TextMeshProUGUI txt in __instance.GetComponentsInChildren()) + { + txt.text = StringStore.TranslateText(txt.text); + txt.font = fontAsset; + if(txt.name.Contains("text (1)")) + { + txt.name = txt.name + "_EVACUATE"; + } + } + } + } +} \ No newline at end of file diff --git a/MelonLoader/Patches/GameObjects/TreasureUpgradeMenu_Patch.cs b/MelonLoader/Patches/GameObjects/TreasureUpgradeMenu_Patch.cs index 1b3c411..a7d43bb 100644 --- a/MelonLoader/Patches/GameObjects/TreasureUpgradeMenu_Patch.cs +++ b/MelonLoader/Patches/GameObjects/TreasureUpgradeMenu_Patch.cs @@ -1,9 +1,11 @@ using HarmonyLib; using Il2Cpp; +using Il2CppSystem; using Il2CppTMPro; using PvZ_Fusion_Translator.AssetStore; using System.Text.RegularExpressions; using UnityEngine; +using UnityEngine.UI; namespace PvZ_Fusion_Translator.Patches.GameObjects { @@ -47,6 +49,15 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects txt.text = StringStore.TranslateText(txt.text); txt.font = fontAsset; } + + foreach (Text txt in __instance.GetComponentsInChildren()) + { + GameObject newTxtObj = Utils.ConvertToTextMeshProUGUI(txt.gameObject, txt.transform.parent, "text"); + TextMeshProUGUI newTxt = newTxtObj.GetComponent(); + newTxt.text = StringStore.TranslateText("植物商店"); + newTxt.autoSizeTextContainer = true; + newTxt.color = new Color(0, 0, 0.3922f, 1); + } } } } \ No newline at end of file diff --git a/MelonLoader/Patches/Managers/InGameUI_Patch.cs b/MelonLoader/Patches/Managers/InGameUI_Patch.cs index b06b4e5..8a297bc 100644 --- a/MelonLoader/Patches/Managers/InGameUI_Patch.cs +++ b/MelonLoader/Patches/Managers/InGameUI_Patch.cs @@ -1,9 +1,10 @@ using HarmonyLib; using Il2Cpp; using Il2CppTMPro; +using PvZ_Fusion_Translator; using PvZ_Fusion_Translator.AssetStore; using System.Text.RegularExpressions; -using PvZ_Fusion_Translator; +using Unity.Collections; namespace PvZ_Fusion_Translator.Patches.Managers { @@ -25,8 +26,25 @@ namespace PvZ_Fusion_Translator.Patches.Managers TextMeshProUGUI[] array2 = array; for (int i = 0; i < array2.Length; i++) { - array2[i] = StringStore.TranslateText(array2[i]); - array2[i].text = array2[i].text.Replace("\n", " "); + array2[i].text = array2[i].text.Replace("\n", " "); + + if (array2[i].text.Contains("配方:")) + { + string[] lines = array2[i].text.Split(":"); + lines[1] = Utils.GetPlantNameFromAlmanac(lines[1]); + if (lines[1] == "") + { + Log.LogInfo("Couldn't find plant! (Fusion Showcase)"); + lines[1] = StringStore.TranslateText(lines[1]); + } + array2[i].text = StringStore.TranslateText("配方:") + lines[1]; + array2[i].autoSizeTextContainer = true; + } + else + { + array2[i] = StringStore.TranslateText(array2[i]); + array2[i].text = array2[i].text.Replace("\n", " "); + } } } } diff --git a/MelonLoader/Patches/Managers/TreasureManager_Patch.cs b/MelonLoader/Patches/Managers/TreasureManager_Patch.cs new file mode 100644 index 0000000..9b20132 --- /dev/null +++ b/MelonLoader/Patches/Managers/TreasureManager_Patch.cs @@ -0,0 +1,31 @@ +using HarmonyLib; +using Il2Cpp; +using Il2CppTMPro; +using PvZ_Fusion_Translator.AssetStore; +using UnityEngine; + +namespace PvZ_Fusion_Translator.Patches.Managers +{ + [HarmonyPatch(typeof(TreasureManager))] + public static class TreasureManager_Patch + { + [HarmonyPatch(nameof(TreasureManager.Update))] + [HarmonyPostfix] + private static void Update(TreasureManager __instance) + { + TMP_FontAsset fontAsset = FontStore.LoadTMPFont(Utils.Language.ToString()); + + foreach(TextMeshProUGUI txt in __instance.evaText) + { + txt.autoSizeTextContainer = false; + + if (txt.text != "Evacuate") + { + txt.text = StringStore.TranslateText(txt.text); + txt.name = txt.text + "_EVACUATE"; + txt.font = fontAsset; + } + } + } + } +} diff --git a/MelonLoader/PvZ Fusion Translator [MelonLoader].csproj b/MelonLoader/PvZ Fusion Translator [MelonLoader].csproj index c6c57b0..f0bcc40 100644 --- a/MelonLoader/PvZ Fusion Translator [MelonLoader].csproj +++ b/MelonLoader/PvZ Fusion Translator [MelonLoader].csproj @@ -401,6 +401,7 @@ + @@ -411,8 +412,10 @@ + + @@ -457,6 +460,7 @@ + diff --git a/MelonLoader/Utils.cs b/MelonLoader/Utils.cs index 6422342..736e4ff 100644 --- a/MelonLoader/Utils.cs +++ b/MelonLoader/Utils.cs @@ -1,12 +1,16 @@ -using Il2Cpp; +using Harmony; +using Il2Cpp; using Il2CppTMPro; using MelonLoader; +using MelonLoader.TinyJSON; using PvZ_Fusion_Translator.AssetStore; +using PvZ_Fusion_Translator.Patches.GameObjects.MinorObjects; using System.Diagnostics; using System.Text.RegularExpressions; using UnityEngine; using UnityEngine.Experimental.Rendering; using UnityEngine.UI; +using static Il2Cpp.AlmanacPlantBank; using static MelonLoader.MelonLogger; using static PvZ_Fusion_Translator.FileLoader; @@ -119,12 +123,11 @@ namespace PvZ_Fusion_Translator string currentLanguage = Utils.Language.ToString(); string almanacDir = GetAssetDir(AssetType.Almanac, Utils.Language); string path = Path.Combine(almanacDir, "LawnStringsTranslate.json"); - string moddedPath = Path.Combine(almanacDir, "ModdedPlantsTranslate.json"); if (!File.Exists(path)) { Log.LogError($"LawnStringsTranslate.json file not found at path: {path}"); - Log.LogError("Plant name could not be found! (Big Garden)"); + Log.LogError("Plant name could not be found!"); thePlantName = ""; } else @@ -144,6 +147,102 @@ namespace PvZ_Fusion_Translator return thePlantName; } + public static string GetPlantNameFromAlmanac(string theOriginalPlantName) + { + string originalJson; + string translatedJson; + string thePlantName = ""; + + string currentLanguage = Utils.Language.ToString(); + string almanacDir = GetAssetDir(AssetType.Almanac, Utils.Language); + string dumpDir = GetAssetDir(AssetType.Dumps); + string originalPath = Path.Combine(dumpDir, "LawnStrings.json"); + string path = Path.Combine(almanacDir, "LawnStringsTranslate.json"); + + if ((!File.Exists(path)) || (!File.Exists(originalPath))) + { + Log.LogError($"LawnStringsTranslate.json file not found at path: {path}"); + Log.LogError("Plant name could not be found!"); + thePlantName = ""; + } + else + { + bool foundPlantName = false; + + originalJson = File.ReadAllText(originalPath); + translatedJson = File.ReadAllText(path); + AlmanacPlantBank.PlantData originalPlantData = JsonUtility.FromJson(originalJson); + AlmanacPlantBank.PlantData translatedPlantData = JsonUtility.FromJson(translatedJson); + + for(int i = 0; i < originalPlantData.plants.Count; i++) + { + AlmanacPlantBank.PlantInfo originalPlantInfo = originalPlantData.plants[i]; + KeyValuePair translatedPlantInfo = plantIndices[originalPlantInfo.seedType]; + + if (originalPlantInfo.name == theOriginalPlantName) + { + thePlantName = translatedPlantInfo.Value; + foundPlantName = true; + } + } + + if (!foundPlantName) + { + Log.LogInfo("Couldn't find plant name!"); + thePlantName = ""; + } + } + + return thePlantName; + } + + public static Dictionary> plantIndices = new Dictionary>(); + + public static void RegisterPlantIndices() + { + plantIndices = new Dictionary>(); + string originalJson; + string translatedJson; + + string currentLanguage = Utils.Language.ToString(); + string almanacDir = GetAssetDir(AssetType.Almanac, Utils.Language); + string dumpDir = GetAssetDir(AssetType.Dumps); + string originalPath = Path.Combine(dumpDir, "LawnStrings.json"); + string path = Path.Combine(almanacDir, "LawnStringsTranslate.json"); + + if ((!File.Exists(path)) || (!File.Exists(originalPath))) + { + Log.LogError($"LawnStringsTranslate.json file not found at path: {path}"); + } + else + { + originalJson = File.ReadAllText(originalPath); + translatedJson = File.ReadAllText(path); + AlmanacPlantBank.PlantData originalPlantData = JsonUtility.FromJson(originalJson); + AlmanacPlantBank.PlantData translatedPlantData = JsonUtility.FromJson(translatedJson); + + for(int i = 0; i < originalPlantData.plants.Count; i++) + { + PlantInfo originalPlantInfo = originalPlantData.plants[i]; + PlantInfo translatedPlantInfo = null; + + foreach (PlantInfo info in translatedPlantData.plants) + { + if(info.seedType == originalPlantInfo.seedType) + { + translatedPlantInfo = info; + } + } + + if(translatedPlantInfo != null) + { + KeyValuePair temp = new KeyValuePair(translatedPlantInfo.seedType, translatedPlantInfo.name); + plantIndices.Add(originalPlantInfo.seedType, temp); + } + } + } + } + #if MULTI_LANGUAGE internal static void ChangeLanguage(string language) { @@ -164,7 +263,9 @@ namespace PvZ_Fusion_Translator StringStore.Reload(); TextureStore.Reload(); FileLoader.SaveLanguage(); - } + RegisterPlantIndices(); + Zombie_Patch.LoadHPStrings(); + } #endif #if MULTI_LANGUAGE