Single module modding

This commit is contained in:
2026-11-14 12:18:41 -03:00
parent 605008f584
commit 313098892e
134 changed files with 919 additions and 924 deletions
+13 -12
View File
@@ -21,10 +21,10 @@ runs. When players join from another site, the confirm screen marks mods that ch
## Hooking
```ts
// server.ts: broken blocks drop twice
// broken blocks drop twice
import { engine, hook } from "cubi/engine";
export function setup() {
export function server() {
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());
@@ -48,16 +48,17 @@ made changes too. It returns a function that unhooks it.
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.
- `hook` only works while a phase runs. Unhooking works any time. Hook inside a phase, not at the script's top level,
which runs once on each side, in whichever phase imports the script first.
- 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`, `NoiseTerrain`, `physics` |
| `server` | server scripts | `GameServer`, `ServerWorld`, `ServerPlayer`, `Entity`, `Mob`, `ModRuntime`, the mob goals |
| `client` | client scripts | `Client`, `LocalPlayer`, `ClientLevel`, `GameRenderer`, `Camera`, `GuiScreen` |
| Side | Where | Like |
| -------- | ------------ | ----------------------------------------------------------------------------------------- |
| `common` | every phase | `EverythingRegistry`, `ItemStack`, `Container`, `BlockRules`, `NoiseTerrain`, `physics` |
| `server` | server phase | `GameServer`, `ServerWorld`, `ServerPlayer`, `Entity`, `Mob`, `ModRuntime`, the mob goals |
| `client` | client phase | `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. `cubi/engine`
@@ -67,13 +68,13 @@ also exports `game_version`, the version of the game that's running.
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. The server generates chunks in worldgen workers, and each loads the worldgen scripts into its own copy, so
hooks on the terrain (`engine.common.NoiseTerrain`) go in the worldgen script. A server script's hooks there only
meshing. The server generates chunks in worldgen workers, and each loads the worldgen phases into its own copy, so
hooks on the terrain (`engine.common.NoiseTerrain`) go in the worldgen phase. The server phase's hooks there only
reach the few chunks the game generates itself, so the world would come out different in places.
## Performance
Hooking is cheap to set up: hook in `setup`, before the game is running. Each call to a hooked method then goes through
Hooking is cheap to set up: hook in a phase, 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.
@@ -81,5 +82,5 @@ 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
client mod with it can change anything in the game page, which every client phase can already reach. See
[Security](../concepts/security.md).