From 72c222d59a3aff237ac1c36a2a347a06e337dc51 Mon Sep 17 00:00:00 2001 From: sinaioutlander <49360850+sinaioutlander@users.noreply.github.com> Date: Tue, 18 Aug 2020 17:38:09 +1000 Subject: [PATCH] 1.3.5 - Project structure cleanup - Code refactoring - Adding some extension methods for cleaner looking code - Performance improvements on Object Reflection window --- src/CppExplorer.csproj | 2 ++ src/Extensions/ReflectionExtensions.cs | 16 +++++++++++ src/Extensions/UnityExtensions.cs | 29 ++++++++++++++++++++ src/Helpers/UnityHelpers.cs | 19 +------------ src/Windows/Reflection/FieldInfoHolder.cs | 6 ++-- src/Windows/Reflection/PropertyInfoHolder.cs | 27 +++++++++--------- src/Windows/ReflectionWindow.cs | 2 -- 7 files changed, 65 insertions(+), 36 deletions(-) create mode 100644 src/Extensions/ReflectionExtensions.cs create mode 100644 src/Extensions/UnityExtensions.cs diff --git a/src/CppExplorer.csproj b/src/CppExplorer.csproj index 83bd797..54284cd 100644 --- a/src/CppExplorer.csproj +++ b/src/CppExplorer.csproj @@ -137,6 +137,8 @@ + + diff --git a/src/Extensions/ReflectionExtensions.cs b/src/Extensions/ReflectionExtensions.cs new file mode 100644 index 0000000..66d159e --- /dev/null +++ b/src/Extensions/ReflectionExtensions.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Explorer +{ + public static class ReflectionExtensions + { + public static object Il2CppCast(this object obj, Type castTo) + { + return ReflectionHelpers.Il2CppCast(obj, castTo); + } + } +} diff --git a/src/Extensions/UnityExtensions.cs b/src/Extensions/UnityExtensions.cs new file mode 100644 index 0000000..11e2016 --- /dev/null +++ b/src/Extensions/UnityExtensions.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UnityEngine; + +namespace Explorer +{ + public static class UnityExtensions + { + public static string GetGameObjectPath(this Transform _transform) + { + return GetGameObjectPath(_transform, true); + } + + public static string GetGameObjectPath(this Transform _transform, bool _includeThisName) + { + string path = _includeThisName ? ("/" + _transform.name) : ""; + GameObject gameObject = _transform.gameObject; + while (gameObject.transform.parent != null) + { + gameObject = gameObject.transform.parent.gameObject; + path = "/" + gameObject.name + path; + } + return path; + } + } +} diff --git a/src/Helpers/UnityHelpers.cs b/src/Helpers/UnityHelpers.cs index 70470c9..5bccf64 100644 --- a/src/Helpers/UnityHelpers.cs +++ b/src/Helpers/UnityHelpers.cs @@ -7,7 +7,7 @@ using UnityEngine; namespace Explorer { - public static class UnityHelpers + public class UnityHelpers { private static Camera m_mainCamera; @@ -30,22 +30,5 @@ namespace Explorer return UnityEngine.SceneManagement.SceneManager.GetActiveScene().name; } } - - public static string GetGameObjectPath(this Transform _transform) - { - return GetGameObjectPath(_transform, true); - } - - public static string GetGameObjectPath(this Transform _transform, bool _includeThisName) - { - string path = _includeThisName ? ("/" + _transform.name) : ""; - GameObject gameObject = _transform.gameObject; - while (gameObject.transform.parent != null) - { - gameObject = gameObject.transform.parent.gameObject; - path = "/" + gameObject.name + path; - } - return path; - } } } diff --git a/src/Windows/Reflection/FieldInfoHolder.cs b/src/Windows/Reflection/FieldInfoHolder.cs index b10eb9c..5c4ca86 100644 --- a/src/Windows/Reflection/FieldInfoHolder.cs +++ b/src/Windows/Reflection/FieldInfoHolder.cs @@ -28,7 +28,8 @@ namespace Explorer { var declaringType = this.fieldInfo.DeclaringType; - var cast = ReflectionHelpers.Il2CppCast(obj, declaringType); + //var cast = ReflectionHelpers.Il2CppCast(obj, declaringType); + var cast = obj.Il2CppCast(declaringType); m_value = this.fieldInfo.GetValue(fieldInfo.IsStatic ? null : cast); } else @@ -103,7 +104,8 @@ namespace Explorer { var declaringType = this.fieldInfo.DeclaringType; - var cast = ReflectionHelpers.Il2CppCast(obj, declaringType); + //var cast = ReflectionHelpers.Il2CppCast(obj, declaringType); + var cast = obj.Il2CppCast(declaringType); fieldInfo.SetValue(fieldInfo.IsStatic ? null : cast, m_value); } else diff --git a/src/Windows/Reflection/PropertyInfoHolder.cs b/src/Windows/Reflection/PropertyInfoHolder.cs index 508c565..498e651 100644 --- a/src/Windows/Reflection/PropertyInfoHolder.cs +++ b/src/Windows/Reflection/PropertyInfoHolder.cs @@ -40,7 +40,8 @@ namespace Explorer } else { - var cast = ReflectionHelpers.Il2CppCast(obj, declaringType); + //var cast = ReflectionHelpers.Il2CppCast(obj, declaringType); + var cast = obj.Il2CppCast(declaringType); m_value = this.propInfo.GetValue(this.propInfo.GetAccessors()[0].IsStatic ? null : cast, null); } } @@ -49,19 +50,19 @@ namespace Explorer m_value = this.propInfo.GetValue(obj, null); } } - catch (Exception e) + catch //(Exception e) { - MelonLogger.Log("Exception on PropertyInfoHolder.UpdateValue, Name: " + this.propInfo.Name); - MelonLogger.Log(e.GetType() + ", " + e.Message); + //MelonLogger.Log("Exception on PropertyInfoHolder.UpdateValue, Name: " + this.propInfo.Name); + //MelonLogger.Log(e.GetType() + ", " + e.Message); - var inner = e.InnerException; - while (inner != null) - { - MelonLogger.Log("inner: " + inner.GetType() + ", " + inner.Message); - inner = inner.InnerException; - } + //var inner = e.InnerException; + //while (inner != null) + //{ + // MelonLogger.Log("inner: " + inner.GetType() + ", " + inner.Message); + // inner = inner.InnerException; + //} - m_value = null; + //m_value = null; } } @@ -113,9 +114,7 @@ namespace Explorer } } - var declaring = propInfo.DeclaringType; - var cast = ReflectionHelpers.Il2CppCast(obj, declaring); - + var cast = obj.Il2CppCast(propInfo.DeclaringType); propInfo.SetValue(propInfo.GetAccessors()[0].IsStatic ? null : cast, m_value, null); } catch diff --git a/src/Windows/ReflectionWindow.cs b/src/Windows/ReflectionWindow.cs index 5a4b880..edc7e88 100644 --- a/src/Windows/ReflectionWindow.cs +++ b/src/Windows/ReflectionWindow.cs @@ -48,8 +48,6 @@ namespace Explorer CacheFields(types); CacheProperties(types); - MelonLogger.Log("Cached properties: " + m_PropertyInfos.Length); - UpdateValues(true); }