Skip to content

Commit

Permalink
Unstrip ldlen
Browse files Browse the repository at this point in the history
  • Loading branch information
ds5678 committed Jul 7, 2024
1 parent 15eaeab commit 5e58d9e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
18 changes: 15 additions & 3 deletions Il2CppInterop.Generator/Utils/RuntimeAssemblyReferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ public RuntimeAssemblyReferences(ModuleDefinition module, RewriteGlobalContext g
InitMethodRefs();
}

public ModuleDefinition Module { get; }
public ModuleDefinition Module { get; }
#nullable disable
public Memoize<TypeSignature, IMethodDefOrRef> Il2CppRefrenceArrayctor { get; private set; }
public Lazy<IMethodDefOrRef> Il2CppStringArrayctor { get; private set; }
public Memoize<TypeSignature, IMethodDefOrRef> Il2CppStructArrayctor { get; private set; }
public Memoize<TypeSignature, IMethodDefOrRef> Il2CppRefrenceArrayctor_size { get; private set; }
public Lazy<IMethodDefOrRef> Il2CppStringArrayctor_size { get; private set; }
public Memoize<TypeSignature, IMethodDefOrRef> Il2CppStructArrayctor_size { get; private set; }
public Lazy<IMethodDefOrRef> Il2CppArrayBase_get_Length { get; private set; }
public Lazy<IMethodDefOrRef> IL2CPP_Il2CppObjectBaseToPtr { get; private set; }
public Lazy<IMethodDefOrRef> IL2CPP_Il2CppObjectBaseToPtrNotNull { get; private set; }
public Lazy<IMethodDefOrRef> IL2CPP_Il2CppStringToManaged { get; private set; }
Expand Down Expand Up @@ -69,7 +70,7 @@ public RuntimeAssemblyReferences(ModuleDefinition module, RewriteGlobalContext g
public Lazy<IMethodDefOrRef> CachedScanResultsAttributector { get; private set; }
public Lazy<IMethodDefOrRef> Il2CppSystemDelegateCombine { get; private set; }
public Lazy<IMethodDefOrRef> Il2CppSystemDelegateRemove { get; private set; }
public Lazy<IMethodDefOrRef> Il2CppSystemRuntimeTypeHandleGetRuntimeTypeHandle { get; private set; }
public Lazy<IMethodDefOrRef> Il2CppSystemRuntimeTypeHandleGetRuntimeTypeHandle { get; private set; }

public IMethodDescriptor WriteFieldWBarrier => globalCtx.HasGcWbarrierFieldWrite
? IL2CPP_il2cpp_gc_wbarrier_set_field.Value
Expand All @@ -85,7 +86,7 @@ public RuntimeAssemblyReferences(ModuleDefinition module, RewriteGlobalContext g
public TypeSignature Il2Cpp { get; set; }
public TypeSignature RuntimeReflectionHelper { get; private set; }
public TypeSignature DelegateSupport { get; private set; }
public TypeSignature Il2CppException { get; private set; }
public TypeSignature Il2CppException { get; private set; }
#nullable enable
private TypeSignature ResolveType(string typeName)
{
Expand Down Expand Up @@ -119,6 +120,8 @@ private void InitTypeRefs()

Il2CppArrayBase = new TypeReference(Module, assemblyRef, "Il2CppInterop.Runtime.InteropTypes.Arrays", "Il2CppArrayBase`1").ToTypeSignature();

var nonGenericIl2CppArrayBase = new TypeReference(Module, assemblyRef, "Il2CppInterop.Runtime.InteropTypes.Arrays", "Il2CppArrayBase").ToTypeSignature();

Il2CppStructArray = new TypeReference(Module, assemblyRef, "Il2CppInterop.Runtime.InteropTypes.Arrays", "Il2CppStructArray`1").ToTypeSignature();

Il2CppReferenceArray = new TypeReference(Module, assemblyRef, "Il2CppInterop.Runtime.InteropTypes.Arrays", "Il2CppReferenceArray`1").ToTypeSignature();
Expand All @@ -136,6 +139,7 @@ private void InitTypeRefs()

allTypes["Il2CppInterop.Runtime.InteropTypes.Il2CppObjectBase"] = Il2CppObjectBase;
allTypes["Il2CppInterop.Runtime.Runtime.Il2CppObjectPool"] = Il2CppObjectPool;
allTypes["Il2CppInterop.Runtime.InteropTypes.Arrays.Il2CppArrayBase"] = nonGenericIl2CppArrayBase;
allTypes["Il2CppInterop.Runtime.InteropTypes.Arrays.Il2CppStringArray"] = Il2CppStringArray;
allTypes["Il2CppInterop.Runtime.InteropTypes.Arrays.Il2CppReferenceArray<T>"] = Il2CppReferenceArray;
allTypes["Il2CppInterop.Runtime.InteropTypes.Arrays.Il2CppStructArray<T>"] = Il2CppStructArray;
Expand Down Expand Up @@ -199,6 +203,14 @@ private void InitMethodRefs()
return mr;
});

Il2CppArrayBase_get_Length = new Lazy<IMethodDefOrRef>(() =>
{
var owner = ResolveType("Il2CppInterop.Runtime.InteropTypes.Arrays.Il2CppArrayBase");
var mr = ReferenceCreator.CreateInstanceMethodReference("get_Length", ResolveType("System.Int32"),
owner.ToTypeDefOrRef());
return mr;
});

IL2CPP_Il2CppObjectBaseToPtr = new Lazy<IMethodDefOrRef>(() =>
{
var mr = ReferenceCreator.CreateStaticMethodReference("Il2CppObjectBaseToPtr", ResolveType("System.IntPtr"),
Expand Down
13 changes: 10 additions & 3 deletions Il2CppInterop.Generator/Utils/UnstripTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@ public static bool TranslateMethod(MethodDefinition original, MethodDefinition t
{
if (bodyInstruction.Operand is null)
{
CilInstruction newInstruction;
switch (bodyInstruction.OpCode.Code)
{
case CilCode.Ldlen:
//This is Il2CppArrayBase.Length
return false;
newInstruction = targetBuilder.Add(OpCodes.Callvirt,
imports.Module.DefaultImporter.ImportMethod(imports.Il2CppArrayBase_get_Length.Value));
break;

case CilCode.Ldelema:
//This is Il2CppArrayBase<T>.Pointer + index * sizeof(T) but the T is not known because the operand is null.
Expand Down Expand Up @@ -78,14 +81,18 @@ public static bool TranslateMethod(MethodDefinition original, MethodDefinition t

case >= CilCode.Ldind_I1 and <= CilCode.Ldind_Ref:
//This is for by ref parameters
break;
goto default;

case >= CilCode.Stind_Ref and <= CilCode.Stind_R8:
//This is for by ref parameters
goto default;

default:
//Noop, ldnull, ldarg_0, mul, add, etc.
newInstruction = targetBuilder.Add(bodyInstruction.OpCode);
break;
}

var newInstruction = targetBuilder.Add(bodyInstruction.OpCode);
instructionMap.Add(bodyInstruction, newInstruction);
}
else if (bodyInstruction.OpCode.OperandType == CilOperandType.InlineField)
Expand Down

0 comments on commit 5e58d9e

Please sign in to comment.