Null checks

This commit is contained in:
Teyliu 2025-04-29 15:59:36 -05:00
parent 2a4f1a7c32
commit 7e8c3a6a01
2 changed files with 92 additions and 72 deletions

View file

@ -138,71 +138,87 @@ namespace PvZ_Fusion_Translator.AssetStore
public static void WarningLoad(NoticeMenu noticeMenu) public static void WarningLoad(NoticeMenu noticeMenu)
{ {
//GameAPP.theGameStatus = GameStatus.OpenOptions; if (noticeMenu == null || noticeMenu.transform == null)
//GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(Resources.Load<GameObject>("UI/Prefabs/NoticePauseMenu"), GameAPP.canvas.transform); {
//gameObject.name = "WarningMessage"; MelonLogger.Warning("[WarningStore] NoticeMenu or transform is null - ABORTING");
//TextMeshProUGUI warning = gameObject.transform.GetChild(0).GetChild(0).GetComponent<TextMeshProUGUI>(); return;
//TextMeshProUGUI warningShadow = gameObject.transform.GetChild(0).GetChild(1).GetComponent<TextMeshProUGUI>(); }
//Transform button = gameObject.transform.GetChild(1);
//warning.transform.position = new Vector3(0.7f, 0.4f, 0f);
//warningShadow.transform.position = new Vector3(0.7f, 0.4f, 0f);
#if MULTI_LANGUAGE #if MULTI_LANGUAGE
var (warningMessage, okayButton) = WarningStore.GetWarningMessage(Utils.Language); try
{
var (warningMessage, okayButton) = GetWarningMessage(Utils.Language);
TMP_FontAsset fontAsset = FontStore.LoadTMPFont(Utils.Language.ToString()); TMP_FontAsset fontAsset = FontStore.LoadTMPFont(Utils.Language.ToString());
Transform warningTransform = noticeMenu.transform.FindChild("窗口");
Transform warningTextTransform = warningTransform.FindChild("文字");
Transform warningTextShadowTransform = warningTransform.FindChild("文字2");
Transform[] array = [warningTextTransform, warningTextShadowTransform]; Transform warningTransform = noticeMenu.transform.Find("窗口");
if (warningTransform == null)
for (int i = 0; i < array.Length; i++)
{ {
if (array[i] != null) MelonLogger.Warning("[WarningStore] '窗口' transform not found - ABORTING");
{ return;
array[i].GetComponent<TextMeshProUGUI>().text = warningMessage;
array[i].GetComponent<TextMeshProUGUI>().font = fontAsset;
} }
Transform warningTextTransform = warningTransform.Find("文字");
Transform warningTextShadowTransform = warningTransform.Find("文字2");
if (warningTextTransform == null || warningTextShadowTransform == null)
{
MelonLogger.Warning("[WarningStore] Could not find text components (文字/文字2)");
return;
}
TextMeshProUGUI warningText = warningTextTransform.GetComponent<TextMeshProUGUI>();
TextMeshProUGUI shadowText = warningTextShadowTransform.GetComponent<TextMeshProUGUI>();
if (warningText == null || shadowText == null)
{
MelonLogger.Warning("[WarningStore] TextMeshProUGUI components missing");
return;
}
warningText.text = warningMessage;
shadowText.text = warningMessage;
if (fontAsset != null)
{
warningText.font = fontAsset;
shadowText.font = fontAsset;
}
else
{
MelonLogger.Warning("[WarningStore] Font asset is null");
}
MelonLogger.Msg("Warning message loaded successfully!");
}
catch (Exception e)
{
MelonLogger.Error($"Critical error in WarningLoad: {e}");
} }
#endif #endif
//warning.text = warningMessage;
//warningShadow.text = warningMessage;
//warning.font = FontStore.LoadTMPFont(Utils.Language.ToString());
//warningShadow.font = FontStore.LoadTMPFont(Utils.Language.ToString());
//button.GetComponent<PauseMenu_Btn>().buttonNumber = 10;
//button.GetChild(0).GetComponent<TextMeshProUGUI>().text = okayButton;
//button.GetChild(0).GetComponent<TextMeshProUGUI>().font = FontStore.LoadTMPFont(Utils.Language.ToString());
//button.GetChild(1).GetComponent<TextMeshProUGUI>().text = okayButton;
//gameObject.SetActive(false);
//gameObject.SetActive(true);
} }
#if MULTI_LANGUAGE #if MULTI_LANGUAGE
public static bool isWarningMessageLoaded = false; public static bool isWarningMessageLoaded = false;
public static void WarningReload(Utils.LanguageEnum language, NoticeMenu noticeMenu) public static void WarningReload(Utils.LanguageEnum language, NoticeMenu noticeMenu)
{ {
// Log.LogInfo("Current Language is " + Utils.OldLanguage); if (noticeMenu == null)
// Log.LogInfo("Changed Language to " + language); {
MelonLogger.Warning("[WarningStore] NoticeMenu is null in WarningReload");
return;
}
if (Utils.OldLanguage.ToString() == language.ToString() && Utils.OldLanguage.ToString() != "English") if (Utils.OldLanguage.ToString() == language.ToString() && Utils.OldLanguage.ToString() != "English")
{ {
return; return;
} }
if (!isWarningMessageLoaded) if (!isWarningMessageLoaded)
{ {
WarningLoad(noticeMenu); WarningLoad(noticeMenu);
isWarningMessageLoaded = true; isWarningMessageLoaded = true;
} }
return;
} }
#else #else
public static bool isWarningMessageLoaded = false; public static bool isWarningMessageLoaded = false;
public static void WarningReload() public static void WarningReload()

View file

@ -1,6 +1,5 @@
using HarmonyLib; using HarmonyLib;
using Il2Cpp; using Il2Cpp;
using Il2CppTMPro;
using PvZ_Fusion_Translator.AssetStore; using PvZ_Fusion_Translator.AssetStore;
using UnityEngine; using UnityEngine;
@ -9,12 +8,17 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects
[HarmonyPatch(typeof(MainMenu))] [HarmonyPatch(typeof(MainMenu))]
public class MainMenu_Patch public class MainMenu_Patch
{ {
private static bool _warningShown = false;
[HarmonyPostfix] [HarmonyPostfix]
[HarmonyPatch(nameof(MainMenu.FixedUpdate))] [HarmonyPatch(nameof(MainMenu.FixedUpdate))]
private static void FixedUpdate(MainMenu __instance) private static void FixedUpdate(MainMenu __instance)
{ {
NoticeMenu noticeMenu = NoticeMenu.Instance; if (_warningShown || NoticeMenu.Instance == null)
WarningStore.WarningReload(Utils.Language, noticeMenu); return;
WarningStore.WarningReload(Utils.Language, NoticeMenu.Instance);
_warningShown = true;
} }
} }
} }