Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access violation when trying to get field value #39

Open
Patrick-van-Halm opened this issue Nov 30, 2023 · 14 comments
Open

Access violation when trying to get field value #39

Patrick-van-Halm opened this issue Nov 30, 2023 · 14 comments

Comments

@Patrick-van-Halm
Copy link

I am trying to add a light component to the camera which is stored in the player class stored in an obfuscated value

IL2CPP::Thread::Attach(IL2CPP::Domain::Get());
Unity::il2cppClass* unityEngineLightType = IL2CPP::Class::Find("UnityEngine.Light");
Unity::il2cppObject* unityEngineLightInstance = Unity::Object::New(unityEngineLightType);
Unity::CComponent* playerInstance = Unity::Object::FindObjectOfType<Unity::CComponent>("Player");
Unity::CCamera* cam = playerInstance->GetMemberValue<Unity::CCamera*>("ഠപര\u0D3Bബഢരഫധ");
cam->AddComponent(unityEngineLightInstance);
Unity::CComponent* unityEngineLightComponent = cam->GetComponent("UnityEngine.Light");
unityEngineLightComponent->SetPropertyValue<float>("intensity", 10);
IL2CPP::Thread::Detach(IL2CPP::Domain::Get());

And I am getting an error within GetMemberValue

Unity::il2cppFieldInfo* pField = reinterpret_cast<Unity::il2cppFieldInfo * (IL2CPP_CALLING_CONVENTION)(void*, const char*)>(Functions.m_ClassGetFieldFromName)(m_Object.m_pClass, m_pMemberName);

Unity version: 2022.3.5f1
Error: Access violation reading location 0xFFFFFFFFFFFFFFFF

@sneakyevil
Copy link
Owner

You could wrap the string in u8"..." if that doesn't work maybe best way would be to use get all fields and cache the offset and read it directly.

@Patrick-van-Halm
Copy link
Author

So I tried both, the u8 method throws the same error and getting all fields causes also a access violation writing location.

Within the GetFields method

return reinterpret_cast<Unity::il2cppFieldInfo * (IL2CPP_CALLING_CONVENTION)(void*, void**)>(Functions.m_ClassGetFields)(m_pClass, m_pIterator);
std::vector<Unity::il2cppFieldInfo*> fields;
playerInstance->FetchFields(&fields);

for (Unity::il2cppFieldInfo* field : fields) {
      std::cout << field->m_pName << std::endl; // Just so I can add a breakpoint
}

Am I possibly accessing a class wrong? I am going to try and run it within a different application to see if it works there.

@Patrick-van-Halm
Copy link
Author

Alright with a quick test on a different game I found the issue. It seems like

Unity::CComponent* playerInstance = Unity::Object::FindObjectOfType<Unity::CComponent>("Player");

returns an incorrect pointer. If I look at the debug information the m_pName is not equal to "Player" but rather equal to "ðžê—z�".

@Patrick-van-Halm
Copy link
Author

And when I change the type to Unity::il2cppClass it does contain the correct name. So I assume the definition of the Unity components are changed so I will look into that

@Patrick-van-Halm
Copy link
Author

It also seems like the flag "UNITY_VERSION_2022_3_8F1" is an incorrect version I currently have traced it back to Unity 2021.3.27f1

@extremeblackliu
Copy link
Collaborator

It also seems like the flag "UNITY_VERSION_2022_3_8F1" is an incorrect version I currently have traced it back to Unity 2021.3.27f1

this flag is used for unity version that higher than 2022.3.8, your problem can be solve by using field offset from target class.
seems you are trying to add a light component to camera, if the camera is the eye camera, you can try get the camera from static class Camera by calling Camera.get_current() or Camera.get_main()

@karaok1
Copy link
Contributor

karaok1 commented Dec 3, 2023

Alright with a quick test on a different game I found the issue. It seems like

Unity::CComponent* playerInstance = Unity::Object::FindObjectOfType<Unity::CComponent>("Player");

returns an incorrect pointer. If I look at the debug information the m_pName is not equal to "Player" but rather equal to "ðžê—z�".

The problem might be that you are searching for the player, but the player is probably a GameObject rather than being a Component. They are different things in Unity. The former one has an instance and is an object in the hierarchy but a component is not.

@Patrick-van-Halm
Copy link
Author

Alright with a quick test on a different game I found the issue. It seems like

Unity::CComponent* playerInstance = Unity::Object::FindObjectOfType<Unity::CComponent>("Player");

returns an incorrect pointer. If I look at the debug information the m_pName is not equal to "Player" but rather equal to "ðžê—z�".

The problem might be that you are searching for the player, but the player is probably a GameObject rather than being a Component. They are different things in Unity. The former one has an instance and is an object in the hierarchy but a component is not.

I know the difference however in using Il2CppDumper it specifically mentions Player in the root namespace. The class also returns correctly with the corresponding class so yes it's a component.

@Patrick-van-Halm
Copy link
Author

It also seems like the flag "UNITY_VERSION_2022_3_8F1" is an incorrect version I currently have traced it back to Unity 2021.3.27f1

this flag is used for unity version that higher than 2022.3.8, your problem can be solve by using field offset from target class. seems you are trying to add a light component to camera, if the camera is the eye camera, you can try get the camera from static class Camera by calling Camera.get_current() or Camera.get_main()

I know its just accessing other classes causes the same error...

@extremeblackliu
Copy link
Collaborator

extremeblackliu commented Dec 3, 2023

I know the difference however in using Il2CppDumper it specifically mentions Player in the root namespace. The class also returns correctly with the corresponding class so yes it's a component.

see

The problem might be that you are searching for the player, but the player is probably a GameObject rather than being a Component. They are different things in Unity. The former one has an instance and is an object in the hierarchy but a component is not.

game obfuscate doesn't affects system classes like gameobject, transform, camera ... etc, so when you get their classname, you should have non-obfuscate name or you messed up.
try get it as gameobject then get the component which is you need from the gameobject.
this step is literally same as you writing c# script for unity, just think it.

@extremeblackliu
Copy link
Collaborator

also, own thread is not recommended, its very unstable and randomly crashes.
use https://sneakyevil.gitbook.io/il2cpp-resolver/callback/onupdate for instead

@karaok1
Copy link
Contributor

karaok1 commented Dec 4, 2023

May I ask, which game is this?

@Patrick-van-Halm
Copy link
Author

also, own thread is not recommended, its very unstable and randomly crashes. use https://sneakyevil.gitbook.io/il2cpp-resolver/callback/onupdate for instead

Also tried this without any luck

@Patrick-van-Halm
Copy link
Author

May I ask, which game is this?

Phasmophobia

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants