Added support for translating modded plants/zombies. Name the json files ModdedZombiesTranslate or ModdedPlantsTranslate

This commit is contained in:
Nero Dynamo 2025-05-04 14:23:35 +08:00
parent 675a615819
commit e8a30374a1
2 changed files with 103 additions and 30 deletions

View file

@ -2,8 +2,8 @@
using Il2Cpp; using Il2Cpp;
using Il2CppTMPro; using Il2CppTMPro;
using PvZ_Fusion_Translator.AssetStore; using PvZ_Fusion_Translator.AssetStore;
using static PvZ_Fusion_Translator.FileLoader;
using UnityEngine; using UnityEngine;
using static PvZ_Fusion_Translator.FileLoader;
namespace PvZ_Fusion_Translator.Patches.Managers namespace PvZ_Fusion_Translator.Patches.Managers
{ {
@ -11,8 +11,8 @@ namespace PvZ_Fusion_Translator.Patches.Managers
public static partial class AlmanacMgrZombie_Patch public static partial class AlmanacMgrZombie_Patch
{ {
[HarmonyPatch(nameof(AlmanacMgrZombie.InitNameAndInfoFromJson))] [HarmonyPatch(nameof(AlmanacMgrZombie.InitNameAndInfoFromJson))]
[HarmonyPrefix] [HarmonyPostfix]
private static bool InitNameAndInfoFromJson(AlmanacMgrZombie __instance) private static void InitNameAndInfoFromJson(AlmanacMgrZombie __instance)
{ {
#if MULTI_LANGUAGE #if MULTI_LANGUAGE
string currentLanguage = Utils.Language.ToString(); string currentLanguage = Utils.Language.ToString();
@ -22,11 +22,12 @@ namespace PvZ_Fusion_Translator.Patches.Managers
string currentLanguage = "English"; string currentLanguage = "English";
#endif #endif
string path = Path.Combine(almanacDir, "ZombieStringsTranslate.json"); string path = Path.Combine(almanacDir, "ZombieStringsTranslate.json");
string moddedPath = Path.Combine(almanacDir, "ModdedZombiesTranslate.json");
if (!File.Exists(path)) if (!File.Exists(path))
{ {
Log.LogError($"ZombieStringsTranslate.json file not found at path: {path}"); Log.LogError($"ZombieStringsTranslate.json file not found at path: {path}");
return true; return;
} }
#if OBFUSCATE #if OBFUSCATE
@ -77,12 +78,38 @@ namespace PvZ_Fusion_Translator.Patches.Managers
component.font = fontAsset; component.font = fontAsset;
component2.font = fontAsset; component2.font = fontAsset;
component3.font = fontAsset; component3.font = fontAsset;
return false;
} }
} }
return true; if (File.Exists(moddedPath))
{
string moddedJson;
moddedJson = File.ReadAllText(moddedPath);
AlmanacMgrZombie.ZombieAlmanacData moddedZombieData = JsonUtility.FromJson<AlmanacMgrZombie.ZombieAlmanacData>(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))] [HarmonyPatch(nameof(AlmanacMgrZombie.OnMouseDown))]

View file

@ -1,8 +1,6 @@
using HarmonyLib; using HarmonyLib;
using Il2Cpp; using Il2Cpp;
using Il2CppTMPro; using Il2CppTMPro;
using MelonLoader;
using MelonLoader.TinyJSON;
using PvZ_Fusion_Translator.AssetStore; using PvZ_Fusion_Translator.AssetStore;
using UnityEngine; using UnityEngine;
using static PvZ_Fusion_Translator.FileLoader; using static PvZ_Fusion_Translator.FileLoader;
@ -24,6 +22,7 @@ namespace PvZ_Fusion_Translator.Patches.Managers
string currentLanguage = "English"; string currentLanguage = "English";
#endif #endif
string path = Path.Combine(almanacDir, "LawnStringsTranslate.json"); string path = Path.Combine(almanacDir, "LawnStringsTranslate.json");
string moddedPath = Path.Combine(almanacDir, "ModdedPlantsTranslate.json");
if (!File.Exists(path)) if (!File.Exists(path))
{ {
@ -61,23 +60,24 @@ namespace PvZ_Fusion_Translator.Patches.Managers
TextMeshPro component3 = __instance.plantName.transform.GetChild(0).GetComponent<TextMeshPro>(); TextMeshPro component3 = __instance.plantName.transform.GetChild(0).GetComponent<TextMeshPro>();
TextMeshPro component4 = __instance.cost.GetComponent<TextMeshPro>(); TextMeshPro component4 = __instance.cost.GetComponent<TextMeshPro>();
Il2Cpp.AlmanacPlantBank.PlantData plantData = JsonUtility.FromJson<Il2Cpp.AlmanacPlantBank.PlantData>(json); AlmanacPlantBank.PlantData plantData = JsonUtility.FromJson<AlmanacPlantBank.PlantData>(json);
foreach (Il2Cpp.AlmanacPlantBank.PlantInfo plantInfo in plantData.plants) foreach (AlmanacPlantBank.PlantInfo plantInfo in plantData.plants)
{ {
if (plantInfo.seedType == __instance.theSeedType) if (plantInfo.seedType == __instance.theSeedType)
{ {
component.autoSizeTextContainer = false; component.autoSizeTextContainer = false;
component.text = plantInfo.info + "\n\n" + plantInfo.introduce; component.text = plantInfo.info + "\n\n" + plantInfo.introduce;
component.overflowMode = TextOverflowModes.Page; component.overflowMode = TextOverflowModes.Page;
//fix dimensions for cost text // fix dimensions for cost text
component.rectTransform.offsetMax = new Vector2(component.rectTransform.offsetMax.x, 27.3839f); component.rectTransform.offsetMax = new Vector2(component.rectTransform.offsetMax.x, 27.3839f);
component.rectTransform.offsetMin = new Vector2(component.rectTransform.offsetMin.x, -29.3079f); 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); 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; component2.autoSizeTextContainer = true;
component3.text = Utils.RemoveColorTags(plantInfo.name); component3.text = Utils.RemoveColorTags(plantInfo.name);
@ -85,7 +85,7 @@ namespace PvZ_Fusion_Translator.Patches.Managers
component4.text = plantInfo.cost; component4.text = plantInfo.cost;
if (hasAlmanacFont) if (hasAlmanacFont)
{ {
component.font = almanacFontAsset; component.font = almanacFontAsset;
component4.font = almanacFontAsset; component4.font = almanacFontAsset;
@ -100,12 +100,58 @@ namespace PvZ_Fusion_Translator.Patches.Managers
} }
} }
if (File.Exists(moddedPath))
{
string moddedJson;
moddedJson = File.ReadAllText(moddedPath);
AlmanacPlantBank.PlantData moddedPlantData = JsonUtility.FromJson<AlmanacPlantBank.PlantData>(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; return;
} }
[HarmonyPatch(nameof(AlmanacPlantBank.OnMouseDown))] [HarmonyPatch(nameof(AlmanacPlantBank.OnMouseDown))]
[HarmonyPrefix] [HarmonyPrefix]
private static bool OnMouseDown(Il2Cpp.AlmanacPlantBank __instance) private static bool OnMouseDown(AlmanacPlantBank __instance)
{ {
TextMeshPro component = __instance.introduce.GetComponent<TextMeshPro>(); TextMeshPro component = __instance.introduce.GetComponent<TextMeshPro>();
if (component != null) if (component != null)
@ -116,15 +162,15 @@ namespace PvZ_Fusion_Translator.Patches.Managers
return true; return true;
} }
[HarmonyPatch(nameof(AlmanacPlantBank.PVPInit))] [HarmonyPatch(nameof(AlmanacPlantBank.PVPInit))]
[HarmonyPostfix] [HarmonyPostfix]
private static void PVPInit(Il2Cpp.AlmanacPlantBank __instance) private static void PVPInit(Il2Cpp.AlmanacPlantBank __instance)
{ {
TextMeshPro component = __instance.introduce.GetComponent<TextMeshPro>(); TextMeshPro component = __instance.introduce.GetComponent<TextMeshPro>();
if (component != null) if (component != null)
{ {
component.autoSizeTextContainer = false; component.autoSizeTextContainer = false;
} }
} }
} }
} }