From 7216014ac2ae4c94f2266656b1620271d143490a Mon Sep 17 00:00:00 2001 From: SillyStar-Github <90102598+SillyStar-Github@users.noreply.github.com> Date: Sun, 11 May 2025 15:51:58 -0400 Subject: [PATCH] MESSY bepinex stuff + some melonloader --- BepInEx/AssetStore/AudioStore.cs | 17 +- BepInEx/AssetStore/TextureStore.cs | 1 - BepInEx/Core.cs | 26 +- BepInEx/FileLoader.cs | 618 +++++++++--------- .../BaseTextObjects/InGameText_Patch.cs | 3 +- .../Patches/GameObjects/AbyssBagMenu_Patch.cs | 1 + .../GameObjects/AbyssCheckInBag_Patch.cs | 1 + .../GameObjects/AbyssDevelopMenu_Patch.cs | 5 +- .../Patches/GameObjects/AbyssMenu2_Patch.cs | 1 + .../Patches/GameObjects/AbyssMenu_Patch.cs | 4 +- .../GameObjects/AbyssSettlementMenu_Patch.cs | 62 +- .../ButtonObjects/AbyssBuffButton_Patch.cs | 2 +- .../ButtonObjects/CustomLevelEnter_Patch.cs | 1 + .../ButtonObjects/InGameBtn_Patch.cs | 8 +- .../ButtonObjects/OptLanguageBtn_Patch.cs | 169 ++++- BepInEx/Patches/GameObjects/MainMenu_Patch.cs | 12 +- .../MinorObjects/PlayerName_Patch.cs | 1 - .../Patches/GameObjects/OtherMenu_Patch.cs | 20 + .../TravelBuffOptionButton_Patch.cs | 21 + .../Patches/GameObjects/TravelStore_Patch.cs | 17 +- .../Patches/GameObjects/UIDifficulty_Patch.cs | 3 +- .../Patches/GameObjects/UIZombieNum_Patch.cs | 4 +- .../Managers/AlmanacMgrZombie_Patch.cs | 46 +- .../Managers/AlmanacPlantBank_Patch.cs | 89 ++- .../Managers/OppositeBuffManager_Patch.cs | 3 +- .../Patches/Managers/TravelBuffMgr_Patch.cs | 1 - .../OtherManagers/AchievementClip_Patch.cs | 5 +- .../PvZ Fusion Translator [BepInEx].csproj | 95 ++- BepInEx/Utils.cs | 226 ++++--- MelonLoader/AssetStore/AudioStore.cs | 45 +- MelonLoader/AssetStore/TextureStore.cs | 1 - MelonLoader/FileLoader.cs | 12 +- .../ButtonObjects/OptLanguageBtn_Patch.cs | 10 +- 33 files changed, 958 insertions(+), 572 deletions(-) create mode 100644 BepInEx/Patches/GameObjects/OtherMenu_Patch.cs diff --git a/BepInEx/AssetStore/AudioStore.cs b/BepInEx/AssetStore/AudioStore.cs index cbe784c..862cb09 100644 --- a/BepInEx/AssetStore/AudioStore.cs +++ b/BepInEx/AssetStore/AudioStore.cs @@ -148,16 +148,19 @@ namespace PvZ_Fusion_Translator__BepInEx_ { public static void Prefix(UnityEngine.AudioSource __instance) { - if (__instance.clip == null) + if(Utils.customAudio) + { + if (__instance.clip == null) return; - string audioClipName = __instance.clip.name; + string audioClipName = __instance.clip.name; - if (AudioStore.AudioClips.TryGetValue(audioClipName, out AudioClip replaceClip)) - { - __instance.pitch = 1; - __instance.clip = replaceClip; - } + if (AudioStore.AudioClips.TryGetValue(audioClipName, out AudioClip replaceClip)) + { + __instance.pitch = 1; + __instance.clip = replaceClip; + } + } } } } diff --git a/BepInEx/AssetStore/TextureStore.cs b/BepInEx/AssetStore/TextureStore.cs index f5a2f9f..8a222c0 100644 --- a/BepInEx/AssetStore/TextureStore.cs +++ b/BepInEx/AssetStore/TextureStore.cs @@ -17,7 +17,6 @@ namespace PvZ_Fusion_Translator__BepInEx_.AssetStore #if MULTI_LANGUAGE RestoreTextures(); #endif - FileLoader.LoadDefaultTextures(); FileLoader.LoadTextures(); } diff --git a/BepInEx/Core.cs b/BepInEx/Core.cs index 5fb581a..57e7cd6 100644 --- a/BepInEx/Core.cs +++ b/BepInEx/Core.cs @@ -46,7 +46,7 @@ public class Core : BasePlugin StringStore.Init(); FontStore.Init(); - InitCoroutine(); + InitCoroutine(); } public override bool Unload() @@ -67,7 +67,6 @@ public class Core : BasePlugin public void InitCoroutine() { replaceTextureRoutine = MonoInstance.StartCoroutine(TextureStore.ReplaceTexturesCoroutine()); - Log.LogDebug("Coroutine Started"); } public static void ShowToast(string message) { @@ -111,10 +110,25 @@ public class Core : BasePlugin private void LoadConfig() { string mainCategory = "PvZ_Fusion_Translator"; - configDefaultTextures = Config.Bind(new ConfigDefinition(mainCategory, "DefaultTextures"), false, new ConfigDescription("Use Default Textures + Translation Textures", new AcceptableValueList(true, false))); - configDefaultAudio = Config.Bind(new ConfigDefinition(mainCategory, "DefaultAudio"), false, new ConfigDescription("Use Default Audio", new AcceptableValueList(true, false))); - configLanguage = Config.Bind(new ConfigDefinition(mainCategory, "Language"), "English", new ConfigDescription("Load the Game in this Language", new AcceptableValueList("English", "French", "Italian", "German", "Spanish", "Portuguese", "Indonesian", "Vietnamese", "Javanese", "Russian", "Japanese", "Korean"))); - } + + if(Config.TryGetEntry(new ConfigDefinition(mainCategory, "DefaultTextures"), out configDefaultTextures) == false) + { + configDefaultTextures = Config.Bind(new ConfigDefinition(mainCategory, "DefaultTextures"), false, new ConfigDescription("Use Default Textures + Translation Textures", new AcceptableValueList(true, false))); + } + bool defaultTextures = Config.TryGetEntry(new ConfigDefinition("PvZ_Fusion_Translator", "DefaultTextures"), out configDefaultTextures); + + if (Config.TryGetEntry(new ConfigDefinition(mainCategory, "DefaultAudio"), out configDefaultAudio) == false) + { + configDefaultAudio = Config.Bind(new ConfigDefinition(mainCategory, "DefaultAudio"), false, new ConfigDescription("Use Default Audio", new AcceptableValueList(true, false))); + } + bool defaultAudio = Config.TryGetEntry(new ConfigDefinition("PvZ_Fusion_Translator", "DefaultAudio"), out configDefaultAudio); + + if (Config.TryGetEntry(new ConfigDefinition(mainCategory, "Language"), out configLanguage) == false) + { + configLanguage = Config.Bind(new ConfigDefinition(mainCategory, "Language"), "English", new ConfigDescription("Load the Game in this Language", new AcceptableValueList("English", "French", "Italian", "German", "Spanish", "Portuguese", "Indonesian", "Vietnamese", "Javanese", "Russian", "Japanese", "Korean"))); + } + bool language = Config.TryGetEntry(new ConfigDefinition("PvZ_Fusion_Translator", "Language"), out configLanguage); + } } public class UnityCoroutineHelper : MonoBehaviour diff --git a/BepInEx/FileLoader.cs b/BepInEx/FileLoader.cs index b3640cf..a2058a0 100644 --- a/BepInEx/FileLoader.cs +++ b/BepInEx/FileLoader.cs @@ -1,4 +1,5 @@ -using PvZ_Fusion_Translator__BepInEx_.AssetStore; +using BepInEx.Configuration; +using PvZ_Fusion_Translator__BepInEx_.AssetStore; using PvZ_Fusion_Translator__BepInEx_.Patches.OtherManagers; using System; using System.Collections.Generic; @@ -9,336 +10,365 @@ using UnityEngine; namespace PvZ_Fusion_Translator__BepInEx_ { - internal static class FileLoader - { - internal enum AssetType - { - Textures, - Strings, - Dumps, - Almanac, - } + internal static class FileLoader + { + internal enum AssetType + { + Textures, + Strings, + Dumps, + Almanac, + } - public static string GetAssetDir(AssetType assetType, Utils.LanguageEnum? language = null) - { - string languagePath = language.HasValue ? ("Localization" + Path.DirectorySeparatorChar + language.ToString()) : string.Empty; - return Path.Combine(Core.Instance.modsDirectory, languagePath, assetType.ToString()); - } + public static string GetAssetDir(AssetType assetType, Utils.LanguageEnum? language = null) + { + string languagePath = language.HasValue ? ("Localization" + Path.DirectorySeparatorChar + language.ToString()) : string.Empty; + return Path.Combine(Core.Instance.modsDirectory, languagePath, assetType.ToString()); + } + // private static string GetAssetDir(AssetType assetType) => Path.Combine(Core.Instance.modsDirectory, assetType.ToString()); - internal static void LoadStrings() => LoadStrings(Utils.Language); - internal static void LoadStrings(Utils.LanguageEnum language) - { - string stringDir = GetAssetDir(AssetType.Strings, Utils.Language); - if (!Directory.Exists(stringDir)) - { - Directory.CreateDirectory(stringDir); - } - try - { - foreach (string filepath in Directory.EnumerateFiles(stringDir, "*.json", SearchOption.AllDirectories)) - { +#if MULTI_LANGUAGE + internal static void LoadStrings() => LoadStrings(Utils.Language); - #if OBFUSCATE + internal static void LoadStrings(Utils.LanguageEnum language) +#else + internal static void LoadStrings(bool isLog = false) +#endif + { +#if MULTI_LANGUAGE + string stringDir = GetAssetDir(AssetType.Strings, Utils.Language); +#else + string stringDir = GetAssetDir(AssetType.Strings); +#endif + if (!Directory.Exists(stringDir)) + { + Directory.CreateDirectory(stringDir); + } + try + { + foreach (string filepath in Directory.EnumerateFiles(stringDir, "*.json", SearchOption.AllDirectories)) + { + +#if OBFUSCATE if (CheckSumStore.IsModified(filepath)) { Log.LogError("File {0} was modified!" , filepath); continue; } - #endif +#endif - string fileName = Path.GetFileNameWithoutExtension(filepath); - StringStore.stringsDict[fileName] = filepath; - #if DEBUG - Log.LogInfo("Loading File : " + filepath); - #endif - string jsonString = File.ReadAllText(filepath); - if (fileName.EndsWith("_strings")) + string fileName = Path.GetFileNameWithoutExtension(filepath); + StringStore.stringsDict[fileName] = filepath; +#if DEBUG + Log.LogInfo("Loading File : " + filepath); +#endif + string jsonString = File.ReadAllText(filepath); + if (fileName.EndsWith("_strings")) + { + Dictionary dictionary = System.Text.Json.JsonSerializer.Deserialize>(jsonString); + foreach (var (key, value) in dictionary) + { + StringStore.translationString[key] = value; + } + } + else if (fileName.EndsWith("_regexs")) + { + Dictionary dictionary = System.Text.Json.JsonSerializer.Deserialize>(jsonString); + foreach (var (key, value) in dictionary) + { + StringStore.translationStringRegex[key] = value; + } + } + } + SaveStrings(); + DumpJson(); + } + catch (Exception e) + { + Log.LogError("Error loading string."); + Log.LogError(e.GetType() + " " + e.Message); + } + Log.LogInfo("Strings loaded successfully."); + } + + + internal static void LoadTextures() + { + ConfigEntry defaultTextureEntry; + Core.Instance.Config.TryGetEntry(new ConfigDefinition("PvZ_Fusion_Translator", "DefaultTextures"), out defaultTextureEntry); + + // Default or Custom Texturse -> English Textures -> Localized Textures + try + { + if (!defaultTextureEntry.Value) + { + LoadCustomTextures(); + } + } + catch (Exception e) + { + Log.LogError("Error loading texture setting."); + Log.LogError($"{e.GetType()} {e.Message}"); + } + +#if MULTI_LANGUAGE + if (Utils.Language != Utils.LanguageEnum.English) + { + LoadLocalizedTextures(Utils.LanguageEnum.English); + } + LoadLocalizedTextures(Utils.Language); +#else + LoadLocalizedTextures(); +#endif + + Log.LogInfo("Textures loaded successfully."); + } + + internal static void LoadLocalizedTextures(Utils.LanguageEnum? language = null) + { + ConfigEntry defaultTextureEntry; + Core.Instance.Config.TryGetEntry(new ConfigDefinition("PvZ_Fusion_Translator", "DefaultTextures"), out defaultTextureEntry); + + string textureDir = GetAssetDir(AssetType.Textures, language); + + if (!Directory.Exists(textureDir)) + { + Directory.CreateDirectory(textureDir); + } + try + { + foreach (string filepath in Directory.EnumerateFiles(textureDir, "*.png", SearchOption.AllDirectories)) + { + if (filepath.Contains("[Custom Textures]", StringComparison.OrdinalIgnoreCase) && defaultTextureEntry.Value) + { + continue; + } + +#if OBFUSCATE + if (CheckSumStore.IsModified(filepath)) { - Dictionary dictionary = JsonSerializer.Deserialize>(jsonString); - foreach (var (key, value) in dictionary) + Log.LogError("File {0} was modified!" , filepath); + continue; + } +#endif + +#if DEBUG + Log.LogDebug("Loading File : " + filepath); +#endif + + Texture2D texture2D = Utils.LoadImage(filepath); + TextureStore.textureDict[Path.GetFileNameWithoutExtension(filepath)] = filepath; + } + } + + catch (Exception e) + { + Log.LogError("Error loading Texture."); + Log.LogError(e.GetType() + " " + e.Message); + } + } + + internal static void LoadDefaultTextures() + { + if (Core.Instance.configDefaultTextures.Value) + { + string textureDefaultDir = Path.Combine(Core.Instance.modsDirectory, AssetType.Dumps.ToString(), "Default Textures [Do Not Remove]"); + if (!Directory.Exists(textureDefaultDir)) + { + Directory.CreateDirectory(textureDefaultDir); + } + try + { + foreach (string filepath in Directory.EnumerateFiles(textureDefaultDir, "*.png", SearchOption.AllDirectories)) + { + + #if OBFUSCATE + if (CheckSumStore.IsModified(filepath)) { - StringStore.translationString[key] = value; + Log.LogError("File {0} was modified!" , filepath); + continue; } - } - else if (fileName.EndsWith("_regexs")) - { - Dictionary dictionary = JsonSerializer.Deserialize>(jsonString); - foreach (var (key, value) in dictionary) - { - StringStore.translationStringRegex[key] = value; - } - } - } - SaveStrings(); - #if DEBUG - DumpJson(); - #endif - } - catch (Exception e) - { - Log.LogError("Error loading string."); - Log.LogError(e.GetType() + " " + e.Message); - } - Log.LogInfo("Strings loaded successfully."); - } + #endif - public static bool loadDefaultTextures = Core.Instance.configDefaultTextures.Value; - internal static void LoadTextures() - { - // Default or Custom Texturse -> English Textures -> Localized Textures - try - { - if (loadDefaultTextures == true) - { - LoadDefaultTextures(); - } - else - { - LoadCustomTextures(); - } - } - catch (Exception e) - { - Log.LogError("Error loading texture setting."); - Log.LogError($"{e.GetType()} {e.Message}"); - } + Texture2D texture2D = Utils.LoadImage(filepath); + TextureStore.textureDict[Path.GetFileNameWithoutExtension(filepath)] = filepath; + } + } - if (Utils.Language != Utils.LanguageEnum.English) - { - LoadLocalizedTextures(Utils.LanguageEnum.English); - } - LoadLocalizedTextures(Utils.Language); + catch (Exception e) + { + Log.LogError("Error loading Texture."); + Log.LogError(e.GetType() + " " + e.Message); + } + } + } - Log.LogInfo("Textures loaded successfully."); - } + internal static void LoadCustomTextures() + { + string texturePackDir = Path.Combine(Core.Instance.modsDirectory, "[Custom Textures]"); - internal static void LoadLocalizedTextures(Utils.LanguageEnum? language = null) - { - string textureDir = GetAssetDir(AssetType.Textures, language); - - if (!Directory.Exists(textureDir)) - { - Directory.CreateDirectory(textureDir); - } - try - { - foreach (string filepath in Directory.EnumerateFiles(textureDir, "*.png", SearchOption.AllDirectories)) - { - if (filepath.Contains("[Custom Textures]", StringComparison.OrdinalIgnoreCase) && loadDefaultTextures) - { - continue; - } - - #if OBFUSCATE + if (!Directory.Exists(texturePackDir)) + { + Directory.CreateDirectory(texturePackDir); + } + try + { + foreach (string filepath in Directory.EnumerateFiles(texturePackDir, "*.png", SearchOption.AllDirectories)) + { +#if OBFUSCATE if (CheckSumStore.IsModified(filepath)) { Log.LogError("File {0} was modified!" , filepath); continue; } - #endif +#endif - #if DEBUG - Log.LogDebug("Loading File : " + filepath); - #endif + Texture2D texture2D = Utils.LoadImage(filepath); + TextureStore.textureDict[Path.GetFileNameWithoutExtension(filepath)] = filepath; + } + } - Texture2D texture2D = Utils.LoadImage(filepath); - TextureStore.textureDict[Path.GetFileNameWithoutExtension(filepath)] = filepath; - } - } + catch (Exception e) + { + Log.LogError("Error loading Texture."); + Log.LogError(e.GetType() + " " + e.Message); + } + } - catch (Exception e) - { - Log.LogError("Error loading Texture."); - Log.LogError(e.GetType() + " " + e.Message); - } - } + internal static void SaveStrings() + { +#if MULTI_LANGUAGE + string stringDir = GetAssetDir(AssetType.Strings, Utils.Language); +#else + string stringDir = GetAssetDir(AssetType.Strings); +#endif + if (!Directory.Exists(stringDir)) + { + Directory.CreateDirectory(stringDir); + } - internal static void LoadDefaultTextures() - { - string textureDefaultDir = Path.Combine(Core.Instance.modsDirectory, AssetType.Dumps.ToString(), "Default Textures [Do Not Remove]"); - if (!Directory.Exists(textureDefaultDir)) - { - Directory.CreateDirectory(textureDefaultDir); - } - try - { - foreach (string filepath in Directory.EnumerateFiles(textureDefaultDir, "*.png", SearchOption.AllDirectories)) - { - #if OBFUSCATE - if (CheckSumStore.IsModified(filepath)) - { - Log.LogError("File {0} was modified!" , filepath); - continue; - } - #endif + string translationStringRegex = System.Text.Json.JsonSerializer.Serialize(StringStore.translationStringRegex, new JsonSerializerOptions + { + WriteIndented = true, + Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping + }); - Texture2D texture2D = Utils.LoadImage(filepath); - TextureStore.textureDict[Path.GetFileNameWithoutExtension(filepath)] = filepath; - } - } + File.WriteAllText(Path.Combine(stringDir, "translation_regexs.json"), translationStringRegex); - catch (Exception e) - { - Log.LogError("Error loading Texture."); - Log.LogError(e.GetType() + " " + e.Message); - } - } + string translationString = System.Text.Json.JsonSerializer.Serialize(StringStore.translationString, new JsonSerializerOptions + { + WriteIndented = true, + Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping + }); - internal static void LoadCustomTextures() - { - string texturePackDir = Path.Combine(Core.Instance.modsDirectory, "[Custom Textures]"); + File.WriteAllText(Path.Combine(stringDir, "translation_strings.json"), translationString); + } - if (!Directory.Exists(texturePackDir)) - { - Directory.CreateDirectory(texturePackDir); - } - try - { - foreach (string filepath in Directory.EnumerateFiles(texturePackDir, "*.png", SearchOption.AllDirectories)) - { - #if OBFUSCATE - if (CheckSumStore.IsModified(filepath)) - { - Log.LogError("File {0} was modified!" , filepath); - continue; - } - #endif + public static void DumpJson() + { + string dumpDir = GetAssetDir(AssetType.Dumps); - Texture2D texture2D = Utils.LoadImage(filepath); - TextureStore.textureDict[Path.GetFileNameWithoutExtension(filepath)] = filepath; - } - } + if (!Directory.Exists(dumpDir)) + { + Directory.CreateDirectory(dumpDir); + } + string LawnStrings = Resources.Load("LawnStrings").text; + string ZombieStrings = Resources.Load("ZombieStrings").text; + File.WriteAllText(Path.Combine(dumpDir, "LawnStrings.json"), LawnStrings); + File.WriteAllText(Path.Combine(dumpDir, "ZombieStrings.json"), ZombieStrings); - catch (Exception e) - { - Log.LogError("Error loading Texture."); - Log.LogError(e.GetType() + " " + e.Message); - } - } + Dictionary achievementsList = new Dictionary(); + foreach (Il2CppSystem.Collections.Generic.KeyValuePair> entry in AchievementClip.achievementsText) + { + achievementsList.Add(entry.Key, new AchievementObject(entry.Key, entry.Value.Item1, entry.Value.Item2)); + } + File.WriteAllText(Path.Combine(dumpDir, "AchievementsText.json"), JsonSerializer.Serialize(achievementsList, new JsonSerializerOptions + { + WriteIndented = true, + Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping + })); + } - internal static void SaveStrings() - { - string stringDir = GetAssetDir(AssetType.Strings, Utils.Language); - if (!Directory.Exists(stringDir)) - { - Directory.CreateDirectory(stringDir); - } +#if DEBUG + public static void DumpUntranslatedStrings(string text) + { + string dumpDir = GetAssetDir(AssetType.Dumps); + string jsonFile = Path.Combine(dumpDir, "UntranslatedStrings.json"); - string translationStringRegex = JsonSerializer.Serialize(StringStore.translationStringRegex, new JsonSerializerOptions - { - WriteIndented = true, - Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping - }); + if (!Directory.Exists(dumpDir)) + { + Directory.CreateDirectory(dumpDir); + } - File.WriteAllText(Path.Combine(stringDir, "translation_regexs.json"), translationStringRegex); + if (!File.Exists(jsonFile)) + { + File.WriteAllText(jsonFile, "{}"); // Initialize empty JSON object + } - string translationString = JsonSerializer.Serialize(StringStore.translationString, new JsonSerializerOptions - { - WriteIndented = true, - Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping - }); + string json = File.ReadAllText(jsonFile); + var untranslatedStrings = JsonSerializer.Deserialize>(json); - File.WriteAllText(Path.Combine(stringDir, "translation_strings.json"), translationString); - } + if (!untranslatedStrings.ContainsKey(text)) + { + untranslatedStrings[text] = text; + var options = new JsonSerializerOptions + { + Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, + WriteIndented = true + }; + File.WriteAllText(jsonFile, JsonSerializer.Serialize(untranslatedStrings, options)); + } + } +#endif - #if DEBUG - public static void DumpJson() - { - string dumpDir = GetAssetDir(AssetType.Dumps); +#if MULTI_LANGUAGE + internal static void LoadLanguage() + { + try + { + // Load the language preference as a string and parse it into the enum + ConfigEntry languageEntry; + Core.Instance.Config.TryGetEntry(new ConfigDefinition("PvZ_Fusion_Translator", "Language"), out languageEntry); + string languageName = languageEntry.Value; + if (Enum.TryParse(languageName, out Utils.LanguageEnum loadedLanguage)) + { + Utils.Language = loadedLanguage; + } + else + { + Log.LogWarning($"Invalid language '{languageName}' found in preferences. Falling back to English."); + Utils.Language = Utils.LanguageEnum.English; // Default fallback + } + Log.LogWarning($"Loaded language {languageName}"); + } + catch (Exception e) + { + Log.LogError("Error loading language setting."); + Log.LogError($"{e.GetType()} {e.Message}"); + } + Log.LogInfo($"Language has been loaded: {Utils.Language}"); + } - if (!Directory.Exists(dumpDir)) - { - Directory.CreateDirectory(dumpDir); - } - string LawnStrings = Resources.Load("LawnStrings").text; - string ZombieStrings = Resources.Load("ZombieStrings").text; - File.WriteAllText(Path.Combine(dumpDir, "LawnStrings.json"), LawnStrings); - File.WriteAllText(Path.Combine(dumpDir, "ZombieStrings.json"), ZombieStrings); + internal static void SaveLanguage() + { + try + { + // Save the current language as a string + ConfigEntry languageEntry; + Core.Instance.Config.TryGetEntry(new ConfigDefinition("PvZ_Fusion_Translator", "Language"), out languageEntry); + languageEntry.Value = Utils.Language.ToString(); + Core.Instance.Config.Save(); - Dictionary achievementsList = new Dictionary(); - foreach (Il2CppSystem.Collections.Generic.KeyValuePair> entry in AchievementClip.achievementsText) - { - achievementsList.Add(entry.Key, new AchievementObject(entry.Key, entry.Value.Item1, entry.Value.Item2)); - } - File.WriteAllText(Path.Combine(dumpDir, "AchievementsText.json"), JsonSerializer.Serialize(achievementsList, new JsonSerializerOptions - { - WriteIndented = true, - Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping - })); - } - #endif - - #if DEBUG && MELON - public static void DumpUntranslatedStrings(string text) - { - string dumpDir = GetAssetDir(AssetType.Dumps); - string jsonFile = Path.Combine(dumpDir, "UntranslatedStrings.json"); - - if (!Directory.Exists(dumpDir)) - { - Directory.CreateDirectory(dumpDir); - } - - if (!File.Exists(jsonFile)) - { - File.WriteAllText(jsonFile, "{}"); // Initialize empty JSON object - } - - string json = File.ReadAllText(jsonFile); - JObject untranslatedStrings = JObject.Parse(json); - - if (!untranslatedStrings.ContainsKey(text)) - { - untranslatedStrings[text] = text; - File.WriteAllText(jsonFile, untranslatedStrings.ToString()); - } - } - #endif - - internal static void LoadLanguage() - { - try - { - // Load the language preference as a string and parse it into the enum - string languageName = Core.Instance.configLanguage.Value; - if (Enum.TryParse(languageName, out Utils.LanguageEnum loadedLanguage)) - { - Utils.Language = loadedLanguage; - } - else - { - Log.LogWarning($"Invalid language '{languageName}' found in preferences. Falling back to English."); - Utils.Language = Utils.LanguageEnum.English; // Default fallback - } - Log.LogWarning($"Loaded language {languageName}"); - } - catch (Exception e) - { - Log.LogError("Error loading language setting."); - Log.LogError($"{e.GetType()} {e.Message}"); - } - Log.LogInfo($"Language has been loaded: {Utils.Language}"); - } - - internal static void SaveLanguage() - { - try - { - // Save the current language as a string - if (Core.Instance.Config.TryGetEntry("PvZ_Fusion_Translator", "Language", out var languageEntry)) - { - languageEntry.Value = Utils.Language.ToString(); - } - - // Ensure changes are written to the config file - // MelonPreferences.Save(); - } - catch (Exception e) - { - Log.LogError("Error saving language setting."); - Log.LogError($"{e.GetType()} {e.Message}"); - } - // Log.LogInfo($"Language has been saved: {Utils.Language}"); - } - } + // Ensure changes are written to the config file + // MelonPreferences.Save(); + } + catch (Exception e) + { + Log.LogError("Error saving language setting."); + Log.LogError($"{e.GetType()} {e.Message}"); + } + // Log.LogInfo($"Language has been saved: {Utils.Language}"); + } +#endif + } } diff --git a/BepInEx/Patches/BaseTextObjects/InGameText_Patch.cs b/BepInEx/Patches/BaseTextObjects/InGameText_Patch.cs index 591e8d9..623f0f2 100644 --- a/BepInEx/Patches/BaseTextObjects/InGameText_Patch.cs +++ b/BepInEx/Patches/BaseTextObjects/InGameText_Patch.cs @@ -1,8 +1,7 @@ using HarmonyLib; - +using System; using TMPro; using PvZ_Fusion_Translator__BepInEx_.AssetStore; -using System; namespace PvZ_Fusion_Translator__BepInEx_.Patches.BaseTextObjects { diff --git a/BepInEx/Patches/GameObjects/AbyssBagMenu_Patch.cs b/BepInEx/Patches/GameObjects/AbyssBagMenu_Patch.cs index 5325526..22a8ee2 100644 --- a/BepInEx/Patches/GameObjects/AbyssBagMenu_Patch.cs +++ b/BepInEx/Patches/GameObjects/AbyssBagMenu_Patch.cs @@ -1,6 +1,7 @@ using HarmonyLib; using TMPro; + using PvZ_Fusion_Translator__BepInEx_.AssetStore; using UnityEngine; using UnityEngine.TextCore.Text; diff --git a/BepInEx/Patches/GameObjects/AbyssCheckInBag_Patch.cs b/BepInEx/Patches/GameObjects/AbyssCheckInBag_Patch.cs index 2b2603a..f468b2f 100644 --- a/BepInEx/Patches/GameObjects/AbyssCheckInBag_Patch.cs +++ b/BepInEx/Patches/GameObjects/AbyssCheckInBag_Patch.cs @@ -1,6 +1,7 @@ using HarmonyLib; using TMPro; + using PvZ_Fusion_Translator__BepInEx_.AssetStore; using UnityEngine; using UnityEngine.TextCore.Text; diff --git a/BepInEx/Patches/GameObjects/AbyssDevelopMenu_Patch.cs b/BepInEx/Patches/GameObjects/AbyssDevelopMenu_Patch.cs index eeeb6eb..479c0bf 100644 --- a/BepInEx/Patches/GameObjects/AbyssDevelopMenu_Patch.cs +++ b/BepInEx/Patches/GameObjects/AbyssDevelopMenu_Patch.cs @@ -1,7 +1,6 @@ using System.Text.RegularExpressions; -using System.Collections.Generic; using HarmonyLib; - +using System.Collections.Generic; using TMPro; using PvZ_Fusion_Translator__BepInEx_.AssetStore; using UnityEngine.TextCore.Text; @@ -22,7 +21,7 @@ namespace PvZ_Fusion_Translator__BepInEx_.Patches.GameObjects TextMeshProUGUI upgradeCostText = __instance.transform.FindChild("UpgradeCost").GetComponent(); TMP_FontAsset fontAsset = FontStore.LoadTMPFont(Utils.Language.ToString()); - string regexStr = "【([^\\s]+)】\n当前等级:([^\\s]+)\n\n升级条件:\n([^\\s]+)\n\n升级消耗:([^\\s]+)"; + string regexStr = "([^\\s]+)\n当前等级:([^\\s]+)\n\n升级条件:\n([^\\s]+)\n\n升级消耗:([^\\s]+)"; Regex regex = new Regex(regexStr); diff --git a/BepInEx/Patches/GameObjects/AbyssMenu2_Patch.cs b/BepInEx/Patches/GameObjects/AbyssMenu2_Patch.cs index 033836b..8dbc04e 100644 --- a/BepInEx/Patches/GameObjects/AbyssMenu2_Patch.cs +++ b/BepInEx/Patches/GameObjects/AbyssMenu2_Patch.cs @@ -1,6 +1,7 @@ using HarmonyLib; using TMPro; + using PvZ_Fusion_Translator__BepInEx_.AssetStore; using UnityEngine; using UnityEngine.TextCore.Text; diff --git a/BepInEx/Patches/GameObjects/AbyssMenu_Patch.cs b/BepInEx/Patches/GameObjects/AbyssMenu_Patch.cs index 779e9cf..3e98623 100644 --- a/BepInEx/Patches/GameObjects/AbyssMenu_Patch.cs +++ b/BepInEx/Patches/GameObjects/AbyssMenu_Patch.cs @@ -1,13 +1,15 @@ using HarmonyLib; using TMPro; +using System; +using System.Collections.Generic; using PvZ_Fusion_Translator__BepInEx_.AssetStore; using Unity.VisualScripting; using UnityEngine; using System.Text.RegularExpressions; -using System.Collections.Generic; using UnityEngine.UI; + namespace PvZ_Fusion_Translator__BepInEx_.Patches.GameObjects { [HarmonyPatch(typeof(AbyssMenu))] diff --git a/BepInEx/Patches/GameObjects/AbyssSettlementMenu_Patch.cs b/BepInEx/Patches/GameObjects/AbyssSettlementMenu_Patch.cs index 7c53d32..1434127 100644 --- a/BepInEx/Patches/GameObjects/AbyssSettlementMenu_Patch.cs +++ b/BepInEx/Patches/GameObjects/AbyssSettlementMenu_Patch.cs @@ -1,11 +1,10 @@ using HarmonyLib; - using TMPro; +using System.Collections.Generic; using PvZ_Fusion_Translator__BepInEx_.AssetStore; using Unity.VisualScripting; using UnityEngine; using System.Text.RegularExpressions; -using System.Collections.Generic; namespace PvZ_Fusion_Translator__BepInEx_.Patches.GameObjects { @@ -19,45 +18,36 @@ namespace PvZ_Fusion_Translator__BepInEx_.Patches.GameObjects { TMP_FontAsset fontAsset = FontStore.LoadTMPFont(Utils.Language.ToString()); - string regexStr1 = "第(\\d+)关\n通关用时:(\\d+)分(\\d+)秒\n杀敌数量:(\\d+)\n关卡叶绿素产出:(\\d+)\n极限模式产出:(\\d+)\n非首胜无额外产出\n产出叶绿素总计:(\\d+)\n"; - string regexStr2 = "第(\\d+)关\n通关用时:(\\d+)分(\\d+)秒\n杀敌数量:(\\d+)\n关卡叶绿素产出:(\\d+)\n极限模式产出:(\\d+)\n首胜产出:(\\d+)\n剩余小推车数量:(\\d+),获得叶绿素:(\\d+)\n产出叶绿素总计:(\\d+)"; + string[] regexStrList = [ + "第(\\d+)关\n通关用时:(\\d+)分(\\d+)秒\n杀敌数量:(\\d+)\n关卡叶绿素产出:(\\d+)\n极限模式产出:(\\d+)\n非首胜无额外产出\n产出叶绿素总计:(\\d+)", + "第(\\d+)关\n通关用时:(\\d+)分(\\d+)秒\n杀敌数量:(\\d+)\n关卡叶绿素产出:(\\d+)\n极限模式产出:(\\d+)\n首胜产出:(\\d+)\n剩余小推车数量:(\\d+),获得叶绿素:(\\d+)\n产出叶绿素总计:(\\d+)", + "第(\\d+)关\n通关用时:(\\d+)分(\\d+)秒\n杀敌数量:(\\d+)\n关卡叶绿素产出:(\\d+)\n首胜产出:(\\d+)\n剩余小推车数量:(\\d+),获得叶绿素:(\\d+)\n产出叶绿素总计:(\\d+)", + "第(\\d+)关\n通关用时:(\\d+)分(\\d+)秒\n杀敌数量:(\\d+)\n关卡叶绿素产出:(\\d+)\n困难模式产出:(\\d+)\n首胜产出:(\\d+)\n剩余小推车数量:(\\d+),获得叶绿素:(\\d+)\n产出叶绿素总计:(\\d+)", + "第(\\d+)关\n通关用时:(\\d+)分(\\d+)秒\n杀敌数量:(\\d+)\n关卡叶绿素产出:(\\d+)\n非首胜无额外产出\n产出叶绿素总计:(\\d+)", + "第(\\d+)关\n通关用时:(\\d+)分(\\d+)秒\n杀敌数量:(\\d+)\n关卡叶绿素产出:(\\d+)\n困难模式产出:(\\d+)\n非首胜无额外产出\n产出叶绿素总计:(\\d+)" + ]; - Regex regex1 = new Regex(regexStr1); - Regex regex2 = new Regex(regexStr2); - - if (regex1.IsMatch(__instance.settleInfo.text)) + foreach(string regexStr in regexStrList) { - Match match = regex1.Match(__instance.settleInfo.text); - int groupCount = match.Groups.Count; + Regex regex = new Regex(regexStr); - List dynamicParts = []; - - for (int i = 1; i < groupCount; i++) + if (regex.IsMatch(__instance.settleInfo.text)) { - string groupValue = match.Groups[i].Value; - string translatedValue = StringStore.translationString.ContainsKey(groupValue) ? StringStore.translationString[groupValue] : groupValue; - dynamicParts.Add(translatedValue); + Match match = regex.Match(__instance.settleInfo.text); + int groupCount = match.Groups.Count; + + List dynamicParts = []; + + 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); + } + + __instance.settleInfo.text = string.Format(StringStore.translationStringRegex[regexStr], [.. dynamicParts]); + __instance.settleInfo.font = fontAsset; } - - __instance.settleInfo.text = string.Format(StringStore.translationStringRegex[regexStr1], [.. dynamicParts]); - __instance.settleInfo.font = fontAsset; - } - else if (regex2.IsMatch(__instance.settleInfo.text)) - { - Match match = regex2.Match(__instance.settleInfo.text); - int groupCount = match.Groups.Count; - - List dynamicParts = []; - - 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); - } - - __instance.settleInfo.text = string.Format(StringStore.translationStringRegex[regexStr2], [.. dynamicParts]); - __instance.settleInfo.font = fontAsset; } } } diff --git a/BepInEx/Patches/GameObjects/ButtonObjects/AbyssBuffButton_Patch.cs b/BepInEx/Patches/GameObjects/ButtonObjects/AbyssBuffButton_Patch.cs index 54f42b8..3ab8bd5 100644 --- a/BepInEx/Patches/GameObjects/ButtonObjects/AbyssBuffButton_Patch.cs +++ b/BepInEx/Patches/GameObjects/ButtonObjects/AbyssBuffButton_Patch.cs @@ -5,7 +5,7 @@ using HarmonyLib; using TMPro; using PvZ_Fusion_Translator__BepInEx_.AssetStore; using UnityEngine; -using MelonLoader; + namespace PvZ_Fusion_Translator__BepInEx_.Patches.GameObjects.ButtonObjects { diff --git a/BepInEx/Patches/GameObjects/ButtonObjects/CustomLevelEnter_Patch.cs b/BepInEx/Patches/GameObjects/ButtonObjects/CustomLevelEnter_Patch.cs index 8bb0756..2f67bc2 100644 --- a/BepInEx/Patches/GameObjects/ButtonObjects/CustomLevelEnter_Patch.cs +++ b/BepInEx/Patches/GameObjects/ButtonObjects/CustomLevelEnter_Patch.cs @@ -1,6 +1,7 @@ using HarmonyLib; using TMPro; + using PvZ_Fusion_Translator__BepInEx_.AssetStore; using UnityEngine; diff --git a/BepInEx/Patches/GameObjects/ButtonObjects/InGameBtn_Patch.cs b/BepInEx/Patches/GameObjects/ButtonObjects/InGameBtn_Patch.cs index 21823a0..102a731 100644 --- a/BepInEx/Patches/GameObjects/ButtonObjects/InGameBtn_Patch.cs +++ b/BepInEx/Patches/GameObjects/ButtonObjects/InGameBtn_Patch.cs @@ -1,6 +1,7 @@ using HarmonyLib; using TMPro; +using PvZ_Fusion_Translator__BepInEx_.AssetStore; using UnityEngine; namespace PvZ_Fusion_Translator__BepInEx_.Patches.GameObjects.ButtonObjects @@ -30,6 +31,11 @@ namespace PvZ_Fusion_Translator__BepInEx_.Patches.GameObjects.ButtonObjects } } } - } + + foreach(TextMeshProUGUI text in __instance.transform.GetComponentsInChildren()) + { + text.text = StringStore.TranslateText(text.text); + } + } } } diff --git a/BepInEx/Patches/GameObjects/ButtonObjects/OptLanguageBtn_Patch.cs b/BepInEx/Patches/GameObjects/ButtonObjects/OptLanguageBtn_Patch.cs index f445d18..e732656 100644 --- a/BepInEx/Patches/GameObjects/ButtonObjects/OptLanguageBtn_Patch.cs +++ b/BepInEx/Patches/GameObjects/ButtonObjects/OptLanguageBtn_Patch.cs @@ -1,22 +1,30 @@ #if MULTI_LANGUAGE -using System; -using System.Linq; -using System.Collections.Generic; -using System.Threading.Tasks; using HarmonyLib; - +using System.Collections.Generic; +using System; +using System.IO; +using System.Linq; +using System.Threading.Tasks; using TMPro; using PvZ_Fusion_Translator__BepInEx_; using PvZ_Fusion_Translator__BepInEx_.AssetStore; using UnityEngine; +using UnityEngine.UIElements; using Object = UnityEngine.Object; +using BepInEx.Configuration; +using BepInEx; +using UnityEngine.Experimental.Rendering.RenderGraphModule; +using BepInEx.Unity.IL2CPP.Utils; +using Unity.VisualScripting.FullSerializer; 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__BepInEx_.Patches.GameObjects.ButtonObjects @@ -24,19 +32,27 @@ namespace PvZ_Fusion_Translator__BepInEx_.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; @@ -68,12 +84,45 @@ namespace PvZ_Fusion_Translator__BepInEx_.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()]; @@ -94,6 +143,30 @@ namespace PvZ_Fusion_Translator__BepInEx_.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()]; @@ -134,15 +207,28 @@ namespace PvZ_Fusion_Translator__BepInEx_.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); @@ -156,6 +242,30 @@ namespace PvZ_Fusion_Translator__BepInEx_.Patches.GameObjects.ButtonObjects } } + private static void ToggleCustomAssets(string type) + { + if (type != "Textures" && type != "Audio") return; + + if(type == "Textures") + { + Utils.customTextures = !Utils.customTextures; + ConfigEntry customTexturesEntry; + Core.Instance.Config.TryGetEntry(new ConfigDefinition("PvZ_Fusion_Translator", "DefaultTextures"), out customTexturesEntry); + customTexturesEntry.BoxedValue = !Utils.customTextures; + TextureStore.Reload(); + Coroutine replaceTextureRoutine = Core.MonoInstance.StartCoroutine(TextureStore.ReplaceTexturesCoroutine()); + } + + if (type == "Audio") + { + Utils.customAudio = !Utils.customAudio; + ConfigEntry customAudioEntry; + Core.Instance.Config.TryGetEntry(new ConfigDefinition("PvZ_Fusion_Translator", "DefaultAudio"), out customAudioEntry); + customAudioEntry.BoxedValue = !Utils.customAudio; + } + Core.Instance.Config.Save(); + } + [HarmonyPatch(typeof(OptionBtn))] public static class OptLangBtn_Patch { @@ -166,8 +276,10 @@ namespace PvZ_Fusion_Translator__BepInEx_.Patches.GameObjects.ButtonObjects if (!buttonsCreated || cachedTemplateButton == null) { buttonsCreated = false; - CreateLanguageButtons(__instance); - } + togglesCreated = false; + CreateLanguageButtons(__instance); + CreateToggleButtons(__instance); + } } [HarmonyPatch("OnMouseUpAsButton")] @@ -199,7 +311,21 @@ namespace PvZ_Fusion_Translator__BepInEx_.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!", 0.1f); + } + else if (toggleType == "Audio") + { + FlashMessage(toggleData.Button, "Toggled custom audio!", 0.1f); + } + } + } [HarmonyPatch(nameof(OptionBtn.Update))] [HarmonyPostfix] @@ -209,7 +335,12 @@ namespace PvZ_Fusion_Translator__BepInEx_.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/BepInEx/Patches/GameObjects/MainMenu_Patch.cs b/BepInEx/Patches/GameObjects/MainMenu_Patch.cs index ee24357..36bbd6b 100644 --- a/BepInEx/Patches/GameObjects/MainMenu_Patch.cs +++ b/BepInEx/Patches/GameObjects/MainMenu_Patch.cs @@ -1,6 +1,5 @@ using HarmonyLib; -using TMPro; using PvZ_Fusion_Translator__BepInEx_.AssetStore; using UnityEngine; @@ -9,12 +8,17 @@ namespace PvZ_Fusion_Translator__BepInEx_.Patches.GameObjects [HarmonyPatch(typeof(MainMenu))] public class MainMenu_Patch { + private static bool _warningShown = false; + [HarmonyPostfix] [HarmonyPatch(nameof(MainMenu.FixedUpdate))] private static void FixedUpdate(MainMenu __instance) { - NoticeMenu noticeMenu = NoticeMenu.Instance; - WarningStore.WarningReload(Utils.Language, noticeMenu); + if (_warningShown || NoticeMenu.Instance == null) + return; + + WarningStore.WarningReload(Utils.Language, NoticeMenu.Instance); + _warningShown = true; } } -} +} \ No newline at end of file diff --git a/BepInEx/Patches/GameObjects/MinorObjects/PlayerName_Patch.cs b/BepInEx/Patches/GameObjects/MinorObjects/PlayerName_Patch.cs index c8fe7fe..feefb35 100644 --- a/BepInEx/Patches/GameObjects/MinorObjects/PlayerName_Patch.cs +++ b/BepInEx/Patches/GameObjects/MinorObjects/PlayerName_Patch.cs @@ -1,6 +1,5 @@ using HarmonyLib; -using TMPro; using PvZ_Fusion_Translator__BepInEx_.AssetStore; namespace PvZ_Fusion_Translator__BepInEx_.Patches.GameObjects.MinorObjects diff --git a/BepInEx/Patches/GameObjects/OtherMenu_Patch.cs b/BepInEx/Patches/GameObjects/OtherMenu_Patch.cs new file mode 100644 index 0000000..c055cbd --- /dev/null +++ b/BepInEx/Patches/GameObjects/OtherMenu_Patch.cs @@ -0,0 +1,20 @@ +using HarmonyLib; + +using TMPro; +using PvZ_Fusion_Translator__BepInEx_.AssetStore; +using UnityEngine; + +namespace PvZ_Fusion_Translator__BepInEx_.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/BepInEx/Patches/GameObjects/TravelBuffOptionButton_Patch.cs b/BepInEx/Patches/GameObjects/TravelBuffOptionButton_Patch.cs index 0a478a0..87269ea 100644 --- a/BepInEx/Patches/GameObjects/TravelBuffOptionButton_Patch.cs +++ b/BepInEx/Patches/GameObjects/TravelBuffOptionButton_Patch.cs @@ -14,6 +14,27 @@ namespace PvZ_Fusion_Translator__BepInEx_.Patches.GameObjects { __instance.introduce.text = StringStore.TranslateText(__instance.introduce.text); } + + [HarmonyPatch(nameof(TravelBuffOptionButton.Awake))] + [HarmonyPostfix] + private static void Awake(TravelBuffOptionButton __instance) + { + __instance.introduce.text = StringStore.TranslateText(__instance.introduce.text); + } + + [HarmonyPatch(nameof(TravelBuffOptionButton.OnAnimOver))] + [HarmonyPostfix] + private static void OnAnimOver(TravelBuffOptionButton __instance) + { + __instance.introduce.text = StringStore.TranslateText(__instance.introduce.text); + } + + [HarmonyPatch(nameof(TravelBuffOptionButton.SetBackground))] + [HarmonyPostfix] + private static void SetBackground(TravelBuffOptionButton __instance) + { + __instance.introduce.text = StringStore.TranslateText(__instance.introduce.text); + } //[HarmonyPatch(nameof(TravelMenuMgr.SetText))] //[HarmonyPostfix] //private static void SetText(TravelMenuMgr __instance) diff --git a/BepInEx/Patches/GameObjects/TravelStore_Patch.cs b/BepInEx/Patches/GameObjects/TravelStore_Patch.cs index 2ba6773..171d2d7 100644 --- a/BepInEx/Patches/GameObjects/TravelStore_Patch.cs +++ b/BepInEx/Patches/GameObjects/TravelStore_Patch.cs @@ -1,9 +1,8 @@ using HarmonyLib; - +using System; using TMPro; using PvZ_Fusion_Translator__BepInEx_.AssetStore; using UnityEngine; -using System; namespace PvZ_Fusion_Translator__BepInEx_.Patches.GameObjects { @@ -24,7 +23,12 @@ namespace PvZ_Fusion_Translator__BepInEx_.Patches.GameObjects { StringStore.TranslateTextTransform(moneyTransform); } - } + + foreach (TextMeshProUGUI text in __instance.points) + { + text.text = StringStore.TranslateText(text.text); + } + } [HarmonyPatch(nameof(TravelStore.SetText), new Type[] { typeof(string)})] [HarmonyPostfix] @@ -34,6 +38,11 @@ namespace PvZ_Fusion_Translator__BepInEx_.Patches.GameObjects { text.text = StringStore.TranslateText(text.text); } - } + + foreach (TextMeshProUGUI text in __instance.points) + { + text.text = StringStore.TranslateText(text.text); + } + } } } diff --git a/BepInEx/Patches/GameObjects/UIDifficulty_Patch.cs b/BepInEx/Patches/GameObjects/UIDifficulty_Patch.cs index 413194c..d1d1a5c 100644 --- a/BepInEx/Patches/GameObjects/UIDifficulty_Patch.cs +++ b/BepInEx/Patches/GameObjects/UIDifficulty_Patch.cs @@ -15,8 +15,7 @@ namespace PvZ_Fusion_Translator__BepInEx_.Patches.GameObjects #if MULTI_LANGUAGE TMP_FontAsset fontAsset = FontStore.LoadTMPFont(Utils.Language.ToString()); - string difficultyText = StringStore.patchesStore["Difficulty"][Utils.Language.ToString()]; - __instance.t.text = string.Format(difficultyText + " {0}", GameAPP.difficulty); + __instance.t.text = StringStore.TranslateText(__instance.t.text); #else TMP_FontAsset fontAsset = FontStore.LoadTMPFont(); string difficultyText = StringStore.patchesStore["Difficulty"]["Spanish"]; diff --git a/BepInEx/Patches/GameObjects/UIZombieNum_Patch.cs b/BepInEx/Patches/GameObjects/UIZombieNum_Patch.cs index 0614e94..d89a8cf 100644 --- a/BepInEx/Patches/GameObjects/UIZombieNum_Patch.cs +++ b/BepInEx/Patches/GameObjects/UIZombieNum_Patch.cs @@ -15,8 +15,8 @@ namespace PvZ_Fusion_Translator__BepInEx_.Patches.GameObjects #if MULTI_LANGUAGE TMP_FontAsset fontAsset = FontStore.LoadTMPFont(Utils.Language.ToString()); - string zombieText = StringStore.patchesStore["Zombies"][Utils.Language.ToString()]; - __instance.t.text = string.Format(zombieText + " {0}", Board.Instance.enermyCount); + string zombieText = StringStore.TranslateText(__instance.t.text); + __instance.t.text = zombieText; #else TMP_FontAsset fontAsset = FontStore.LoadTMPFont(); string zombieText = StringStore.patchesStore["Zombies"]["Spanish"]; diff --git a/BepInEx/Patches/Managers/AlmanacMgrZombie_Patch.cs b/BepInEx/Patches/Managers/AlmanacMgrZombie_Patch.cs index 14cb727..6c1658a 100644 --- a/BepInEx/Patches/Managers/AlmanacMgrZombie_Patch.cs +++ b/BepInEx/Patches/Managers/AlmanacMgrZombie_Patch.cs @@ -1,10 +1,9 @@ using HarmonyLib; - +using System.IO; using TMPro; using PvZ_Fusion_Translator__BepInEx_.AssetStore; -using static PvZ_Fusion_Translator__BepInEx_.FileLoader; using UnityEngine; -using System.IO; +using static PvZ_Fusion_Translator__BepInEx_.FileLoader; namespace PvZ_Fusion_Translator__BepInEx_.Patches.Managers { @@ -12,8 +11,8 @@ namespace PvZ_Fusion_Translator__BepInEx_.Patches.Managers public static partial class AlmanacMgrZombie_Patch { [HarmonyPatch(nameof(AlmanacMgrZombie.InitNameAndInfoFromJson))] - [HarmonyPrefix] - private static bool InitNameAndInfoFromJson(AlmanacMgrZombie __instance) + [HarmonyPostfix] + private static void InitNameAndInfoFromJson(AlmanacMgrZombie __instance) { #if MULTI_LANGUAGE string currentLanguage = Utils.Language.ToString(); @@ -23,18 +22,19 @@ namespace PvZ_Fusion_Translator__BepInEx_.Patches.Managers string currentLanguage = "English"; #endif string path = Path.Combine(almanacDir, "ZombieStringsTranslate.json"); + string moddedPath = Path.Combine(almanacDir, "ModdedZombiesTranslate.json"); if (!File.Exists(path)) { Log.LogError($"ZombieStringsTranslate.json file not found at path: {path}"); - return true; + return; } #if OBFUSCATE if (CheckSumStore.IsModified(path)) { Log.LogError("File {0} was modified!", path); - return true; + return; } #endif @@ -78,12 +78,38 @@ namespace PvZ_Fusion_Translator__BepInEx_.Patches.Managers component.font = fontAsset; component2.font = fontAsset; component3.font = fontAsset; - - return false; } } - return true; + if (File.Exists(moddedPath)) + { + string moddedJson; + moddedJson = File.ReadAllText(moddedPath); + + AlmanacMgrZombie.ZombieAlmanacData moddedZombieData = JsonUtility.FromJson(moddedJson); + + foreach (AlmanacMgrZombie.ZombieInfo zombieInfo in moddedZombieData.zombies) + { + if (zombieInfo.theZombieType == __instance.theZombieType) + { + component.text = zombieInfo.info + "\n\n" + zombieInfo.introduce; + component.overflowMode = TextOverflowModes.Page; + component2.text = zombieInfo.name; + component2.autoSizeTextContainer = true; + component3.text = Utils.RemoveColorTags(zombieInfo.name); + component3.autoSizeTextContainer = true; + + if (hasAlmanacFont) + component.font = almanacFontAsset; + else + component.font = fontAsset; + component2.font = fontAsset; + component3.font = fontAsset; + } + } + } + + return; } [HarmonyPatch(nameof(AlmanacMgrZombie.OnMouseDown))] diff --git a/BepInEx/Patches/Managers/AlmanacPlantBank_Patch.cs b/BepInEx/Patches/Managers/AlmanacPlantBank_Patch.cs index b85b05d..dae4d68 100644 --- a/BepInEx/Patches/Managers/AlmanacPlantBank_Patch.cs +++ b/BepInEx/Patches/Managers/AlmanacPlantBank_Patch.cs @@ -1,7 +1,6 @@ using HarmonyLib; - -using TMPro; using System.IO; +using TMPro; using PvZ_Fusion_Translator__BepInEx_.AssetStore; using UnityEngine; using static PvZ_Fusion_Translator__BepInEx_.FileLoader; @@ -23,6 +22,7 @@ namespace PvZ_Fusion_Translator__BepInEx_.Patches.Managers string currentLanguage = "English"; #endif string path = Path.Combine(almanacDir, "LawnStringsTranslate.json"); + string moddedPath = Path.Combine(almanacDir, "ModdedPlantsTranslate.json"); if (!File.Exists(path)) { @@ -60,23 +60,24 @@ namespace PvZ_Fusion_Translator__BepInEx_.Patches.Managers TextMeshPro component3 = __instance.plantName.transform.GetChild(0).GetComponent(); TextMeshPro component4 = __instance.cost.GetComponent(); - AlmanacPlantBank.PlantData plantData = JsonUtility.FromJson(json); + AlmanacPlantBank.PlantData plantData = JsonUtility.FromJson(json); foreach (AlmanacPlantBank.PlantInfo plantInfo in plantData.plants) { if (plantInfo.seedType == __instance.theSeedType) { - component.autoSizeTextContainer = false; - component.text = plantInfo.info + "\n\n" + plantInfo.introduce; + component.autoSizeTextContainer = false; + component.text = plantInfo.info + "\n\n" + plantInfo.introduce; component.overflowMode = TextOverflowModes.Page; - //fix dimensions for cost text - component.rectTransform.offsetMax = new Vector2(component.rectTransform.offsetMax.x, 27.3839f); + // fix dimensions for cost text + component.rectTransform.offsetMax = new Vector2(component.rectTransform.offsetMax.x, 27.3839f); component.rectTransform.offsetMin = new Vector2(component.rectTransform.offsetMin.x, -29.3079f); - component.rectTransform.sizeDelta = new Vector2(component.rectTransform.sizeDelta.x, 50.917f); + component.rectTransform.sizeDelta = new Vector2(component.rectTransform.sizeDelta.x, 50.917f); component.transform.localPosition = new Vector3(component.transform.localPosition.x, component.transform.localPosition.y + 0.15f, component.transform.localPosition.z); + - component2.text = plantInfo.name; + component2.text = plantInfo.name; component2.autoSizeTextContainer = true; component3.text = Utils.RemoveColorTags(plantInfo.name); @@ -84,7 +85,7 @@ namespace PvZ_Fusion_Translator__BepInEx_.Patches.Managers component4.text = plantInfo.cost; - if (hasAlmanacFont) + if (hasAlmanacFont) { component.font = almanacFontAsset; component4.font = almanacFontAsset; @@ -99,10 +100,56 @@ namespace PvZ_Fusion_Translator__BepInEx_.Patches.Managers } } + if (File.Exists(moddedPath)) + { + string moddedJson; + moddedJson = File.ReadAllText(moddedPath); + + AlmanacPlantBank.PlantData moddedPlantData = JsonUtility.FromJson(moddedJson); + + foreach (AlmanacPlantBank.PlantInfo plantInfo in moddedPlantData.plants) + { + if (plantInfo.seedType == __instance.theSeedType) + { + component.autoSizeTextContainer = false; + component.text = plantInfo.info + "\n\n" + plantInfo.introduce; + component.overflowMode = TextOverflowModes.Page; + + // fix dimensions for cost text + component.rectTransform.offsetMax = new Vector2(component.rectTransform.offsetMax.x, 27.3839f); + component.rectTransform.offsetMin = new Vector2(component.rectTransform.offsetMin.x, -29.3079f); + component.rectTransform.sizeDelta = new Vector2(component.rectTransform.sizeDelta.x, 50.917f); + component.transform.localPosition = new Vector3(component.transform.localPosition.x, component.transform.localPosition.y + 0.15f, component.transform.localPosition.z); + + + component2.text = plantInfo.name; + component2.autoSizeTextContainer = true; + + component3.text = Utils.RemoveColorTags(plantInfo.name); + component3.autoSizeTextContainer = true; + + component4.text = plantInfo.cost; + + if (hasAlmanacFont) + { + component.font = almanacFontAsset; + component4.font = almanacFontAsset; + } + else + { + component.font = fontAsset; + component4.font = fontAsset; + } + component2.font = fontAsset; + component3.font = fontAsset; + } + } + } + return; } - [HarmonyPatch(nameof(AlmanacPlantBank.OnMouseDown))] + [HarmonyPatch(nameof(AlmanacPlantBank.OnMouseDown))] [HarmonyPrefix] private static bool OnMouseDown(AlmanacPlantBank __instance) { @@ -115,15 +162,15 @@ namespace PvZ_Fusion_Translator__BepInEx_.Patches.Managers return true; } - [HarmonyPatch(nameof(AlmanacPlantBank.PVPInit))] - [HarmonyPostfix] - private static void PVPInit(AlmanacPlantBank __instance) - { - TextMeshPro component = __instance.introduce.GetComponent(); - if (component != null) - { + [HarmonyPatch(nameof(AlmanacPlantBank.PVPInit))] + [HarmonyPostfix] + private static void PVPInit(AlmanacPlantBank __instance) + { + TextMeshPro component = __instance.introduce.GetComponent(); + if (component != null) + { component.autoSizeTextContainer = false; - } - } - } + } + } + } } \ No newline at end of file diff --git a/BepInEx/Patches/Managers/OppositeBuffManager_Patch.cs b/BepInEx/Patches/Managers/OppositeBuffManager_Patch.cs index c1a6d95..9a14a3e 100644 --- a/BepInEx/Patches/Managers/OppositeBuffManager_Patch.cs +++ b/BepInEx/Patches/Managers/OppositeBuffManager_Patch.cs @@ -1,8 +1,7 @@ using HarmonyLib; - +using System; using TMPro; using PvZ_Fusion_Translator__BepInEx_.AssetStore; -using System; namespace PvZ_Fusion_Translator__BepInEx_.Patches.Managers { diff --git a/BepInEx/Patches/Managers/TravelBuffMgr_Patch.cs b/BepInEx/Patches/Managers/TravelBuffMgr_Patch.cs index 1457aa6..b98b0aa 100644 --- a/BepInEx/Patches/Managers/TravelBuffMgr_Patch.cs +++ b/BepInEx/Patches/Managers/TravelBuffMgr_Patch.cs @@ -1,5 +1,4 @@ using HarmonyLib; - using System; namespace PvZ_Fusion_Translator__BepInEx_.Patches.Managers diff --git a/BepInEx/Patches/OtherManagers/AchievementClip_Patch.cs b/BepInEx/Patches/OtherManagers/AchievementClip_Patch.cs index df50783..a9c1ca7 100644 --- a/BepInEx/Patches/OtherManagers/AchievementClip_Patch.cs +++ b/BepInEx/Patches/OtherManagers/AchievementClip_Patch.cs @@ -1,7 +1,6 @@ -using System.IO; +using HarmonyLib; +using System.IO; using System.Collections.Generic; -using HarmonyLib; - using TMPro; using Newtonsoft.Json; using PvZ_Fusion_Translator__BepInEx_.AssetStore; diff --git a/BepInEx/PvZ Fusion Translator [BepInEx].csproj b/BepInEx/PvZ Fusion Translator [BepInEx].csproj index ef6ece2..ac777dd 100644 --- a/BepInEx/PvZ Fusion Translator [BepInEx].csproj +++ b/BepInEx/PvZ Fusion Translator [BepInEx].csproj @@ -23,6 +23,12 @@ + + + True + false + + $(DefineConstants);MULTI_LANGUAGE;CHEAT;DEBUG @@ -75,26 +81,14 @@ $(DefineConstants);MULTI_LANGUAGE;OBFUSCATE - - $(DefineConstants);MULTI_LANGUAGE;OBFUSCATE - - $(DefineConstants);MULTI_LANGUAGE;OBFUSCATE - - $(DefineConstants);MULTI_LANGUAGE;OBFUSCATE - - $(DefineConstants);MULTI_LANGUAGE;OBFUSCATE - - $(DefineConstants);MULTI_LANGUAGE;OBFUSCATE - - $(DefineConstants);OTHER;OBFUSCATE @@ -110,6 +104,83 @@ $(DefineConstants);MULTI_LANGUAGE;OBFUSCATE;RELEASE + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/BepInEx/Utils.cs b/BepInEx/Utils.cs index 6dcaf4b..a27283e 100644 --- a/BepInEx/Utils.cs +++ b/BepInEx/Utils.cs @@ -1,128 +1,152 @@ -using PvZ_Fusion_Translator__BepInEx_.AssetStore; +using BepInEx; +using BepInEx.Configuration; +using PvZ_Fusion_Translator__BepInEx_.AssetStore; using System; using System.Diagnostics; using System.IO; using System.Text.RegularExpressions; +using Unity.VisualScripting.FullSerializer; using UnityEngine; using UnityEngine.Experimental.Rendering; namespace PvZ_Fusion_Translator__BepInEx_ { - public class Utils - { - internal static bool TryReplaceTexture2D(Texture2D ogTexture) - { - if (ogTexture != null) - { - if (TextureStore.textureDict.TryGetValue(ogTexture.name, out string texturePath)) - { - try - { - ImageConversion.LoadImage(ogTexture, File.ReadAllBytes(texturePath)); + public class Utils + { + internal static bool TryReplaceTexture2D(Texture2D ogTexture) + { + if (ogTexture != null) + { + if (TextureStore.textureDict.TryGetValue(ogTexture.name, out string texturePath)) + { + try + { + ImageConversion.LoadImage(ogTexture, File.ReadAllBytes(texturePath)); - Log.LogDebug("OK! Replaced Texture " + ogTexture.name); + Core.Log.LogDebug("OK! Replaced Texture " + ogTexture.name); - ogTexture.name = "replaced_" + ogTexture.name; - return true; - } - catch (Exception ex) - { - Log.LogError("Failed to replace texture: " + ogTexture.name + " at path: " + texturePath); - Log.LogError(ex.ToString()); - } - } - } - return false; - } + ogTexture.name = "replaced_" + ogTexture.name; + return true; + } + catch (Exception ex) + { + Log.LogError("Failed to replace texture: " + ogTexture.name + " at path: " + texturePath); + Log.LogError(ex.ToString()); + } + } + } + return false; + } - internal static Texture2D LoadImage(string path) - { - if (!File.Exists(path)) - { - throw new FileNotFoundException($"The image file at path '{path}' does not exist."); - } + internal static Texture2D LoadImage(string path) + { + if (!File.Exists(path)) + { + throw new FileNotFoundException($"The image file at path '{path}' does not exist."); + } - byte[] array = File.ReadAllBytes(path); - Texture2D texture2D = new(2, 2, GraphicsFormat.R8G8B8A8_UNorm, TextureCreationFlags.None, 1, IntPtr.Zero, null); - ImageConversion.LoadImage(texture2D, array); - return texture2D; - } + byte[] array = File.ReadAllBytes(path); + Texture2D texture2D = new(2, 2, GraphicsFormat.R8G8B8A8_UNorm, TextureCreationFlags.None, 1, IntPtr.Zero, null); + ImageConversion.LoadImage(texture2D, array); + return texture2D; + } - public static Color HexToColor(string hex) - { + public static Color HexToColor(string hex) + { - hex = hex.Replace("#", ""); + hex = hex.Replace("#", ""); - float r = int.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber) / 255f; - float g = int.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber) / 255f; - float b = int.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber) / 255f; + float r = int.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber) / 255f; + float g = int.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber) / 255f; + float b = int.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber) / 255f; - return new Color(r, g, b); - } + return new Color(r, g, b); + } - public static void OpenSaveDirectory() - { - string saveDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "AppData", "LocalLow", "LanPiaoPiao", "PlantsVsZombiesRH"); - Process.Start("explorer.exe", saveDirectory); - } + public static void OpenSaveDirectory() + { + string saveDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "AppData", "LocalLow", "LanPiaoPiao", "PlantsVsZombiesRH"); + Process.Start("explorer.exe", saveDirectory); + } - public static void OpenTrello() - { - string website = "https://trello.com/b/DcdT1kUp"; - Process.Start(new ProcessStartInfo(website) { UseShellExecute = true }); - } + public static void OpenTrello() + { + string website = "https://trello.com/b/DcdT1kUp"; + Process.Start(new ProcessStartInfo(website) { UseShellExecute = true }); + } - public static string RemoveColorTags(string text) - { - return Regex.Replace(text, @"]+>", ""); - } + public static string RemoveColorTags(string text) + { + return Regex.Replace(text, @"]+>", ""); + } - internal static void ChangeLanguage(string language) - { - OldLanguage = Language; + #if MULTI_LANGUAGE + internal static void ChangeLanguage(string language) + { + OldLanguage = Language; - if (Enum.TryParse(language, out Utils.LanguageEnum lang)) - { - Utils.Language = lang; - } - else - { - // Handle invalid language string - Log.LogError($"Invalid language string: {language}"); - } - - WarningStore.isWarningMessageLoaded = false; - FontStore.Reload(); - StringStore.Reload(); - TextureStore.Reload(); - FileLoader.SaveLanguage(); - } + if (Enum.TryParse(language, out Utils.LanguageEnum lang)) + { + Utils.Language = lang; + } + else + { + // Handle invalid language string + Log.LogError($"Invalid language string: {language}"); + } - public static Utils.LanguageEnum OldLanguage; - public static Utils.LanguageEnum Language; - public enum LanguageEnum - { - // first column - English, - French, - Italian, - German, - Spanish, - Portuguese, + WarningStore.isWarningMessageLoaded = false; + FontStore.Reload(); + StringStore.Reload(); + TextureStore.Reload(); + FileLoader.SaveLanguage(); + } + #endif - // second column - Javanese, //Filipino, - Vietnamese, - Indonesian, - Russian, - Japanese, - Korean, + #if MULTI_LANGUAGE + public static Utils.LanguageEnum OldLanguage; + #endif - // third column - // Ukrainian, - // Slovak, + public static Utils.LanguageEnum Language; - LANG_END - } - } + public static string mainCategory = "PvZ_Fusion_Translator"; + + public static bool customTextures = !Core.Instance.configDefaultTextures.Value; + public static bool customAudio = !Core.Instance.configDefaultAudio.Value; + + public enum LanguageEnum + { + // first column + English, + French, + Italian, + German, + Spanish, + Portuguese, + + // second column + Javanese, //Filipino, + Vietnamese, + Indonesian, + Russian, + Japanese, + Korean, + + // third column + // Ukrainian, + // Slovak, + Polish, + Hungarian, + Arabic, + + LANG_END + } + + public enum ToggleEnum + { + Textures, + Audio, + TOGGLE_END + } + } } \ No newline at end of file diff --git a/MelonLoader/AssetStore/AudioStore.cs b/MelonLoader/AssetStore/AudioStore.cs index b87032b..746ccac 100644 --- a/MelonLoader/AssetStore/AudioStore.cs +++ b/MelonLoader/AssetStore/AudioStore.cs @@ -24,15 +24,7 @@ namespace PvZ_Fusion_Translator.AssetStore public static void Init() { - bool loadCustomAudio = MelonPreferences.GetEntryValue("PvZ_Fusion_Translator", "DefaultAudio"); - if (loadCustomAudio) - { - return; - } - else - { - LoadAudios(); - } + LoadAudios(); } public static void LoadAudios() { @@ -158,24 +150,27 @@ namespace PvZ_Fusion_Translator.AssetStore public static void AudioPlayPatch(AudioSource __instance) { - if (__instance.clip == null) - return; - - if (LogSounds) - MelonLogger.Msg($"Playing \"{__instance.clip.name}\" from object \"{__instance.gameObject.name}\""); - - string audioClipName = overrideEnabled ? "REPLACE_ALL" : __instance.clip.name; - if (AudioClips.TryGetValue(audioClipName, out AudioClip replaceClip)) + if(Utils.customAudio) { - __instance.pitch = 1; - __instance.clip = replaceClip; - } + if (__instance.clip == null) + return; - #if AUDIO_TESTING - // Update the displayed audio source - var (name, source) = GetAudioSource(__instance.clip.name); - UpdateAudioSourceText(name, source); - #endif + if (LogSounds) + MelonLogger.Msg($"Playing \"{__instance.clip.name}\" from object \"{__instance.gameObject.name}\""); + + string audioClipName = overrideEnabled ? "REPLACE_ALL" : __instance.clip.name; + if (AudioClips.TryGetValue(audioClipName, out AudioClip replaceClip)) + { + __instance.pitch = 1; + __instance.clip = replaceClip; + } + + #if AUDIO_TESTING + // Update the displayed audio source + var (name, source) = GetAudioSource(__instance.clip.name); + UpdateAudioSourceText(name, source); + #endif + } } } } diff --git a/MelonLoader/AssetStore/TextureStore.cs b/MelonLoader/AssetStore/TextureStore.cs index 1a5504a..9947d33 100644 --- a/MelonLoader/AssetStore/TextureStore.cs +++ b/MelonLoader/AssetStore/TextureStore.cs @@ -16,7 +16,6 @@ namespace PvZ_Fusion_Translator.AssetStore #if MULTI_LANGUAGE RestoreTextures(); #endif - FileLoader.LoadDefaultTextures(); FileLoader.LoadTextures(); } diff --git a/MelonLoader/FileLoader.cs b/MelonLoader/FileLoader.cs index c2d42ae..42eb115 100644 --- a/MelonLoader/FileLoader.cs +++ b/MelonLoader/FileLoader.cs @@ -96,14 +96,10 @@ namespace PvZ_Fusion_Translator try { - if (loadDefaultTextures == true) + if (Utils.customTextures) { - LoadDefaultTextures(); - } - else - { - LoadCustomTextures(); - } + LoadCustomTextures(); + } } catch (Exception e) { @@ -167,7 +163,7 @@ namespace PvZ_Fusion_Translator internal static void LoadDefaultTextures() { - if(Utils.customTextures) + if(Utils.customTextures == false) { string textureDefaultDir = Path.Combine(Core.Instance.modsDirectory, AssetType.Dumps.ToString(), "Default Textures [Do Not Remove]"); if (!Directory.Exists(textureDefaultDir)) diff --git a/MelonLoader/Patches/GameObjects/ButtonObjects/OptLanguageBtn_Patch.cs b/MelonLoader/Patches/GameObjects/ButtonObjects/OptLanguageBtn_Patch.cs index 6eb5f9b..d117b45 100644 --- a/MelonLoader/Patches/GameObjects/ButtonObjects/OptLanguageBtn_Patch.cs +++ b/MelonLoader/Patches/GameObjects/ButtonObjects/OptLanguageBtn_Patch.cs @@ -240,9 +240,11 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.ButtonObjects if(type == "Textures") { - Utils.customTextures = (Utils.customTextures) ? false : true; + Utils.customTextures = !Utils.customTextures; MelonPreferences.SetEntryValue("PvZ_Fusion_Translator", "DefaultTextures", !Utils.customTextures); - } + TextureStore.Reload(); + object replaceTextureRoutine = MelonCoroutines.Start(TextureStore.ReplaceTexturesCoroutine()); + } if (type == "Audio") { @@ -304,11 +306,11 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.ButtonObjects ToggleCustomAssets(toggleType); if (toggleType == "Textures") { - FlashMessage(toggleData.Button, "Toggled custom textures! (Restart game to apply changes.)", 0.1f); + FlashMessage(toggleData.Button, "Toggled custom textures!", 0.1f); } else if (toggleType == "Audio") { - FlashMessage(toggleData.Button, "Toggled custom audio! (Restart game to apply changes.)", 0.1f); + FlashMessage(toggleData.Button, "Toggled custom audio!", 0.1f); } } }