Skip to content

Commit c1482db

Browse files
committed
Revert "Add proper UTF-8 support (BepInEx#228)"
This reverts commit 0271706
1 parent dd0a7c3 commit c1482db

File tree

8 files changed

+70
-85
lines changed

8 files changed

+70
-85
lines changed

Il2CppInterop.Runtime/IL2CPP.cs

Lines changed: 44 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static IL2CPP()
3434
for (var i = 0; i < assembliesCount; i++)
3535
{
3636
var image = il2cpp_assembly_get_image(assemblies[i]);
37-
var name = il2cpp_image_get_name_(image)!;
37+
var name = il2cpp_image_get_name(image);
3838
ourImagesMap[name] = image;
3939
}
4040
}
@@ -69,7 +69,7 @@ public static IntPtr GetIl2CppField(IntPtr clazz, string fieldName)
6969
var field = il2cpp_class_get_field_from_name(clazz, fieldName);
7070
if (field == IntPtr.Zero)
7171
Logger.Instance.LogError(
72-
"Field {FieldName} was not found on class {ClassName}", fieldName, il2cpp_class_get_name_(clazz));
72+
"Field {FieldName} was not found on class {ClassName}", fieldName, il2cpp_class_get_name(clazz));
7373
return field;
7474
}
7575

@@ -84,7 +84,7 @@ public static IntPtr GetIl2CppMethodByToken(IntPtr clazz, int token)
8484
if (il2cpp_method_get_token(method) == token)
8585
return method;
8686

87-
var className = il2cpp_class_get_name_(clazz);
87+
var className = il2cpp_class_get_name(clazz);
8888
Logger.Instance.LogTrace("Unable to find method {ClassName}::{Token}", className, token);
8989

