BepInEx / Il2CppInterop

A tool interoperate between CoreCLR and Il2Cpp at runtime
GNU Lesser General Public License v3.0
185 stars 59 forks source link

Virtual Override Methods not working correctly? #109

Open Deaadman opened 10 months ago

Deaadman commented 10 months ago

I was developing a mod for The Long Dark, trying to create a derived class from within the game. The code snippet goes like this:

public class Panel_ModMenu : Panel_AutoReferenced 
{
    private BasicMenu m_BasicMenu;
    public GameObject m_BasicMenuRoot;

    public Panel_ModMenu(IntPtr ptr) : base(ptr) { }

    public override void Initialize()
    {
        base.Initialize();
        m_BasicMenu = BasicMenu.InstantiateMenu(InterfaceManager.s_BasicMenuPrefab, m_BasicMenuRoot, gameObject, this);
    }

    public override void Enable(bool enable)
    {
        base.Enable(enable);
        if (enable)
        {
            m_BasicMenu.Enable(true);
            GameManager.GetCameraEffects().DepthOfFieldTurnOn();
            return;
        }
        m_BasicMenu.Enable(false);
        GameManager.GetCameraEffects().DepthOfFieldTurnOff(false);
    }
}

The issue I'm facing is with the override void methods. The Panel_Base class which the Panel_AutoReferenced class also inherits isn't abstract. The Initialized override method could be getting called from the virtual void method within the Panel_Base class to initialize this new panel. But it isn't, and whenever I try to call my Initialize method manually - a recursion occurs.

Unless I've missed something, or it's not working because I don't know how to code - this might be an error with this project.

ds5678 commented 1 month ago

Were you able to figure it out?

I suspect this scenario just isn't currently supported. Everything surrounding inheritance and virtual methods is complicated.

Deaadman commented 1 month ago

Were you able to figure it out?

I suspect this scenario just isn't currently supported. Everything surrounding inheritance and virtual methods is complicated.

Unfortunately not, like you said I don't think this specific scenario is supported. You can see what I did as a workaround here - but it doesn't actually register this panel as a proper one, so using methods such as InterfaceManager.GetPanel<>() can't find this new panel I've created.

ds5678 commented 1 month ago

I'm planning on doing a 1.4.6 release soon. It'll include #124, any improvements implemented since 1.4.5, and possibly some additional unstripping improvements. I expect this will be part of the next MelonLoader hotfix.

After that's released, I want to focus on reworking the type system, which would include resolving this issue. I plan for the rewrite to be released slightly before the next big MelonLoader update, which I suspect will happen around November.

Deaadman commented 1 month ago

Sounds good! Being able to inherit off Hinterlands classes would be a major help when it comes to UI work, and potentially other stuff that I haven't gotten into. Maybe weather types too?

Who knows, but I appreciate it regardless!