Skip to content

Commit

Permalink
more utf8 string shenanigans
Browse files Browse the repository at this point in the history
  • Loading branch information
HookedBehemoth committed Oct 25, 2023
1 parent a55a05d commit 62f44fd
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 35 deletions.
60 changes: 35 additions & 25 deletions Il2CppInterop.Runtime/IL2CPP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static IL2CPP()
for (var i = 0; i < assembliesCount; i++)
{
var image = il2cpp_assembly_get_image(assemblies[i]);
var name = Marshal.PtrToStringAnsi(il2cpp_image_get_name(image));
var name = il2cpp_image_get_name(image);
ourImagesMap[name] = image;
}
}
Expand Down Expand Up @@ -69,7 +69,7 @@ public static IntPtr GetIl2CppField(IntPtr clazz, string fieldName)
var field = il2cpp_class_get_field_from_name(clazz, fieldName);
if (field == IntPtr.Zero)
Logger.Instance.LogError(
"Field {FieldName} was not found on class {ClassName}", fieldName, Marshal.PtrToStringUTF8(il2cpp_class_get_name(clazz)));
"Field {FieldName} was not found on class {ClassName}", fieldName, il2cpp_class_get_name(clazz));
return field;
}

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

var className = Marshal.PtrToStringAnsi(il2cpp_class_get_name(clazz));
var className = il2cpp_class_get_name(clazz);
Logger.Instance.LogTrace("Unable to find method {ClassName}::{Token}", className, token);

