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

Commit a90292f

Browse files
committed
Prevent Unity crashing on PropertyInfo evaluation
Unity crashes from checking Canvas.renderingDisplaySize on a Canvas set to WorldSpace with no worldCamera set.
1 parent d0bccae commit a90292f

File tree

5 files changed

+36
-8
lines changed

5 files changed

+36
-8
lines changed

src/CacheObject/CacheMember.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public abstract class CacheMember : CacheObjectBase
2121
{
2222
public abstract Type DeclaringType { get; }
2323
public string NameForFiltering { get; protected set; }
24-
public object DeclaringInstance => IsStatic ? null : (m_declaringInstance ?? (m_declaringInstance = Owner.Target.TryCast(DeclaringType)));
24+
public object DeclaringInstance => IsStatic ? null : (m_declaringInstance ??= Owner.Target.TryCast(DeclaringType));
2525
private object m_declaringInstance;
2626

2727
public abstract bool IsStatic { get; }
@@ -94,8 +94,8 @@ protected override void SetValueState(CacheObjectCell cell, ValueStateArgs args)
9494
base.SetValueState(cell, args);
9595
}
9696

97-
private static readonly Color evalEnabledColor = new Color(0.15f, 0.25f, 0.15f);
98-
private static readonly Color evalDisabledColor = new Color(0.15f, 0.15f, 0.15f);
97+
private static readonly Color evalEnabledColor = new(0.15f, 0.25f, 0.15f);
98+
private static readonly Color evalDisabledColor = new(0.15f, 0.15f, 0.15f);
9999

100100
protected override bool TryAutoEvaluateIfUnitialized(CacheObjectCell objectcell)
101101
{
@@ -242,7 +242,7 @@ private static void TryCacheMember(MemberInfo member, List<CacheMember> list, Ha
242242

243243
var sig = GetSig(member);
244244

245-
//ExplorerCore.Log($"Trying to cache member {sig}... ({member.MemberType})");
245+
// ExplorerCore.Log($"Trying to cache member {sig}... ({member.MemberType})");
246246

247247
CacheMember cached;
248248
Type returnType;

src/CacheObject/CacheProperty.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
using System.Linq;
44
using System.Reflection;
55
using System.Text;
6+
using UnityEngine;
67
using UnityExplorer.Inspectors;
8+
using UnityExplorer.Runtime;
79

810
namespace UnityExplorer.CacheObject
911
{
@@ -28,6 +30,8 @@ protected override object TryEvaluate()
2830
{
2931
try
3032
{
33+
UnityCrashPrevention.CheckPropertyInfoEvaluation(this);
34+
3135
object ret;
3236
if (HasArguments)
3337
ret = PropertyInfo.GetValue(DeclaringInstance, this.Evaluator.TryParseArguments());

src/Inspectors/InspectorManager.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace UnityExplorer
1818
{
1919
public static class InspectorManager
2020
{
21-
public static readonly List<InspectorBase> Inspectors = new List<InspectorBase>();
21+
public static readonly List<InspectorBase> Inspectors = new();
2222

2323
public static InspectorBase ActiveInspector { get; private set; }
2424
private static InspectorBase lastActiveInspector;
@@ -94,17 +94,17 @@ internal static void CloseAllTabs()
9494
}
9595

9696
private static void CreateInspector<T>(object target, bool staticReflection = false,
97-
CacheObjectBase sourceCache = null) where T : InspectorBase
97+
CacheObjectBase parentObject = null) where T : InspectorBase
9898
{
9999
var inspector = Pool<T>.Borrow();
100100
Inspectors.Add(inspector);
101101
inspector.Target = target;
102102

103-
if (sourceCache != null && sourceCache.CanWrite)
103+
if (parentObject != null && parentObject.CanWrite)
104104
{
105105
// only set parent cache object if we are inspecting a struct, otherwise there is no point.
106106
if (target.GetType().IsValueType && inspector is ReflectionInspector ri)
107-
ri.ParentCacheObject = sourceCache;
107+
ri.ParentCacheObject = parentObject;
108108
}
109109

110110
UIManager.SetPanelActive(UIManager.Panels.Inspector, true);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using UnityEngine;
6+
using UnityExplorer.CacheObject;
7+
8+
namespace UnityExplorer.Runtime
9+
{
10+
public static class UnityCrashPrevention
11+
{
12+
public static void CheckPropertyInfoEvaluation(CacheProperty cacheProp)
13+
{
14+
if (cacheProp.PropertyInfo.Name == "renderingDisplaySize"
15+
&& cacheProp.Owner.Target is Canvas canvas
16+
&& canvas.renderMode == RenderMode.WorldSpace
17+
&& !canvas.worldCamera)
18+
{
19+
throw new Exception("Canvas is set to RenderMode.WorldSpace but has no worldCamera, cannot get value.");
20+
}
21+
}
22+
}
23+
}

src/UnityExplorer.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@
259259
<Compile Include="CacheObject\Views\CacheListEntryCell.cs" />
260260
<Compile Include="CacheObject\Views\CacheMemberCell.cs" />
261261
<Compile Include="CacheObject\Views\CacheObjectCell.cs" />
262+
<Compile Include="Runtime\UnityCrashPrevention.cs" />
262263
<Compile Include="UI\DisplayManager.cs" />
263264
<Compile Include="UI\Notification.cs" />
264265
<Compile Include="UI\Panels\ClipboardPanel.cs" />

0 commit comments

Comments
 (0)