diff --git a/MelonLoader/Patches/GameObjects/AbyssMenu_Patch.cs b/MelonLoader/Patches/GameObjects/AbyssMenu_Patch.cs index f3160c2..d088920 100644 --- a/MelonLoader/Patches/GameObjects/AbyssMenu_Patch.cs +++ b/MelonLoader/Patches/GameObjects/AbyssMenu_Patch.cs @@ -53,6 +53,7 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects } txt.text = string.Format(StringStore.translationStringRegex[regexStr], [.. dynamicParts]); + MelonLogger.Msg(txt.text); txt.font = fontAsset; } } diff --git a/MelonLoader/Patches/GameObjects/AbyssSettlementMenu_Patch.cs b/MelonLoader/Patches/GameObjects/AbyssSettlementMenu_Patch.cs new file mode 100644 index 0000000..9efa9ac --- /dev/null +++ b/MelonLoader/Patches/GameObjects/AbyssSettlementMenu_Patch.cs @@ -0,0 +1,64 @@ +using HarmonyLib; +using Il2Cpp; +using Il2CppTMPro; +using MelonLoader; +using PvZ_Fusion_Translator.AssetStore; +using Unity.VisualScripting; +using UnityEngine; +using System.Text.RegularExpressions; + +namespace PvZ_Fusion_Translator.Patches.GameObjects +{ + [HarmonyPatch(typeof(AbyssSettlementMenu))] + public static class AbyssSettlementMenu_Patch + { + + [HarmonyPatch(nameof(AbyssSettlementMenu.InitSettle))] + [HarmonyPostfix] + private static void InitSettle(AbyssSettlementMenu __instance) + { + TMP_FontAsset fontAsset = FontStore.LoadTMPFont(Utils.Language.ToString()); + + string regexStr1 = "第(\\d+)关\n通关用时:(\\d+)分(\\d+)秒\n杀敌数量:(\\d+)\n关卡叶绿素产出:(\\d+)\n极限模式产出:(\\d+)\n非首胜无额外产出\n产出叶绿素总计:(\\d+)\n"; + string regexStr2 = "第(\\d+)关\n通关用时:(\\d+)分(\\d+)秒\n杀敌数量:(\\d+)\n关卡叶绿素产出:(\\d+)\n极限模式产出:(\\d+)\n首胜产出:(\\d+)\n剩余小推车数量:(\\d+),获得叶绿素:(\\d+)\n产出叶绿素总计:(\\d+)"; + + Regex regex1 = new Regex(regexStr1); + Regex regex2 = new Regex(regexStr2); + + if (regex1.IsMatch(__instance.settleInfo.text)) + { + Match match = regex1.Match(__instance.settleInfo.text); + int groupCount = match.Groups.Count; + + List 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); + } + + __instance.settleInfo.text = string.Format(StringStore.translationStringRegex[regexStr1], [.. dynamicParts]); + __instance.settleInfo.font = fontAsset; + } + else if (regex2.IsMatch(__instance.settleInfo.text)) + { + Match match = regex2.Match(__instance.settleInfo.text); + int groupCount = match.Groups.Count; + + List 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); + } + + __instance.settleInfo.text = string.Format(StringStore.translationStringRegex[regexStr2], [.. dynamicParts]); + __instance.settleInfo.font = fontAsset; + } + } + } +} diff --git a/MelonLoader/Patches/GameObjects/ButtonObjects/UIButton_Patch.cs b/MelonLoader/Patches/GameObjects/ButtonObjects/UIButton_Patch.cs new file mode 100644 index 0000000..07769c8 --- /dev/null +++ b/MelonLoader/Patches/GameObjects/ButtonObjects/UIButton_Patch.cs @@ -0,0 +1,53 @@ +using HarmonyLib; +using Il2Cpp; +using Il2CppTMPro; +using PvZ_Fusion_Translator.AssetStore; +using UnityEngine; +using UnityEngine.UI; + +namespace PvZ_Fusion_Translator.Patches.GameObjects.ButtonObjects +{ + [HarmonyPatch(typeof(UIButton))] + public static class UIButton_Patch + { + [HarmonyPatch(nameof(UIButton.Start))] + [HarmonyPostfix] + private static void Start(UIButton __instance) + { + Transform txtTransform = __instance.transform.FindChild("text"); + string fontPath = ""; + TMP_FontAsset fontAsset = null; + + if(txtTransform != null) + { + string fontsDir = Path.Combine(Core.Instance.modsDirectory, "[Custom Fonts]"); + + foreach (string file in Directory.GetFiles(fontsDir)) + { + string fileName = Path.GetFileNameWithoutExtension(file); + string fileExtension = Path.GetExtension(file); + + if (fileExtension == ".ttf" || fileExtension == ".otf") + { + if (!fileName.EndsWith("_Almanac") && !fileName.EndsWith("_Fallback")) + { + fontPath = Path.Combine("PvZ_Fusion_Translator", "[Custom Fonts]", fileName + fileExtension); + + fontAsset = FontHandler.LoadTMPFont(fontPath, true); + fontAsset.name = fileName; + fontAssetDict.Add(fileName, fontAsset); + + Log.LogInfo($"Font for language '{fileName}' loaded"); + } + } + } + + string fontPath = Path.Combine("PvZ_Fusion_Translator", "[Custom Fonts]", fileName + fileExtension); + Font fontAsset = FontHandler.LoadFont(fontPath, false); + + txtTransform.GetComponent().text = StringStore.TranslateText(txtTransform.GetComponent().text); + txtTransform.GetComponent().font = fontAsset; + } + } + } +} diff --git a/MelonLoader/PvZ Fusion Translator [MelonLoader].csproj b/MelonLoader/PvZ Fusion Translator [MelonLoader].csproj index 1724f37..9cce69e 100644 --- a/MelonLoader/PvZ Fusion Translator [MelonLoader].csproj +++ b/MelonLoader/PvZ Fusion Translator [MelonLoader].csproj @@ -448,10 +448,12 @@ + +