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

Commit

Permalink
3.3.5 - fix Il2Cpp Hashtable, boxed strings
Browse files Browse the repository at this point in the history
  • Loading branch information
sinai-dev committed Apr 3, 2021
1 parent 7443f65 commit 113f2fd
Show file tree
Hide file tree
Showing 27 changed files with 133 additions and 68 deletions.
1 change: 1 addition & 0 deletions src/Core/CSharp/ScriptInteraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using UnityExplorer.Core.Runtime;
using UnityExplorer.UI.Main.CSConsole;
using UnityExplorer.UI.Main.Home;
using UnityExplorer.UI.Inspectors;

namespace UnityExplorer.Core.CSharp
{
Expand Down
8 changes: 4 additions & 4 deletions src/Core/ReflectionUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using BF = System.Reflection.BindingFlags;
using UnityExplorer.Core.Runtime;

namespace UnityExplorer.Core
namespace UnityExplorer
{
public static class ReflectionUtility
{
Expand All @@ -18,7 +18,7 @@ public static class ReflectionUtility
/// </summary>
/// <param name="obj">The object to get the true Type for.</param>
/// <returns>The most accurate Type of the object which could be identified.</returns>
public static Type GetType(this object obj)
public static Type GetActualType(this object obj)
{
if (obj == null)
return null;
Expand All @@ -32,7 +32,7 @@ public static Type GetType(this object obj)
/// <param name="obj">The object to cast</param>
/// <returns>The object, cast to the underlying Type if possible, otherwise the original object.</returns>
public static object Cast(this object obj)
=> ReflectionProvider.Instance.Cast(obj, GetType(obj));
=> ReflectionProvider.Instance.Cast(obj, GetActualType(obj));

/// <summary>
/// Cast an object to a Type, if possible.
Expand Down Expand Up @@ -105,7 +105,7 @@ from type in asm.TryGetTypes()
/// <summary>
/// Get all base types of the provided Type, including itself.
/// </summary>
public static Type[] GetAllBaseTypes(this object obj) => GetAllBaseTypes(GetType(obj));
public static Type[] GetAllBaseTypes(this object obj) => GetAllBaseTypes(GetActualType(obj));

/// <summary>
/// Get all base types of the provided Type, including itself.
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Runtime/Il2Cpp/Il2CppReflection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public static object Il2CppCast(object obj, Type castTo)
IntPtr castFromPtr = il2cpp_object_get_class(ilObj.Pointer);

if (!il2cpp_class_is_assignable_from(castToPtr, castFromPtr))
return obj;
return null;

if (RuntimeSpecificsStore.IsInjected(castToPtr))
return UnhollowerBaseLib.Runtime.ClassInjectorBase.GetMonoObjectFromIl2CppPointer(ilObj.Pointer);
Expand Down
22 changes: 22 additions & 0 deletions src/Core/TestClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UnityExplorer
{
#if CPP
public static class TestClass
{
public static Il2CppSystem.Collections.Hashtable testHashset;

static TestClass()
{
testHashset = new Il2CppSystem.Collections.Hashtable();
testHashset.Add("key1", "itemOne");
testHashset.Add("key2", "itemTwo");
testHashset.Add("key3", "itemThree");
}
}
#endif
}
4 changes: 3 additions & 1 deletion src/ExplorerCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace UnityExplorer
public class ExplorerCore
{
public const string NAME = "UnityExplorer";
public const string VERSION = "3.3.4";
public const string VERSION = "3.3.5";
public const string AUTHOR = "Sinai";
public const string GUID = "com.sinai.unityexplorer";

Expand Down Expand Up @@ -46,6 +46,8 @@ public static void Init(IExplorerLoader loader)
UIManager.Init();

Log($"{NAME} {VERSION} initialized.");

// InspectorManager.Instance.Inspect(typeof(TestClass));
}

public static void Update()
Expand Down
6 changes: 5 additions & 1 deletion src/Loader/ML/MelonLoaderConfigHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ public override void Init()
{
prefCategory = MelonPreferences.CreateCategory(CTG_NAME, $"{CTG_NAME} Settings");

MelonPreferences.Mapper.RegisterMapper(KeycodeReader, KeycodeWriter);
try
{
MelonPreferences.Mapper.RegisterMapper(KeycodeReader, KeycodeWriter);
}
catch { }
}

public override void LoadConfig()
Expand Down
6 changes: 3 additions & 3 deletions src/UI/CacheObject/CacheMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using UnityExplorer.Core;
using UnityExplorer.UI.Utility;
using UnityExplorer.UI.InteractiveValues;
using UnityExplorer.UI.Main.Home.Inspectors.Reflection;
using UnityExplorer.UI.Inspectors.Reflection;

namespace UnityExplorer.UI.CacheObject
{
Expand Down Expand Up @@ -86,15 +86,15 @@ public override void UpdateValue()
{
try
{
Type baseType = ReflectionUtility.GetType(IValue.Value) ?? FallbackType;
Type baseType = ReflectionUtility.GetActualType(IValue.Value) ?? FallbackType;

if (!ReflectionProvider.Instance.IsReflectionSupported(baseType))
throw new Exception("Type not supported with reflection");

UpdateReflection();

if (IValue.Value != null)
IValue.Value = IValue.Value.Cast(ReflectionUtility.GetType(IValue.Value));
IValue.Value = IValue.Value.Cast(ReflectionUtility.GetActualType(IValue.Value));
}
catch (Exception e)
{
Expand Down
2 changes: 1 addition & 1 deletion src/UI/CacheObject/CacheObjectBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public virtual void UpdateValue()
// if the type has changed fundamentally, make a new interactivevalue for it
var type = value == null
? FallbackType
: ReflectionUtility.GetType(value);
: ReflectionUtility.GetActualType(value);

var ivalueType = InteractiveValue.GetIValueForType(type);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using UnityExplorer.Core.Runtime;
using UnityExplorer.UI.Utility;

namespace UnityExplorer.UI.Main.Home.Inspectors.GameObjects
namespace UnityExplorer.UI.Inspectors.GameObjects
{
public class ChildList
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using UnityExplorer.Core.Runtime;
using UnityExplorer.UI.Utility;

namespace UnityExplorer.UI.Main.Home.Inspectors.GameObjects
namespace UnityExplorer.UI.Inspectors.GameObjects
{
public class ComponentList
{
Expand Down Expand Up @@ -75,7 +75,7 @@ internal void RefreshComponentList()

var text = s_compListTexts[i];

text.text = SignatureHighlighter.ParseFullSyntax(ReflectionUtility.GetType(comp), true);
text.text = SignatureHighlighter.ParseFullSyntax(ReflectionUtility.GetActualType(comp), true);

var toggle = s_compToggles[i];
#if CPP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using UnityExplorer.Core.Runtime;
using UnityExplorer.Core.Unity;

namespace UnityExplorer.UI.Main.Home.Inspectors.GameObjects
namespace UnityExplorer.UI.Inspectors.GameObjects
{
public class GameObjectControls
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using UnityExplorer.Core.Runtime;
using UnityExplorer.Core.Unity;

namespace UnityExplorer.UI.Main.Home.Inspectors.GameObjects
namespace UnityExplorer.UI.Inspectors.GameObjects
{
public class GameObjectInspector : InspectorBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using UnityExplorer.Core.Runtime;
using UnityExplorer.UI;
using UnityExplorer.UI.Main;
using UnityExplorer.UI.Inspectors;

namespace UnityExplorer.UI.Main.Home
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using UnityEngine.UI;
using UnityExplorer.Core.Unity;

namespace UnityExplorer.UI.Main.Home.Inspectors
namespace UnityExplorer.UI.Inspectors
{
public abstract class InspectorBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
using UnityEngine.UI;
using UnityExplorer.Core.Runtime;
using UnityExplorer.UI.Main.Home;
using UnityExplorer.UI.Main.Home.Inspectors;
using UnityExplorer.UI.CacheObject;
using UnityExplorer.UI.Main.Home.Inspectors.GameObjects;
using UnityExplorer.UI.Main.Home.Inspectors.Reflection;
using UnityExplorer.UI.Inspectors.GameObjects;
using UnityExplorer.UI.Inspectors.Reflection;

namespace UnityExplorer.UI.Main.Home
namespace UnityExplorer.UI.Inspectors
{
public class InspectorManager
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using UnityExplorer.Core.Config;
using UnityExplorer.Core.Runtime;

namespace UnityExplorer.UI.Main.Home.Inspectors.Reflection
namespace UnityExplorer.UI.Inspectors.Reflection
{
public enum MemberScopes
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
using UnityExplorer.Core.Config;
using UnityExplorer.Core.Runtime;
using UnityExplorer.UI.CacheObject;
using UnityExplorer.UI.Main;
using UnityExplorer.UI.Main.Home;
using UnityExplorer.UI.Utility;

namespace UnityExplorer.UI.Main.Home.Inspectors.Reflection
namespace UnityExplorer.UI.Inspectors.Reflection
{
public class ReflectionInspector : InspectorBase
{
Expand Down Expand Up @@ -78,7 +80,7 @@ public ReflectionInspector(object target) : base(target)
if (this is StaticInspector)
m_targetType = target as Type;
else
m_targetType = ReflectionUtility.GetType(target);
m_targetType = ReflectionUtility.GetActualType(target);

m_targetTypeShortName = SignatureHighlighter.ParseFullSyntax(m_targetType, false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Linq;
using System.Text;

namespace UnityExplorer.UI.Main.Home.Inspectors.Reflection
namespace UnityExplorer.UI.Inspectors.Reflection
{
public class StaticInspector : ReflectionInspector
{
Expand Down
49 changes: 33 additions & 16 deletions src/UI/InteractiveValues/InteractiveDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,32 +218,37 @@ internal override void OnToggleSubcontent(bool active)

private IDictionary EnumerateWithReflection()
{
var valueType = ReflectionUtility.GetType(Value);
var valueType = ReflectionUtility.GetActualType(Value);

// get keys and values
var keys = valueType.GetProperty("Keys").GetValue(Value, null);
var values = valueType.GetProperty("Values").GetValue(Value, null);

// create lists to hold them
var keyList = new List<object>();
var valueList = new List<object>();

// store entries with reflection
EnumerateCollection(keys, keyList);
EnumerateCollection(values, valueList);
var hashtable = Value.Cast(typeof(Il2CppSystem.Collections.Hashtable)) as Il2CppSystem.Collections.Hashtable;

if (hashtable != null)
{
EnumerateCppHashtable(hashtable, keyList, valueList);
}
else
{
var keys = valueType.GetProperty("Keys").GetValue(Value, null);
var values = valueType.GetProperty("Values").GetValue(Value, null);

EnumerateCppIDictionary(keys, keyList);
EnumerateCppIDictionary(values, valueList);
}

// make actual mono dictionary
var dict = (IDictionary)Activator.CreateInstance(typeof(Dictionary<,>)
.MakeGenericType(m_typeOfKeys, m_typeofValues));
var dict = Activator.CreateInstance(typeof(Dictionary<,>)
.MakeGenericType(m_typeOfKeys, m_typeofValues))
as IDictionary;

// finally iterate into mono dictionary
for (int i = 0; i < keyList.Count; i++)
dict.Add(keyList[i], valueList[i]);

return dict;
}

private void EnumerateCollection(object collection, List<object> list)
private void EnumerateCppIDictionary(object collection, List<object> list)
{
// invoke GetEnumerator
var enumerator = collection.GetType().GetMethod("GetEnumerator").Invoke(collection, null);
Expand All @@ -258,11 +263,23 @@ private void EnumerateCollection(object collection, List<object> list)
list.Add(current.GetValue(enumerator, null));
}
}

private void EnumerateCppHashtable(Il2CppSystem.Collections.Hashtable hashtable, List<object> keys, List<object> values)
{
for (int i = 0; i < hashtable.buckets.Count; i++)
{
var bucket = hashtable.buckets[i];
if (bucket == null || bucket.key == null)
continue;
keys.Add(bucket.key);
values.Add(bucket.val);
}
}
#endif

#endregion
#endregion

#region UI CONSTRUCTION
#region UI CONSTRUCTION

internal GameObject m_listContent;
internal LayoutElement m_listLayout;
Expand Down
33 changes: 19 additions & 14 deletions src/UI/InteractiveValues/InteractiveEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using UnityExplorer.Core;
using UnityExplorer.Core.Config;
using UnityExplorer.Core.Unity;
using UnityExplorer.UI;
Expand Down Expand Up @@ -38,11 +39,11 @@ public override bool SubContentWanted

internal IEnumerable RefIEnumerable;
internal IList RefIList;
#if CPP
internal Il2CppSystem.Collections.ICollection CppICollection;
#else
internal ICollection CppICollection = null;
#endif
//#if CPP
// internal object CppICollection;
//#else
// internal object CppICollection = null;
//#endif

internal readonly Type m_baseEntryType;

Expand All @@ -55,13 +56,17 @@ public override void OnValueUpdated()
RefIEnumerable = Value as IEnumerable;
RefIList = Value as IList;

#if CPP
if (Value != null && RefIList == null)
{
try { CppICollection = (Value as Il2CppSystem.Object).TryCast<Il2CppSystem.Collections.ICollection>(); }
catch { }
}
#endif
//#if CPP
// if (Value != null && RefIList == null)
// {
// try
// {
// var type = typeof(Il2CppSystem.Collections.ICollection).MakeGenericType(this.m_baseEntryType);
// CppICollection = (Value as Il2CppSystem.Object).Cast(type);
// }
// catch { }
// }
//#endif

if (m_subContentParent.activeSelf)
{
Expand Down Expand Up @@ -91,8 +96,8 @@ public override void RefreshUIForValue()
if (Value != null)
{
string count = "?";
if (m_recacheWanted && (RefIList != null || CppICollection != null))
count = RefIList?.Count.ToString() ?? CppICollection.Count.ToString();
if (m_recacheWanted && RefIList != null)// || CppICollection != null))
count = RefIList.Count.ToString();// ?? CppICollection.Count.ToString();
else if (!m_recacheWanted)
count = m_entries.Count.ToString();

Expand Down
Loading

0 comments on commit 113f2fd

Please sign in to comment.