diff --git a/BepInEx/PvZ Fusion Translator [BepInEx].csproj b/BepInEx/PvZ Fusion Translator [BepInEx].csproj index dcddb25..ef6ece2 100644 --- a/BepInEx/PvZ Fusion Translator [BepInEx].csproj +++ b/BepInEx/PvZ Fusion Translator [BepInEx].csproj @@ -13,7 +13,7 @@ https://nuget.samboy.dev/v3/index.json PvZ_Fusion_Translator__BepInEx_ - C:\Users\rainb\Desktop\PC_PVZ-Fusion_2.5-BepInEx + C:\Users\rainb\Desktop\PC_PVZ-Fusion_2.5.1-BepInEx Debug;DebugEnglish;ReleaseMulti;ReleaseEnglish;DebugTesting;ReleaseOther;DebugObfuscate;ReleaseObfuscate;DebugMulti AnyCPU;x64 diff --git a/MelonLoader/Patches/GameObjects/ButtonObjects/InGameBtn_Patch.cs b/MelonLoader/Patches/GameObjects/ButtonObjects/InGameBtn_Patch.cs index d645ee9..dda9ac9 100644 --- a/MelonLoader/Patches/GameObjects/ButtonObjects/InGameBtn_Patch.cs +++ b/MelonLoader/Patches/GameObjects/ButtonObjects/InGameBtn_Patch.cs @@ -1,6 +1,7 @@ using HarmonyLib; using Il2Cpp; using Il2CppTMPro; +using PvZ_Fusion_Translator.AssetStore; using UnityEngine; namespace PvZ_Fusion_Translator.Patches.GameObjects.ButtonObjects @@ -30,6 +31,11 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.ButtonObjects } } } - } + + foreach(TextMeshProUGUI text in __instance.transform.GetComponentsInChildren()) + { + text.text = StringStore.TranslateText(text.text); + } + } } } diff --git a/MelonLoader/Patches/GameObjects/ButtonObjects/OptLanguageBtn_Patch.cs b/MelonLoader/Patches/GameObjects/ButtonObjects/OptLanguageBtn_Patch.cs index b0c1e5f..6eb5f9b 100644 --- a/MelonLoader/Patches/GameObjects/ButtonObjects/OptLanguageBtn_Patch.cs +++ b/MelonLoader/Patches/GameObjects/ButtonObjects/OptLanguageBtn_Patch.cs @@ -2,9 +2,11 @@ using HarmonyLib; using Il2Cpp; using Il2CppTMPro; +using MelonLoader; using PvZ_Fusion_Translator; using PvZ_Fusion_Translator.AssetStore; using UnityEngine; +using UnityEngine.UIElements; using Object = UnityEngine.Object; public class OptionButtonData @@ -12,7 +14,9 @@ public class OptionButtonData public OptionBtn Button { get; set; } public Vector3 Position { get; set; } public Utils.LanguageEnum? Language { get; set; } // Null if it's the "Next" button + public Utils.ToggleEnum? Toggle { get; set; } // Null if it's the "Next" button public bool IsNextButton { get; set; } + public bool shifted = false; } namespace PvZ_Fusion_Translator.Patches.GameObjects.ButtonObjects @@ -20,19 +24,27 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.ButtonObjects internal class OptLanguageBtn_Patch { public static Dictionary LanguageBtnDict = new(); - private static bool buttonsCreated = false; - private static OptionBtn cachedTemplateButton; + public static Dictionary ToggleBtnDict = new(); + private static bool buttonsCreated = false; + private static bool togglesCreated = false; + private static OptionBtn cachedTemplateButton; + private static OptionBtn cachedTemplateToggleButton; - private const float startX = 4.3241f + 2.56f; + private const float startX = 4.3241f + 2.56f; private const float startY = 2.7769f; private const float ySpacing = 1.2505f; - private static List AvailableLanguages; - private const int LanguagesPerPage = 5; + private const float toggleStartX = 4.3241f; + private const float toggleStartY = -2.2251f; + + private static List AvailableLanguages; + private static List AvailableToggles; + private const int LanguagesPerPage = 5; private static int currentPage = 0; private static OptionBtn[] buttonSlots = new OptionBtn[6]; // 5 language buttons + 1 next button + private static OptionBtn[] toggleSlots = new OptionBtn[2]; - public static void CreateLanguageButtons(OptionBtn templateButton) + public static void CreateLanguageButtons(OptionBtn templateButton) { if (buttonsCreated) return; buttonsCreated = true; @@ -64,12 +76,45 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.ButtonObjects UpdatePage(); } + public static void CreateToggleButtons(OptionBtn templateButton) + { + if (togglesCreated) return; + togglesCreated = true; + cachedTemplateToggleButton = templateButton; + + AvailableToggles = Enum.GetValues(typeof(Utils.ToggleEnum)) + .Cast() + .Where(lang => lang != Utils.ToggleEnum.TOGGLE_END) + .ToList(); + + for (int i = 0; i < 2; i++) + { + var newButton = Object.Instantiate(templateButton, templateButton.transform.parent); + newButton.optionType = 100 + i; + + float yPos = toggleStartY - i * ySpacing; + Vector3 pos = new(toggleStartX, yPos); + newButton.transform.position = pos; + + toggleSlots[i] = newButton; + + ToggleBtnDict[newButton.GetInstanceID()] = new OptionButtonData + { + Button = newButton, + Position = pos + }; + } + + UpdatePage(); + } + private static void UpdatePage() { int startIndex = currentPage * LanguagesPerPage; int langCount = AvailableLanguages.Count; + int toggleCount = (togglesCreated) ? AvailableToggles.Count : 0; - for (int i = 0; i < 5; i++) + for (int i = 0; i < 5; i++) { var btn = buttonSlots[i]; var data = LanguageBtnDict[btn.GetInstanceID()]; @@ -90,6 +135,30 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.ButtonObjects } } + if (togglesCreated) + { + for (int i = 0; i < 2; i++) + { + var btn = toggleSlots[i]; + var data = ToggleBtnDict[btn.GetInstanceID()]; + + if (i < toggleCount) + { + var toggle = AvailableToggles[i]; + data.Toggle = toggle; + data.IsNextButton = false; + btn.gameObject.SetActive(true); + UpdateButtonText(btn, toggle.ToString()); + } + else + { + data.Language = null; + data.IsNextButton = false; + btn.gameObject.SetActive(false); + } + } + } + // "Next" button always visible at index 5 var nextBtn = buttonSlots[5]; var nextData = LanguageBtnDict[nextBtn.GetInstanceID()]; @@ -130,15 +199,28 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.ButtonObjects } } - private static void FlashMessage(OptionBtn button, string message) + private static void FlashMessage(OptionBtn button, string message, float yShift = 0.0f) { var child = button.transform.GetChild(0); if (child != null) { var text = child.GetComponent(); if (text != null) - { - text.text = message; + { + text.text = message; + OptionButtonData data; + bool isToggle = ToggleBtnDict.TryGetValue(button.GetInstanceID(), out data); + + if(isToggle) + { + text.transform.position = new Vector3(text.transform.position.x, text.transform.position.y + (ToggleBtnDict[button.GetInstanceID()].shifted ? 0.0f : yShift), text.transform.position.z); + ToggleBtnDict[button.GetInstanceID()].shifted = true; + } + else + { + text.transform.position = new Vector3(text.transform.position.x, text.transform.position.y + (LanguageBtnDict[button.GetInstanceID()].shifted ? 0.0f : yShift), text.transform.position.z); + LanguageBtnDict[button.GetInstanceID()].shifted = true; + } } child.gameObject.SetActive(true); @@ -152,6 +234,24 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.ButtonObjects } } + private static void ToggleCustomAssets(string type) + { + if (type != "Textures" && type != "Audio") return; + + if(type == "Textures") + { + Utils.customTextures = (Utils.customTextures) ? false : true; + MelonPreferences.SetEntryValue("PvZ_Fusion_Translator", "DefaultTextures", !Utils.customTextures); + } + + if (type == "Audio") + { + Utils.customAudio = (Utils.customAudio) ? false : true; + MelonPreferences.SetEntryValue("PvZ_Fusion_Translator", "DefaultAudio", !Utils.customAudio); + } + MelonPreferences.Save(); + } + [HarmonyPatch(typeof(OptionBtn))] public static class OptLangBtn_Patch { @@ -162,8 +262,10 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.ButtonObjects if (!buttonsCreated || cachedTemplateButton == null) { buttonsCreated = false; - CreateLanguageButtons(__instance); - } + togglesCreated = false; + CreateLanguageButtons(__instance); + CreateToggleButtons(__instance); + } } [HarmonyPatch("OnMouseUpAsButton")] @@ -195,7 +297,21 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.ButtonObjects } } } - } + + if (ToggleBtnDict.TryGetValue(__instance.GetInstanceID(), out var toggleData)) + { + string toggleType = toggleData.Toggle.ToString(); + ToggleCustomAssets(toggleType); + if (toggleType == "Textures") + { + FlashMessage(toggleData.Button, "Toggled custom textures! (Restart game to apply changes.)", 0.1f); + } + else if (toggleType == "Audio") + { + FlashMessage(toggleData.Button, "Toggled custom audio! (Restart game to apply changes.)", 0.1f); + } + } + } [HarmonyPatch(nameof(OptionBtn.Update))] [HarmonyPostfix] @@ -205,7 +321,12 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.ButtonObjects { btnData.Button.transform.position = btnData.Position; } - } + + if (ToggleBtnDict.TryGetValue(__instance.GetInstanceID(), out var toggleData)) + { + toggleData.Button.transform.position = toggleData.Position; + } + } } } } diff --git a/MelonLoader/Patches/GameObjects/OtherMenu_Patch.cs b/MelonLoader/Patches/GameObjects/OtherMenu_Patch.cs new file mode 100644 index 0000000..8fed8a9 --- /dev/null +++ b/MelonLoader/Patches/GameObjects/OtherMenu_Patch.cs @@ -0,0 +1,20 @@ +using HarmonyLib; +using Il2Cpp; +using Il2CppTMPro; +using PvZ_Fusion_Translator.AssetStore; +using UnityEngine; + +namespace PvZ_Fusion_Translator.Patches.GameObjects +{ + [HarmonyPatch(typeof(OtherMenu))] + public static class OtherMenu_Patch + { + [HarmonyPatch(nameof(OtherMenu.UpdateText))] + [HarmonyPostfix] + private static void UpdateText(OtherMenu __instance) + { + __instance.text1 = StringStore.TranslateText(__instance.text1); + __instance.text2 = StringStore.TranslateText(__instance.text2); + } + } +} diff --git a/MelonLoader/PvZ Fusion Translator [MelonLoader].csproj b/MelonLoader/PvZ Fusion Translator [MelonLoader].csproj index 404554f..22a4ed1 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_2.4.2 + C:\Users\rainb\Desktop\PC_PVZ-Fusion_2.5.1 AnyCPU;ARM64;x64 @@ -452,6 +452,7 @@ + diff --git a/MelonLoader/Utils.cs b/MelonLoader/Utils.cs index 0aceab7..de1ae12 100644 --- a/MelonLoader/Utils.cs +++ b/MelonLoader/Utils.cs @@ -1,4 +1,5 @@ -using PvZ_Fusion_Translator.AssetStore; +using MelonLoader; +using PvZ_Fusion_Translator.AssetStore; using System.Diagnostics; using System.Text.RegularExpressions; using UnityEngine; @@ -104,7 +105,10 @@ namespace PvZ_Fusion_Translator public static Utils.LanguageEnum Language; - public enum LanguageEnum + public static bool customTextures = !MelonPreferences.GetEntryValue("PvZ_Fusion_Translator", "DefaultTextures"); + public static bool customAudio = !MelonPreferences.GetEntryValue("PvZ_Fusion_Translator", "DefaultAudio"); + + public enum LanguageEnum { // first column English, @@ -131,5 +135,12 @@ namespace PvZ_Fusion_Translator LANG_END } + + public enum ToggleEnum + { + Textures, + Audio, + TOGGLE_END + } } } \ No newline at end of file