Skip to content

Commit af34228

Browse files
committed
Improve unstripping support for newarr:
* Fail for struct target types * Use Il2CppStringArray for System.String
1 parent d145384 commit af34228

File tree

1 file changed

+53
-41
lines changed

1 file changed

+53
-41
lines changed

Il2CppInterop.Generator/Utils/UnstripTranslator.cs

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -41,47 +41,47 @@ public static bool TranslateMethod(MethodDefinition original, MethodDefinition t
4141
{
4242
if (bodyInstruction.Operand is null)
4343
{
44-
switch (bodyInstruction.OpCode.Code)
44+
switch (bodyInstruction.OpCode.Code)
4545
{
46-
case CilCode.Ldlen:
47-
//This is Il2CppArrayBase.Length
48-
return false;
49-
50-
case CilCode.Ldelema:
51-
//This is Il2CppArrayBase<T>.Pointer + index * sizeof(T) but the T is not known because the operand is null.
52-
return false;
53-
54-
case CilCode.Ldelem:
55-
//This is Il2CppArrayBase<T>.set_Item but the T is not known because the operand is null.
56-
return false;
57-
58-
case CilCode.Stelem:
59-
//This is Il2CppArrayBase<T>.set_Item but the T is not known because the operand is null.
60-
return false;
61-
62-
case CilCode.Ldelem_Ref:
63-
//This is Il2CppReferenceArray<T>.get_Item but the T is not known because the operand is null.
64-
return false;
65-
66-
case CilCode.Stelem_Ref:
67-
//This is Il2CppReferenceArray<T>.set_Item but the T is not known because the operand is null.
68-
return false;
69-
70-
case >= CilCode.Ldelem_I1 and <= CilCode.Ldelem_R8:
71-
//This is Il2CppStructArray<T>.get_Item
72-
return false;
73-
74-
case >= CilCode.Stelem_I and <= CilCode.Stelem_R8:
75-
//This is Il2CppStructArray<T>.set_Item
76-
return false;
77-
78-
case >= CilCode.Ldind_I1 and <= CilCode.Ldind_Ref:
79-
//This is for by ref parameters
80-
break;
81-
82-
case >= CilCode.Stind_Ref and <= CilCode.Stind_R8:
83-
//This is for by ref parameters
84-
break;
46+
case CilCode.Ldlen:
47+
//This is Il2CppArrayBase.Length
48+
return false;
49+
50+
case CilCode.Ldelema:
51+
//This is Il2CppArrayBase<T>.Pointer + index * sizeof(T) but the T is not known because the operand is null.
52+
return false;
53+
54+
case CilCode.Ldelem:
55+
//This is Il2CppArrayBase<T>.set_Item but the T is not known because the operand is null.
56+
return false;
57+
58+
case CilCode.Stelem:
59+
//This is Il2CppArrayBase<T>.set_Item but the T is not known because the operand is null.
60+
return false;
61+
62+
case CilCode.Ldelem_Ref:
63+
//This is Il2CppReferenceArray<T>.get_Item but the T is not known because the operand is null.
64+
return false;
65+
66+
case CilCode.Stelem_Ref:
67+
//This is Il2CppReferenceArray<T>.set_Item but the T is not known because the operand is null.
68+
return false;
69+
70+
case >= CilCode.Ldelem_I1 and <= CilCode.Ldelem_R8:
71+
//This is Il2CppStructArray<T>.get_Item
72+
return false;
73+
74+
case >= CilCode.Stelem_I and <= CilCode.Stelem_R8:
75+
//This is Il2CppStructArray<T>.set_Item
76+
return false;
77+
78+
case >= CilCode.Ldind_I1 and <= CilCode.Ldind_Ref:
79+
//This is for by ref parameters
80+
break;
81+
82+
case >= CilCode.Stind_Ref and <= CilCode.Stind_R8:
83+
//This is for by ref parameters
84+
break;
8585
}
8686

8787
var newInstruction = targetBuilder.Add(bodyInstruction.OpCode);
@@ -198,7 +198,19 @@ public static bool TranslateMethod(MethodDefinition original, MethodDefinition t
198198
{
199199
var newInstruction = targetBuilder.Add(OpCodes.Conv_I8);
200200

201-
var il2cppTypeArray = imports.Il2CppReferenceArray.MakeGenericInstanceType(targetType).ToTypeDefOrRef();
201+
ITypeDefOrRef il2cppTypeArray;
202+
if (targetType.IsValueType)
203+
{
204+
return false;
205+
}
206+
else if (targetType.FullName == "System.String")
207+
{
208+
il2cppTypeArray = imports.Il2CppStringArray.ToTypeDefOrRef();
209+
}
210+
else
211+
{
212+
il2cppTypeArray = imports.Il2CppReferenceArray.MakeGenericInstanceType(targetType).ToTypeDefOrRef();
213+
}
202214
targetBuilder.Add(OpCodes.Newobj, imports.Module.DefaultImporter.ImportMethod(
203215
CecilAdapter.CreateInstanceMethodReference(".ctor", imports.Module.Void(), il2cppTypeArray, imports.Module.Long())));
204216
instructionMap.Add(bodyInstruction, newInstruction);

0 commit comments

Comments
 (0)