sneakyevil / IL2CPP_Resolver

A run-time API resolver for IL2CPP Unity.
https://sneakyevil.gitbook.io/il2cpp-resolver/
The Unlicense
362 stars 67 forks source link

Docs probably outdated, example throws pointer error #7

Closed ReijiAzuma closed 2 years ago

ReijiAzuma commented 2 years ago

Hello,

First of all I would like to say sorry for my shitty C++ skills, I've been trying to run the example of getting the GameObject list but it seems I've stumbled upon pointer hell.

image

Also the Unity::il2cppArray type has no m_pArray, I'm thinking this has been replaced with m_pValues?

Other than that, the lib seems to be working fine, I've successfully printed methods of my PlayerManager class, but I have no idea how to call them. CClass has the CallMethod function, I got the method pointer through IL2CPP::Class::Utils::GetMethodPointer, but how do I get a CClass in the first place? I couldn't find a constructor and got stuck.

I would really appreciate some support, and thank you from the bottom of my heart for the nice lib!

sneakyevil commented 2 years ago

You can already see that when you hover over the FindObjectsOfType function it shows you the return value. So there are 2 pointers. I changed some stuff since the wiki so the actual FindObjectsOfType just need type without pointer.

ex.

Unity::il2cppArray<Unity::CGameObject*>* m_pObjects = Unity::Object::FindObjectsOfType<Unity::CGameObject>(UNITY_GAMEOBJECT_CLASS);

Also the Unity::il2cppArray type has no m_pArray, I'm thinking this has been replaced with m_pValues?

Use defined function to get array value:

Unity::CGameObject* m_pObject = m_pObjects->operator[](/* index */);

Other than that, the lib seems to be working fine, I've successfully printed methods of my PlayerManager class, but I have no idea how to call them. CClass has the CallMethod function, I got the method pointer through IL2CPP::Class::Utils::GetMethodPointer, but how do I get a CClass in the first place? I couldn't find a constructor and got stuck.

There is no way to get easily initialized class in memory besides hooking some function and stealing the pointer, finding gameobject that has the class connected to it via component or calling constructor function.

Also this depends if the function even needs class pointer. If the function is defined "statically" you can just call the function directly with pointer as:

reinterpret_cast<void(__fastcall*)(/* args */)>(/* pointer to function */)(/* args values */);

You gonna need to debug some stuff to figure out multiple stuff for desired game or whatever you're doing to see how the method is handled. I also still recommend dumping the game with il2cppDumper and checking dump.cs & script.json for easier work.

ReijiAzuma commented 2 years ago

Thanks for clarifying the first part, I can now get the player GameObject just fine and it works like a charm!

I also still recommend dumping the game with il2cppDumper and checking dump.cs & script.json for easier work.

I did that and I'm trying to apply what I've learned modding with BepInEx to C++.

As for the second part, in my game's case, the PlayerManager exposes a static Instance() method as well as a static _instance field so I guess that's easy for getting the initialized class in memory. If I call it then I should be able to get the pointer and afterwards the aforementioned fields, like the characterList. But how do I adapt it to the lib, like getting a CClass or something else from the instance pointer.

sneakyevil commented 2 years ago

You might need to understand that when you're getting the class via Instance it can diff from the actual class you want to get which holds desired fields. Thats when I mentioned the requirement of hooking or finding gameobject that holds the component (with the class)

The IL2CPP::CClass is literally just class handler for any custom class the game offers so you can easily use some function such as, get/set fields & properties or call/get methods.

You gonna need to reinterpret_cast the class pointer to the IL2CPP::CClass if you wanna use it.