Skip to content

Plugin Development Tutorial

3cxc edited this page Oct 19, 2026 · 4 revisions

Plugin Development Tutorial

This tutorial walks you through creating a minimal plugin for Unknown: Site-0 using NativePluginKit.


1. Create the Project

dotnet new classlib -n HelloWorldPlugin -f net9.0
cd HelloWorldPlugin

Edit HelloWorldPlugin.csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
  </PropertyGroup>

  <PropertyGroup>
    <PublishAot>true</PublishAot>
    <PublishRelease>true</PublishRelease>
    <PublishTrimmed>true</PublishTrimmed>
    <TrimMode>full</TrimMode>
    <NativeLib>Shared</NativeLib>
    <IlcExportUnmanagedEntrypoints>true</IlcExportUnmanagedEntrypoints>
    <IlcOptimizationPreference>Size</IlcOptimizationPreference>
  </PropertyGroup>

  <PropertyGroup>
    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)' == 'Release'">
    <ControlFlowGuard>Guard</ControlFlowGuard>
    <IlcGenerateStackTraceData>false</IlcGenerateStackTraceData>
    <IlcGenerateCompleteTypeMetadata>false</IlcGenerateCompleteTypeMetadata>
    <IlcScanReflection>false</IlcScanReflection>
    <IlcDisableReflection>true</IlcDisableReflection>
    <IlcTrimMetadata>true</IlcTrimMetadata>
    <InvariantGlobalization>true</InvariantGlobalization>
    <MetadataUpdaterSupport>false</MetadataUpdaterSupport>
    <DebuggerSupport>false</DebuggerSupport>
    <DebugSymbols>false</DebugSymbols>
    <DebugType>none</DebugType>
    <StripSymbols>true</StripSymbols>
    <StackTraceSupport>true</StackTraceSupport>
    <TrimmerRemoveSymbols>true</TrimmerRemoveSymbols>
    <EventSourceSupport>false</EventSourceSupport>
    <MetricsSupport>false</MetricsSupport>
    <UseSystemResourceKeys>true</UseSystemResourceKeys>
    <HotReloadEnabled>false</HotReloadEnabled>
    <HttpActivityPropagationSupport>false</HttpActivityPropagationSupport>
    <XmlResolverIsNetworkingEnabledByDefault>false</XmlResolverIsNetworkingEnabledByDefault>
    <EnableUnsafeBinaryFormatterSerialization>false</EnableUnsafeBinaryFormatterSerialization>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="NativePluginKit" Version="3.0.0" />
    <UnmanagedEntryPointsAssembly Include="NativePluginKit" />
  </ItemGroup>

</Project>

2. Write the Plugin

Create HelloWorldPlugin.cs:

using NativePluginKit.Features.Logging;
using NativePluginKit.Loader.Plugins;
using NativePluginKit.Loader.Plugins.Metadata;

namespace HelloWorldPlugin;

public partial class HelloWorldPlugin : Plugin, IPluginMetadata
{
    public static string Name        => "HelloWorldPlugin";
    public static string Description => "A simple hello world plugin.";
    public static string Author      => "Your Name";
    public static Version Version    => new(1, 0, 0);
    public static Version RequiredApiVersion => new(3, 0, 0);

    protected override void OnStart()
    {
        Log.PrintLog("Hello, World!");
    }

    public override void OnStop() { }
}

Note — The class must be declared partial and implement IPluginMetadata. Metadata properties are static because they are read before any plugin instance exists. A source generator emits the required GetPluginInfo, OnInit, and OnStop exports for you.


3. Use Host APIs

Logging

Log.PrintLog("Something happened.");
Log.PrintWarning("A warning.");
Log.PrintError("An error.");
Log.Debug("Debug information.");

When called from a plugin, output is automatically prefixed with the plugin name, e.g. [HelloWorldPlugin] Something happened.

Enumerating cards

using NativePluginKit.Features.Cards;

for (int i = 0; i < Card.GetCardCount(); i++)
{
    string name = Card.GetCardNameAt(i);
    Log.PrintLog($"{name} ({Card.GetCardTypeEnum(name)}) costs {Card.GetCost(name)}");
}

Enumerating entities

using NativePluginKit.Features.Entities;
using NativePluginKit.Features.Wrappers;

foreach (EntityId id in Entity.GetAllEntityIds())
{
    if (!Entity.Contains(id)) continue;
    Log.PrintLog($"{Entity.GetDisplayName(id)}: {Entity.GetCurrentHP(id)}/{Entity.GetMaxHP(id)}");
}

Subscribing to events

using NativePluginKit.Events;
using NativePluginKit.Features.Events;

EventBus.On<EntityDamagedEvent>((int)EventType.EntityDamaged, OnEntityDamaged);

private void OnEntityDamaged(EntityDamagedEvent e)
{
    var id = new EntityId(e.EntityId.Low, e.EntityId.High);
    Log.PrintLog($"{Entity.GetDisplayName(id)} took {e.HpDamage} damage");
}

Accessing the Raw API Table

Warning

PluginContext.CurrentApi is available but discouraged.

The wrapper classes (Card, Entity, Log, etc.) already encapsulate every supported operation, including argument marshaling, signature caching, and try/catch protection at the ABI boundary. Bypassing them means handling all of that yourself.

HostApiTable api = Api;

4. Build & Deploy

dotnet publish -c Release -r win-x64

Copy bin/Release/net9.0/win-x64/publish/HelloWorldPlugin.dll into the host's <GameRoot>/plugins/ directory:

<GameRoot>/
├── ExampleClient.exe
└── plugins/
    └── HelloWorldPlugin.dll

Launch the client. You should see:

Loading plugin: HelloWorldPlugin v1.0.0.0 by Your Name
Hello, World!

5. Lifecycle

Stage Method Description
Load GetPluginInfo Exported entry point. Lazily builds and caches PluginInfo on first call.
Load OnInit Exported entry point. Creates the instance, injects HostApiTable, calls OnStart, and registers [MethodExport] methods.
Start OnStart() Business initialization.
Stop OnStop Exported entry point. Invokes the plugin instance's OnStop() method, then frees the cached PluginInfo.

6. Notes

  • Do not throw exceptions from OnStart / OnStop; they cannot cross the ABI boundary
  • Keep plugins self-contained, depending only on NativePluginKit and the BCL
  • Do not use Console.WriteLine; the Godot editor cannot see the log
  • Free all unmanaged resources in OnStop, including unsubscribing from events via EventBus.Off<T>

6. Notes

  • Do not throw exceptions from OnStart / OnStop; they cannot cross the ABI boundary and will be swallowed by the generated stub, returning default values to the caller
  • Do not use Console.WriteLine; the host cannot see it
  • Release all unmanaged resources in OnStop, including unsubscribing from events via EventBus.Off<T>
  • Each assembly may contain only one class inheriting from Plugin; the source generator emits entry points per class, and multiple classes in the same assembly would produce duplicate symbol exports