This repository has been archived on 2026-10-07. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
pz-rcon-data-bridge/README.md
T

120 lines
6.2 KiB
Markdown

# RCON Data Bridge
A Project Zomboid (B42) server mod that turns the existing RCON connection into a small, allowlisted RPC surface. It adds no new port and no new authentication. Anything that can already run `changeoption` and `showoptions` can call it.
Out of the box it exposes world and player statistics and a few admin operations. This README is for mod developers who want to **register their own operations** so that RCON tools and bots can call into their mod.
## Adding your own op
An op is a named Lua function. External callers invoke it by name, pass a table of arguments, and get back either data or an error code.
### 1. Depend on this mod
In your mod's `mod.info`:
```
require=RconDataBridge
```
### 2. Register from `Events.OnServerStarted`
Put this in a server-side Lua file, for example `media/lua/server/MyMod/MyMod_Ops.lua`:
```lua
local function onServerStarted()
RDB_OpRegistry.register("mymod_get_stash", {
argsSchema = { name = "string" },
handler = function(args)
local stash = MyMod.findStash(args.name)
if not stash then
return false, "STASH_NOT_FOUND", "No stash named " .. args.name
end
return true, { name = stash.name, items = stash.itemCount }
end,
})
end
Events.OnServerStarted.Add(onServerStarted)
```
Do not register at file top level. Load order between mods is not guaranteed, so `RDB_OpRegistry` may not exist yet when your file first runs. `OnServerStarted` fires after every mod's files have loaded.
### The handler contract
A handler receives the request's `args` table (never `nil`) and returns one of:
| Return | Meaning |
|---------------------------------|-------------------------------------------------|
| `true, data` | Success. `data` is any JSON-serializable table. |
| `false, "CODE", "message"` | A normal, expected failure. |
- **Error codes are free-form strings.** The built-in codes (`SCHEMA_ERROR`, `OP_NOT_ALLOWED`, `RATE_LIMITED`, and so on) are used by the bridge itself, but you can return any code of your own.
- **A handler that throws** is caught by the bridge. The caller receives `INTERNAL_ERROR` and the real Lua error is logged server-side only. It never reaches the client.
- **`argsSchema`** is optional and shallow. It maps each required top-level argument to a Lua type name (`"string"`, `"number"`, `"boolean"`, `"table"`). A missing or wrongly typed argument is rejected with `SCHEMA_ERROR` before your handler runs. Anything more specific (ranges, nested shapes, optional arguments) is your handler's job to check.
- **Handlers run synchronously on the server tick.** Keep them fast, and do not block.
### Naming
Op names are flat `snake_case`, verb first, with no dots. All ops share one namespace, this mod's own included, and registering an existing name silently replaces it. Prefix yours with your mod's name (`mymod_get_stash`) to avoid collisions.
## Things to know before you ship
**Keep responses small.** Responses travel through a single server option with a payload budget of about 4 KB of JSON, shared by every response the bridge currently holds. A single response larger than that budget is never delivered, and the caller simply times out. Return summaries, and paginate large collections with a cursor argument (see how `list_players` does it).
**Requests are rate limited and idempotent.** There is one global limit (10 requests per 10 seconds by default, admin-tunable in the mod's Sandbox Options). A caller that re-sends the same request `id` within 60 seconds gets the original cached response back and your handler does not run again. This protects side-effecting ops from retries, but it also means a handler is not guaranteed to run once per call.
**Admins can switch your op off.** The `set_op_enabled` operation lets an admin disable any non-core op at runtime. A disabled op answers `OP_NOT_ALLOWED`. That state lives in memory only and resets on restart.
**Removing an op.** `RDB_OpRegistry.unregister("mymod_get_stash")` removes it entirely. You rarely need this, since ops last until the server stops.
**Discovery.** Registered ops appear in `RconDataBridge_ProtocolVersion` (the `ops` array) and in the `list_ops` operation, which also reports whether each op is enabled.
## Calling an op
The wire protocol is the same for every op, built-in or not. A request is a compact JSON **array** of envelopes, base64 encoded, written with `changeoption`:
```
changeoption RconDataBridge_Request <base64>
```
where the decoded payload is:
```json
[{"v":2,"id":"any-unique-id","op":"mymod_get_stash","args":{"name":"north"}}]
```
Responses appear (also base64 encoded) in `RconDataBridge_Response`, as an array you filter by `id`:
```json
[{"v":2,"id":"any-unique-id","ok":true,"data":{"name":"north","items":12}}]
```
Requests must be compact JSON with no whitespace, and the whole decoded batch is limited to 4096 bytes. Several envelopes can share one `changeoption` call, and they are queued and processed in order.
Rather than speak this by hand, use the companion TypeScript client, `rcon-data-bridge-client`. Give it a [Zod](https://zod.dev) schema for your op and you get validated, fully typed results:
```ts
import { z } from "zod";
import { RconDataBridgeClient } from "rcon-data-bridge-client";
const client = await RconDataBridgeClient.connect({ host: "127.0.0.1", port: 27015, password: "..." });
const Stash = z.object({ name: z.string(), items: z.number() });
const stash = await client.with(Stash).call("mymod_get_stash", { name: "north" });
// stash: { name: string; items: number }
```
## Testing your op locally
1. Enable both mods on a dedicated server and set an `RCONPort` and `RCONPassword` in its `.ini`.
2. Start the server and wait for `[RconDataBridge] INFO: bootstrap complete` in the log.
3. Call `list_ops` and check that your op is listed.
4. Call your op. Lua changes need a full server restart, since there is no hot reload.
The `IllegalArgumentException` lines from `ServerOptions.addOption` during boot are harmless. They appear when the save already contains the bridge's options from a previous run.
## License
AGPL-3.0-or-later. See `LICENSE`.