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

@ -134,85 +134,101 @@ namespace PvZ_Fusion_Translator.AssetStore
return warningMessages["English"]; // Default to English if language code is not found return warningMessages["English"]; // Default to English if language code is not found
} }
} }
#endif #endif
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.Find("窗口");
Transform warningTransform = noticeMenu.transform.FindChild("窗口"); if (warningTransform == null)
Transform warningTextTransform = warningTransform.FindChild("文字"); {
Transform warningTextShadowTransform = warningTransform.FindChild("文字2"); MelonLogger.Warning("[WarningStore] '窗口' transform not found - ABORTING");
return;
}
Transform[] array = [warningTextTransform, warningTextShadowTransform]; Transform warningTextTransform = warningTransform.Find("文字");
Transform warningTextShadowTransform = warningTransform.Find("文字2");
for (int i = 0; i < array.Length; i++) if (warningTextTransform == null || warningTextShadowTransform == null)
{ {
if (array[i] != null) MelonLogger.Warning("[WarningStore] Could not find text components (文字/文字2)");
{ return;
array[i].GetComponent<TextMeshProUGUI>().text = warningMessage; }
array[i].GetComponent<TextMeshProUGUI>().font = fontAsset;
}
}
#endif
//warning.text = warningMessage; TextMeshProUGUI warningText = warningTextTransform.GetComponent<TextMeshProUGUI>();
//warningShadow.text = warningMessage; TextMeshProUGUI shadowText = warningTextShadowTransform.GetComponent<TextMeshProUGUI>();
//warning.font = FontStore.LoadTMPFont(Utils.Language.ToString());
//warningShadow.font = FontStore.LoadTMPFont(Utils.Language.ToString());
//button.GetComponent<PauseMenu_Btn>().buttonNumber = 10; if (warningText == null || shadowText == null)
//button.GetChild(0).GetComponent<TextMeshProUGUI>().text = okayButton; {
//button.GetChild(0).GetComponent<TextMeshProUGUI>().font = FontStore.LoadTMPFont(Utils.Language.ToString()); MelonLogger.Warning("[WarningStore] TextMeshProUGUI components missing");
//button.GetChild(1).GetComponent<TextMeshProUGUI>().text = okayButton; return;
//gameObject.SetActive(false); }
//gameObject.SetActive(true);
}
#if MULTI_LANGUAGE warningText.text = warningMessage;
shadowText.text = warningMessage;
public static bool isWarningMessageLoaded = false;
public static void WarningReload(Utils.LanguageEnum language, NoticeMenu noticeMenu) if (fontAsset != null)
{ {
// Log.LogInfo("Current Language is " + Utils.OldLanguage); warningText.font = fontAsset;
// Log.LogInfo("Changed Language to " + language); shadowText.font = fontAsset;
}
else
{
MelonLogger.Warning("[WarningStore] Font asset is null");
}
if (Utils.OldLanguage.ToString() == language.ToString() && Utils.OldLanguage.ToString() != "English") MelonLogger.Msg("Warning message loaded successfully!");
{ }
return; catch (Exception e)
} {
if (!isWarningMessageLoaded) MelonLogger.Error($"Critical error in WarningLoad: {e}");
{ }
WarningLoad(noticeMenu); #endif
isWarningMessageLoaded = true; }
} #if MULTI_LANGUAGE
public static bool isWarningMessageLoaded = false;
return; public static void WarningReload(Utils.LanguageEnum language, NoticeMenu noticeMenu)
} {
#else if (noticeMenu == null)
{
MelonLogger.Warning("[WarningStore] NoticeMenu is null in WarningReload");
return;
}
public static bool isWarningMessageLoaded = false; if (Utils.OldLanguage.ToString() == language.ToString() && Utils.OldLanguage.ToString() != "English")
{
return;
}
public static void WarningReload() if (!isWarningMessageLoaded)
{ {
if (!WarningStore.isWarningMessageLoaded) WarningLoad(noticeMenu);
{ isWarningMessageLoaded = true;
WarningStore.WarningLoad(); }
WarningStore.isWarningMessageLoaded = true; }
} #else
} public static bool isWarningMessageLoaded = false;
#endif
} public static void WarningReload()
{
if (!WarningStore.isWarningMessageLoaded)
{
WarningStore.WarningLoad();
WarningStore.isWarningMessageLoaded = true;
}
}
#endif
}
} }

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;
} }
} }
} }