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

CallMethod results in a crash #21

Closed ALittlePatate closed 2 years ago

ALittlePatate commented 2 years ago

I am currently rewriting my cheat for Devour using your library but i can't call a function without an access violation happening. Original C# code (using MelonLoader) :

NolanRankController NolanRank = UnityEngine.Object.FindObjectOfType<NolanRankController>();
NolanRank.SetRank(50);

New C++ code (using IL2CPP Resolver) :

void OnUpdate() {
    if (settings::spoof_level) {
        Unity::CGameObject* NolanBehaviour = Unity::Object::FindObjectOfType<Unity::CGameObject>("NolanRankController");
        NolanBehaviour->CallMethodSafe<void*>("SetRank", 50);
    }
}

The code crashes at void* GetMethodPointer(Unity::il2cppClass* m_pClass, const char* m_pMethodName, int m_iArgs), line 176 of Class.cpp :

Unity::il2cppMethodInfo* pMethod = reinterpret_cast<Unity::il2cppMethodInfo*(IL2CPP_CALLING_CONVENTION)(void*, const char*, int)>(Data.Functions.m_pClassGetMethodFromName)(m_pClass, m_pMethodName, m_iArgs);

Result : "Access violation in GameAssembly.dll when reading at 0xFFFFFFFF20000133."

What am i missing there ?

extremeblackliu commented 2 years ago

component is attached to a gameobject, you need to get component first.

Unity::CGameObject* AnyObject = Unity::GameObject::Find("TargetGameObjectName");
Unity::CComponent*  NolanRankController = AnyObject->GetComponent("NolanRankController");
NolanRankController->CallMethod<void*>("SetRank", 50);