To create an instance of a Mono Class, use mono_object_new(klass) where klass is the token of the class that you want to create its instance.
For example, if you want to create an instance of class PlayerStats from namespace GameBattle:
Code: Select all
local classId = mono_findClass("GameBattle", "PlayerStats")
local instanceAddress = mono_object_new(classId) -- this function returns the address of created class's instance
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Problem
I am hacking a Mono game and I want to create an instance of a Mono class ParameterChanger. After researching, I learned that to create an instance of a Mono class, we have to:
- Allocate memory for the instance of the Mono Class
- Invoking the constructor method for that instance
My attemptsI know how to invoke the constructor method, but I do not know how to deal with the first step. My question is, how to properly allocate memory for the instance of a Mono Class?
- Do we manually allocate memory by using a Lua function like allocateMemory(size) and make sure that the size of allocated memory is exactly equal to or bigger than the total size of all fields of the Mono Class? Will this work?
- Can we use the function mono_object_new(kclass)? I cannot find the documentation for this Lua function in monoscript, but after researching and referencing the Mono project for embedding C/C++, it seems that this function is used to allocate memory for an instance of a Mono Class, given the id of the class.
- For the first approach, Let's say the size of an instance of Mono Class ParamterChanger is 30 bytes. I allocated more than 30 bytes and call the constructor of the class ParameterChanger on the address of allocated memory, then dissected data structure of that address to inspect the internal data, and it worked as I expected
- But it is safe to do this?
- Or do we have to allocate memory with a size exactly equal to the size of an instance of Mono Class?
- For the second approach, after launching Cheat Engine, the first time I run mono_object_new(classId) the monopipe crashes, then I have to activate mono feature again. After that, every call to mono_object_new(classId) won't crash monopipe and I obtain an address from the return value:
- Why does it happen? How to fix it?
- Does mono_object_new(classId) actually allocate memory with the right size for the instance of Mono Class internally? If it is not, what does this function do?