|
1 |
| -using System; |
| 1 | +using HarmonyLib; |
| 2 | +using System; |
2 | 3 | using System.Collections.Generic;
|
3 | 4 | using System.Linq;
|
4 | 5 | using System.Text;
|
|
7 | 8 |
|
8 | 9 | namespace UnityExplorer.Runtime
|
9 | 10 | {
|
10 |
| - public static class UnityCrashPrevention |
| 11 | + internal static class UnityCrashPrevention |
11 | 12 | {
|
12 |
| - public static void CheckPropertyInfoEvaluation(CacheProperty cacheProp) |
| 13 | + internal static void Init() |
13 | 14 | {
|
14 |
| - if (cacheProp.PropertyInfo.Name == "renderingDisplaySize" |
15 |
| - && cacheProp.Owner.Target is Canvas canvas |
16 |
| - && canvas.renderMode == RenderMode.WorldSpace |
17 |
| - && !canvas.worldCamera) |
| 15 | + try |
18 | 16 | {
|
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."); |
20 | 19 | }
|
| 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."); |
21 | 36 | }
|
22 | 37 | }
|
23 | 38 | }
|
0 commit comments