Hazel

Native Plugins

A native plugin is an ordinary Bevy plugin, shipped as Rust source, compiled on the machine that installs it, and loaded at startup. It links the real Bevy and the real contract crate, so it gets &mut World, calls app.add_systems, and sees the same Transform the engine does. There is no FFI and no command vocabulary in between.

// plugins/spin-thing/src/lib.rs
use bevy::prelude::*;

#[derive(Component)]
pub struct Spin;

pub struct SpinThing;

impl Plugin for SpinThing {
    fn build(&self, app: &mut App) {
        app.add_systems(Update, spin);
    }
}

fn spin(time: Res<Time>, mut q: Query<&mut Transform, With<Spin>>) {
    for mut t in &mut q {
        t.rotate_y(time.delta_secs());
    }
}

renzora::plugin!(SpinThing);

The last line is the only Renzora-specific thing in it.

Native, or in the workspace?

Native pluginIn-workspace plugin
Lives in<exe dir>/plugins/<name>/crates/renzora_<name>/
Crate typedylib, built from source on installrlib, compiled into the binary
Needs an engine checkoutnoyes
Declared byrenzora::plugin! in sourcea [[package.metadata.renzora.plugin]] table

A native plugin is something a user installs (usually from the Marketplace). An engine feature is a crate in the workspace. If what you are writing should ship with the engine, it is not a native plugin.

Layout

A plugin is a directory under plugins/, named by its id:

<exe dir>/
  bevy_dylib-<hash>.dll   renzora_dylib.dll      the shared images
  sdk/                                           what plugins compile against
  plugins/
    spin-thing/                                  shipped as source
      Cargo.toml                                 crate-type = ["dylib"]
      src/lib.rs
      build/spin_thing.dll                       what rustc produced
      build/stamp.txt                            what it was built against
      build/deps/                                its third-party crates, if any

A directory counts as a plugin source when it holds src/lib.rs and a Cargo.toml whose crate-type line names "dylib". Submodules work as in any crate. The library is build/<id with - as _>.<ext>.

On macOS the plugins installed by a user live in Application Support rather than inside the .app, because writing into a signed bundle invalidates its signature. The loader reads both roots, and a plugin installed by the user takes precedence over one of the same name shipped in the bundle.

Why it can link the real Bevy

Two shared images make it sound. The dynamic_linking feature, on by default, builds Bevy as bevy_dylib and the whole contract (every crates/api layer, plus the UI framework under renzora::ui) as renzora_dylib. The editor and every plugin link those same two files, so there is one World type and one copy of every process-global table: the translation table, the Console and Problems buffers, the theme palette, the viewport toolbar lists.

A privately linked copy of either would compile, load and then fail silently: raw translation keys in the UI, log lines in a buffer nobody drains, a panel painted in default colours. There is no runtime check that could catch that, so the loader's rule is blunt: in a build without dynamic_linking it loads no native plugin at all, and records each one as skipped with the reason.

Declaring it: renzora::plugin!

renzora::plugin!(SpinThing);            // Editor scope (the default)
renzora::plugin!(SpinThing, Runtime);   // also loaded by a shipped game
renzora::plugin!(SpinThing::new(4));    // takes an expression, not just a type

The macro (in crates/api/renzora_platform_api/src/native_plugin.rs) emits three unmangled symbols:

SymbolWhat it is
renzora_native_plugin_ctorfn() -> Box<dyn Plugin>, the entry point
renzora_native_plugin_scopeextern "C" fn() -> u8; 0 is Editor, 1 is Runtime. Absent or unknown reads as Editor
renzora_native_plugin_abia 128-byte record: RENZORA_NATIVE_PLUGIN_ABI= then the engine stamp it was built against, NUL padded