9090
return NativeStructUtils.GetMethodInfoForMissingMethod(className + "::" + token);
@@ -110,7 +110,7 @@ public static IntPtr GetIl2CppMethod(IntPtr clazz, bool isGeneric, string method
110110
IntPtr method;
111111
while ((method = il2cpp_class_get_methods(clazz, ref iter)) != IntPtr.Zero)
112112
{
113-
if (il2cpp_method_get_name_(method) != methodName)
113+
if (il2cpp_method_get_name(method) != methodName)
114114
continue;
115115

116116
if (il2cpp_method_get_param_count(method) != argTypes.Length)
@@ -120,7 +120,7 @@ public static IntPtr GetIl2CppMethod(IntPtr clazz, bool isGeneric, string method
120120
continue;
121121

122122
var returnType = il2cpp_method_get_return_type(method);
123-
var returnTypeNameActual = il2cpp_type_get_name_(returnType);
123+
var returnTypeNameActual = il2cpp_type_get_name(returnType);
124124
if (returnTypeNameActual != returnTypeName)
125125
continue;
126126

@@ -131,7 +131,7 @@ public static IntPtr GetIl2CppMethod(IntPtr clazz, bool isGeneric, string method
131131
for (var i = 0; i < argTypes.Length; i++)
132132
{
133133
var paramType = il2cpp_method_get_param(method, (uint)i);
134-
var typeName = il2cpp_type_get_name_(paramType);
134+
var typeName = il2cpp_type_get_name(paramType);
135135
if (typeName != argTypes[i])
136136
{
137137
badType = true;
@@ -144,19 +144,19 @@ public static IntPtr GetIl2CppMethod(IntPtr clazz, bool isGeneric, string method
144144
return method;
145145
}
146146

147-
var className = il2cpp_class_get_name_(clazz);
147+
var className = il2cpp_class_get_name(clazz);
148148

149149
if (methodsSeen == 1)
150150
{
151151
Logger.Instance.LogTrace(
152152
"Method {ClassName}::{MethodName} was stubbed with a random matching method of the same name", className, methodName);
153153
Logger.Instance.LogTrace(
154-
"Stubby return type/target: {LastMethod} / {ReturnTypeName}", il2cpp_type_get_name_(il2cpp_method_get_return_type(lastMethod)), returnTypeName);
154+
"Stubby return type/target: {LastMethod} / {ReturnTypeName}", il2cpp_type_get_name(il2cpp_method_get_return_type(lastMethod)), returnTypeName);
155155
Logger.Instance.LogTrace("Stubby parameter types/targets follow:");
156156
for (var i = 0; i < argTypes.Length; i++)
157157
{
158158
var paramType = il2cpp_method_get_param(lastMethod, (uint)i);
159-
var typeName = il2cpp_type_get_name_(paramType);
159+
var typeName = il2cpp_type_get_name(paramType);
160160
Logger.Instance.LogTrace(" {TypeName} / {ArgType}", typeName, argTypes[i]);
161161
}
162162

@@ -171,17 +171,17 @@ public static IntPtr GetIl2CppMethod(IntPtr clazz, bool isGeneric, string method
171171
iter = IntPtr.Zero;
172172
while ((method = il2cpp_class_get_methods(clazz, ref iter)) != IntPtr.Zero)
173173
{
174-
if (il2cpp_method_get_name_(method) != methodName)
174+
if (il2cpp_method_get_name(method) != methodName)
175175
continue;
176176

177177
var nParams = il2cpp_method_get_param_count(method);
178178
Logger.Instance.LogTrace("Method starts");
179179
Logger.Instance.LogTrace(
180-
" return {MethodTypeName}", il2cpp_type_get_name_(il2cpp_method_get_return_type(method)));
180+
" return {MethodTypeName}", il2cpp_type_get_name(il2cpp_method_get_return_type(method)));
181181
for (var i = 0; i < nParams; i++)
182182
{
183183
var paramType = il2cpp_method_get_param(method, (uint)i);
184-
var typeName = il2cpp_type_get_name_(paramType);
184+
var typeName = il2cpp_type_get_name(paramType);
185185
Logger.Instance.LogTrace(" {TypeName}", typeName);
186186
}
187187

@@ -236,13 +236,18 @@ public static IntPtr GetIl2CppNestedType(IntPtr enclosingType, string nestedType
236236
}
237237

238238
while ((nestedTypePtr = il2cpp_class_get_nested_types(enclosingType, ref iter)) != IntPtr.Zero)
239-
if (il2cpp_class_get_name_(nestedTypePtr) == nestedTypeName)
239+
if (il2cpp_class_get_name(nestedTypePtr) == nestedTypeName)
240240
return nestedTypePtr;
241241

242-
Logger.Instance.LogError(
243-
"Nested type {NestedTypeName} on {EnclosingTypeName} not found!", nestedTypeName, il2cpp_class_get_name_(enclosingType));
242+
Logger.Instance.LogTrace("Failed to find nested type through enumeration, falling back to reflection");
244243

245-
return IntPtr.Zero;
244+
var result = RuntimeReflectionHelper.GetNestedTypeViaReflection(enclosingType, nestedTypeName);
245+
246+
if (result == IntPtr.Zero)
247+
Logger.Instance.LogError(
248+
"Nested type {NestedTypeName} on {EnclosingTypeName} not found!", nestedTypeName, il2cpp_class_get_name(enclosingType));
249+
250+
return result;
246251
}
247252

248253
public static void ThrowIfNull(object arg)
@@ -513,16 +518,12 @@ public static extern IntPtr il2cpp_class_get_method_from_name(IntPtr klass,
513518
[MarshalAs(UnmanagedType.LPUTF8Str)] string name, int argsCount);
514519

515520
[DllImport("libil2cpp.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
516-
public static extern nint il2cpp_class_get_name(IntPtr klass);
517-
518-
public static string? il2cpp_class_get_name_(IntPtr klass)
519-
=> Marshal.PtrToStringUTF8(il2cpp_class_get_name(klass));
521+
[return: MarshalAs(UnmanagedType.LPUTF8Str)]
522+
public static extern string il2cpp_class_get_name(IntPtr klass);
520523

521524
[DllImport("libil2cpp.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
522-
public static extern nint il2cpp_class_get_namespace(IntPtr klass);
523-
524-
public static string? il2cpp_class_get_namespace_(IntPtr klass)
525-
=> Marshal.PtrToStringUTF8(il2cpp_class_get_namespace(klass));
525+
[return: MarshalAs(UnmanagedType.LPUTF8Str)]
526+
public static extern string il2cpp_class_get_namespace(IntPtr klass);
526527

527528
[DllImport("libil2cpp.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
528529
public static extern IntPtr il2cpp_class_get_parent(IntPtr klass);
@@ -585,11 +586,9 @@ public static extern IntPtr il2cpp_class_get_method_from_name(IntPtr klass,
585586
[DllImport("libil2cpp.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
586587
public static extern IntPtr il2cpp_class_get_image(IntPtr klass);
587588

588-
[DllImport("libil2cpp.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
589-
public static extern nint il2cpp_class_get_assemblyname(IntPtr klass);
590-
591-
public static string? il2cpp_class_get_assemblyname_(IntPtr klass)
592-
=> Marshal.PtrToStringUTF8(il2cpp_class_get_assemblyname(klass));
589+
[DllImport("GameAssembly", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
590+
[return: MarshalAs(UnmanagedType.LPUTF8Str)]
591+
public static extern string il2cpp_class_get_assemblyname(IntPtr klass);
593592

594593
[DllImport("libil2cpp.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
595594
public static extern int il2cpp_class_get_rank(IntPtr klass);
@@ -635,10 +634,8 @@ public static extern IntPtr
635634
public static extern int il2cpp_field_get_flags(IntPtr field);
636635

637636
[DllImport("libil2cpp.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
638-
public static extern nint il2cpp_field_get_name(IntPtr field);
639-
640-
public static string? il2cpp_field_get_name_(IntPtr field)
641-
=> Marshal.PtrToStringUTF8(il2cpp_field_get_name(field));
637+
[return: MarshalAs(UnmanagedType.LPUTF8Str)]
638+
public static extern string il2cpp_field_get_name(IntPtr field);
642639

643640
[DllImport("libil2cpp.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
644641
public static extern IntPtr il2cpp_field_get_parent(IntPtr field);
@@ -728,11 +725,9 @@ public static extern IntPtr il2cpp_unity_liveness_calculation_begin(IntPtr filte
728725
[DllImport("libil2cpp.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
729726
public static extern IntPtr il2cpp_method_get_declaring_type(IntPtr method);
730727

731-
[DllImport("libil2cpp.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
732-
public static extern nint il2cpp_method_get_name(IntPtr method);
733-
734-
public static string? il2cpp_method_get_name_(IntPtr method)
735-
=> Marshal.PtrToStringUTF8(il2cpp_method_get_name(method));
728+
[DllImport("GameAssembly", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
729+
[return: MarshalAs(UnmanagedType.LPUTF8Str)]
730+
public static extern string il2cpp_method_get_name(IntPtr method);
736731

737732
[MethodImpl(MethodImplOptions.AggressiveInlining)]
738733
public static IntPtr il2cpp_method_get_from_reflection(IntPtr method)
@@ -780,10 +775,8 @@ public static IntPtr il2cpp_method_get_from_reflection(IntPtr method)
780775
public static extern uint il2cpp_method_get_token(IntPtr method);
781776

782777
[DllImport("libil2cpp.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
783-
public static extern nint il2cpp_method_get_param_name(IntPtr method, uint index);
784-
785-
public static string? il2cpp_method_get_param_name_(IntPtr method, uint index)
786-
=> Marshal.PtrToStringUTF8(il2cpp_method_get_param_name(method, index));
778+
[return: MarshalAs(UnmanagedType.LPUTF8Str)]
779+
public static extern string il2cpp_method_get_param_name(IntPtr method, uint index);
787780

788781
[DllImport("libil2cpp.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
789782
public static extern void il2cpp_profiler_install(IntPtr prof, IntPtr shutdown_callback);
@@ -815,10 +808,8 @@ public static IntPtr il2cpp_method_get_from_reflection(IntPtr method)
815808
public static extern IntPtr il2cpp_property_get_set_method(IntPtr prop);
816809

817810
[DllImport("libil2cpp.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
818-
public static extern nint il2cpp_property_get_name(IntPtr prop);
819-
820-
public static string? il2cpp_property_get_name_(IntPtr prop)
821-
=> Marshal.PtrToStringUTF8(il2cpp_property_get_name(prop));
811+
[return: MarshalAs(UnmanagedType.LPUTF8Str)]
812+
public static extern string il2cpp_property_get_name(IntPtr prop);
822813

823814
[DllImport("libil2cpp.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
824815
public static extern IntPtr il2cpp_property_get_parent(IntPtr prop);
@@ -960,11 +951,9 @@ public static extern IntPtr il2cpp_runtime_invoke_convert_args(IntPtr method, In
960951
[DllImport("libil2cpp.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
961952
public static extern IntPtr il2cpp_type_get_class_or_element_class(IntPtr type);
962953

963-
[DllImport("libil2cpp.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
964-
public static extern nint il2cpp_type_get_name(IntPtr type);
965-
966-
public static string? il2cpp_type_get_name_(IntPtr type)
967-
=> Marshal.PtrToStringUTF8(il2cpp_type_get_name(type));
954+
[DllImport("GameAssembly", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
955+
[return: MarshalAs(UnmanagedType.LPUTF8Str)]
956+
public static extern string il2cpp_type_get_name(IntPtr type);
968957

969958
[DllImport("libil2cpp.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
970959
[return: MarshalAs(UnmanagedType.I1)]
@@ -984,16 +973,12 @@ public static extern IntPtr il2cpp_runtime_invoke_convert_args(IntPtr method, In
984973
public static extern IntPtr il2cpp_image_get_assembly(IntPtr image);
985974

986975
[DllImport("libil2cpp.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
987-
public static extern nint il2cpp_image_get_name(IntPtr image);
988-
989-
public static string? il2cpp_image_get_name_(IntPtr image)
990-
=> Marshal.PtrToStringUTF8(il2cpp_image_get_name(image));
976+
[return: MarshalAs(UnmanagedType.LPUTF8Str)]
977+
public static extern string il2cpp_image_get_name(IntPtr image);
991978

992979
[DllImport("libil2cpp.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
993-
public static extern nint il2cpp_image_get_filename(IntPtr image);
994-
995-
public static string? il2cpp_image_get_filename_(IntPtr image)
996-
=> Marshal.PtrToStringUTF8(il2cpp_image_get_filename(image));
980+
[return: MarshalAs(UnmanagedType.LPUTF8Str)]
981+
public static extern string il2cpp_image_get_filename(IntPtr image);
997982

998983
[DllImport("libil2cpp.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
999984
public static extern IntPtr il2cpp_image_get_entry_point(IntPtr image);

Il2CppInterop.Runtime/Injection/ClassInjector.Debug.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ private static string ToString(Il2CppClass* il2CppClass)
2323
private static string ToString(Il2CppTypeStruct* il2CppType)
2424
{
2525
if (il2CppType == default) return "null";
26-
return IL2CPP.il2cpp_type_get_name_((IntPtr)il2CppType)!;
26+
return IL2CPP.il2cpp_type_get_name((IntPtr)il2CppType);
2727
}
2828

2929
public static void Dump(Il2CppClass* il2CppClass)

Il2CppInterop.Runtime/Injection/ClassInjector.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ public static void RegisterTypeInIl2Cpp(Type type, RegisterTypeOptions options)
235235
classPointer.HasFinalize = true;
236236
classPointer.IsVtableInitialized = true;
237237

238-
classPointer.Name = Marshal.StringToCoTaskMemUTF8(type.Name);
239-
classPointer.Namespace = Marshal.StringToCoTaskMemUTF8(type.Namespace ?? string.Empty);
238+
classPointer.Name = Marshal.StringToHGlobalAnsi(type.Name);
239+
classPointer.Namespace = Marshal.StringToHGlobalAnsi(type.Namespace ?? string.Empty);
240240

241241
classPointer.ThisArg.Type = classPointer.ByValArg.Type = Il2CppTypeEnum.IL2CPP_TYPE_CLASS;
242242
classPointer.ThisArg.ByRef = true;
@@ -257,7 +257,7 @@ public static void RegisterTypeInIl2Cpp(Type type, RegisterTypeOptions options)
257257
for (var i = 0; i < classPointer.FieldCount; i++)
258258
{
259259
var fieldInfo = UnityVersionHandler.Wrap(il2cppFields + i * UnityVersionHandler.FieldInfoSize());
260-
fieldInfo.Name = Marshal.StringToCoTaskMemUTF8(fieldsToInject[i].Name);
260+
fieldInfo.Name = Marshal.StringToHGlobalAnsi(fieldsToInject[i].Name);
261261
fieldInfo.Parent = classPointer.ClassPointer;
262262
fieldInfo.Offset = fieldOffset;
263263

@@ -347,7 +347,7 @@ static void FindAbstractMethods(List<INativeMethodInfoStruct> list, INativeClass
347347
for (var i = 0; i < klass.MethodCount; i++)
348348
{
349349
var baseMethod = UnityVersionHandler.Wrap(klass.Methods[i]);
350-
var name = Marshal.PtrToStringUTF8(baseMethod.Name)!;
350+
var name = Marshal.PtrToStringAnsi(baseMethod.Name)!;
351351

352352
if (baseMethod.Flags.HasFlag(Il2CppMethodFlags.METHOD_ATTRIBUTE_ABSTRACT))
353353
{
@@ -357,7 +357,7 @@ static void FindAbstractMethods(List<INativeMethodInfoStruct> list, INativeClass
357357
{
358358
var existing = list.SingleOrDefault(m =>
359359
{
360-
if (Marshal.PtrToStringUTF8(m.Name) != name) return false;
360+
if (Marshal.PtrToStringAnsi(m.Name) != name) return false;
361361
if (m.ParametersCount != baseMethod.ParametersCount) return false;
362362
if (GetIl2CppTypeFullName(m.ReturnType) != GetIl2CppTypeFullName(baseMethod.ReturnType)) return false;
363363

@@ -416,7 +416,7 @@ INativeMethodInfoStruct HandleAbstractMethod(int position)
416416
baseMethod = HandleAbstractMethod(i);
417417
}
418418

419-
var methodName = Marshal.PtrToStringUTF8(baseMethod.Name);
419+
var methodName = Marshal.PtrToStringAnsi(baseMethod.Name);
420420

421421
if (methodName == "Finalize") // slot number is not static
422422
{
@@ -465,7 +465,7 @@ INativeMethodInfoStruct HandleAbstractMethod(int position)
465465
for (var j = 0; j < interfaces[i].MethodCount; j++)
466466
{
467467
var vTableMethod = UnityVersionHandler.Wrap(interfaces[i].Methods[j]);
468-
var methodName = Marshal.PtrToStringUTF8(vTableMethod.Name);
468+
var methodName = Marshal.PtrToStringAnsi(vTableMethod.Name);
469469
if (!infos.TryGetValue((methodName, vTableMethod.ParametersCount, vTableMethod.IsGeneric),
470470
out var methodIndex))
471471
{
@@ -598,7 +598,7 @@ private static bool IsMethodEligible(MethodInfo method)
598598
INativeClassStruct declaringClass)
599599
{
600600
var converted = UnityVersionHandler.NewMethod();
601-
converted.Name = Marshal.StringToCoTaskMemUTF8(methodName);
601+
converted.Name = Marshal.StringToHGlobalAnsi(methodName);
602602
converted.Class = declaringClass.ClassPointer;
603603

604604
Delegate invoker;
@@ -630,7 +630,7 @@ private static bool IsMethodEligible(MethodInfo method)
630630
internal static Il2CppMethodInfo* ConvertMethodInfo(MethodInfo monoMethod, INativeClassStruct declaringClass)
631631
{
632632
var converted = UnityVersionHandler.NewMethod();
633-
converted.Name = Marshal.StringToCoTaskMemUTF8(monoMethod.Name);
633+
converted.Name = Marshal.StringToHGlobalAnsi(monoMethod.Name);
634634
converted.Class = declaringClass.ClassPointer;
635635

636636
var parameters = monoMethod.GetParameters();
@@ -645,7 +645,7 @@ private static bool IsMethodEligible(MethodInfo method)
645645
var param = UnityVersionHandler.Wrap(paramsArray[i]);
646646
if (UnityVersionHandler.ParameterInfoHasNamePosToken())
647647
{
648-
param.Name = Marshal.StringToCoTaskMemUTF8(parameterInfo.Name);
648+
param.Name = Marshal.StringToHGlobalAnsi(parameterInfo.Name);
649649
param.Position = i;
650650
param.Token = 0;
651651
}
@@ -1118,7 +1118,7 @@ private static string GetIl2CppTypeFullName(Il2CppTypeStruct* typePointer)
11181118

11191119
var fullName = new StringBuilder();
11201120

1121-
var namespaceName = Marshal.PtrToStringUTF8(klass.Namespace);
1121+
var namespaceName = Marshal.PtrToStringAnsi(klass.Namespace);
11221122
if (!string.IsNullOrEmpty(namespaceName))
11231123
{
11241124
fullName.Append(namespaceName);
@@ -1128,13 +1128,13 @@ private static string GetIl2CppTypeFullName(Il2CppTypeStruct* typePointer)
11281128
var declaringType = klass;
11291129
while ((declaringType = UnityVersionHandler.Wrap(declaringType.DeclaringType)) != default)
11301130
{
1131-
fullName.Append(Marshal.PtrToStringUTF8(declaringType.Name));
1131+
fullName.Append(Marshal.PtrToStringAnsi(declaringType.Name));
11321132
fullName.Append('+');
11331133
}
11341134

1135-
fullName.Append(Marshal.PtrToStringUTF8(klass.Name));
1135+
fullName.Append(Marshal.PtrToStringAnsi(klass.Name));
11361136

1137-
var assemblyName = Marshal.PtrToStringUTF8(assembly.Name.Name);
1137+
var assemblyName = Marshal.PtrToStringAnsi(assembly.Name.Name);
11381138
if (assemblyName != "mscorlib")
11391139
{
11401140
fullName.Append(", ");

0 commit comments

Comments
 (0)