Skip to content

Commit

Permalink
Function to get MonoReflectionMethod and unbox a MonoObject
Browse files Browse the repository at this point in the history
  • Loading branch information
PeaceBeUponYou committed May 13, 2023
1 parent d9c9dad commit f908fa8
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
38 changes: 38 additions & 0 deletions Cheat Engine/MonoDataCollector/MonoDataCollector/PipeServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ void CPipeServer::InitMono()
mono_type_get_name = (MONO_TYPE_GET_NAME)GetProcAddress(hMono, "il2cpp_type_get_name");
mono_type_get_type = (MONO_TYPE_GET_TYPE)GetProcAddress(hMono, "il2cpp_type_get_type");
il2cpp_type_get_object = (IL2CPP_TYPE_GET_OBJECT)GetProcAddress(hMono, "il2cpp_type_get_object");
il2cpp_method_get_object = (IL2CPP_METHOD_GET_OBJECT)GetProcAddress(hMono, "il2cpp_method_get_object");
mono_type_get_name_full = (MONO_TYPE_GET_NAME_FULL)GetProcAddress(hMono, "il2cpp_type_get_name_full");

mono_method_get_name = (MONO_METHOD_GET_NAME)GetProcAddress(hMono, "il2cpp_method_get_name");
Expand Down Expand Up @@ -663,6 +664,7 @@ void CPipeServer::InitMono()
mono_type_get_name = (MONO_TYPE_GET_NAME)GetProcAddress(hMono, "mono_type_get_name");
mono_type_get_type = (MONO_TYPE_GET_TYPE)GetProcAddress(hMono, "mono_type_get_type");
mono_type_get_object = (MONO_TYPE_GET_OBJECT)GetProcAddress(hMono, "mono_type_get_object");
mono_method_get_object = (MONO_METHOD_GET_OBJECT)GetProcAddress(hMono, "mono_method_get_object");
mono_type_get_name_full = (MONO_TYPE_GET_NAME_FULL)GetProcAddress(hMono, "mono_type_get_name_full");

mono_method_get_name = (MONO_METHOD_GET_NAME)GetProcAddress(hMono, "mono_method_get_name");
Expand Down Expand Up @@ -1849,6 +1851,35 @@ void CPipeServer::GetReflectionTypeOfClassType()
}
}

void CPipeServer::GetReflectionMethodOfMethod()
{
//returns MonoReflectionMethod* equavalent of MonoMethodInfo (C#)
void* method = (void*)ReadQword(); //MonoMethod*
void* klass = (void*)ReadQword(); //MonoClass*

try
{
if (il2cpp && il2cpp_method_get_object)
WriteQword((UINT64)il2cpp_method_get_object(method, klass));
else if (mono_method_get_object)
WriteQword((UINT64)mono_method_get_object(domain, method, klass));
else
WriteQword(0);
}
catch (...)
{
OutputDebugString("error at GetReflectionMethodOfMethod");
WriteQword(0);
}
}

void CPipeServer::UnBoxMonoObject()
{
void* object = (void*)ReadQword();
WriteQword(object ? (UINT64)mono_object_unbox(object) : 0);
OutputDebugString("Unbox Object Called");
}


