Skip to content

Request: Native modding support #3

@carson-coder

Description

@carson-coder

I propose adding native support to modding object ir application. For example, if you have a game with a UI class with the following code:

class UI
{
    Screen screen;
    public UI(Screen screen)
    {
        this.screen = screen;
        Console.WriteLine("UI Constructor");
    }

    public void render(List<UIElement> elements)
    {
        screen.startFrame();
        foreach (UIElement element in elements) {
            element.render()
        }
        screen.endFrame();
    }
}

If you want to make a mod that draws text on the screen from an api you can use the following code:

using ObjectIR.modding;

// no actual code but used so that we have a UI reference
[Replace]
class UI
{
    _class("Screen") screen; // _class allows us to say that screen is of type `Screen` without having to make the Screen type
    public UI() {}
    public void render(List<_class("UIElement")> elements) {}
}

[Patch(UI)] // Say that UIPatch will patch UI
class UIPatch : UI // Must inherit target class
{
    API api;

    public override UIPatch(Screen screen)
        :  base(screen) 
    {
        Console.WriteLine("UI Patch Initalizing");
        api = new API("https://www.example.com/api");
    }

    public override void render(List<UIElement> elements)
    {
        // This time we will inject our text into the elements list and then render the elements
        elements.Add(new Text(api.get_text()));
        base.render(elements); // make sure you remember to call the base function
    }
}

If a class does not inherit the class they are patching then it will throw a error. If a function overrides another function but does not call the overridden function (as far as the compiler can tell) then a warning will be thrown.

I am also thinking that during compilation the --disable-modding argument can be used to prevent modding the program.

Also some helper functions will exist like public Mod[] loaded_mods() to get a list of loaded mods and public boolean IsModded() to know if the program is modded. Also if mods are loaded then the program will print a message containing a list of all loaded mods when the program starts. This can be disabled at compile time with --disable-modded-warning

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions