diff --git a/BepInEx/Patches/Managers/AlmanacPlantBank_Patch.cs b/BepInEx/Patches/Managers/AlmanacPlantBank_Patch.cs index c488424..74af07a 100644 --- a/BepInEx/Patches/Managers/AlmanacPlantBank_Patch.cs +++ b/BepInEx/Patches/Managers/AlmanacPlantBank_Patch.cs @@ -60,22 +60,22 @@ namespace PvZ_Fusion_Translator__BepInEx_.Patches.Managers TextMeshPro component3 = __instance.plantName.transform.GetChild(0).GetComponent(); TextMeshPro component4 = __instance.cost.GetComponent(); TextMeshPro component5 = __instance.introduce.GetComponentInChildren(); -#if DEBUG + #if DEBUG Log.LogWarning($"Introduce text: {component?.text}"); Log.LogWarning($"Plant name: {component2?.text}"); -#endif - component.text = Utils.RemoveColorTags(component.text); + #endif + component.text = component.text ?? string.Empty; AlmanacPlantBank.PlantData plantData = JsonUtility.FromJson(json); foreach (AlmanacPlantBank.PlantInfo plantInfo in plantData.plants) { - if (plantInfo.seedType == __instance.theSeedType) + if (plantInfo.seedType == __instance.theSeedType && !string.IsNullOrEmpty(plantInfo.name)) { -#if DEBUG + #if DEBUG Log.LogInfo($"Processing plant type: {plantInfo.seedType}"); Log.LogInfo($"Plant info: {plantInfo.info}, Name: {plantInfo.name}, Cost: {plantInfo.cost}, Introduce: {plantInfo.introduce}"); -#endif + #endif component.autoSizeTextContainer = false; component.text = plantInfo.info + "\n\n" + plantInfo.introduce; component.overflowMode = TextOverflowModes.Page; @@ -88,7 +88,10 @@ namespace PvZ_Fusion_Translator__BepInEx_.Patches.Managers component2.text = plantInfo.name; component2.autoSizeTextContainer = true; - component3.text = Utils.RemoveColorTags(plantInfo.name); + #if DEBUG + Log.LogInfo($"Processing plant name: '{plantInfo.name}' (null: {plantInfo.name == null})"); + #endif + component3.text = Utils.RemoveColorTags(plantInfo.name ?? string.Empty); component3.autoSizeTextContainer = true; component4.text = plantInfo.cost; @@ -116,7 +119,7 @@ namespace PvZ_Fusion_Translator__BepInEx_.Patches.Managers foreach (AlmanacPlantBank.PlantInfo plantInfo in moddedPlantData.plants) { - if (plantInfo.seedType == __instance.theSeedType) + if (plantInfo.seedType == __instance.theSeedType && !string.IsNullOrEmpty(plantInfo.name)) { component.autoSizeTextContainer = false; component.text = plantInfo.info + "\n\n" + plantInfo.introduce; @@ -130,7 +133,10 @@ namespace PvZ_Fusion_Translator__BepInEx_.Patches.Managers component2.text = plantInfo.name; component2.autoSizeTextContainer = true; - component3.text = Utils.RemoveColorTags(plantInfo.name); + #if DEBUG + Log.LogInfo($"Processing plant name: '{plantInfo.name}' (null: {plantInfo.name == null})"); + #endif + component3.text = Utils.RemoveColorTags(plantInfo.name ?? string.Empty); component3.autoSizeTextContainer = true; component4.text = plantInfo.cost; @@ -189,10 +195,10 @@ namespace PvZ_Fusion_Translator__BepInEx_.Patches.Managers if (Input.GetKey(KeyCode.LeftControl)) { FileLoader.DumpUntranslatedStrings(__instance.introduce.GetComponent().text); - Log.LogInfo($"Info: {__instance.introduce.GetComponent().text}"); + //Log.LogInfo($"Info: {__instance.introduce.GetComponent().text}"); FileLoader.DumpUntranslatedStrings(__instance.plantName.GetComponent().text); - Log.LogInfo($"PlantName Name: {__instance.plantName.GetComponent().text}"); - Log.LogInfo($"PlantShadow Name: {__instance.plantName.transform.GetChild(0).GetComponent().text}"); + //Log.LogInfo($"PlantName Name: {__instance.plantName.GetComponent().text}"); + //Log.LogInfo($"PlantShadow Name: {__instance.plantName.transform.GetChild(0).GetComponent().text}"); } string currentLanguage = Utils.Language.ToString(); __instance.introduce.text = StringStore.TranslateText(__instance.introduce.text); diff --git a/BepInEx/Utils.cs b/BepInEx/Utils.cs index c46d525..c564633 100644 --- a/BepInEx/Utils.cs +++ b/BepInEx/Utils.cs @@ -1,15 +1,16 @@ -using BepInEx; -using BepInEx.Configuration; -using PvZ_Fusion_Translator__BepInEx_.AssetStore; -using System; +using System; using System.Diagnostics; using System.IO; using System.Text.RegularExpressions; +using BepInEx; +using BepInEx.Configuration; +using PvZ_Fusion_Translator__BepInEx_.AssetStore; using TMPro; using Unity.VisualScripting.FullSerializer; using UnityEngine; using UnityEngine.Experimental.Rendering; using UnityEngine.UI; +using static PvZ_Fusion_Translator__BepInEx_.FileLoader; namespace PvZ_Fusion_Translator__BepInEx_ { @@ -79,7 +80,14 @@ namespace PvZ_Fusion_Translator__BepInEx_ public static string RemoveColorTags(string text) { - return Regex.Replace(text, @"]+>", ""); + if (string.IsNullOrEmpty(text)) + return text ?? string.Empty; + + // Remove opening color tags like + string withoutOpenTags = Regex.Replace(text, @"]+>", string.Empty, RegexOptions.IgnoreCase); + // Remove closing color tags like + string withoutCloseTags = Regex.Replace(withoutOpenTags, @"", string.Empty, RegexOptions.IgnoreCase); + return withoutCloseTags; } public static GameObject ConvertToTextMeshProUGUI(GameObject originalText, Transform parent, string name) @@ -112,13 +120,58 @@ namespace PvZ_Fusion_Translator__BepInEx_ newGoBackText.color = color; } + //public static string GetPlantNameFromAlmanac(PlantType thePlantType) + //{ + // string json; + // string thePlantName = ""; + + // string currentLanguage = Utils.Language.ToString(); + // string almanacDir = FileLoader.GetAssetDir(FileLoader.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)"); + // thePlantName = ""; + // } + // else + // { + // try + // { + // json = File.ReadAllText(path); + // AlmanacPlantBank.PlantData plantData = JsonUtility.FromJson(json); + // if (plantData != null && plantData.plants != null) + // { + // foreach (AlmanacPlantBank.PlantInfo plantInfo in plantData.plants) + // { + // if (plantInfo.seedType == (int)thePlantType) + // { + // // Remove any ID numbers from the name (e.g., "Peashooter (0)" -> "Peashooter", "Peashooter26" -> "Peashooter") + // // thePlantName = System.Text.RegularExpressions.Regex.Replace(plantInfo.name, @"\s*\(\d+\)$|\d+$", ""); + // break; + // } + // } + // } + // } + // catch (System.Exception ex) + // { + // Log.LogError($"Error parsing JSON in GetPlantNameFromAlmanac: {ex.Message}"); + // thePlantName = ""; + // } + // } + + // return thePlantName; + //} + public static string GetPlantNameFromAlmanac(PlantType thePlantType) { string json; string thePlantName = ""; string currentLanguage = Utils.Language.ToString(); - string almanacDir = FileLoader.GetAssetDir(FileLoader.AssetType.Almanac, Utils.Language); + string almanacDir = GetAssetDir(AssetType.Almanac, Utils.Language); string path = Path.Combine(almanacDir, "LawnStringsTranslate.json"); string moddedPath = Path.Combine(almanacDir, "ModdedPlantsTranslate.json"); @@ -130,28 +183,16 @@ namespace PvZ_Fusion_Translator__BepInEx_ } else { - try + json = File.ReadAllText(path); + AlmanacPlantBank.PlantData plantData = JsonUtility.FromJson(json); + + foreach (AlmanacPlantBank.PlantInfo plantInfo in plantData.plants) { - json = File.ReadAllText(path); - AlmanacPlantBank.PlantData plantData = JsonUtility.FromJson(json); - if (plantData != null && plantData.plants != null) + if (plantInfo.seedType == (int)thePlantType) { - foreach (AlmanacPlantBank.PlantInfo plantInfo in plantData.plants) - { - if (plantInfo.seedType == (int)thePlantType) - { - // Remove any ID numbers from the name (e.g., "Peashooter (0)" -> "Peashooter", "Peashooter26" -> "Peashooter") - thePlantName = System.Text.RegularExpressions.Regex.Replace(plantInfo.name, @"\s*\(\d+\)$|\d+$", ""); - break; - } - } + thePlantName = plantInfo.name; } } - catch (System.Exception ex) - { - Log.LogError($"Error parsing JSON in GetPlantNameFromAlmanac: {ex.Message}"); - thePlantName = ""; - } } return thePlantName; diff --git a/MelonLoader/Patches/Managers/AlmanacPlantBank_Patch.cs b/MelonLoader/Patches/Managers/AlmanacPlantBank_Patch.cs index 25d4b8d..3b17692 100644 --- a/MelonLoader/Patches/Managers/AlmanacPlantBank_Patch.cs +++ b/MelonLoader/Patches/Managers/AlmanacPlantBank_Patch.cs @@ -62,9 +62,9 @@ namespace PvZ_Fusion_Translator.Patches.Managers AlmanacPlantBank.PlantData plantData = JsonUtility.FromJson(json); - foreach (AlmanacPlantBank.PlantInfo plantInfo in plantData.plants) + foreach (AlmanacPlantBank.PlantInfo plantInfo in plantData.plants) { - if (plantInfo.seedType == __instance.theSeedType) + if (plantInfo.seedType == __instance.theSeedType && !string.IsNullOrEmpty(plantInfo.name)) { component.autoSizeTextContainer = false; component.text = plantInfo.info + "\n\n" + plantInfo.introduce; @@ -80,7 +80,7 @@ namespace PvZ_Fusion_Translator.Patches.Managers component2.text = plantInfo.name; component2.autoSizeTextContainer = true; - component3.text = Utils.RemoveColorTags(plantInfo.name); + component3.text = Utils.RemoveColorTags(plantInfo.name ?? string.Empty); component3.autoSizeTextContainer = true; component4.text = plantInfo.cost; @@ -107,9 +107,9 @@ namespace PvZ_Fusion_Translator.Patches.Managers AlmanacPlantBank.PlantData moddedPlantData = JsonUtility.FromJson(moddedJson); - foreach (AlmanacPlantBank.PlantInfo plantInfo in moddedPlantData.plants) + foreach (AlmanacPlantBank.PlantInfo plantInfo in moddedPlantData.plants) { - if (plantInfo.seedType == __instance.theSeedType) + if (plantInfo.seedType == __instance.theSeedType && !string.IsNullOrEmpty(plantInfo.name)) { component.autoSizeTextContainer = false; component.text = plantInfo.info + "\n\n" + plantInfo.introduce; @@ -125,7 +125,7 @@ namespace PvZ_Fusion_Translator.Patches.Managers component2.text = plantInfo.name; component2.autoSizeTextContainer = true; - component3.text = Utils.RemoveColorTags(plantInfo.name); + component3.text = Utils.RemoveColorTags(plantInfo.name ?? string.Empty); component3.autoSizeTextContainer = true; component4.text = plantInfo.cost; diff --git a/MelonLoader/Utils.cs b/MelonLoader/Utils.cs index 6422342..38b9bdf 100644 --- a/MelonLoader/Utils.cs +++ b/MelonLoader/Utils.cs @@ -76,10 +76,17 @@ namespace PvZ_Fusion_Translator Process.Start(new ProcessStartInfo(website) { UseShellExecute = true }); } - public static string RemoveColorTags(string text) - { - return Regex.Replace(text, @"]+>", ""); - } + public static string RemoveColorTags(string text) + { + if (string.IsNullOrEmpty(text)) + return text ?? string.Empty; + + // Remove opening color tags like + string withoutOpenTags = Regex.Replace(text, @"]+>", string.Empty, RegexOptions.IgnoreCase); + // Remove closing color tags like + string withoutCloseTags = Regex.Replace(withoutOpenTags, @"", string.Empty, RegexOptions.IgnoreCase); + return withoutCloseTags; + } public static GameObject ConvertToTextMeshProUGUI(GameObject originalText, Transform parent, string name) {