Zombie HP text rework

This commit is contained in:
Dakosha 2025-06-15 19:39:22 +03:00
parent ea94331c83
commit 4c15c96d0d
4 changed files with 75 additions and 8 deletions

View file

@ -1,6 +1,7 @@
using Il2Cpp; using Il2Cpp;
using MelonLoader; using MelonLoader;
using PvZ_Fusion_Translator.AssetStore; using PvZ_Fusion_Translator.AssetStore;
using PvZ_Fusion_Translator.Patches.GameObjects.MinorObjects;
using PvZ_Fusion_Translator.Patches.OtherManagers; using PvZ_Fusion_Translator.Patches.OtherManagers;
using System.Text.Encodings.Web; using System.Text.Encodings.Web;
using System.Text.Json; using System.Text.Json;
@ -76,6 +77,7 @@ namespace PvZ_Fusion_Translator
{ {
StringStore.translationStringRegex[key] = value; StringStore.translationStringRegex[key] = value;
} }
Zombie_Patch.LoadHPStrings();
} }
} }
SaveStrings(); SaveStrings();

View file

@ -1,6 +1,5 @@
using HarmonyLib; using HarmonyLib;
using Il2Cpp; using Il2Cpp;
using PvZ_Fusion_Translator.AssetStore;
namespace PvZ_Fusion_Translator.Patches.GameObjects.MinorObjects namespace PvZ_Fusion_Translator.Patches.GameObjects.MinorObjects
{ {
@ -10,8 +9,8 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.MinorObjects
[HarmonyPostfix] [HarmonyPostfix]
public static void UpdateHealthText(TrainingDummy __instance) public static void UpdateHealthText(TrainingDummy __instance)
{ {
StringStore.TranslateText(__instance.healthText); Zombie_Patch.TranslateHpUI(__instance.healthText);
StringStore.TranslateText(__instance.healthTextShadow); Zombie_Patch.TranslateHpUI(__instance.healthTextShadow);
} }
} }
} }

View file

@ -1,19 +1,41 @@
using HarmonyLib; using HarmonyLib;
using Il2Cpp; using Il2Cpp;
using PvZ_Fusion_Translator.AssetStore; using PvZ_Fusion_Translator.AssetStore;
using System.Text.RegularExpressions;
using UnityEngine; using UnityEngine;
using Il2CppTMPro;
namespace PvZ_Fusion_Translator.Patches.GameObjects.MinorObjects namespace PvZ_Fusion_Translator.Patches.GameObjects.MinorObjects
{ {
[HarmonyPatch(typeof(Zombie))] [HarmonyPatch(typeof(Zombie))]
public static class Zombie_Patch public static class Zombie_Patch
{ {
private static Dictionary<string, string> HPStrings = new Dictionary<string, string>()
{
{ "最后一击:(\\d+)\nDPS:(.+)$", "Last damage: {0}\nDPS: {1}" },
{ "^HP:(\\d+)/(\\d+)\n\\D+(\\d+)/(\\d+)\n\\D+(\\d+)/(\\d+)$", "HP: {0}/{1}\nType 1 armor: {2}/{3}\nType 2 armor: {4}/{5}" },
{ "^HP:(\\d+)/(\\d+)\n\\D+(\\d+)/(\\d+)$", "HP: {0}/{1}\nType 1 armor: {2}/{3}" },
{ "^HP:(\\d+)/(\\d+)$", "HP: {0}/{1}" },
};
public static void LoadHPStrings()
{
foreach (var hp in HPStrings)
if (StringStore.translationStringRegex.ContainsKey(hp.Key))
HPStrings[hp.Key] = StringStore.translationStringRegex[hp.Key];
}
[HarmonyPatch(nameof(Zombie.Start))] [HarmonyPatch(nameof(Zombie.Start))]
[HarmonyPostfix] [HarmonyPostfix]
public static void Start(Zombie __instance) public static void Start(Zombie __instance)
{ {
StringStore.TranslateText(__instance.healthText); TranslateHpUI(__instance.healthText);
StringStore.TranslateText(__instance.healthTextShadow); TranslateHpUI(__instance.healthTextShadow);
if (__instance.theFirstArmorHealth == 0 && __instance.theSecondArmorHealth != 0)
{
__instance.healthText.text = __instance.healthText.text.Replace("Type 1", "Type 2");
__instance.healthTextShadow.text = __instance.healthText.text.Replace("Type 1", "Type 2");
}
if (__instance.healthText.transform.rotation != Quaternion.Euler(0, 0, 0)) if (__instance.healthText.transform.rotation != Quaternion.Euler(0, 0, 0))
__instance.healthText.transform.rotation = Quaternion.Euler(0f, 0f, 0f); __instance.healthText.transform.rotation = Quaternion.Euler(0f, 0f, 0f);
} }
@ -24,10 +46,54 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.MinorObjects
{ {
if (__instance.theZombieType == ZombieType.TrainingDummy || !Board.Instance.showZombieHealth) if (__instance.theZombieType == ZombieType.TrainingDummy || !Board.Instance.showZombieHealth)
return; return;
StringStore.TranslateText(__instance.healthText); TranslateHpUI(__instance.healthText);
StringStore.TranslateText(__instance.healthTextShadow); TranslateHpUI(__instance.healthTextShadow);
if (__instance.theFirstArmorHealth == 0 && __instance.theSecondArmorHealth != 0)
{
__instance.healthText.text = __instance.healthText.text.Replace("Type 1", "Type 2");
__instance.healthTextShadow.text = __instance.healthText.text.Replace("Type 1", "Type 2");
}
if (__instance.healthText.transform.rotation != Quaternion.Euler(0f, 0f, 0f)) if (__instance.healthText.transform.rotation != Quaternion.Euler(0f, 0f, 0f))
__instance.healthText.transform.rotation = Quaternion.Euler(0f, 0f, 0f); __instance.healthText.transform.rotation = Quaternion.Euler(0f, 0f, 0f);
} }
public static void TranslateHpUI(TextMeshPro textTMP)
{
if (textTMP)
{
string origText = textTMP.text;
string translatedText = TranslateHPText(origText);
textTMP.text = translatedText == origText ? origText : translatedText;
textTMP.autoSizeTextContainer = false;
if (translatedText != origText)
textTMP.font = FontStore.LoadTMPFont(Utils.Language.ToString());
}
}
public static string TranslateHPText(string originalText)
{
foreach (var entry in HPStrings)
{
var regex = new Regex(entry.Key);
if (regex.IsMatch(originalText))
{
var match = regex.Match(originalText);
int groupCount = match.Groups.Count;
List<string> dynamicParts = new List<string>();
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);
}
return originalText = string.Format(entry.Value, [.. dynamicParts]);
}
}
return originalText;
}
} }
} }

View file

@ -13,7 +13,7 @@
<LangVersion>latest</LangVersion> <LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Configurations>Debug;DebugEnglish;ReleaseMulti;ReleaseEnglish;DebugTesting;ReleaseOther;DebugObfuscate;ReleaseObfuscate</Configurations> <Configurations>Debug;DebugEnglish;ReleaseMulti;ReleaseEnglish;DebugTesting;ReleaseOther;DebugObfuscate;ReleaseObfuscate</Configurations>
<GamePath>D:\PvZ Fusion\Game Files</GamePath> <GamePath>G:\ML-Fusion 2.6.1\Game Files</GamePath>
<Platforms>AnyCPU;ARM64;x64</Platforms> <Platforms>AnyCPU;ARM64;x64</Platforms>
</PropertyGroup> </PropertyGroup>