Made instance inspector helper (owner gameobject/name), force loading unhollowed Assembly-CSharp on game start

This commit is contained in:
sinaioutlander 2020-11-19 16:47:18 +11:00
parent e77e4cce07
commit 97dbecaa2a
5 changed files with 127 additions and 38 deletions

View File

@ -8,13 +8,14 @@ using UnityExplorer.Inspectors;
using System.IO; using System.IO;
using UnityExplorer.Unstrip; using UnityExplorer.Unstrip;
using UnityEngine.SceneManagement; using UnityEngine.SceneManagement;
using UnityExplorer.Helpers;
namespace UnityExplorer namespace UnityExplorer
{ {
public class ExplorerCore public class ExplorerCore
{ {
public const string NAME = "UnityExplorer"; public const string NAME = "UnityExplorer";
public const string VERSION = "3.0.1"; public const string VERSION = "3.0.2";
public const string AUTHOR = "Sinai"; public const string AUTHOR = "Sinai";
public const string GUID = "com.sinai.unityexplorer"; public const string GUID = "com.sinai.unityexplorer";
public const string EXPLORER_FOLDER = @"Mods\UnityExplorer"; public const string EXPLORER_FOLDER = @"Mods\UnityExplorer";
@ -41,6 +42,10 @@ namespace UnityExplorer
Instance = this; Instance = this;
#if CPP
ReflectionHelpers.TryLoadGameModules();
#endif
if (!Directory.Exists(EXPLORER_FOLDER)) if (!Directory.Exists(EXPLORER_FOLDER))
Directory.CreateDirectory(EXPLORER_FOLDER); Directory.CreateDirectory(EXPLORER_FOLDER);

View File

@ -105,21 +105,19 @@ namespace UnityExplorer.Helpers
return getType; return getType;
} }
private static readonly Dictionary<Type, IntPtr> ClassPointers = new Dictionary<Type, IntPtr>(); private static readonly Dictionary<Type, IntPtr> CppClassPointers = new Dictionary<Type, IntPtr>();
public static object Il2CppCast(this object obj, Type castTo) public static object Il2CppCast(this object obj, Type castTo)
{ {
if (!(obj is Il2CppSystem.Object ilObj)) if (!(obj is Il2CppSystem.Object ilObj))
{
return obj; return obj;
}
if (!Il2CppTypeNotNull(castTo, out IntPtr castToPtr)) if (!Il2CppTypeNotNull(castTo, out IntPtr castToPtr))
return obj; return obj;
IntPtr classPtr = il2cpp_object_get_class(ilObj.Pointer); IntPtr castFromPtr = il2cpp_object_get_class(ilObj.Pointer);
if (!il2cpp_class_is_assignable_from(castToPtr, classPtr)) if (!il2cpp_class_is_assignable_from(castToPtr, castFromPtr))
return obj; return obj;
if (RuntimeSpecificsStore.IsInjected(castToPtr)) if (RuntimeSpecificsStore.IsInjected(castToPtr))
@ -128,24 +126,21 @@ namespace UnityExplorer.Helpers
return Activator.CreateInstance(castTo, ilObj.Pointer); return Activator.CreateInstance(castTo, ilObj.Pointer);
} }
public static bool Il2CppTypeNotNull(Type type) public static bool Il2CppTypeNotNull(Type type) => Il2CppTypeNotNull(type, out _);
{
return Il2CppTypeNotNull(type, out _);
}
public static bool Il2CppTypeNotNull(Type type, out IntPtr il2cppPtr) public static bool Il2CppTypeNotNull(Type type, out IntPtr il2cppPtr)
{ {
if (!ClassPointers.ContainsKey(type)) if (!CppClassPointers.ContainsKey(type))
{ {
il2cppPtr = (IntPtr)typeof(Il2CppClassPointerStore<>) il2cppPtr = (IntPtr)typeof(Il2CppClassPointerStore<>)
.MakeGenericType(new Type[] { type }) .MakeGenericType(new Type[] { type })
.GetField("NativeClassPtr", BF.Public | BF.Static) .GetField("NativeClassPtr", BF.Public | BF.Static)
.GetValue(null); .GetValue(null);
ClassPointers.Add(type, il2cppPtr); CppClassPointers.Add(type, il2cppPtr);
} }
else else
il2cppPtr = ClassPointers[type]; il2cppPtr = CppClassPointers[type];
return il2cppPtr != IntPtr.Zero; return il2cppPtr != IntPtr.Zero;
} }
@ -181,31 +176,42 @@ namespace UnityExplorer.Helpers
} }
} }
#if CPP
internal static void TryLoadGameModules()
{
LoadModule("Assembly-CSharp");
LoadModule("Assembly-CSharp-firstpass");
}
public static bool LoadModule(string module) public static bool LoadModule(string module)
{ {
#if CPP
#if ML #if ML
string path = $@"MelonLoader\Managed\{module}.dll"; var path = $@"MelonLoader\Managed\{module}.dll";
#else #else
var path = $@"BepInEx\unhollowed\{module}.dll"; var path = $@"BepInEx\unhollowed\{module}.dll";
#endif #endif
if (!File.Exists(path))
{ return LoadModuleInternal(path);
}
internal static bool LoadModuleInternal(string fullPath)
{
if (!File.Exists(fullPath))
return false; return false;
}
try try
{ {
Assembly.Load(File.ReadAllBytes(path)); Assembly.Load(File.ReadAllBytes(fullPath));
return true; return true;
} }
catch (Exception e) catch (Exception e)
{ {
ExplorerCore.Log(e.GetType() + ", " + e.Message); Console.WriteLine(e.GetType() + ", " + e.Message);
} }
#endif
return false; return false;
} }
#endif
public static bool IsEnumerable(Type t) public static bool IsEnumerable(Type t)
{ {

View File

@ -12,8 +12,17 @@ namespace UnityExplorer.Input
{ {
private static IHandleInput m_inputModule; private static IHandleInput m_inputModule;
public static Vector3 MousePosition => m_inputModule.MousePosition;
public static bool GetKeyDown(KeyCode key) => m_inputModule.GetKeyDown(key);
public static bool GetKey(KeyCode key) => m_inputModule.GetKey(key);
public static bool GetMouseButtonDown(int btn) => m_inputModule.GetMouseButtonDown(btn);
public static bool GetMouseButton(int btn) => m_inputModule.GetMouseButton(btn);
public static void Init() public static void Init()
{ {
#if CPP
if (InputSystem.TKeyboard != null || (ReflectionHelpers.LoadModule("Unity.InputSystem") && InputSystem.TKeyboard != null)) if (InputSystem.TKeyboard != null || (ReflectionHelpers.LoadModule("Unity.InputSystem") && InputSystem.TKeyboard != null))
{ {
m_inputModule = new InputSystem(); m_inputModule = new InputSystem();
@ -22,6 +31,7 @@ namespace UnityExplorer.Input
{ {
m_inputModule = new LegacyInput(); m_inputModule = new LegacyInput();
} }
#endif
if (m_inputModule == null) if (m_inputModule == null)
{ {
@ -29,13 +39,5 @@ namespace UnityExplorer.Input
m_inputModule = new NoInput(); m_inputModule = new NoInput();
} }
} }
public static Vector3 MousePosition => m_inputModule.MousePosition;
public static bool GetKeyDown(KeyCode key) => m_inputModule.GetKeyDown(key);
public static bool GetKey(KeyCode key) => m_inputModule.GetKey(key);
public static bool GetMouseButtonDown(int btn) => m_inputModule.GetMouseButtonDown(btn);
public static bool GetMouseButton(int btn) => m_inputModule.GetMouseButton(btn);
} }
} }

View File

@ -45,19 +45,95 @@ namespace UnityExplorer.Inspectors.Reflection
public void ConstructInstanceHelpers() public void ConstructInstanceHelpers()
{ {
if (!typeof(Component).IsAssignableFrom(m_targetType) && !typeof(UnityEngine.Object).IsAssignableFrom(m_targetType))
return;
var rowObj = UIFactory.CreateHorizontalGroup(Content, new Color(0.1f, 0.1f, 0.1f));
var rowGroup = rowObj.GetComponent<HorizontalLayoutGroup>();
rowGroup.childForceExpandWidth = true;
rowGroup.childControlWidth = true;
rowGroup.spacing = 5;
rowGroup.padding.top = 2;
rowGroup.padding.bottom = 2;
rowGroup.padding.right = 2;
rowGroup.padding.left = 2;
var rowLayout = rowObj.AddComponent<LayoutElement>();
rowLayout.minHeight = 25;
rowLayout.flexibleWidth = 5000;
if (typeof(Component).IsAssignableFrom(m_targetType))
{
ConstructCompHelper(rowObj);
}
ConstructUObjHelper(rowObj);
// WIP // WIP
//if (m_targetType == typeof(Texture2D)) //if (m_targetType == typeof(Texture2D))
// ConstructTextureHelper(); // ConstructTextureHelper();
}
// todo other helpers internal void ConstructCompHelper(GameObject rowObj)
{
var labelObj = UIFactory.CreateLabel(rowObj, TextAnchor.MiddleLeft);
var labelLayout = labelObj.AddComponent<LayoutElement>();
labelLayout.minWidth = 90;
labelLayout.minHeight = 25;
labelLayout.flexibleWidth = 0;
var labelText = labelObj.GetComponent<Text>();
labelText.text = "GameObject:";
//if (typeof(Component).IsAssignableFrom(m_targetType)) #if MONO
//{ var comp = Target as Component;
//} #else
//else if (typeof(UnityEngine.Object).IsAssignableFrom(m_targetType)) var comp = (Target as Il2CppSystem.Object).TryCast<Component>();
//{ #endif
//}
var goBtnObj = UIFactory.CreateButton(rowObj, new Color(0.2f, 0.5f, 0.2f));
var goBtnLayout = goBtnObj.AddComponent<LayoutElement>();
goBtnLayout.minHeight = 25;
goBtnLayout.minWidth = 200;
goBtnLayout.flexibleWidth = 0;
var text = goBtnObj.GetComponentInChildren<Text>();
text.text = comp.name;
var btn = goBtnObj.GetComponent<Button>();
btn.onClick.AddListener(() => { InspectorManager.Instance.Inspect(comp.gameObject); });
}
internal void ConstructUObjHelper(GameObject rowObj)
{
var labelObj = UIFactory.CreateLabel(rowObj, TextAnchor.MiddleLeft);
var labelLayout = labelObj.AddComponent<LayoutElement>();
labelLayout.minWidth = 60;
labelLayout.minHeight = 25;
labelLayout.flexibleWidth = 0;
var labelText = labelObj.GetComponent<Text>();
labelText.text = "Name:";
#if MONO
var uObj = Target as UnityEngine.Object;
#else
var uObj = (Target as Il2CppSystem.Object).TryCast<UnityEngine.Object>();
#endif
var inputObj = UIFactory.CreateInputField(rowObj, 14, 3, 1);
var inputLayout = inputObj.AddComponent<LayoutElement>();
inputLayout.minHeight = 25;
inputLayout.flexibleWidth = 2000;
var inputField = inputObj.GetComponent<InputField>();
inputField.readOnly = true;
inputField.text = uObj.name;
//var goBtnObj = UIFactory.CreateButton(rowObj, new Color(0.2f, 0.5f, 0.2f));
//var goBtnLayout = goBtnObj.AddComponent<LayoutElement>();
//goBtnLayout.minHeight = 25;
//goBtnLayout.minWidth = 200;
//goBtnLayout.flexibleWidth = 0;
//var text = goBtnObj.GetComponentInChildren<Text>();
//text.text = comp.name;
//var btn = goBtnObj.GetComponent<Button>();
//btn.onClick.AddListener(() => { InspectorManager.Instance.Inspect(comp.gameObject); });
} }
//internal bool showingTextureHelper; //internal bool showingTextureHelper;

View File

@ -179,7 +179,7 @@ namespace UnityExplorer.Inspectors
var pi = member as PropertyInfo; var pi = member as PropertyInfo;
var fi = member as FieldInfo; var fi = member as FieldInfo;
if (IsBlacklisted(sig) || mi != null && IsBlacklisted(mi)) if (IsBlacklisted(sig) || (mi != null && IsBlacklisted(mi)))
continue; continue;
var args = mi?.GetParameters() ?? pi?.GetIndexParameters(); var args = mi?.GetParameters() ?? pi?.GetIndexParameters();
@ -581,7 +581,7 @@ namespace UnityExplorer.Inspectors
internal void ConstructMemberList() internal void ConstructMemberList()
{ {
var scrollobj = UIFactory.CreateScrollView(Content, out m_scrollContent, out m_sliderScroller, new Color(0.08f, 0.08f, 0.08f)); var scrollobj = UIFactory.CreateScrollView(Content, out m_scrollContent, out m_sliderScroller, new Color(0.05f, 0.05f, 0.05f));
m_scrollContentRect = m_scrollContent.GetComponent<RectTransform>(); m_scrollContentRect = m_scrollContent.GetComponent<RectTransform>();