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

Injected fields returning injected types cause a crash #97

Open
simonkellly opened this issue Jun 17, 2023 · 2 comments
Open

Injected fields returning injected types cause a crash #97

simonkellly opened this issue Jun 17, 2023 · 2 comments

Comments

@simonkellly
Copy link
Contributor

simonkellly commented Jun 17, 2023

If you do the following

[BepInAutoPlugin]
public partial class TestPluginPlugin : BasePlugin
{
    public override void Load()
    {
        ClassInjector.RegisterTypeInIl2Cpp<MyBehaviour>();
        ClassInjector.RegisterTypeInIl2Cpp<MyTestObj>();
        
        MyBehaviour myBehaviour = new GameObject().AddComponent(Il2CppType.Of<MyBehaviour>()).Cast<MyBehaviour>();
        MyTestObj myTestObj = new GameObject().AddComponent(Il2CppType.Of<MyTestObj>()).Cast<MyTestObj>();
        myTestObj.myBehaviour.Value = myBehaviour;

        MyTestObj myNewTest = Object.Instantiate(myTestObj).Cast<MyTestObj>();
        Log.LogMessage("myNewTest.myBehaviour.mySpriteRenderer: " + myNewTest.myBehaviour.Value);
    }
}

public class MyBehaviour : MonoBehaviour
{
    public MyBehaviour(IntPtr ptr) : base(ptr) { }
    
    public Il2CppReferenceField<GameObject> mySpriteRenderer;
}

public class MyTestObj : MonoBehaviour
{
    public MyTestObj(IntPtr ptr) : base(ptr) { }

    public Il2CppReferenceField<MyBehaviour > myBehaviour;
}

The game will crash once you call Object.Instantiate(myTestObj)

Though, if you switch the return type to be something more generic i.e MonoBehaviour in myBehaviour, it works fine

@simonkellly simonkellly changed the title Injected fields which return injected types throw null reference exception when injecting in wrong order Injected fields returning injected types cause a crash Jun 17, 2023
@Alexejhero
Copy link

💤

@Alexejhero
Copy link

image

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

2 participants