PVZFusionTranslation/MelonLoader/Patches/GameObjects/AlmanacPlantMenu_Patch.cs
2026-07-06 05:16:02 -04:00

137 lines
5.1 KiB
C#

using HarmonyLib;
using Il2Cpp;
using Il2CppAlmanacData;
using Il2CppTMPro;
using MelonLoader.TinyJSON;
using PvZ_Fusion_Translator.AssetStore;
using System.Text.Json;
using System.Text.RegularExpressions;
using Unity.Collections;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.UIElements;
using static PvZ_Fusion_Translator.FileLoader;
using static PvZ_Fusion_Translator.Utils;
namespace PvZ_Fusion_Translator.Patches.GameObjects
{
[HarmonyPatch(typeof(AlmanacPlantMenu))]
public static class AlmanacPlantMenu_Patch
{
public static string almanacJson = "";
public static Dictionary<int, Il2CppAlmanacData.PlantInfo> plantInfoDict = new();
[HarmonyPatch(nameof(AlmanacPlantMenu.Awake))]
[HarmonyPostfix]
private static void Awake(AlmanacPlantMenu __instance)
{
TMP_FontAsset font = FontStore.LoadTMPFont(Utils.Language.ToString());
foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren<TextMeshProUGUI>(true))
{
bool isPlantName = text.gameObject.name.Contains("Name") && text.transform.parent.name != "Background";
if (text.gameObject.name != "Description" && !isPlantName && !text.gameObject.name.Contains("Cost"))
{
text.text = StringStore.TranslateText(text.text);
text.font = font;
text.autoSizeTextContainer = true;
}
}
foreach (AlmanacCardUI almanacCardUI in __instance.GetComponentsInChildren<AlmanacCardUI>())
{
AlmanacCardUI_Patch.Awake(almanacCardUI);
}
// skin button resize
AlmanacPlantWindow window = __instance.window;
GameObject skinButton = window.skinButton;
GameObject skinTextObj = skinButton.transform.Find("text_1").gameObject;
skinButton.transform.localScale /= 1.75f;
skinButton.transform.localPosition = new Vector3(0.0f, 111.345f, 0.0f);
skinButton.transform.GetChild(1).Translate(new Vector3(-0.35f, 0));
skinButton.transform.GetChild(2).Translate(new Vector3(0.35f, 0));
}
[HarmonyPatch(nameof(AlmanacPlantMenu.SearchChanged))]
[HarmonyPrefix]
public static bool SearchChanged(AlmanacPlantMenu __instance, ref TMP_InputField inputField)
{
string search = inputField.text;
if (search != StringStore.TranslateText("搜索") && search != "")
{
string currentLanguage = Utils.Language.ToString();
string almanacDir = GetAssetDir(AssetType.Almanac, Utils.Language);
string path = Path.Combine(almanacDir, "LawnStringsTranslate.json");
string json = AlmanacPlantMenu_Patch.almanacJson;
TMP_FontAsset fontAsset = FontStore.LoadTMPFont(currentLanguage);
AlmanacData plantData = JsonUtility.FromJson<AlmanacData>(json);
Il2CppSystem.Collections.Generic.List<PlantType> searchedPlants = new();
foreach (PlantInfo plantInfo in plantData.plants)
{
if (plantInfo.name.ToLower().Contains(search.ToLower()))
{
searchedPlants.Add((PlantType)plantInfo.seedType);
}
else if ($"{plantInfo.seedType}".ToLower().StartsWith(search.ToLower()))
{
searchedPlants.Add((PlantType)plantInfo.seedType);
}
}
__instance.ShowPlants(searchedPlants);
return false;
}
return true;
}
[HarmonyPatch(nameof(AlmanacPlantMenu.ShowPlants), argumentTypes: [typeof(Il2CppSystem.Func<PlantType, bool>)])]
[HarmonyPostfix]
public static void ShowPlants(AlmanacPlantMenu __instance, ref Func<PlantType, bool> condition)
{
foreach (AlmanacCardUI cardUI in __instance.cards)
{
AlmanacCardUI_Patch.Awake(cardUI);
}
}
[HarmonyPatch(nameof(AlmanacPlantMenu.ClassicMix))]
[HarmonyPostfix]
public static void ClassicMix(AlmanacPlantMenu __instance)
{
foreach (AlmanacCardUI cardUI in __instance.cards)
{
AlmanacCardUI_Patch.Awake(cardUI);
}
}
public static void LoadPlantAlmanacData(string json)
{
if (json == null) return;
almanacJson = json;
var plantData = JsonUtility.FromJson<Il2CppAlmanacData.AlmanacData>(almanacJson);
foreach (Il2CppAlmanacData.PlantInfo plantInfo in plantData.plants)
{
if(plantInfoDict.ContainsKey(plantInfo.seedType))
{
plantInfoDict[plantInfo.seedType] = plantInfo;
}
else
{
plantInfoDict.Add(plantInfo.seedType, plantInfo);
}
}
}
}
}