Engine api

This commit is contained in:
2026-10-06 20:27:17 -03:00
parent d13938550d
commit 69c5c187db
27 changed files with 1026 additions and 90 deletions
+11 -4
View File
@@ -41,11 +41,15 @@ The code is in `common/mod_loader.ts`, `common/mod_data.ts`, `common/mod_check.t
**In the API types but throwing:** forms, HUD elements, keybinds, `g.slider`, `register_terrain`.
**Not built at all:** common scripts, the registry freeze and hash, overrides, engine access.
**Not built at all:** common scripts, the registry freeze and hash, overrides.
**Engine access** is built (phase 3): `bworld/engine`, `hook`, `engine_access` and its checks, and `physics` as the
first plain object mods can hook. See [Engine Access](modding/misc/engine.md). The code is in `common/hooks.ts`,
`common/mod_api/engine.ts` and the `engine_modules.ts` of each side.
**Known gaps:**
- Manifest `dependencies` version ranges and `game_version` aren't checked.
- Manifest `dependencies` version ranges aren't checked, and `game_version` only is for engine access mods.
- Mods can't throw a `CommandError` the server recognizes: it's checked with `instanceof`, and the class isn't in the
API, so a mod's bundled copy is a different class. Export it from `bworld/server`, or recognize it by `name`.
- Tools only lose durability when their owner dies. Nothing else uses it.
@@ -183,8 +187,8 @@ function hook<T, K extends keyof T>(target: T, method: K, handlers: {
before?(self: T, args: Parameters<T[K]>): { result: ReturnType<T[K]> } | void;
// wraps the method, calling original (or not)
replace?(self: T, args: Parameters<T[K]>, original: T[K]): ReturnType<T[K]>;
// runs last, and returns the result the caller gets
after?(self: T, args: Parameters<T[K]>, result: ReturnType<T[K]>): ReturnType<T[K]>;
// runs last, and returns the result the caller gets. undefined keeps the result it was given
after?(self: T, args: Parameters<T[K]>, result: ReturnType<T[K]>): ReturnType<T[K]> | void;
// lower runs earlier, the default is 1000. ties go in load order
priority?: number;
}): () => void; // unhook
@@ -196,6 +200,9 @@ function hook<T, K extends keyof T>(target: T, method: K, handlers: {
- Types are the engine's own source, so `check-mods` typechecks against exactly the engine the mod will run on.
- Several mods can hook one method: `before` hooks run in priority order, `replace` hooks nest, `after` hooks chain
results. A hook that throws is logged under its mod and skipped.
- `hook` only works while a mod's script loads or runs `setup`, which is how it knows the mod. Each side clears every
hook before it loads mods, so tests and a client joining another server start clean.
- The wrapper is rebuilt when hooks change, with only the steps they use, and the last unhook puts the original back.
- Module exports can't be reassigned, so functions mods may want to change move onto plain objects the engine calls
through (like `engine.common.physics.move_body`). `#private` members can't be hooked, so they're kept for things with
rules that must hold, like a container's slots.