return NativeStructUtils.GetMethodInfoForMissingMethod(className + "::" + token);
Expand All @@ -110,7 +110,7 @@ public static IntPtr GetIl2CppMethod(IntPtr clazz, bool isGeneric, string method
IntPtr method;
while ((method = il2cpp_class_get_methods(clazz, ref iter)) != IntPtr.Zero)
{
if (Marshal.PtrToStringAnsi(il2cpp_method_get_name(method)) != methodName)
if (il2cpp_method_get_name(method) != methodName)
continue;

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

var returnType = il2cpp_method_get_return_type(method);
var returnTypeNameActual = Marshal.PtrToStringAnsi(il2cpp_type_get_name(returnType));
var returnTypeNameActual = il2cpp_type_get_name(returnType);
if (returnTypeNameActual != returnTypeName)
continue;

Expand All @@ -131,7 +131,7 @@ public static IntPtr GetIl2CppMethod(IntPtr clazz, bool isGeneric, string method
for (var i = 0; i < argTypes.Length; i++)
{
var paramType = il2cpp_method_get_param(method, (uint)i);
var typeName = Marshal.PtrToStringAnsi(il2cpp_type_get_name(paramType));
var typeName = il2cpp_type_get_name(paramType);
if (typeName != argTypes[i])
{
badType = true;
Expand All @@ -144,19 +144,19 @@ public static IntPtr GetIl2CppMethod(IntPtr clazz, bool isGeneric, string method
return method;
}

var className = Marshal.PtrToStringAnsi(il2cpp_class_get_name(clazz));
var className = il2cpp_class_get_name(clazz);

if (methodsSeen == 1)
{
Logger.Instance.LogTrace(
"Method {ClassName}::{MethodName} was stubbed with a random matching method of the same name", className, methodName);
Logger.Instance.LogTrace(
"Stubby return type/target: {LastMethod} / {ReturnTypeName}", Marshal.PtrToStringUTF8(il2cpp_type_get_name(il2cpp_method_get_return_type(lastMethod))), returnTypeName);
"Stubby return type/target: {LastMethod} / {ReturnTypeName}", il2cpp_type_get_name(il2cpp_method_get_return_type(lastMethod)), returnTypeName);
Logger.Instance.LogTrace("Stubby parameter types/targets follow:");
for (var i = 0; i < argTypes.Length; i++)
{
var paramType = il2cpp_method_get_param(lastMethod, (uint)i);
var typeName = Marshal.PtrToStringAnsi(il2cpp_type_get_name(paramType));
var typeName = il2cpp_type_get_name(paramType);
Logger.Instance.LogTrace(" {TypeName} / {ArgType}", typeName, argTypes[i]);
}

Expand All @@ -171,17 +171,17 @@ public static IntPtr GetIl2CppMethod(IntPtr clazz, bool isGeneric, string method
iter = IntPtr.Zero;
while ((method = il2cpp_class_get_methods(clazz, ref iter)) != IntPtr.Zero)
{
if (Marshal.PtrToStringAnsi(il2cpp_method_get_name(method)) != methodName)
if (il2cpp_method_get_name(method) != methodName)
continue;

var nParams = il2cpp_method_get_param_count(method);
Logger.Instance.LogTrace("Method starts");
Logger.Instance.LogTrace(
" return {MethodTypeName}", Marshal.PtrToStringUTF8(il2cpp_type_get_name(il2cpp_method_get_return_type(method))));
" return {MethodTypeName}", il2cpp_type_get_name(il2cpp_method_get_return_type(method)));
for (var i = 0; i < nParams; i++)
{
var paramType = il2cpp_method_get_param(method, (uint)i);
var typeName = Marshal.PtrToStringAnsi(il2cpp_type_get_name(paramType));
var typeName = il2cpp_type_get_name(paramType);
Logger.Instance.LogTrace(" {TypeName}", typeName);
}

Expand Down Expand Up @@ -236,11 +236,11 @@ public static IntPtr GetIl2CppNestedType(IntPtr enclosingType, string nestedType
}

while ((nestedTypePtr = il2cpp_class_get_nested_types(enclosingType, ref iter)) != IntPtr.Zero)
if (Marshal.PtrToStringUTF8(il2cpp_class_get_name(nestedTypePtr)) == nestedTypeName)
if (il2cpp_class_get_name(nestedTypePtr) == nestedTypeName)
return nestedTypePtr;

Logger.Instance.LogError(
"Nested type {NestedTypeName} on {EnclosingTypeName} not found!", nestedTypeName, Marshal.PtrToStringUTF8(il2cpp_class_get_name(enclosingType)));
"Nested type {NestedTypeName} on {EnclosingTypeName} not found!", nestedTypeName, il2cpp_class_get_name(enclosingType));

return IntPtr.Zero;
}
Expand Down Expand Up @@ -510,13 +510,15 @@ public static extern IntPtr il2cpp_class_get_field_from_name(IntPtr klass,

[DllImport("GameAssembly", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr il2cpp_class_get_method_from_name(IntPtr klass,
[MarshalAs(UnmanagedType.LPStr)] string name, int argsCount);
[MarshalAs(UnmanagedType.LPUTF8Str)] string name, int argsCount);

[DllImport("GameAssembly", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr il2cpp_class_get_name(IntPtr klass);
[return: MarshalAs(UnmanagedType.LPUTF8Str)]
public static extern string il2cpp_class_get_name(IntPtr klass);

[DllImport("GameAssembly", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr il2cpp_class_get_namespace(IntPtr klass);
[return: MarshalAs(UnmanagedType.LPUTF8Str)]
public static extern string il2cpp_class_get_namespace(IntPtr klass);

[DllImport("GameAssembly", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr il2cpp_class_get_parent(IntPtr klass);
Expand Down Expand Up @@ -580,7 +582,8 @@ public static extern IntPtr il2cpp_class_get_method_from_name(IntPtr klass,
public static extern IntPtr il2cpp_class_get_image(IntPtr klass);

[DllImport("GameAssembly", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr il2cpp_class_get_assemblyname(IntPtr klass);
[return: MarshalAs(UnmanagedType.LPUTF8Str)]
public static extern string il2cpp_class_get_assemblyname(IntPtr klass);

[DllImport("GameAssembly", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern int il2cpp_class_get_rank(IntPtr klass);
Expand Down Expand Up @@ -626,7 +629,8 @@ public static extern IntPtr
public static extern int il2cpp_field_get_flags(IntPtr field);

[DllImport("GameAssembly", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr il2cpp_field_get_name(IntPtr field);
[return: MarshalAs(UnmanagedType.LPUTF8Str)]
public static extern string il2cpp_field_get_name(IntPtr field);

[DllImport("GameAssembly", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr il2cpp_field_get_parent(IntPtr field);
Expand Down Expand Up @@ -717,7 +721,8 @@ public static extern IntPtr il2cpp_unity_liveness_calculation_begin(IntPtr filte
public static extern IntPtr il2cpp_method_get_declaring_type(IntPtr method);

[DllImport("GameAssembly", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr il2cpp_method_get_name(IntPtr method);
[return: MarshalAs(UnmanagedType.LPUTF8Str)]
public static extern string il2cpp_method_get_name(IntPtr method);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IntPtr il2cpp_method_get_from_reflection(IntPtr method)
Expand Down Expand Up @@ -765,7 +770,8 @@ public static IntPtr il2cpp_method_get_from_reflection(IntPtr method)
public static extern uint il2cpp_method_get_token(IntPtr method);

[DllImport("GameAssembly", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr il2cpp_method_get_param_name(IntPtr method, uint index);
[return: MarshalAs(UnmanagedType.LPUTF8Str)]
public static extern string il2cpp_method_get_param_name(IntPtr method, uint index);

[DllImport("GameAssembly", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern void il2cpp_profiler_install(IntPtr prof, IntPtr shutdown_callback);
Expand Down Expand Up @@ -797,7 +803,8 @@ public static IntPtr il2cpp_method_get_from_reflection(IntPtr method)
public static extern IntPtr il2cpp_property_get_set_method(IntPtr prop);

[DllImport("GameAssembly", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr il2cpp_property_get_name(IntPtr prop);
[return: MarshalAs(UnmanagedType.LPUTF8Str)]
public static extern string il2cpp_property_get_name(IntPtr prop);

[DllImport("GameAssembly", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr il2cpp_property_get_parent(IntPtr prop);
Expand Down Expand Up @@ -940,7 +947,8 @@ public static extern IntPtr il2cpp_runtime_invoke_convert_args(IntPtr method, In
public static extern IntPtr il2cpp_type_get_class_or_element_class(IntPtr type);

[DllImport("GameAssembly", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr il2cpp_type_get_name(IntPtr type);
[return: MarshalAs(UnmanagedType.LPUTF8Str)]
public static extern string il2cpp_type_get_name(IntPtr type);

[DllImport("GameAssembly", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
[return: MarshalAs(UnmanagedType.I1)]
Expand All @@ -960,10 +968,12 @@ public static extern IntPtr il2cpp_runtime_invoke_convert_args(IntPtr method, In
public static extern IntPtr il2cpp_image_get_assembly(IntPtr image);

[DllImport("GameAssembly", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr il2cpp_image_get_name(IntPtr image);
[return: MarshalAs(UnmanagedType.LPUTF8Str)]
public static extern string il2cpp_image_get_name(IntPtr image);

[DllImport("GameAssembly", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr il2cpp_image_get_filename(IntPtr image);
[return: MarshalAs(UnmanagedType.LPUTF8Str)]
public static extern string il2cpp_image_get_filename(IntPtr image);

[DllImport("GameAssembly", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr il2cpp_image_get_entry_point(IntPtr image);
Expand Down
2 changes: 1 addition & 1 deletion Il2CppInterop.Runtime/Injection/ClassInjector.Debug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private static string ToString(Il2CppClass* il2CppClass)
private static string ToString(Il2CppTypeStruct* il2CppType)
{
if (il2CppType == default) return "null";
return Marshal.PtrToStringAnsi(IL2CPP.il2cpp_type_get_name((IntPtr)il2CppType));
return IL2CPP.il2cpp_type_get_name((IntPtr)il2CppType);
}

public static void Dump(Il2CppClass* il2CppClass)
Expand Down
4 changes: 2 additions & 2 deletions Il2CppInterop.Runtime/InteropTypes/Il2CppObjectBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ internal void CreateGCHandle(IntPtr objHdl)
public T Cast<T>() where T : Il2CppObjectBase
{
return TryCast<T>() ?? throw new InvalidCastException(
$"Can't cast object of type {Marshal.PtrToStringAnsi(IL2CPP.il2cpp_class_get_name(IL2CPP.il2cpp_object_get_class(Pointer)))} to type {typeof(T)}");
$"Can't cast object of type {IL2CPP.il2cpp_class_get_name(IL2CPP.il2cpp_object_get_class(Pointer))} to type {typeof(T)}");
}

internal static unsafe T UnboxUnsafe<T>(IntPtr pointer)
Expand All @@ -71,7 +71,7 @@ internal static unsafe T UnboxUnsafe<T>(IntPtr pointer)
var ownClass = IL2CPP.il2cpp_object_get_class(pointer);
if (!IL2CPP.il2cpp_class_is_assignable_from(nestedTypeClassPointer, ownClass))
throw new InvalidCastException(
$"Can't cast object of type {Marshal.PtrToStringAnsi(IL2CPP.il2cpp_class_get_name(ownClass))} to type {typeof(T)}");
$"Can't cast object of type {IL2CPP.il2cpp_class_get_name(ownClass)} to type {typeof(T)}");

return Unsafe.AsRef<T>(IL2CPP.il2cpp_object_unbox(pointer).ToPointer());
}
Expand Down
14 changes: 7 additions & 7 deletions Il2CppInterop.Runtime/Runtime/Il2CppApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,12 @@ public static IntPtr il2cpp_class_get_method_from_name(IntPtr klass, [MarshalAs(
return IL2CPP.il2cpp_class_get_method_from_name(klass, name, argsCount);
}

public static IntPtr il2cpp_class_get_name(IntPtr klass)
public static string il2cpp_class_get_name(IntPtr klass)
{
return IL2CPP.il2cpp_class_get_name(klass);
}

public static IntPtr il2cpp_class_get_namespace(IntPtr klass)
public static string il2cpp_class_get_namespace(IntPtr klass)
{
return IL2CPP.il2cpp_class_get_namespace(klass);
}
Expand Down Expand Up @@ -357,7 +357,7 @@ public static IntPtr il2cpp_class_get_image(IntPtr klass)
return IL2CPP.il2cpp_class_get_image(klass);
}

public static IntPtr il2cpp_class_get_assemblyname(IntPtr klass)
public static string il2cpp_class_get_assemblyname(IntPtr klass)
{
return IL2CPP.il2cpp_class_get_assemblyname(klass);
}
Expand Down Expand Up @@ -629,12 +629,12 @@ public static IntPtr il2cpp_image_get_assembly(IntPtr image)
return IL2CPP.il2cpp_image_get_assembly(image);
}

public static IntPtr il2cpp_image_get_name(IntPtr image)
public static string il2cpp_image_get_name(IntPtr image)
{
return IL2CPP.il2cpp_image_get_name(image);
}

public static IntPtr il2cpp_image_get_filename(IntPtr image)
public static string il2cpp_image_get_filename(IntPtr image)
{
return IL2CPP.il2cpp_image_get_filename(image);
}
Expand Down Expand Up @@ -742,7 +742,7 @@ public static uint il2cpp_method_get_token(IntPtr method)
return UnityVersionHandler.Wrap((Il2CppMethodInfo*)method).Token;
}

public static IntPtr il2cpp_method_get_param_name(IntPtr method, uint index)
public static string il2cpp_method_get_param_name(IntPtr method, uint index)
{
return IL2CPP.il2cpp_method_get_param_name(method, index);
}
Expand Down Expand Up @@ -1050,7 +1050,7 @@ public static IntPtr il2cpp_type_get_class_or_element_class(IntPtr type)
return IL2CPP.il2cpp_type_get_class_or_element_class(type);
}

public static IntPtr il2cpp_type_get_name(IntPtr type)
public static string il2cpp_type_get_name(IntPtr type)
{
return IL2CPP.il2cpp_type_get_name(type);
}
Expand Down

0 comments on commit 62f44fd

Please sign in to comment.