Skip to content

API Loader Constants

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

NativePluginKit.Loader.Constants

Shared ABI definitions exchanged between host and plugin.


HostApiAttribute

[AttributeUsage(AttributeTargets.Field)]
public sealed class HostApiAttribute : Attribute
{
    public string TargetNamespace { get; }
    public string ClassName { get; }
    public string MethodName { get; }
    public Type NativeDelegate { get; }
    public Type? PublicDelegate { get; }

    public HostApiAttribute(string targetNamespace, string className,
        string methodName, Type nativeDelegate, Type? publicDelegate = null);
}

Marks a field in HostApiTable so the source generator produces a public API class. NativeDelegate describes the raw ABI signature (pointer parameters); PublicDelegate describes the ergonomic signature (managed types like string).


HostApiTable

[StructLayout(LayoutKind.Sequential)]
public struct HostApiTable
{
    // ~70 IntPtr fields, one per generated API method.
}

Do not edit HostApiTable manually — every field carries a [HostApi] attribute that drives code generation. Adding an API is: add field + add attribute + extend HostApiBridgeBuilder.Create + implement the delegate in the host.

Field groups (in order):

Group Example fields
Infrastructure FreeString
Scene TransitionToScene, Scene_GetCurrentScenePath
Logging PrintLog, PrintWarning, PrintError, LogDebug
Config Config_GetCharacterImagePath, Config_GetExecutableDirectory, Config_GetPluginsDirectory
Timing Timing_GetTimeSinceStartup
Events RegisterEventCallback, UnregisterEventCallback
Story Story_GetCurrentTimelineName, Story_IsBattle, Story_StartTimeline, Story_EndTimeline, Story_SendSignal
Cards Card_GetCardCount, Card_GetCardNameAt, ..., Card_GetAppliedEffectParam2
Entities Entity_GetCount, Entity_GetIdAt, ..., Entity_GetEffectMaxStacks

HostApiBridgeBuilder

public static unsafe class HostApiBridgeBuilder
{
    public static HostApiTable Create(
        FreeStringDelegate freeString,
        TransitionToSceneDelegate transition,
        GetStringDelegate getCurrentScenePath,
        PrintLogDelegate printLog,
        // ... ~70 parameters
        );

    // Delegates are nested types (TransitionToSceneDelegate, PrintLogDelegate, ...)
}

Static factory that converts managed delegates to unmanaged function pointers and fills a HostApiTable.

Lifetime: the builder keeps strong references to every delegate it receives, so callers do not need to keep them alive separately.

Delegates use bool return values where the ergonomic API returns bool (e.g. RegisterEventDelegate), and int return values for C-ABI-safe enums.


PluginInfo

[StructLayout(LayoutKind.Sequential)]
public struct PluginInfo
{
    public IntPtr NamePtr;
    public IntPtr DescriptionPtr;
    public IntPtr AuthorPtr;
    public int MajorVersion, MinorVersion, BuildVersion, RevisionVersion;
    public int RequiredApiMajor, RequiredApiMinor;

    public string  GetName();
    public string  GetDescription();
    public string  GetAuthor();
    public Version GetVersion();
    public Version GetRequiredApiVersion();
}

Metadata blob returned by the plugin's GetPluginInfo export. Strings are UTF-8 pointers allocated with StringToCoTaskMemUTF8 and remain valid for the lifetime of the plugin.


PluginInitDelegate

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void PluginInitDelegate(IntPtr hostApiTable);

Matches the plugin's OnInit export.

Clone this wiki locally