Skip to content

API Loader Plugins

3cxc edited this page Sep 13, 2026 · 2 revisions

NativePluginKit.Loader.Plugins


Plugin

public abstract class Plugin
{
    public HostApiTable Api { get; }
    public abstract string Name { get; }
    public abstract string Description { get; }
    public abstract string Author { get; }
    public virtual Version? Version { get; }
    public abstract Version RequiredApiVersion { get; }

    protected abstract void OnStart();
    public abstract void OnStop();
}

Base class for all plugins.

Lifecycle

  1. Module load — __GeneratedModuleInit (marked [ModuleInitializer]) calls SetPluginInfo(new YourPlugin()) so metadata is available before OnInit.
  2. OnInit — the source generator emits this export. It creates the plugin instance and calls Plugin.Initialize(hostApiTablePtr, instance), which:
    • Populates PluginContext.CurrentApi.
    • Calls OnStart().
  3. OnStop export — the generator emits __GeneratedOnStop (exported as OnStop) which calls the instance's OnStop() and clears the reference.

Required usage

public partial class MyPlugin : Plugin   // ← partial is mandatory
{
    public override string Name => "MyPlugin";
    public override string Description => "...";
    public override string Author => "...";
    public override Version RequiredApiVersion => new(1, 0, 0);

    protected override void OnStart() { /* ... */ }
    public override void OnStop() { /* ... */ }
}

PluginContext

public static class PluginContext
{
    public static HostApiTable CurrentApi { get; internal set; }
}

Global accessor for the host's API table. Set once during Initialize, read by every generated API method. Plugins should not modify it.

Clone this wiki locally