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,75 +134,91 @@ namespace PvZ_Fusion_Translator.AssetStore
return warningMessages["English"]; // Default to English if language code is not found
}
}
#endif
#endif
public static void WarningLoad(NoticeMenu noticeMenu)
{
//GameAPP.theGameStatus = GameStatus.OpenOptions;
//GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(Resources.Load<GameObject>("UI/Prefabs/NoticePauseMenu"), GameAPP.canvas.transform);
//gameObject.name = "WarningMessage";
//TextMeshProUGUI warning = gameObject.transform.GetChild(0).GetChild(0).GetComponent<TextMeshProUGUI>();
//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
var (warningMessage, okayButton) = WarningStore.GetWarningMessage(Utils.Language);
if (noticeMenu == null || noticeMenu.transform == null)
{
MelonLogger.Warning("[WarningStore] NoticeMenu or transform is null - ABORTING");
return;
}
#if MULTI_LANGUAGE
try
{
var (warningMessage, okayButton) = GetWarningMessage(Utils.Language);
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];
for (int i = 0; i < array.Length; i++)
Transform warningTransform = noticeMenu.transform.Find("窗口");
if (warningTransform == null)
{
if (array[i] != null)
MelonLogger.Warning("[WarningStore] '窗口' transform not found - ABORTING");
return;
}
Transform warningTextTransform = warningTransform.Find("文字");
Transform warningTextShadowTransform = warningTransform.Find("文字2");
if (warningTextTransform == null || warningTextShadowTransform == null)
{
array[i].GetComponent<TextMeshProUGUI>().text = warningMessage;
array[i].GetComponent<TextMeshProUGUI>().font = fontAsset;
}
}
#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);
MelonLogger.Warning("[WarningStore] Could not find text components (文字/文字2)");
return;
}
#if MULTI_LANGUAGE
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
}
#if MULTI_LANGUAGE
public static bool isWarningMessageLoaded = false;
public static void WarningReload(Utils.LanguageEnum language, NoticeMenu noticeMenu)
{
// Log.LogInfo("Current Language is " + Utils.OldLanguage);
// Log.LogInfo("Changed Language to " + language);
if (noticeMenu == null)
{
MelonLogger.Warning("[WarningStore] NoticeMenu is null in WarningReload");
return;
}
if (Utils.OldLanguage.ToString() == language.ToString() && Utils.OldLanguage.ToString() != "English")
{
return;
}
if (!isWarningMessageLoaded)
{
WarningLoad(noticeMenu);
isWarningMessageLoaded = true;
}
return;
}
#else
#else
public static bool isWarningMessageLoaded = false;
public static void WarningReload()
@ -213,6 +229,6 @@ namespace PvZ_Fusion_Translator.AssetStore
WarningStore.isWarningMessageLoaded = true;
}
}
#endif
#endif
}
}

View file

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