Skip to content

Commit

Permalink
Improve Ldtoken handling during unstripping
Browse files Browse the repository at this point in the history
  • Loading branch information
ds5678 committed Sep 14, 2024
1 parent bba128e commit f4c8d4e
Showing 1 changed file with 14 additions and 27 deletions.
41 changes: 14 additions & 27 deletions Il2CppInterop.Generator/Utils/UnstripTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,36 +246,23 @@ public static bool TranslateMethod(MethodDefinition original, MethodDefinition t
}
else if (bodyInstruction.OpCode.OperandType == CilOperandType.InlineTok)
{
var targetTok = (bodyInstruction.Operand as ITypeDefOrRef)?.ToTypeSignature();
if (targetTok == null)
return false;
if (targetTok is GenericParameterSignature genericParam)
{
if (genericParam.ParameterType is GenericParameterType.Type)
{
var newTypeOwner =
Pass80UnstripMethods.ResolveTypeInNewAssemblies(globalContext, original.DeclaringType?.ToTypeSignature(), imports)?.Resolve();
if (newTypeOwner == null)
return false;
var name = original.DeclaringType!.GenericParameters[genericParam.Index].Name;
targetTok = newTypeOwner.GenericParameters.Single(it => it.Name == name).ToTypeSignature();
}
else
{
var name = original.GenericParameters[genericParam.Index].Name;
targetTok = target.GenericParameters.Single(it => it.Name == name).ToTypeSignature();
}
}
else
switch (bodyInstruction.Operand)
{
targetTok = Pass80UnstripMethods.ResolveTypeInNewAssemblies(globalContext, targetTok, imports);
if (targetTok == null)
case ITypeDefOrRef typeDefOrRef:
{
var targetTok = Pass80UnstripMethods.ResolveTypeInNewAssemblies(globalContext, typeDefOrRef.ToTypeSignature(), imports);
if (targetTok == null)
return false;

var newInstruction = targetBuilder.Add(OpCodes.Call,
imports.Module.DefaultImporter.ImportMethod(imports.Il2CppSystemRuntimeTypeHandleGetRuntimeTypeHandle.Value.MakeGenericInstanceMethod(targetTok)));
instructionMap.Add(bodyInstruction, newInstruction);
}
break;
default:
// Ldtoken is also used for members, which is not implemented.
return false;
}

var newInstruction = targetBuilder.Add(OpCodes.Call,
imports.Module.DefaultImporter.ImportMethod(imports.Il2CppSystemRuntimeTypeHandleGetRuntimeTypeHandle.Value.MakeGenericInstanceMethod(targetTok)));
instructionMap.Add(bodyInstruction, newInstruction);
}
else if (bodyInstruction.OpCode.OperandType is CilOperandType.InlineSwitch && bodyInstruction.Operand is IReadOnlyList<ICilLabel> labels)
{
Expand Down

0 comments on commit f4c8d4e

Please sign in to comment.