PVZFusionTranslation/MelonLoader/Patches/GameObjects/AbyssMenu_Patch.cs
2025-08-09 13:41:39 -04:00

68 lines
2.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using HarmonyLib;
using Il2Cpp;
using Il2CppTMPro;
using MelonLoader;
using PvZ_Fusion_Translator.AssetStore;
using Unity.VisualScripting;
using UnityEngine;
using System.Text.RegularExpressions;
using UnityEngine.UI;
using MelonLoader.Utils;
namespace PvZ_Fusion_Translator.Patches.GameObjects
{
[HarmonyPatch(typeof(AbyssMenu))]
public static class AbyssMenu_Patch
{
[HarmonyPatch(nameof(AbyssMenu.Awake))]
[HarmonyPostfix]
private static void Awake(AbyssMenu __instance)
{
TMP_FontAsset fontAsset = FontStore.LoadTMPFont(Utils.Language.ToString());
foreach (TextMeshProUGUI txt in __instance.levelInfo)
{
txt.text = StringStore.TranslateText(txt.text, false);
txt.font = fontAsset;
}
foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren<TextMeshProUGUI>())
{
text.text = StringStore.TranslateText(text.text);
text.font = fontAsset;
}
}
[HarmonyPatch(nameof(AbyssMenu.UpdateLevelInfo))]
[HarmonyPostfix]
private static void UpdateLevelInfo(AbyssMenu __instance)
{
TMP_FontAsset fontAsset = FontStore.LoadTMPFont(Utils.Language.ToString());
foreach (TextMeshProUGUI txt in __instance.levelInfo)
{
string regexStr = "第(\\d+)关\n场景:([^\\s]+)\n总波数:(\\d+)波\n特殊环境:([^\\s:]+)\n减伤强度:(\\d+)";
Regex regex = new Regex(regexStr);
if(regex.IsMatch(txt.text))
{
Match match = regex.Match(txt.text);
int groupCount = match.Groups.Count;
List<string> dynamicParts = [];
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);
}
txt.text = string.Format(StringStore.translationStringRegex[regexStr], [.. dynamicParts]);
txt.font = fontAsset;
}
}
}
}
}