From 55e4d5b9376acf2641342ff892b93902c212f456 Mon Sep 17 00:00:00 2001 From: Dakosha <149136937+Dak0sha@users.noreply.github.com> Date: Thu, 26 Jun 2025 00:17:56 +0300 Subject: [PATCH] MachineNut HP text patch --- .../GameObjects/MinorObjects/Plant_Patch.cs | 63 +++++++++++++------ 1 file changed, 43 insertions(+), 20 deletions(-) diff --git a/MelonLoader/Patches/GameObjects/MinorObjects/Plant_Patch.cs b/MelonLoader/Patches/GameObjects/MinorObjects/Plant_Patch.cs index 3aad6c8..0e0cbb2 100644 --- a/MelonLoader/Patches/GameObjects/MinorObjects/Plant_Patch.cs +++ b/MelonLoader/Patches/GameObjects/MinorObjects/Plant_Patch.cs @@ -1,24 +1,47 @@ -#if FIX -using HarmonyLib; +using HarmonyLib; using Il2Cpp; -using PvZ_Fusion_Translator.AssetStore; -using UnityEngine; +using System.Text.RegularExpressions; namespace PvZ_Fusion_Translator.Patches.GameObjects.MinorObjects { - [HarmonyPatch(typeof(Plant))] - public static class Plant_Patch - { - [HarmonyPostfix] - [HarmonyPatch(nameof(Plant.UpdateHealthText))] - private static void UpdateHealthText(Plant __instance) - { - Transform healthTransform = __instance.transform.Find("health"); - if (healthTransform != null) - { - StringStore.TranslateTextTransform(__instance.transform); - } - } - } -} -#endif \ No newline at end of file + [HarmonyPatch(typeof(Plant))] + public static class Plant_Patch + { + [HarmonyPostfix] + [HarmonyPatch(nameof(Plant.Awake))] + private static void Awake(Plant __instance) + { + if (!(__instance.thePlantType == PlantType.SuperMachineNut || __instance.thePlantType == PlantType.MachineNut)) + return; + __instance.healthSlider.healthText.text = __instance.healthSlider.healthTextShadow.text = ChangeHPText(__instance.healthSlider.healthTextShadow.text); + } + + [HarmonyPostfix] + [HarmonyPatch(nameof(Plant.UpdateText))] + private static void Update(Plant __instance) + { + if (!(__instance.thePlantType == PlantType.SuperMachineNut || __instance.thePlantType == PlantType.MachineNut)) + return; + if (__instance.thePlantHealth < __instance.thePlantMaxHealth) + return; + __instance.healthSlider.healthText.text = __instance.healthSlider.healthTextShadow.text = ChangeHPText(__instance.healthSlider.healthTextShadow.text); + } + + static string ChangeHPText(string originalText) + { + var regex = new Regex(@"(\d+)\d\d\d/(\d)000"); + if (regex.IsMatch(originalText)) + { + var match = regex.Match(originalText); + int groupCount = match.Groups.Count; + + List dynamicParts = new List(); + + for (int i = 1; i < groupCount; i++) + dynamicParts.Add(match.Groups[i].Value); + return originalText = string.Format("{0}k/{1}k", [.. dynamicParts]); + } + return originalText; + } + } +} \ No newline at end of file