Put the AdminBridge GameScript under version control
Continuous Integration / lint-and-security (pull_request) Failing after 19s
Continuous Integration / tests-and-coverage (pull_request) Successful in 24s

The last commit noted in passing that the server-side half of the admin
GameScript channel "is not in this repo -- docker/config is gitignored --
so it has to be updated separately for any of this to work." That was true
of all nine features the README documents: list_vehicles, list_stations,
list_cargo, get_timetable, get_station, get_station_cargo, get_dispatch and
the event stream all answer from 705 lines of Squirrel that no clone could
reproduce, no reviewer could see, and CI never touched.

The bridge now lives in gamescript/AdminBridge/ with its own README, the
same arrangement docker/patches/ uses for the local JGRPP patches, and
docker-compose.yml bind-mounts it read-only over the container's
game/AdminBridge. docker/config stays ignored -- it also holds savegames,
downloaded content and generated config -- so the copy under it is now
shadowed and can be deleted. main.nut is byte-identical to what was running,
apart from the version work below.

Adds a version handshake, because the channel gives no way to tell a stale
bridge from a hung one: a bridge that does not recognise a command drops it
silently, so a client ahead of the server sees nothing but timeouts. The
bridge now answers get_version with its protocol version plus its command
and event catalogues, and get_bridge_version() raises when that is below
GS_BRIDGE_VERSION. It is opt-in rather than checked on connect: GameScripts
do not tick while the game is paused, so an automatic check would refuse to
connect to a paused server. A bridge older than 4 predates get_version
itself and can only fail by timing out, so the E2E test catches that and
reports it by name instead.

tests/test_gamescript.py gives CI a foothold on the GameScript without a
Squirrel toolchain: it parses the .nut files and pins the protocol version
across info.nut, main.nut and protocol.py, the event catalogue against
GameEventType, and the command table against the commands client.py sends.
The version has to be declared three times because a GameScript cannot read
its own info.nut at runtime -- GSController.GetVersion() returns the OpenTTD
version, not the script's.

info.nut also gains MinVersionToLoad() { return 1; }. The engine defaults it
to GetVersion(), so without it this bump would orphan every savegame pinned
to version 3: the scanner finds no compatible script and falls back with a
warning. The bridge keeps no savegame state, so any version can take over.

HandleCommand now dispatches through the same table get_version reports,
rather than an if/else chain, so the catalogue a client feature-detects
against cannot drift from what is implemented.

Co-Authored-By: Claude <[email protected]>
This commit is contained in:
2026-08-31 19:06:33 +02:00
co-authored by Claude
parent 90a07392cf
commit f7ca395a4f
17 changed files with 1110 additions and 7 deletions
+31
View File
@@ -657,6 +657,37 @@ class OpenTTDAdminClient:
raise ValueError(f"{context}: {data['error']}")
return data
async def get_bridge_version(self, minimum=None, timeout=5.0):
"""Ask the AdminBridge GameScript which protocol version it speaks, and check it is new enough.
Every other GameScript method here needs a bridge that understands the command it sends.
Against an older bridge those commands are simply ignored and the caller waits out its
timeout with no explanation, so call this once after connecting to turn that into an
immediate, named failure.
Returns a dict with "version" (the bridge's protocol version), "commands" (the command
names it answers) and "events" (the event kinds it can push) — the two catalogues allow
feature-detecting a single command instead of comparing version numbers.
`minimum` defaults to GS_BRIDGE_VERSION, the version this client is written against;
pass 0 to read the version without requiring anything of it.
Raises ValueError if the bridge is older than `minimum`, and asyncio.TimeoutError if it
does not answer at all — note that a bridge predating get_version itself (version 3 and
earlier) can only fail that second way, as can a paused game or a server with no bridge
loaded. ConnectionError if the admin connection drops while waiting.
"""
from .protocol import GS_BRIDGE_VERSION
if minimum is None:
minimum = GS_BRIDGE_VERSION
data = await self._gs_query({"command": "get_version"}, timeout, "get_version")
version = data.get("version", 0)
if version < minimum:
raise ValueError(
f"AdminBridge GameScript is version {version}, but at least {minimum} is "
f"required; update the server's copy from gamescript/AdminBridge/")
return data
async def get_timetable(self, vehicle_id, timeout=5.0):
"""Fetch an authoritative timetable snapshot for a vehicle via the AdminBridge GameScript.
+7
View File
@@ -220,6 +220,13 @@ class NetworkAuthenticationMethod(IntEnum):
X25519_PAKE = 1
X25519_AuthorizedKey = 2
# Version of the AdminBridge GameScript's JSON protocol this client is written against. The
# bridge reports its own version via get_version (OpenTTDAdminClient.get_bridge_version()), and
# an older one will not understand everything sent here. The bridge source lives in
# gamescript/AdminBridge/; tests/test_gamescript.py keeps this in step with the version declared
# there, so bumping one without the other fails in CI rather than at runtime.
GS_BRIDGE_VERSION = 4
class GameEventType(StrEnum):
"""Event kinds the AdminBridge GameScript can push over the Admin Network.