From b61ac481b998aa42729087f8ae16c46224417d3d Mon Sep 17 00:00:00 2001
From: sinaioutlander <49360850+sinaioutlander@users.noreply.github.com>
Date: Wed, 28 Oct 2020 07:14:00 +1100
Subject: [PATCH] Fix null-coalescing operators in the C# console crashing the
game
---
src/Explorer.csproj | 5 +-
src/Input/IHandleInput.cs | 2 -
src/Input/InputManager.cs | 2 -
src/Input/InputSystem.cs | 46 +++++++--------
src/Input/LegacyInput.cs | 22 +++----
src/Input/NoInput.cs | 2 -
src/UI/Main/Console/AutoCompleter.cs | 2 +-
src/UI/Main/Console/CSharpLexer.cs | 4 +-
src/UI/Main/Console/ScriptInteraction.cs | 58 ++++---------------
src/UI/Main/ConsolePage.cs | 18 +++---
src/UI/Main/InspectorManager.cs | 47 +++++++--------
src/UI/Main/Inspectors/GameObjectInspector.cs | 4 +-
src/UI/Main/Inspectors/InstanceInspector.cs | 16 +----
src/UI/Main/Inspectors/ReflectionInspector.cs | 31 ++++++++++
src/UI/Main/Inspectors/StaticInspector.cs | 4 +-
src/UI/Main/SceneExplorer.cs | 12 ++--
16 files changed, 126 insertions(+), 149 deletions(-)
create mode 100644 src/UI/Main/Inspectors/ReflectionInspector.cs
diff --git a/src/Explorer.csproj b/src/Explorer.csproj
index b1a921e..632b7ef 100644
--- a/src/Explorer.csproj
+++ b/src/Explorer.csproj
@@ -336,14 +336,15 @@
-
-
+
+
+
diff --git a/src/Input/IHandleInput.cs b/src/Input/IHandleInput.cs
index f855ad5..df4d509 100644
--- a/src/Input/IHandleInput.cs
+++ b/src/Input/IHandleInput.cs
@@ -4,8 +4,6 @@ namespace ExplorerBeta.Input
{
public interface IHandleInput
{
- void Init();
-
Vector2 MousePosition { get; }
bool GetKeyDown(KeyCode key);
diff --git a/src/Input/InputManager.cs b/src/Input/InputManager.cs
index 90157af..f28264c 100644
--- a/src/Input/InputManager.cs
+++ b/src/Input/InputManager.cs
@@ -29,8 +29,6 @@ namespace ExplorerBeta.Input
ExplorerCore.LogWarning("Could not find any Input module!");
m_inputModule = new NoInput();
}
-
- m_inputModule.Init();
}
public static Vector3 MousePosition => m_inputModule.MousePosition;
diff --git a/src/Input/InputSystem.cs b/src/Input/InputSystem.cs
index 473d9c6..00d99fc 100644
--- a/src/Input/InputSystem.cs
+++ b/src/Input/InputSystem.cs
@@ -7,6 +7,29 @@ namespace ExplorerBeta.Input
{
public class InputSystem : IHandleInput
{
+ public InputSystem()
+ {
+ ExplorerCore.Log("Initializing new InputSystem support...");
+
+ m_kbCurrentProp = TKeyboard.GetProperty("current");
+ m_kbIndexer = TKeyboard.GetProperty("Item", new Type[] { TKey });
+
+ var btnControl = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Controls.ButtonControl");
+ m_btnIsPressedProp = btnControl.GetProperty("isPressed");
+ m_btnWasPressedProp = btnControl.GetProperty("wasPressedThisFrame");
+
+ m_mouseCurrentProp = TMouse.GetProperty("current");
+ m_leftButtonProp = TMouse.GetProperty("leftButton");
+ m_rightButtonProp = TMouse.GetProperty("rightButton");
+
+ m_positionProp = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Pointer")
+ .GetProperty("position");
+
+ m_readVector2InputMethod = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.InputControl`1")
+ .MakeGenericType(typeof(Vector2))
+ .GetMethod("ReadValue");
+ }
+
public static Type TKeyboard => m_tKeyboard ?? (m_tKeyboard = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Keyboard"));
private static Type m_tKeyboard;
@@ -80,28 +103,5 @@ namespace ExplorerBeta.Input
default: throw new NotImplementedException();
}
}
-
- public void Init()
- {
- ExplorerCore.Log("Initializing new InputSystem support...");
-
- m_kbCurrentProp = TKeyboard.GetProperty("current");
- m_kbIndexer = TKeyboard.GetProperty("Item", new Type[] { TKey });
-
- var btnControl = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Controls.ButtonControl");
- m_btnIsPressedProp = btnControl.GetProperty("isPressed");
- m_btnWasPressedProp = btnControl.GetProperty("wasPressedThisFrame");
-
- m_mouseCurrentProp = TMouse.GetProperty("current");
- m_leftButtonProp = TMouse.GetProperty("leftButton");
- m_rightButtonProp = TMouse.GetProperty("rightButton");
-
- m_positionProp = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.Pointer")
- .GetProperty("position");
-
- m_readVector2InputMethod = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.InputControl`1")
- .MakeGenericType(typeof(Vector2))
- .GetMethod("ReadValue");
- }
}
}
diff --git a/src/Input/LegacyInput.cs b/src/Input/LegacyInput.cs
index 2c43d12..b28558c 100644
--- a/src/Input/LegacyInput.cs
+++ b/src/Input/LegacyInput.cs
@@ -7,6 +7,17 @@ namespace ExplorerBeta.Input
{
public class LegacyInput : IHandleInput
{
+ public LegacyInput()
+ {
+ ExplorerCore.Log("Initializing Legacy Input support...");
+
+ m_mousePositionProp = TInput.GetProperty("mousePosition");
+ m_getKeyMethod = TInput.GetMethod("GetKey", new Type[] { typeof(KeyCode) });
+ m_getKeyDownMethod = TInput.GetMethod("GetKeyDown", new Type[] { typeof(KeyCode) });
+ m_getMouseButtonMethod = TInput.GetMethod("GetMouseButton", new Type[] { typeof(int) });
+ m_getMouseButtonDownMethod = TInput.GetMethod("GetMouseButtonDown", new Type[] { typeof(int) });
+ }
+
public static Type TInput => m_tInput ?? (m_tInput = ReflectionHelpers.GetTypeByName("UnityEngine.Input"));
private static Type m_tInput;
@@ -25,16 +36,5 @@ namespace ExplorerBeta.Input
public bool GetMouseButton(int btn) => (bool)m_getMouseButtonMethod.Invoke(null, new object[] { btn });
public bool GetMouseButtonDown(int btn) => (bool)m_getMouseButtonDownMethod.Invoke(null, new object[] { btn });
-
- public void Init()
- {
- ExplorerCore.Log("Initializing Legacy Input support...");
-
- m_mousePositionProp = TInput.GetProperty("mousePosition");
- m_getKeyMethod = TInput.GetMethod("GetKey", new Type[] { typeof(KeyCode) });
- m_getKeyDownMethod = TInput.GetMethod("GetKeyDown", new Type[] { typeof(KeyCode) });
- m_getMouseButtonMethod = TInput.GetMethod("GetMouseButton", new Type[] { typeof(int) });
- m_getMouseButtonDownMethod = TInput.GetMethod("GetMouseButtonDown", new Type[] { typeof(int) });
- }
}
}
diff --git a/src/Input/NoInput.cs b/src/Input/NoInput.cs
index 253b5ca..4accd67 100644
--- a/src/Input/NoInput.cs
+++ b/src/Input/NoInput.cs
@@ -13,7 +13,5 @@ namespace ExplorerBeta.Input
public bool GetMouseButton(int btn) => false;
public bool GetMouseButtonDown(int btn) => false;
-
- public void Init() { }
}
}
diff --git a/src/UI/Main/Console/AutoCompleter.cs b/src/UI/Main/Console/AutoCompleter.cs
index 572713d..1455175 100644
--- a/src/UI/Main/Console/AutoCompleter.cs
+++ b/src/UI/Main/Console/AutoCompleter.cs
@@ -159,7 +159,7 @@ namespace ExplorerBeta.UI.Main.Console
}
}
- private static readonly char[] splitChars = new[] { '{', '}', ',', ';', '<', '>', '(', ')', '[', ']', '=', '|', '&' };
+ private static readonly char[] splitChars = new[] { '{', '}', ',', ';', '<', '>', '(', ')', '[', ']', '=', '|', '&', '?' };
public static void CheckAutocomplete()
{
diff --git a/src/UI/Main/Console/CSharpLexer.cs b/src/UI/Main/Console/CSharpLexer.cs
index b637086..c901d40 100644
--- a/src/UI/Main/Console/CSharpLexer.cs
+++ b/src/UI/Main/Console/CSharpLexer.cs
@@ -28,7 +28,7 @@ namespace ExplorerBeta.UI.Main.Console
if in int into is join let lock long new null object on orderby out
ref remove return sbyte select short sizeof stackalloc string
switch throw true try typeof uint ulong ushort
- value var where while yield"
+ var where while yield"
};
public static KeywordMatch invalidKeywordMatcher = new KeywordMatch()
@@ -36,7 +36,7 @@ namespace ExplorerBeta.UI.Main.Console
highlightColor = new Color(0.95f, 0.10f, 0.10f, 1.0f),
keywords = @"abstract async base class delegate enum explicit extern fixed get
implicit interface internal namespace operator override params private protected public
- using partial readonly sealed set static struct this unchecked unsafe virtual volatile void"
+ using partial readonly sealed set static struct this unchecked unsafe value virtual volatile void"
};
private static char[] delimiterSymbolCache = null;
diff --git a/src/UI/Main/Console/ScriptInteraction.cs b/src/UI/Main/Console/ScriptInteraction.cs
index d169108..e88338f 100644
--- a/src/UI/Main/Console/ScriptInteraction.cs
+++ b/src/UI/Main/Console/ScriptInteraction.cs
@@ -27,66 +27,28 @@ namespace ExplorerBeta.UI.Main.Console
public static object CurrentTarget()
{
- throw new NotImplementedException("TODO");
+ return InspectorManager.Instance?.m_activeInspector?.Target;
}
public static object[] AllTargets()
{
- throw new NotImplementedException("TODO");
+ int count = InspectorManager.Instance?.m_currentInspectors.Count ?? 0;
+ object[] ret = new object[count];
+ for (int i = 0; i < count; i++)
+ {
+ ret[i] = InspectorManager.Instance?.m_currentInspectors[i].Target;
+ }
+ return ret;
}
public static void Inspect(object obj)
{
- throw new NotImplementedException("TODO");
+ InspectorManager.Instance.Inspect(obj);
}
public static void Inspect(Type type)
{
- throw new NotImplementedException("TODO");
+ InspectorManager.Instance.Inspect(type);
}
-
- // public static void Help()
- // {
- // ExplorerCore.Log(@"
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- // C# Console Help
-
- //The following helper methods are available:
-
- //void Log(object message)
- // prints a message to the console window and debug log
- // usage: Log(""hello world"");
-
- //void AddUsing(string directive)
- // adds a using directive to the console.
- // usage: AddUsing(""UnityEngine.UI"");
-
- //void GetUsing()
- // logs the current using directives to the debug console
- // usage: GetUsing();
-
- //void Reset()
- // resets the C# console, clearing all variables and using directives.
- // usage: Reset();
- //");
-
- //TODO:
-
- //ExplorerCore.Log("object CurrentTarget()");
- //ExplorerCore.Log(" returns the target object of the current tab (in tab view mode only)");
- //ExplorerCore.Log(" usage: var target = CurrentTarget();");
- //ExplorerCore.Log("");
- //ExplorerCore.Log("object[] AllTargets()");
- //ExplorerCore.Log(" returns an object[] array containing all currently inspected objects");
- //ExplorerCore.Log(" usage: var targets = AllTargets();");
- //ExplorerCore.Log("");
- //ExplorerCore.Log("void Inspect(object obj)");
- //ExplorerCore.Log(" inspects the provided object in a new window.");
- //ExplorerCore.Log(" usage: Inspect(Camera.main);");
- //ExplorerCore.Log("");
- //ExplorerCore.Log("void Inspect(Type type)");
- //ExplorerCore.Log(" attempts to inspect the provided type with static-only reflection.");
- //ExplorerCore.Log(" usage: Inspect(typeof(Camera));");
- //}
}
}
\ No newline at end of file
diff --git a/src/UI/Main/ConsolePage.cs b/src/UI/Main/ConsolePage.cs
index 5475434..dba3d53 100644
--- a/src/UI/Main/ConsolePage.cs
+++ b/src/UI/Main/ConsolePage.cs
@@ -324,16 +324,14 @@ namespace ExplorerBeta.UI.Main
The following helper methods are available:
-* Log(""message""); logs a message to the debug console
-* AddUsing(""SomeNamespace""); adds a using directive to the C# console
-* GetUsing(); logs the current using directives to the debug console
-* Reset(); resets all using directives and variables
-
-TODO:
-* CurrentTarget(); returns the currently inspected target on the Home page
-* AllTargets(); returns an object[] array containing all inspected instances
-* Inspect(someObject) to inspect an instance, eg. Inspect(Camera.main);
-* Inspect(typeof(SomeClass)) to inspect a Class with static reflection
+* Log(""message""); logs a message to the debug console
+* CurrentTarget(); returns the currently inspected target on the Home page
+* AllTargets(); returns an object[] array containing all inspected instances
+* Inspect(someObject) to inspect an instance, eg. Inspect(Camera.main);
+* Inspect(typeof(SomeClass)) to inspect a Class with static reflection
+* AddUsing(""SomeNamespace""); adds a using directive to the C# console
+* GetUsing(); logs the current using directives to the debug console
+* Reset(); resets all using directives and variables
";
var linesTextObj = UIFactory.CreateUIObject("LinesText", mainTextObj.gameObject);
diff --git a/src/UI/Main/InspectorManager.cs b/src/UI/Main/InspectorManager.cs
index 23ca588..73da696 100644
--- a/src/UI/Main/InspectorManager.cs
+++ b/src/UI/Main/InspectorManager.cs
@@ -19,8 +19,9 @@ namespace ExplorerBeta.UI.Main
public InspectorBase m_activeInspector;
public readonly List m_currentInspectors = new List();
+
public GameObject m_tabBarContent;
- public GameObject m_rightPaneContent;
+ public GameObject m_inspectorContent;
public void Update()
{
@@ -121,11 +122,11 @@ namespace ExplorerBeta.UI.Main
public void ConstructInspectorPane()
{
- m_rightPaneContent = UIFactory.CreateVerticalGroup(HomePage.Instance.Content, new Color(72f / 255f, 72f / 255f, 72f / 255f));
- LayoutElement rightLayout = m_rightPaneContent.AddComponent();
+ var mainObj = UIFactory.CreateVerticalGroup(HomePage.Instance.Content, new Color(72f / 255f, 72f / 255f, 72f / 255f));
+ LayoutElement rightLayout = mainObj.AddComponent();
rightLayout.flexibleWidth = 999999;
- VerticalLayoutGroup rightGroup = m_rightPaneContent.GetComponent();
+ var rightGroup = mainObj.GetComponent();
rightGroup.childForceExpandHeight = true;
rightGroup.childForceExpandWidth = true;
rightGroup.childControlHeight = true;
@@ -136,40 +137,40 @@ namespace ExplorerBeta.UI.Main
rightGroup.padding.top = 8;
rightGroup.padding.bottom = 8;
- GameObject inspectorTitle = UIFactory.CreateLabel(m_rightPaneContent, TextAnchor.UpperLeft);
+ var inspectorTitle = UIFactory.CreateLabel(mainObj, TextAnchor.UpperLeft);
Text title = inspectorTitle.GetComponent();
title.text = "Inspector";
title.fontSize = 20;
- LayoutElement titleLayout = inspectorTitle.AddComponent();
+ var titleLayout = inspectorTitle.AddComponent();
titleLayout.minHeight = 30;
titleLayout.flexibleHeight = 0;
- m_tabBarContent = UIFactory.CreateGridGroup(m_rightPaneContent, new Vector2(185, 20), new Vector2(5, 2), new Color(0.1f, 0.1f, 0.1f, 1));
+ m_tabBarContent = UIFactory.CreateGridGroup(mainObj, new Vector2(185, 20), new Vector2(5, 2), new Color(0.1f, 0.1f, 0.1f, 1));
- GridLayoutGroup gridGroup = m_tabBarContent.GetComponent();
+ var gridGroup = m_tabBarContent.GetComponent();
gridGroup.padding.top = 4;
gridGroup.padding.left = 4;
gridGroup.padding.right = 4;
gridGroup.padding.bottom = 4;
- // TEMP DUMMY INSPECTOR
+ // inspector content area
- GameObject dummyInspectorObj = UIFactory.CreateVerticalGroup(m_rightPaneContent, new Color(0.1f, 0.1f, 0.1f, 1.0f));
+ m_inspectorContent = UIFactory.CreateVerticalGroup(mainObj, new Color(0.1f, 0.1f, 0.1f, 1.0f));
- VerticalLayoutGroup dummyGroup = dummyInspectorObj.GetComponent();
- dummyGroup.childForceExpandHeight = true;
- dummyGroup.childForceExpandWidth = true;
- dummyGroup.childControlHeight = true;
- dummyGroup.childControlWidth = true;
- dummyGroup.spacing = 5;
- dummyGroup.padding.top = 5;
- dummyGroup.padding.left = 5;
- dummyGroup.padding.right = 5;
- dummyGroup.padding.bottom = 5;
+ var contentGroup = m_inspectorContent.GetComponent();
+ contentGroup.childForceExpandHeight = true;
+ contentGroup.childForceExpandWidth = true;
+ contentGroup.childControlHeight = true;
+ contentGroup.childControlWidth = true;
+ contentGroup.spacing = 5;
+ contentGroup.padding.top = 5;
+ contentGroup.padding.left = 5;
+ contentGroup.padding.right = 5;
+ contentGroup.padding.bottom = 5;
- LayoutElement dummyLayout = dummyInspectorObj.AddComponent();
- dummyLayout.preferredHeight = 900;
- dummyLayout.flexibleHeight = 10000;
+ var contentLayout = m_inspectorContent.AddComponent();
+ contentLayout.preferredHeight = 900;
+ contentLayout.flexibleHeight = 10000;
}
#endregion
diff --git a/src/UI/Main/Inspectors/GameObjectInspector.cs b/src/UI/Main/Inspectors/GameObjectInspector.cs
index 66c859c..4162616 100644
--- a/src/UI/Main/Inspectors/GameObjectInspector.cs
+++ b/src/UI/Main/Inspectors/GameObjectInspector.cs
@@ -1,4 +1,6 @@
-using UnityEngine;
+using System;
+using System.Collections.Generic;
+using UnityEngine;
namespace ExplorerBeta.UI.Main.Inspectors
{
diff --git a/src/UI/Main/Inspectors/InstanceInspector.cs b/src/UI/Main/Inspectors/InstanceInspector.cs
index 1481ab8..d01a48d 100644
--- a/src/UI/Main/Inspectors/InstanceInspector.cs
+++ b/src/UI/Main/Inspectors/InstanceInspector.cs
@@ -3,25 +3,13 @@ using ExplorerBeta.Helpers;
namespace ExplorerBeta.UI.Main.Inspectors
{
- public class InstanceInspector : InspectorBase
+ public class InstanceInspector : ReflectionInspector
{
// todo
- public override string TabLabel => $" [R] {m_targetTypeShortName}";
- private readonly string m_targetTypeShortName;
+ public override string TabLabel => $" [R] {base.TabLabel}";
public InstanceInspector(object target) : base(target)
{
- // todo
-
- Type type = ReflectionHelpers.GetActualType(target);
-
- if (type == null)
- {
- // TODO
- return;
- }
-
- m_targetTypeShortName = type.Name;
}
diff --git a/src/UI/Main/Inspectors/ReflectionInspector.cs b/src/UI/Main/Inspectors/ReflectionInspector.cs
new file mode 100644
index 0000000..c1c2a80
--- /dev/null
+++ b/src/UI/Main/Inspectors/ReflectionInspector.cs
@@ -0,0 +1,31 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using ExplorerBeta.Helpers;
+
+namespace ExplorerBeta.UI.Main.Inspectors
+{
+ public class ReflectionInspector : InspectorBase
+ {
+ public override string TabLabel => m_targetTypeShortName;
+
+ private readonly string m_targetTypeShortName;
+
+ public ReflectionInspector(object target) : base(target)
+ {
+ Type type = ReflectionHelpers.GetActualType(target);
+
+ if (type == null)
+ {
+ // TODO
+ return;
+ }
+
+ m_targetTypeShortName = type.Name;
+ }
+
+
+
+ }
+}
diff --git a/src/UI/Main/Inspectors/StaticInspector.cs b/src/UI/Main/Inspectors/StaticInspector.cs
index 5470a6b..d294bae 100644
--- a/src/UI/Main/Inspectors/StaticInspector.cs
+++ b/src/UI/Main/Inspectors/StaticInspector.cs
@@ -2,9 +2,9 @@
namespace ExplorerBeta.UI.Main.Inspectors
{
- public class StaticInspector : InspectorBase
+ public class StaticInspector : ReflectionInspector
{
- public override string TabLabel => " [S] TODO";
+ public override string TabLabel => $" [S] {base.TabLabel}";
public StaticInspector(Type type) : base(type)
{
diff --git a/src/UI/Main/SceneExplorer.cs b/src/UI/Main/SceneExplorer.cs
index 37e1342..b2ceef8 100644
--- a/src/UI/Main/SceneExplorer.cs
+++ b/src/UI/Main/SceneExplorer.cs
@@ -9,7 +9,6 @@ using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
-
namespace ExplorerBeta.UI.Main
{
public class SceneExplorer
@@ -404,7 +403,7 @@ namespace ExplorerBeta.UI.Main
Button inspectButton = m_mainInspectBtn.GetComponent