void CPipeServer::GetVTableFromClass(void)
{
Expand Down Expand Up @@ -2988,7 +3019,14 @@ void CPipeServer::Start(void)
case MONOCMD_GETREFLECTIONTYPEOFCLASSTYPE:
GetReflectionTypeOfClassType();
break;

case MONOCMD_GETREFLECTIONMETHODOFMONOMETHOD:
GetReflectionMethodOfMethod();
break;

case MONOCMD_MONOOBJECTUNBOX:
UnBoxMonoObject();
break;

case MONOCMD_FREE:
FreeObject();
Expand Down
12 changes: 10 additions & 2 deletions Cheat Engine/MonoDataCollector/MonoDataCollector/PipeServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


//yyyymmdd
#define MONO_DATACOLLECTORVERSION 20230409
#define MONO_DATACOLLECTORVERSION 20230512

#define MONO_TYPE_NAME_FORMAT_IL 0
#define MONO_TYPE_NAME_FORMAT_REFLECTION 1
Expand Down Expand Up @@ -73,7 +73,9 @@
#define MONOCMD_GETCLASSTYPE 55
#define MONOCMD_GETCLASSOFTYPE 56
#define MONOCMD_GETTYPEOFMONOTYPE 57
#define MONOCMD_GETREFLECTIONTYPEOFCLASSTYPE 58
#define MONOCMD_GETREFLECTIONTYPEOFCLASSTYPE 58
#define MONOCMD_GETREFLECTIONMETHODOFMONOMETHOD 59
#define MONOCMD_MONOOBJECTUNBOX 60


typedef struct {} MonoType;
Expand Down Expand Up @@ -146,6 +148,8 @@ typedef void* (__cdecl* MONO_TYPE_GET_CLASS)(void* type);
typedef int (__cdecl *MONO_TYPE_GET_TYPE)(void *type);
typedef void* (__cdecl *MONO_TYPE_GET_OBJECT)(void *domain, void *type);
typedef void* (__cdecl *IL2CPP_TYPE_GET_OBJECT)(void *type);
typedef void* (__cdecl *MONO_METHOD_GET_OBJECT)(void *domain, void *method, void* klass);
typedef void* (__cdecl *IL2CPP_METHOD_GET_OBJECT)(void* method, void* klass);


typedef char* (__cdecl *MONO_TYPE_GET_NAME_FULL)(void *type, int format);
Expand Down Expand Up @@ -321,6 +325,8 @@ class CPipeServer : Pipe
MONO_TYPE_GET_TYPE mono_type_get_type;
MONO_TYPE_GET_OBJECT mono_type_get_object; //return a ReflectionType* object
IL2CPP_TYPE_GET_OBJECT il2cpp_type_get_object;
MONO_METHOD_GET_OBJECT mono_method_get_object;
IL2CPP_METHOD_GET_OBJECT il2cpp_method_get_object;
MONO_TYPE_IS_STRUCT mono_type_is_struct;
MONO_TYPE_GET_CLASS mono_type_get_class;
MONO_TYPE_GET_NAME_FULL mono_type_get_name_full;
Expand Down Expand Up @@ -453,6 +459,8 @@ class CPipeServer : Pipe
void GetClassOfType();
void GetTypeOfMonoType();
void GetReflectionTypeOfClassType();
void GetReflectionMethodOfMethod();
void UnBoxMonoObject();
void GetVTableFromClass();
void GetStaticFieldAddressFromClass();
void GetTypeClass();
Expand Down
30 changes: 28 additions & 2 deletions Cheat Engine/bin/autorun/monoscript.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ local dpiscale=getScreenDPI()/96

mono_timeout=0 --change to 0 to never timeout (meaning: 0 will freeze your face off if it breaks on a breakpoint, just saying ...)

MONO_DATACOLLECTORVERSION=20230409
MONO_DATACOLLECTORVERSION=20230512

MONOCMD_INITMONO=0
MONOCMD_OBJECT_GETCLASS=1
Expand Down Expand Up @@ -85,6 +85,8 @@ MONOCMD_GETCLASSTYPE = 55
MONOCMD_GETCLASSOFTYPE = 56
MONOCMD_GETTYPEOFMONOTYPE = 57
MONOCMD_GETREFLECTIONTYPEOFCLASSTYPE = 58
MONOCMD_GETREFLECTIONMETHODOFMONOMETHOD = 59
MONOCMD_MONOOBJECTUNBOX = 60

MONO_TYPE_END = 0x00 -- End of List
MONO_TYPE_VOID = 0x01
Expand Down Expand Up @@ -146,6 +148,7 @@ monoTypeToVartypeLookup[MONO_TYPE_FNPTR]=vtPointer
monoTypeToVartypeLookup[MONO_TYPE_GENERICINST]=vtPointer
monoTypeToVartypeLookup[MONO_TYPE_ARRAY]=vtPointer
monoTypeToVartypeLookup[MONO_TYPE_SZARRAY]=vtPointer
monoTypeToVartypeLookup[MONO_TYPE_VALUETYPE]=vtPointer --needed for structs when returned by invoking a method( even though they are not qwords)

monoTypeToCStringLookup={}
monoTypeToCStringLookup[MONO_TYPE_END]='void'
Expand Down Expand Up @@ -1566,6 +1569,28 @@ function mono_classtype_get_reflectiontype(monotype)
return retv
end

function mono_method_get_reflectiontype(method,klass)
assert(method,'Error: "method" was nil. It is supposed to be a MonoMethod*')
assert(klass,'Error: "klass" was nil. It is supposed to be a MonoClass*')
monopipe.lock()
monopipe.writeByte(MONOCMD_GETREFLECTIONMETHODOFMONOMETHOD)
monopipe.writeQword(method)
monopipe.writeQword(klass)
local retv = monopipe.readQword()
monopipe.unlock()
return retv
end

function mono_object_unbox(monoobject)
assert(monoobject,'Error: "monoobject" was nil. It is supposed to be a MonoObject*')
monopipe.lock()
monopipe.writeByte(MONOCMD_MONOOBJECTUNBOX)
monopipe.writeQword(monoobject)
local retv = monopipe.readQword()
monopipe.unlock()
return retv
end

function mono_class_getArrayElementClass(klass)
--if debug_canBreak() then return nil end

Expand Down Expand Up @@ -2740,6 +2765,7 @@ end

function mono_readObject()
local vtype = monopipe.readByte()
--print(vtype)
if vtype == MONO_TYPE_VOID then
return monopipe.readQword()
elseif vtype == MONO_TYPE_STRING then
Expand Down Expand Up @@ -3010,7 +3036,7 @@ function mono_invoke_method(domain, method, object, args)
end

local result=mono_readObject()

--print(type(result),result)
if monopipe then
monopipe.unlock()
return result
Expand Down

0 comments on commit f908fa8

Please sign in to comment.