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

Commit 08cff33

Browse files
committed
Fix issues with Il2Cpp nullables
1 parent 6033200 commit 08cff33

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/Core/Reflection/Il2CppReflection.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ internal override Type Internal_GetActualType(object obj)
132132
return null;
133133

134134
var type = obj.GetType();
135-
136135
try
137136
{
138137
if (IsString(obj))
@@ -216,7 +215,7 @@ internal override object Internal_TryCast(object obj, Type castTo)
216215
// from other structs to il2cpp object
217216
else if (typeof(Il2CppSystem.Object).IsAssignableFrom(castTo))
218217
{
219-
return BoxIl2CppObject(obj);
218+
return BoxIl2CppObject(obj).TryCast(castTo);
220219
}
221220
else
222221
return obj;
@@ -295,7 +294,27 @@ public object UnboxCppObject(Il2CppObjectBase cppObj, Type toType)
295294
try
296295
{
297296
if (toType.IsEnum)
297+
{
298+
// Check for nullable enums
299+
var type = cppObj.GetType();
300+
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Il2CppSystem.Nullable<>))
301+
{
302+
var nullable = cppObj.TryCast(type);
303+
var nullableHasValueProperty = type.GetProperty("HasValue");
304+
if ((bool)nullableHasValueProperty.GetValue(nullable, null))
305+
{
306+
// nullable has a value.
307+
var nullableValueProperty = type.GetProperty("Value");
308+
return Enum.Parse(toType, nullableValueProperty.GetValue(nullable, null).ToString());
309+
}
310+
// nullable and no current value.
311+
return cppObj;
312+
}
313+
298314
return Enum.Parse(toType, cppObj.ToString());
315+
}
316+
317+
// Not enum, unbox with Il2CppObjectBase.Unbox
299318

300319
var name = toType.AssemblyQualifiedName;
301320

0 commit comments

Comments
 (0)