Use a patch instead of manual check on every property

This commit is contained in:
Sinai 2022-02-21 01:45:46 +11:00
parent a90292f47f
commit 64193ff1b0
2 changed files with 23 additions and 10 deletions

View File

@ -30,8 +30,6 @@ namespace UnityExplorer.CacheObject
{
try
{
UnityCrashPrevention.CheckPropertyInfoEvaluation(this);
object ret;
if (HasArguments)
ret = PropertyInfo.GetValue(DeclaringInstance, this.Evaluator.TryParseArguments());

View File

@ -1,4 +1,5 @@
using System;
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -7,17 +8,31 @@ using UnityExplorer.CacheObject;
namespace UnityExplorer.Runtime
{
public static class UnityCrashPrevention
internal static class UnityCrashPrevention
{
public static void CheckPropertyInfoEvaluation(CacheProperty cacheProp)
internal static void Init()
{
if (cacheProp.PropertyInfo.Name == "renderingDisplaySize"
&& cacheProp.Owner.Target is Canvas canvas
&& canvas.renderMode == RenderMode.WorldSpace
&& !canvas.worldCamera)
try
{
throw new Exception("Canvas is set to RenderMode.WorldSpace but has no worldCamera, cannot get value.");
ExplorerCore.Harmony.PatchAll(typeof(UnityCrashPrevention));
ExplorerCore.Log("Initialized UnityCrashPrevention.");
}
catch //(Exception ex)
{
//ExplorerCore.Log($"Exception setting up Canvas crash prevention patch: {ex}");
}
}
// In Unity 2020 they introduced "Canvas.renderingDisplaySize".
// If you try to get the value on a Canvas which has a renderMode value of WorldSpace and no worldCamera set,
// the game will Crash when Unity tries to read from a null ptr (I think).
[HarmonyPatch(typeof(Canvas), "renderingDisplaySize", MethodType.Getter)]
[HarmonyPrefix]
internal static void Prefix(Canvas __instance)
{
if (__instance.renderMode == RenderMode.WorldSpace && !__instance.worldCamera)
throw new InvalidOperationException(
"Canvas is set to RenderMode.WorldSpace but not worldCamera is set, cannot get renderingDisplaySize.");
}
}
}