Skip to content

Commit

Permalink
Better handle IL2CPP collections in inspector
Browse files Browse the repository at this point in the history
  • Loading branch information
ManlyMarco committed Feb 2, 2024
1 parent 87c2d8d commit 5a6e984
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using HarmonyLib;
using RuntimeUnityEditor.Core.Inspector.Entries;
using RuntimeUnityEditor.Core.ObjectTree;
using RuntimeUnityEditor.Core.Utils;
Expand Down Expand Up @@ -151,6 +152,39 @@ CallbackCacheEntry GetExportTexEntry(Texture texture)
.Select((x, y) => x is ICacheEntry ? x : new ReadonlyListCacheEntry(x, y))
.Cast<ICacheEntry>());
}
else
{
// Needed for IL2CPP collections since they don't implement IEnumerable
// Can cause side effects if the object is not a real collection
var getEnumeratorM = type.GetMethod("GetEnumerator", AccessTools.all, Type.EmptyTypes);
if (getEnumeratorM != null)
{
try
{
var enumerator = getEnumeratorM.Invoke(objectToOpen, null);
if (enumerator != null)
{
var enumeratorType = enumerator.GetType();
var moveNextM = enumeratorType.GetMethod("MoveNext", AccessTools.all, Type.EmptyTypes);
var currentP = enumeratorType.GetProperty("Current");
if (moveNextM != null && currentP != null)
{
var count = 0;
while ((bool)moveNextM.Invoke(enumerator, null))
{
var current = currentP.GetValue(enumerator, null);
_fieldCache.Add(new ReadonlyListCacheEntry(current, count));
count++;
}
}
}
}
catch (Exception e)
{
RuntimeUnityEditorCore.Logger.Log(LogLevel.Warning, $"Failed to enumerate object \"{objectToOpen}\" ({type.FullName}) : {e}");
}
}
}

// No need if it's not a value type, only used to propagate changes back so it's redundant with classes
var parent = entry.Parent?.Type().IsValueType == true ? entry.Parent : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using HarmonyLib;
using RuntimeUnityEditor.Core.Inspector.Entries;
using RuntimeUnityEditor.Core.Utils;
using UnityEngine;
Expand Down Expand Up @@ -46,7 +47,7 @@ public static string ObjectToString(object value)
if (value is ICollection collection)
return $"Count = {collection.Count}";

if (value is IEnumerable _)
if (value is IEnumerable _ || value.GetType().GetMethod("GetEnumerator", AccessTools.all, Type.EmptyTypes) != null)
{
var property = valueType.GetProperty("Count", BindingFlags.Public | BindingFlags.Instance);
if (property != null && property.CanRead)
Expand All @@ -67,7 +68,7 @@ public static string ObjectToString(object value)
if (valueType.IsGenericType)
{
var baseType = valueType.GetGenericTypeDefinition();
if (baseType == typeof(KeyValuePair<,>))
if (baseType == typeof(KeyValuePair<,>) || baseType == typeof(Il2CppSystem.Collections.Generic.KeyValuePair<,>))
{
//var argTypes = baseType.GetGenericArguments();
var kvpKey = valueType.GetProperty("Key")?.GetValue(value, null);
Expand Down

0 comments on commit 5a6e984

Please sign in to comment.