From 39505475d60d651aa06f02cdd5ad5203f974bb93 Mon Sep 17 00:00:00 2001 From: ds5678 <49847914+ds5678@users.noreply.github.com> Date: Sat, 14 Sep 2024 23:32:56 -0700 Subject: [PATCH] Handle unboxing for generic parameters --- Il2CppInterop.Generator/Utils/UnstripTranslator.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Il2CppInterop.Generator/Utils/UnstripTranslator.cs b/Il2CppInterop.Generator/Utils/UnstripTranslator.cs index fe1a5261..ee9ce42c 100644 --- a/Il2CppInterop.Generator/Utils/UnstripTranslator.cs +++ b/Il2CppInterop.Generator/Utils/UnstripTranslator.cs @@ -214,8 +214,12 @@ public static bool TranslateMethod(MethodDefinition original, MethodDefinition t if (targetType == null) return false; - if (bodyInstruction.OpCode == OpCodes.Castclass && !targetType.IsValueType) + if ((bodyInstruction.OpCode == OpCodes.Castclass && !targetType.IsValueType) || + (bodyInstruction.OpCode == OpCodes.Unbox_Any && targetType is GenericParameterSignature)) { + // Compilers use unbox.any for casting to generic parameter types. + // Castclass is only used for reference types. + // Both can be translated to Il2CppObjectBase.Cast(). var newInstruction = targetBuilder.Add(OpCodes.Call, imports.Module.DefaultImporter.ImportMethod(imports.Il2CppObjectBase_Cast.Value.MakeGenericInstanceMethod(targetType))); instructionMap.Add(bodyInstruction, newInstruction);