From 08cff3386b48a113d9c73f23ff6056db347b366b Mon Sep 17 00:00:00 2001 From: Sinai Date: Tue, 22 Jun 2021 19:46:09 +1000 Subject: [PATCH] Fix issues with Il2Cpp nullables --- src/Core/Reflection/Il2CppReflection.cs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/Core/Reflection/Il2CppReflection.cs b/src/Core/Reflection/Il2CppReflection.cs index cedb948..f4a5121 100644 --- a/src/Core/Reflection/Il2CppReflection.cs +++ b/src/Core/Reflection/Il2CppReflection.cs @@ -132,7 +132,6 @@ namespace UnityExplorer return null; var type = obj.GetType(); - try { if (IsString(obj)) @@ -216,7 +215,7 @@ namespace UnityExplorer // from other structs to il2cpp object else if (typeof(Il2CppSystem.Object).IsAssignableFrom(castTo)) { - return BoxIl2CppObject(obj); + return BoxIl2CppObject(obj).TryCast(castTo); } else return obj; @@ -295,7 +294,27 @@ namespace UnityExplorer try { if (toType.IsEnum) + { + // Check for nullable enums + var type = cppObj.GetType(); + if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Il2CppSystem.Nullable<>)) + { + var nullable = cppObj.TryCast(type); + var nullableHasValueProperty = type.GetProperty("HasValue"); + if ((bool)nullableHasValueProperty.GetValue(nullable, null)) + { + // nullable has a value. + var nullableValueProperty = type.GetProperty("Value"); + return Enum.Parse(toType, nullableValueProperty.GetValue(nullable, null).ToString()); + } + // nullable and no current value. + return cppObj; + } + return Enum.Parse(toType, cppObj.ToString()); + } + + // Not enum, unbox with Il2CppObjectBase.Unbox var name = toType.AssemblyQualifiedName;