using HarmonyLib;
using Il2Cpp;
using Il2CppTMPro;
using PvZ_Fusion_Translator.AssetStore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace PvZ_Fusion_Translator.Patches.GameObjects
{
[HarmonyPatch(typeof(PlantDataMenu))]
public static class PlantDataMenu_Patch
{
[HarmonyPatch(nameof(PlantDataMenu.Start))]
[HarmonyPostfix]
public static void Post_Start(PlantDataMenu __instance)
{
TranslatePlantDataMenu(__instance);
}
[HarmonyPatch(nameof(PlantDataMenu.Update))]
[HarmonyPostfix]
public static void Post_Update(PlantDataMenu __instance)
{
TranslatePlantDataMenu(__instance);
}
public static void TranslatePlantDataMenu(PlantDataMenu __instance)
{
PlantType thePlantType = __instance.plant.thePlantType;
string plantName = Utils.GetPlantNameFromAlmanac(thePlantType);
TMP_FontAsset fontAsset = FontStore.LoadTMPFont(Utils.Language.ToString());
foreach(TextMeshProUGUI text in __instance.nameText)
{
text.text = plantName;
text.font = fontAsset;
}
foreach(TextMeshProUGUI text in __instance.infoText)
{
string toTranslate = text.text;
string startColorTag = "";
string colorPattern = @"(]+>)(.*?)()";
Match match = Regex.Match(toTranslate, colorPattern);
if(match.Success)
{
startColorTag = match.Groups[1].Value;
toTranslate = match.Groups[2].Value;
}
string[] parts = toTranslate.Split("\n");
List translatedParts = new List();
foreach(string part in parts)
{
translatedParts.Add(StringStore.TranslateText(part));
}
string joinedTranslation = String.Join("\n", translatedParts);
if(startColorTag != "")
{
text.text = startColorTag + joinedTranslation + "";
}
else
{
text.text = joinedTranslation;
}
text.font = fontAsset;
}
}
}
}