Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit 64193ff

Browse files
committed
Use a patch instead of manual check on every property
1 parent a90292f commit 64193ff

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

src/CacheObject/CacheProperty.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ protected override object TryEvaluate()
3030
{
3131
try
3232
{
33-
UnityCrashPrevention.CheckPropertyInfoEvaluation(this);
34-
3533
object ret;
3634
if (HasArguments)
3735
ret = PropertyInfo.GetValue(DeclaringInstance, this.Evaluator.TryParseArguments());
Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using HarmonyLib;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Text;
@@ -7,17 +8,31 @@
78

89
namespace UnityExplorer.Runtime
910
{
10-
public static class UnityCrashPrevention
11+
internal static class UnityCrashPrevention
1112
{
12-
public static void CheckPropertyInfoEvaluation(CacheProperty cacheProp)
13+
internal static void Init()
1314
{
14-
if (cacheProp.PropertyInfo.Name == "renderingDisplaySize"
15-
&& cacheProp.Owner.Target is Canvas canvas
16-
&& canvas.renderMode == RenderMode.WorldSpace
17-
&& !canvas.worldCamera)
15+
try
1816
{
19-
throw new Exception("Canvas is set to RenderMode.WorldSpace but has no worldCamera, cannot get value.");
17+
ExplorerCore.Harmony.PatchAll(typeof(UnityCrashPrevention));
18+
ExplorerCore.Log("Initialized UnityCrashPrevention.");
2019
}
20+
catch //(Exception ex)
21+
{
22+
//ExplorerCore.Log($"Exception setting up Canvas crash prevention patch: {ex}");
23+
}
24+
}
25+
26+
// In Unity 2020 they introduced "Canvas.renderingDisplaySize".
27+
// If you try to get the value on a Canvas which has a renderMode value of WorldSpace and no worldCamera set,
28+
// the game will Crash when Unity tries to read from a null ptr (I think).
29+
[HarmonyPatch(typeof(Canvas), "renderingDisplaySize", MethodType.Getter)]
30+
[HarmonyPrefix]
31+
internal static void Prefix(Canvas __instance)
32+
{
33+
if (__instance.renderMode == RenderMode.WorldSpace && !__instance.worldCamera)
34+
throw new InvalidOperationException(
35+
"Canvas is set to RenderMode.WorldSpace but not worldCamera is set, cannot get renderingDisplaySize.");
2136
}
2237
}
2338
}

0 commit comments

Comments
 (0)