From dab7ecd441751704d6a669e42db3391ba6464311 Mon Sep 17 00:00:00 2001
From: sinaioutlander <49360850+sinaioutlander@users.noreply.github.com>
Date: Tue, 29 Sep 2020 05:40:06 +1000
Subject: [PATCH] Cleanups and refactorings, and some small UI fixes
---
src/CachedObjects/CacheFactory.cs | 169 ++++++++
src/CachedObjects/CacheObjectBase.cs | 400 +++++-------------
src/CachedObjects/Object/CacheDictionary.cs | 4 +-
src/CachedObjects/Object/CacheList.cs | 3 +-
src/CachedObjects/Other/CacheMethod.cs | 2 +-
src/CachedObjects/Struct/CacheEnum.cs | 2 +-
src/CachedObjects/Struct/CacheEnumFlags.cs | 45 +-
src/CachedObjects/Struct/CachePrimitive.cs | 222 +++++-----
src/Explorer.csproj | 193 ++++-----
..._BepInPlugin.cs => ExplorerBepInPlugin.cs} | 79 ++--
src/ExplorerCore.cs | 6 +-
...plorer_MelonMod.cs => ExplorerMelonMod.cs} | 4 +-
src/Extensions/ReflectionExtensions.cs | 16 +-
src/Helpers/InputHelper.cs | 2 +-
src/Helpers/ReflectionHelpers.cs | 27 +-
src/Menu/CursorControl.cs | 4 +-
src/Menu/MainMenu/Pages/SearchPage.cs | 2 +-
src/Menu/UIStyles.cs | 2 +-
src/Menu/Windows/GameObjectWindow.cs | 2 +-
src/Menu/Windows/ReflectionWindow.cs | 2 +-
src/Properties/AssemblyInfo.cs | 2 +-
21 files changed, 553 insertions(+), 635 deletions(-)
create mode 100644 src/CachedObjects/CacheFactory.cs
rename src/{Explorer_BepInPlugin.cs => ExplorerBepInPlugin.cs} (60%)
rename src/{Explorer_MelonMod.cs => ExplorerMelonMod.cs} (85%)
diff --git a/src/CachedObjects/CacheFactory.cs b/src/CachedObjects/CacheFactory.cs
new file mode 100644
index 0000000..6faf977
--- /dev/null
+++ b/src/CachedObjects/CacheFactory.cs
@@ -0,0 +1,169 @@
+using System;
+using System.Reflection;
+using UnityEngine;
+
+namespace Explorer
+{
+ public static class CacheFactory
+ {
+ public static CacheObjectBase GetTypeAndCacheObject(object obj)
+ => GetTypeAndCacheObject(obj, null, null);
+
+ public static CacheObjectBase GetTypeAndCacheObject(MemberInfo memberInfo, object declarer)
+ => GetTypeAndCacheObject(null, memberInfo, declarer);
+
+ public static CacheObjectBase GetTypeAndCacheObject(object obj, MemberInfo memberInfo, object declarer)
+ {
+ Type type = null;
+
+ if (memberInfo != null)
+ {
+ if (memberInfo is FieldInfo fi)
+ {
+ type = fi.FieldType;
+ }
+ else if (memberInfo is PropertyInfo pi)
+ {
+ type = pi.PropertyType;
+ }
+ else if (memberInfo is MethodInfo mi)
+ {
+ type = mi.ReturnType;
+ }
+ }
+ else if (obj != null)
+ {
+ type = ReflectionHelpers.GetActualType(obj);
+ }
+
+ if (type == null)
+ {
+ return null;
+ }
+
+ return GetCacheObject(obj, memberInfo, declarer, type);
+ }
+
+ public static CacheObjectBase GetCacheObject(object obj, Type valueType)
+ => GetCacheObject(obj, null, null, valueType);
+
+ private static CacheObjectBase GetCacheObject(object obj, MemberInfo memberInfo, object declaringInstance, Type valueType)
+ {
+ CacheObjectBase cached;
+
+ var pi = memberInfo as PropertyInfo;
+ var mi = memberInfo as MethodInfo;
+
+ // Check if can process args
+ if ((pi != null && !CanProcessArgs(pi.GetIndexParameters()))
+ || (mi != null && !CanProcessArgs(mi.GetParameters())))
+ {
+ return null;
+ }
+
+ if (mi != null)
+ {
+ cached = new CacheMethod();
+ }
+ else if (valueType == typeof(GameObject) || valueType == typeof(Transform))
+ {
+ cached = new CacheGameObject();
+ }
+ else if (valueType.IsPrimitive || valueType == typeof(string))
+ {
+ cached = new CachePrimitive();
+ }
+ else if (valueType.IsEnum)
+ {
+ if (valueType.GetCustomAttributes(typeof(FlagsAttribute), true) is object[] attributes && attributes.Length > 0)
+ {
+ cached = new CacheEnumFlags();
+ }
+ else
+ {
+ cached = new CacheEnum();
+ }
+ }
+ else if (valueType == typeof(Vector2) || valueType == typeof(Vector3) || valueType == typeof(Vector4))
+ {
+ cached = new CacheVector();
+ }
+ else if (valueType == typeof(Quaternion))
+ {
+ cached = new CacheQuaternion();
+ }
+ else if (valueType == typeof(Color))
+ {
+ cached = new CacheColor();
+ }
+ else if (valueType == typeof(Rect))
+ {
+ cached = new CacheRect();
+ }
+ // must check this before IsEnumerable
+ else if (ReflectionHelpers.IsDictionary(valueType))
+ {
+ cached = new CacheDictionary();
+ }
+ else if (ReflectionHelpers.IsEnumerable(valueType))
+ {
+ cached = new CacheList();
+ }
+ else
+ {
+ cached = new CacheOther();
+ }
+
+ cached.Value = obj;
+ cached.ValueType = valueType;
+
+ if (memberInfo != null)
+ {
+ cached.MemInfo = memberInfo;
+ cached.DeclaringType = memberInfo.DeclaringType;
+ cached.DeclaringInstance = declaringInstance;
+ }
+
+ if (pi != null)
+ {
+ cached.m_arguments = pi.GetIndexParameters();
+ }
+ else if (mi != null)
+ {
+ cached.m_arguments = mi.GetParameters();
+ }
+
+ cached.m_argumentInput = new string[cached.m_arguments.Length];
+
+ cached.UpdateValue();
+
+ cached.Init();
+
+ return cached;
+ }
+
+ public static bool CanProcessArgs(ParameterInfo[] parameters)
+ {
+ foreach (var param in parameters)
+ {
+ var pType = param.ParameterType;
+
+ if (pType.IsByRef && pType.HasElementType)
+ {
+ pType = pType.GetElementType();
+ }
+
+ if (pType.IsPrimitive || pType == typeof(string))
+ {
+ continue;
+ }
+ else
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
+ }
+}
diff --git a/src/CachedObjects/CacheObjectBase.cs b/src/CachedObjects/CacheObjectBase.cs
index c5fbe21..f257905 100644
--- a/src/CachedObjects/CacheObjectBase.cs
+++ b/src/CachedObjects/CacheObjectBase.cs
@@ -27,278 +27,12 @@ namespace Explorer
public string RichTextName => m_richTextName ?? GetRichTextName();
private string m_richTextName;
- public bool CanWrite
- {
- get
- {
- if (MemInfo is FieldInfo fi)
- return !(fi.IsLiteral && !fi.IsInitOnly);
- else if (MemInfo is PropertyInfo pi)
- return pi.CanWrite;
- else
- return false;
- }
- }
+ public bool CanWrite => m_canWrite ?? (bool)(m_canWrite = GetCanWrite());
+ private bool? m_canWrite;
public virtual void Init() { }
- public abstract void DrawValue(Rect window, float width);
-
- ///
- /// Get CacheObject from only an object instance
- /// Calls GetCacheObject(obj, memberInfo, declaringInstance) with (obj, null, null)
- public static CacheObjectBase GetCacheObject(object obj)
- {
- return GetCacheObject(obj, null, null);
- }
-
- ///
- /// Get CacheObject from an object instance and provide the value type
- /// Calls GetCacheObjectImpl directly
- public static CacheObjectBase GetCacheObject(object obj, Type valueType)
- {
- return GetCacheObjectImpl(obj, null, null, valueType);
- }
-
- ///
- /// Get CacheObject from only a MemberInfo and declaring instance
- /// Calls GetCacheObject(obj, memberInfo, declaringInstance) with (null, memberInfo, declaringInstance)
- public static CacheObjectBase GetCacheObject(MemberInfo memberInfo, object declaringInstance)
- {
- return GetCacheObject(null, memberInfo, declaringInstance);
- }
-
- ///
- /// Get CacheObject from either an object or MemberInfo, and don't provide the type.
- /// This gets the type and then calls GetCacheObjectImpl
- public static CacheObjectBase GetCacheObject(object obj, MemberInfo memberInfo, object declaringInstance)
- {
- Type type = null;
-
- if (memberInfo != null)
- {
- if (memberInfo is FieldInfo fi)
- {
- type = fi.FieldType;
- }
- else if (memberInfo is PropertyInfo pi)
- {
- type = pi.PropertyType;
- }
- else if (memberInfo is MethodInfo mi)
- {
- type = mi.ReturnType;
- }
- }
- else if (obj != null)
- {
- type = ReflectionHelpers.GetActualType(obj);
- }
-
- if (type == null)
- {
- return null;
- }
-
- return GetCacheObjectImpl(obj, memberInfo, declaringInstance, type);
- }
-
- ///
- /// Actual GetCacheObject implementation (private)
- ///
- private static CacheObjectBase GetCacheObjectImpl(object obj, MemberInfo memberInfo, object declaringInstance, Type valueType)
- {
- CacheObjectBase holder;
-
- var pi = memberInfo as PropertyInfo;
- var mi = memberInfo as MethodInfo;
-
- // Check if can process args
- if ((pi != null && !CanProcessArgs(pi.GetIndexParameters()))
- || (mi != null && !CanProcessArgs(mi.GetParameters())))
- {
- return null;
- }
-
- if (mi != null)
- {
- holder = new CacheMethod();
- }
- else if (valueType == typeof(GameObject) || valueType == typeof(Transform))
- {
- holder = new CacheGameObject();
- }
- else if (valueType.IsPrimitive || valueType == typeof(string))
- {
- holder = new CachePrimitive();
- }
- else if (valueType.IsEnum)
- {
- if (valueType.GetCustomAttributes(typeof(FlagsAttribute), true) is object[] attributes && attributes.Length > 0)
- {
- holder = new CacheEnumFlags();
- }
- else
- {
- holder = new CacheEnum();
- }
- }
- else if (valueType == typeof(Vector2) || valueType == typeof(Vector3) || valueType == typeof(Vector4))
- {
- holder = new CacheVector();
- }
- else if (valueType == typeof(Quaternion))
- {
- holder = new CacheQuaternion();
- }
- else if (valueType == typeof(Color))
- {
- holder = new CacheColor();
- }
- else if (valueType == typeof(Rect))
- {
- holder = new CacheRect();
- }
- // must check this before IsEnumerable
- else if (ReflectionHelpers.IsDictionary(valueType))
- {
- holder = new CacheDictionary();
- }
- else if (ReflectionHelpers.IsEnumerable(valueType))
- {
- holder = new CacheList();
- }
- else
- {
- holder = new CacheOther();
- }
-
- holder.Value = obj;
- holder.ValueType = valueType;
-
- if (memberInfo != null)
- {
- holder.MemInfo = memberInfo;
- holder.DeclaringType = memberInfo.DeclaringType;
- holder.DeclaringInstance = declaringInstance;
- }
-
- if (pi != null)
- {
- holder.m_arguments = pi.GetIndexParameters();
- }
- else if (mi != null)
- {
- holder.m_arguments = mi.GetParameters();
- }
-
- holder.m_argumentInput = new string[holder.m_arguments.Length];
-
- holder.UpdateValue();
-
- holder.Init();
-
- return holder;
- }
-
- public static bool CanProcessArgs(ParameterInfo[] parameters)
- {
- foreach (var param in parameters)
- {
- var pType = param.ParameterType;
-
- if (pType.IsByRef && pType.HasElementType)
- {
- pType = pType.GetElementType();
- }
-
- if (pType.IsPrimitive || pType == typeof(string))
- {
- continue;
- }
- else
- {
- return false;
- }
- }
-
- return true;
- }
-
- public float CalcWhitespace(Rect window)
- {
- if (!(this is IExpandHeight)) return 0f;
-
- float whitespace = (this as IExpandHeight).WhiteSpace;
- if (whitespace > 0)
- {
- ClampLabelWidth(window, ref whitespace);
- }
-
- return whitespace;
- }
-
- public object[] ParseArguments()
- {
- var parsedArgs = new List