Engine api
This commit is contained in:
@@ -71,7 +71,8 @@ matter, the `id` inside does, except for [tags](../resources/tags.md), whose id
|
||||
| `description` | no | A sentence or two. |
|
||||
| `version` | yes | The mod's own version, semver like `1.0.0`. |
|
||||
| `authors` | no | A list of names. |
|
||||
| `game_version` | no | The bworld versions the mod works with, as a semver range. Not checked yet. |
|
||||
| `game_version` | no | The bworld versions the mod works with, as a semver range. Only checked for mods with `engine_access`. |
|
||||
| `engine_access` | no | `true` lets the scripts import `bworld/engine` and hook the engine. See [Engine Access](../misc/engine.md). |
|
||||
| `dependencies` | no | Other mods, `{ "id", "version" }`. A missing dependency is an error, and dependencies load first. The version range isn't checked yet. |
|
||||
| `scripts` | no | Paths to the `server`, `client`, `shared` and `worldgen` scripts. See [Sides](../concepts/sides.md). |
|
||||
| `credits` | no | A markdown file for licenses and thanks, shown on the Credits screen. Raw HTML in it is shown as text. |
|
||||
@@ -93,7 +94,7 @@ so they can import other files and packages, but code imported by several script
|
||||
When a mod's folder has a `deno.json`, its scripts are bundled and typechecked with it, so the mod can have its own
|
||||
imports and npm or JSR packages. Mods without one use the `deno.json` of the folder the build runs in, like the mods in
|
||||
this repository. That config maps the API types, which scripts import as `bworld/server`, `bworld/client`,
|
||||
`bworld/shared` and `bworld/worldgen`:
|
||||
`bworld/shared` and `bworld/worldgen`, and [`bworld/engine`](../misc/engine.md):
|
||||
|
||||
```ts
|
||||
import type { ServerContext } from "bworld/server";
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
# Engine Access
|
||||
|
||||
For what the Game API doesn't cover yet, like a new kind of rendering, different player physics or a new entity type, a
|
||||
mod can reach into the engine itself and hook its methods, like NeoForge's mixins. It isn't a stable API: anything in
|
||||
the engine can change in any version, so a mod using it is tied to one minor version of the game.
|
||||
|
||||
Prefer the Game API. Many mods hooking the same thing is a sign it should become part of it.
|
||||
|
||||
## Asking for It
|
||||
|
||||
```json
|
||||
{ "engine_access": true, "game_version": "~0.1.0" }
|
||||
```
|
||||
|
||||
`game_version` has to be one minor version: `~0.1.0` (0.1.0 and later 0.1 patches) or an exact `0.1.2`. Loading the mod
|
||||
on another version is a load error, so the server doesn't start with it.
|
||||
|
||||
Without `engine_access`, importing `bworld/engine` is an error in `check-mods`, when a `.bmod` loads, and when the
|
||||
script runs. When players join from another site, the confirm screen marks mods that change the engine.
|
||||
|
||||
## Hooking
|
||||
|
||||
```ts
|
||||
// server.ts: broken blocks drop twice
|
||||
import { engine, hook } from "bworld/engine";
|
||||
|
||||
export function setup() {
|
||||
hook(engine.server.GameServer.prototype, "pop_item", {
|
||||
after(game, [x, y, z, stack], entity) {
|
||||
game.spawn_item(entity.x, entity.y, entity.z, stack.clone());
|
||||
return entity;
|
||||
},
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
`hook(target, method, handlers)` changes `target[method]`, usually on a class's prototype, so every instance already
|
||||
made changes too. It returns a function that unhooks it.
|
||||
|
||||
| Handler | Runs |
|
||||
| ------------------------------- | ------------------------------------------------------------------------------------------------------- |
|
||||
| `before(self, args)` | First. Change `args` in place, or return `{ result }` to skip the method and every other hook after it. |
|
||||
| `replace(self, args, original)` | Instead of the method. Call `original(...args)` to run it, or don't. |
|
||||
| `after(self, args, result)` | Last. Returns the result the caller gets. Returning `undefined` keeps the result it was given. |
|
||||
| `priority` | Lower runs earlier, the default is `1000`. Ties go in load order. |
|
||||
|
||||
- Several mods can hook one method. `before` hooks run in priority order, `replace` hooks nest with the earliest
|
||||
outermost, and `after` hooks chain results.
|
||||
- A hook that throws is logged under its mod (`[my_mod] an after hook on GameServer.pop_item threw: ...`) and skipped. A
|
||||
`replace` hook that throws after calling `original` gives what `original` returned, so the method doesn't run twice.
|
||||
- `hook` only works while the mod's script loads or runs `setup`. Unhooking works any time.
|
||||
- Unhooking a method's last hook puts the original back, so a method nobody hooks costs nothing.
|
||||
|
||||
## What's in `engine`
|
||||
|
||||
| Side | Where | Like |
|
||||
| -------- | -------------- | ------------------------------------------------------------------------------------------- |
|
||||
| `common` | every script | `EverythingRegistry`, `ItemStack`, `Container`, `BlockRules`, `OverworldTerrain`, `physics` |
|
||||
| `server` | server scripts | `GameServer`, `ServerWorld`, `ServerPlayer`, `Entity`, `Mob`, `ModRuntime`, the mob goals |
|
||||
| `client` | client scripts | `Client`, `LocalPlayer`, `ClientLevel`, `GameRenderer`, `Camera`, `GuiScreen` |
|
||||
|
||||
The full lists are in `common/engine_modules.ts`, `server/game/engine_modules.ts` and `client/engine_modules.ts`. The
|
||||
types are the engine's own source, so `check-mods` typechecks against exactly the engine the mod runs on.
|
||||
`bworld/engine` also exports `game_version`, the version of the game that's running.
|
||||
|
||||
- Module exports can't be reassigned, so functions mods may want to change are on plain objects the engine calls
|
||||
through: `engine.common.physics.move_body` is how every entity moves, on the server and on clients.
|
||||
- `#private` members can't be hooked. They're kept for things with rules that must hold, like a container's slots.
|
||||
- Hooks reach one JavaScript realm. Chunk workers run their own copy of the engine, so a client hook doesn't change
|
||||
meshing. Worldgen scripts run on the server, next to the server script, so hooking `engine.common.OverworldTerrain`
|
||||
from either changes the terrain.
|
||||
|
||||
## Performance
|
||||
|
||||
Hooking is cheap to set up: hook in `setup`, before the game is running. Each call to a hooked method then goes through
|
||||
a wrapper that runs its hooks, which is fine for something that happens now and then, like a block breaking. On
|
||||
something called many times a tick, like `physics.move_body` or a method of every entity, the wrapper's cost adds up,
|
||||
and the engine can no longer inline the method into its callers.
|
||||
|
||||
## Security
|
||||
|
||||
A server mod with engine access controls the game worker completely, but the worker still has no Deno permissions. A
|
||||
client mod with it can change anything in the game page, which every client script can already reach. See
|
||||
[Security](../concepts/security.md).
|
||||
+11
-4
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user