Write the macro rather than the symbols. The loader finds a plugin by symbol name, and every hand-written mistake (a typo, a missing #[unsafe(no_mangle)], a wrong return type) produces a library that is silently not a plugin, or worse, one whose return register is read as a boxed trait object.

Scope

Editor plugins load only in the editor. Runtime plugins also load in a shipped game. Editor is the default because guessing wrong in that direction is harmless: an editor plugin missing from a game is an absence, while a runtime plugin that should not have shipped is in the player's hands.

The scope is read from the built library, never from the source, because what ships is the library and the two disagree whenever one was edited without a rebuild.

The editor half of a runtime plugin

A Runtime plugin that also adds editor surface (an inspector section, a preset, a panel, a tool) keeps all of that in src/editor.rs, or src/editor/mod.rs with modules below it, with one entry point:

// src/editor.rs
use bevy::prelude::*;
use renzora::{AppEditorExt, InspectorEntry};

pub(crate) fn register(app: &mut App) {
    app.register_inspector(inspector_entry());
}

Do not write mod editor; yourself: the macro declares it. Where the plugin is loaded as a library (the editor, a copy-based export), plugin! declares the module and runs editor::register right after the plugin's own build. Where the plugin is compiled into a lean game export, it declares nothing, so the editor code is never compiled and never reaches the player. The build driver decides which by looking at the disk: it passes --cfg renzora_no_editor when neither file exists.

For a line or two, renzora::editor! { ... } is the inline form of the same thing: items or statements inside it are compiled beside the editor and left out of a lean game build. Imports used only there go inside a block too.

A cargo feature could not do this job: a native plugin is compiled with no features of its own, so #[cfg(feature = "editor")] in it is always false.

The lean exporter checks for editor-contract code outside the editor folder and outside any editor! block, and for a crate root declaring mod editor itself, before it compiles anything, and stops with the file and line.

PluginEditorCopy-based exportLean export
Editor scopeloadsnot loadedleft out of the build
Runtime, no editor folderloadsloadscompiled in
Runtime, with src/editor.rs or src/editor/loads, with its editor halfloads, with its editor halfcompiled in without its editor half

See Export System for the packaging modes.

Building

A plugin is never built by cargo. The compiler driver, crates/renzora_plugin_build, invokes rustc directly: a plugin is one crate whose dependencies are already compiled, so there is no graph to resolve. It hands the plugin two --externs, bevy (the facade rlib) and renzora (the renzora_dylib image), and sets CARGO_MANIFEST_DIR (Bevy's derive macros read the manifest to decide how to write their paths) and RENZORA_PLUGIN_ABI (the stamp plugin! embeds).

The editor compiles a plugin the first time it finds one and whenever it is stale. A Marketplace install compiles it during the install. That work happens before the App is assembled, in a setup pass that restarts into the ordinary boot once every plugin is built, so the loader itself normally finds nothing left to compile. In a source checkout, cargo renzora dist runs the same build at the end.

Never run cargo build in a plugin directory

plugins/ is outside the engine workspace, so cargo would resolve the manifest's bevy to a fresh Bevy from crates.io: a second World type with different TypeIds. The result builds, loads and corrupts the World. renzora::plugin! refuses it: unless the build passes --cfg renzora_build, which only the driver does, the plugin stops at a compile_error! explaining why. rust-analyzer shows that one diagnostic and otherwise works.

The bevy and renzora entries in a plugin's Cargo.toml are there for the IDE and are never read by the build. Keep a bevy entry all the same, because Bevy's derives look up a dependency by that name.

The SDK

What rustc reads at compile time (crate metadata, proc-macro libraries, native import libraries) is the plugin SDK, staged by xtask/src/sdk.rs into dist/<platform>/sdk/ on every build. Its file list comes from cargo's own --message-format=json output, never from a directory scan: deps/ holds several -C metadata variants of the same crate, and matching by name produces a set that looks complete and fails to compile.

The SDK holds only what a plugin can reach: the crates inside the two shared images, as .rmeta metadata rather than whole .rlibs. A plugin typechecks against the metadata and takes the code from the shared images, so the object half of each archive is dead weight. The one exception is the bevy facade, staged whole. If something a plugin needs is missing its code, rustc says so (error[E0461]: crate 'X' required to be available in rlib format); there is no quiet failure.

A release ships the SDK compressed beside the executables as sdk.tar.zst and unpacks it once. The two shared images are not in the archive: they are the same files the engine runs from, so the unpacker links them in from the install.

The SDK is pinned to one exact rustc, because crate metadata is versioned and the compiler refuses another version's. The driver checks the toolchain before compiling and reports which one is needed, rather than relaying error[E0514].

An SDK is only correct when built on its own platform. Proc macros run inside the compiler, so a cross-compiled engine carries proc-macro libraries for the build machine, not the target. Docker cross-builds therefore produce runtimes and export templates, never an editor with an SDK. To produce an editor for a platform you do not own, build natively there (CI uses one native runner per platform). See Cross-Compiling.

The stamp

A plugin is bound to one engine build: its metadata, its TypeIds and its imports all come from that build. So each build records a stamp, the SDK's build_id: a content hash of the shared images a plugin links, plus the rustc version.

It has to be a content hash. Cargo derives artefact filenames from the build configuration, never from source, so a stamp made of filenames did not move when the contract crate changed, and Rust mangles symbols from a crate's stable id rather than its contents, so nothing downstream noticed either.

The stamp lives in two places:

  • build/stamp.txt, compared cheaply to decide whether to rebuild.
  • Inside the library, as the ABI record plugin! writes. This is the one that counts. The loader finds it with a byte search of the file before mapping the image, the same way it finds the constructor symbol, because an image built for another engine is unsafe the moment its initialisers run. A library whose record names another build, or has none, is never mapped.

A plugin is rebuilt when the stamp is absent or stale, when the library's own record disagrees, when the library is missing, or when any source file is newer than the library. Update the engine and every installed plugin quietly rebuilds against it; edit src/ and the next launch rebuilds it. That second rule is the whole development loop for someone working from a downloaded editor with no repository.

A build that fails writes build/failed.txt holding the stamp it failed against, and is not retried while that still describes the inputs: a moved engine or an edited source retries. Without it a broken plugin would reopen the setup pass on every launch. The compiler's output is appended to build/build.log (rotated to build.log.old past 1 MB). RENZORA_BUILD_LOG set to a path collects every log into that file; set to off, 0 or false it switches logging off.

On macOS the driver rewrites each plugin's install names to @rpath after linking (fixup_install_names). dyld keys images by path, and without it a plugin would load a second copy of the shared images and see an engine that never started.

Loading

crates/renzora_native_plugin (NativePluginLoader) makes one pass over the plugin roots during App assembly. For each plugin directory it:

  1. Skips it if the user disabled it (editor only, see below), before touching anything.
  2. Records it as skipped, with the reason, if nothing is built or the build has no shared images.
  3. Reads the library's bytes: no renzora_native_plugin_ctor means it is not a plugin, skipped silently; a foreign stamp means it is refused.
  4. Maps the image, reads the scope, declines an Editor plugin outside the editor, calls the constructor and adds the plugin.

Every outcome is recorded for Settings > Editor > Plugins, which lists what the loader actually did (loaded, disabled, skipped with a reason, or failed with the error), and failures also go to the Console.

Nothing is ever unloaded. Every system a plugin registered is a function pointer into its image, and a Bevy schedule holds those for the life of the App. The loader keeps each Library in ManuallyDrop, including at process teardown, where dropping one has crashed before. A plugin removed or replaced while running is moved aside and deleted at the next launch.

Turning plugins off

The switches in Settings > Editor > Plugins are stored in ~/.renzora/settings.toml under [app] disabled_plugins, keyed by directory name, and take effect at the next launch: Bevy cannot withdraw what a plugin already added to the App. A disabled plugin costs nothing, not even a rebuild check.

The list is an editor preference, so only the editor reads it. A shipped game loads every plugin it was shipped with.

What a plugin can reach

Two crates:

  • bevy: all of it.
  • renzora: the contract, every layer of it re-exported under one name by renzora_sdk (renzora::bus, renzora::physics, renzora::audio, renzora::net, renzora::undo, renzora::lang, renzora::probes and the rest), plus the editor contract flat at the root (renzora::EditorSelection, renzora::AppEditorExt) and the UI framework as renzora::ui.

Nothing else in the workspace is reachable, including another plugin's types, since each plugin is compiled on its own. To talk to another plugin, use the plugin bus. If a plugin needs a type that only an engine crate defines, that type belongs in the contract.

Third-party crates

Add ordinary crates.io dependencies to [dependencies], one per line (foo = { version = "1" }; the [dependencies.foo] sub-table form is refused). The driver strips bevy, renzora and every renzora_* entry, writes the rest into a separate manifest that mentions no Bevy, has cargo build that into build/deps/ with the SDK's pinned toolchain, and passes the results to the plugin's rustc as extra --externs. Cargo resolves your dependencies; it never resolves Bevy.

  • A dependency built on Bevy is refused. Any bevy, bevy_*, renzora or renzora_* crate anywhere in the resolved graph would be a second Bevy and a second World. Bevy-based functionality belongs in the engine, as a crate.
  • serde, serde_json, bytemuck and indexmap come from the engine. Bevy's types implement the engine's serde, and Bevy's GPU and render-phase APIs are bounded on its bytemuck and indexmap, so a privately resolved copy is a different crate and nothing type-checks. Use the re-exports and point the derives at them:
use renzora::serde::{Deserialize, Serialize};

#[derive(Component, Reflect, Serialize, Deserialize)]
#[serde(crate = "renzora::serde")]
#[reflect(Component, Serialize, Deserialize)]
pub struct MySettings {
    pub tint: Vec3,
}
use renzora::bytemuck::{Pod, Zeroable};

#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable)]
#[bytemuck(crate = "renzora::bytemuck")]
struct Instance {
    position: [f32; 4],
}

A plugin with no third-party dependencies never invokes cargo at all, so its build is offline and takes about a second. Dependencies are the one step that needs a network.

Shipping with an exported game

A copy-based export ships a Runtime plugin's library beside the game, with the shared images it was compiled against, and writes plugins/engine-stamp.txt so the game knows its own build and can refuse a foreign library without an SDK. Only the library ships, never src/. A lean single-binary export compiles the plugin's source into the binary instead.

A plugin that needs more than its library in a game, or that a project needs without any scene naming it, declares it with renzora::register_plugin_export:

fn build(&self, _app: &mut App) {
    renzora::register_plugin_export(renzora::PluginExport {
        plugin: "my-plugin".into(),             // the plugin's folder name
        script_extensions: vec!["mls".into()],  // files that mean a project needs it
        stage: Some(stage),
        options: vec![renzora::PluginExportOption {
            id: "bundle_runtime".into(),
            label: "Bundle the runtime".into(),
            help: "Players install nothing.".into(),
            default: true,
        }],
        ..Default::default()
    });
}

fn stage(target: &renzora::PluginExportTarget) -> Result<renzora::PluginExportFiles, String> {
    let mut out = renzora::PluginExportFiles::default();
    if target.option("bundle_runtime") {
        out.copy("runtime/data.bin", target.project_root.join(".cache/data.bin"));
    }
    out.bytes("manifest.txt", "version=1\n");
    Ok(out)
}

stage runs on the editor's main thread when an export starts. It receives the project root, the platform folder name and whether the plugin is linked into a lean binary. The files it lists land in the plugin's own folder in the game (archive_copy / archive_bytes put them in the game's archive instead), where renzora::plugin_dir("<id>") and renzora::game_archive_read find them at run time. An Err stops the export with its message.

Things that will catch you once

  • Give scene content a Name. The Hierarchy lists (Entity, &Name), the scene saver writes only named entities, and in a shipped game (or the editor during play) an unnamed entity with a Transform is despawned by reject_unnamed_entities. For helper geometry that is not scene content, add renzora::HideInHierarchy rather than a name, so it is not written into saved scenes. Entities of a global scene use Persistent.
  • Spawn scene content on OnEnter(SplashState::Editor), not Startup. The project-load teardown despawns named entities after Startup.
  • rustc's diagnostics name renzora_dylib. The --extern alias hides the real crate name everywhere except error messages.

See also