Fix a minor bug with Vietnamese "Peashooter"

This commit is contained in:
TuanAnh2901 2025-09-29 13:53:14 +07:00
parent 68b428f9f4
commit d09efe0008
4 changed files with 100 additions and 46 deletions

View file

@ -60,22 +60,22 @@ namespace PvZ_Fusion_Translator__BepInEx_.Patches.Managers
TextMeshPro component3 = __instance.plantName.transform.GetChild(0).GetComponent<TextMeshPro>();
TextMeshPro component4 = __instance.cost.GetComponent<TextMeshPro>();
TextMeshPro component5 = __instance.introduce.GetComponentInChildren<TextMeshPro>();
#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<AlmanacPlantBank.PlantData>(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<TextMeshPro>().text);
Log.LogInfo($"Info: {__instance.introduce.GetComponent<TextMeshPro>().text}");
//Log.LogInfo($"Info: {__instance.introduce.GetComponent<TextMeshPro>().text}");
FileLoader.DumpUntranslatedStrings(__instance.plantName.GetComponent<TextMeshPro>().text);
Log.LogInfo($"PlantName Name: {__instance.plantName.GetComponent<TextMeshPro>().text}");
Log.LogInfo($"PlantShadow Name: {__instance.plantName.transform.GetChild(0).GetComponent<TextMeshPro>().text}");
//Log.LogInfo($"PlantName Name: {__instance.plantName.GetComponent<TextMeshPro>().text}");
//Log.LogInfo($"PlantShadow Name: {__instance.plantName.transform.GetChild(0).GetComponent<TextMeshPro>().text}");
}
string currentLanguage = Utils.Language.ToString();
__instance.introduce.text = StringStore.TranslateText(__instance.introduce.text);

View file

@ -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, @"<color=[^>]+>", "");
if (string.IsNullOrEmpty(text))
return text ?? string.Empty;
// Remove opening color tags like <color=#FF0000>
string withoutOpenTags = Regex.Replace(text, @"<color=[^>]+>", string.Empty, RegexOptions.IgnoreCase);
// Remove closing color tags like </color>
string withoutCloseTags = Regex.Replace(withoutOpenTags, @"</color>", 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<AlmanacPlantBank.PlantData>(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");
@ -129,30 +182,18 @@ namespace PvZ_Fusion_Translator__BepInEx_
thePlantName = "";
}
else
{
try
{
json = File.ReadAllText(path);
AlmanacPlantBank.PlantData plantData = JsonUtility.FromJson<AlmanacPlantBank.PlantData>(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;
thePlantName = plantInfo.name;
}
}
}
}
catch (System.Exception ex)
{
Log.LogError($"Error parsing JSON in GetPlantNameFromAlmanac: {ex.Message}");
thePlantName = "";
}
}
return thePlantName;
}

View file

@ -64,7 +64,7 @@ namespace PvZ_Fusion_Translator.Patches.Managers
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;
@ -109,7 +109,7 @@ namespace PvZ_Fusion_Translator.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;
@ -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;

View file

@ -78,7 +78,14 @@ namespace PvZ_Fusion_Translator
public static string RemoveColorTags(string text)
{
return Regex.Replace(text, @"<color=[^>]+>", "");
if (string.IsNullOrEmpty(text))
return text ?? string.Empty;
// Remove opening color tags like <color=#FF0000>
string withoutOpenTags = Regex.Replace(text, @"<color=[^>]+>", string.Empty, RegexOptions.IgnoreCase);
// Remove closing color tags like </color>
string withoutCloseTags = Regex.Replace(withoutOpenTags, @"</color>", string.Empty, RegexOptions.IgnoreCase);
return withoutCloseTags;
}
public static GameObject ConvertToTextMeshProUGUI(GameObject originalText, Transform parent, string name)