Add co-op, off by default: rooms through a relay, progress sync between the players' own saves, and a Co-op tab.

Each player keeps their own game and save. One player makes a room, the others join it with its secret, and story flags and, by the room's rules, items, songs, quest items, small keys (a shared pool), rupees and ammunition reach every save. The layer is a host thread with one UDP socket to a relay the players run themselves (tools/coop_relay.py, Python's standard library only); the messages between the players are sealed with a key from the room's secret (XChaCha20-Poly1305, Monocypher 4.0.2 vendored with its licence and a row in THIRD_PARTY_NOTICES.md), every change is signed by the player who made it, a player accepts a room by its members' keys and the key of its rules, the save is copied to coop-backup before anything is applied, and values are set through the game's own routines between frames. The Co-op tab makes, joins, accepts, removes and leaves, and the menu takes typed text for the name, the relay and the secret. This is the contributor's pull request #518 on this line.

Nothing reaches the network while co-op is off: the socket is opened, and the relay's name looked up, only after co-op is on, a relay is set and a room is made or joined. There is no default relay.

Left out of the pull request: the other players drawn in the world (the "puppets") with the copied engine they need. They were unfinished, added two patch sites to the game's per-frame update that could not be put through the differential tests here, and their file helpers clashed with the ones this line already has. So no position is sent: the POS message's codec stays, nothing sends it.

Changed against the pull request:
- The menu's test hook drives the Co-op tab only when it comes from the environment. From nx2navi.ini it could turn co-op on, join a room and accept its offer.
- The log's label for a room is hashed from the secret instead of being its first three characters.
- A game that has not saved yet has no save to copy, and takes nothing from the room until it has saved once; that used to count as a copy made.
- A PING's token no longer carries the PC's uptime.
- On Windows the maker's key file and its folder get an access list with the player's account alone.
- The tests that link the platform layer get the socket library, and the save test a stand-in for the title screen's hook.

Checked on Windows, where the pull request had never been compiled: the runtime builds and links; --probe-coop passes (sockets on 127.0.0.1, the codec against its byte vectors, a fuzz of every parser, two sessions through a hostile mock relay, a relay restart, the sync engine); tests/coop_relay_test.py passes its 61 tests, with the probe through the real relay on 127.0.0.1 at 0 and 30 % loss; tests/coop_page_check.py passes and its deliberate fault fails 48 checks. Not checked: two games in one room (one game runs at a time here), any real network, the tab in the game.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-11 00:11:41 -05:00
parent 47f6aef2f6
commit 7e07e13bac
59 changed files with 20598 additions and 46 deletions

View File

@@ -359,6 +359,7 @@ void plat_file_delete(const char *path);
int plat_file_truncate(const char *path, uint64_t size); // 1 on success
int64_t plat_file_size(const char *path); // bytes of a file, -1 when missing or unreadable
int plat_file_set_readonly(const char *path, int on); // sets or clears the read-only attribute; 1 on success
int plat_file_private(const char *path); // only this account may read it (0600, a folder 0700); 1 on success
// A file that only grows and other processes may read meanwhile: each write lands whole at its end, from any thread,
// and reaches the file even if the process dies right after (no user-space buffer, no flush to the disk).
typedef struct PlatAppend PlatAppend;
@@ -447,8 +448,12 @@ int plat_is_wine(void); // 1 when the Windows
// The file the process's stderr (the log) goes to, as a UTF-8 path; 0 when stderr is not a file (a console, a pipe).
int plat_stderr_path(char *out, size_t cap);
// ---- desktop: the in-game menu's "Copy system info" and "Open log folder"
// ---- desktop: the in-game menu's "Copy system info", "Open log folder" and the Co-op tab's secret
int plat_clipboard_set(const char *utf8); // replaces the clipboard's text; 1 on success
// The clipboard's text as UTF-8 in out (cap bytes with the NUL; a longer text: "" on Windows, cut at a character
// elsewhere); 1 when it had text. With SDL only on the window thread, as SDL asks: the menu goes through
// gfx_window_paste (ui/menu.h).
int plat_clipboard_get(char *out, size_t cap);
int plat_open_folder(const char *path); // shows the folder in the file manager; 1 on success
// ---- dynamic libraries: a library some machines lack (a GPU vendor's), loaded at run time. A bare name is searched
@@ -519,6 +524,34 @@ int plat_ipc_read(PlatIpc *p, void *buf, size_t n, uint32_t ms);
int plat_ipc_write(PlatIpc *p, const void *buf, size_t n); // 1 when all n bytes were written, 0 the connection broke
void plat_ipc_close(PlatIpc *p);
// ---- UDP sockets: the co-op layer's transport (coop.c). One unconnected datagram socket per PlatUdp; who a datagram
// came from is checked by the protocol, not by the OS. On Windows the first call of any of these starts Winsock.
// A PlatAddr is an IPv6 address (an IPv4 one as ::ffff:a.b.c.d), the port and the scope id in the same layout on every
// OS, so two equal addresses compare equal bytewise (plat_addr_eq) whichever way the OS reported them.
typedef struct PlatUdp PlatUdp;
typedef struct { uint8_t b[32]; } PlatAddr;
// host (a name, or an IPv4 or IPv6 literal without brackets) and port into *out. Blocks while the resolver runs: call
// it on a thread that may wait, never on the game's. An IPv4 answer is preferred over an IPv6 one. 1 on success.
int plat_net_resolve(const char *host, uint16_t port, PlatAddr *out);
// A non-blocking UDP socket bound to port (0: any free port) on every local address. dual_stack 1: IPv6 with IPv4 in the
// same socket (IPv4 alone where the machine has no IPv6); 0: IPv4 only. NULL on failure.
PlatUdp *plat_udp_open(uint16_t port, int dual_stack);
uint16_t plat_udp_port(const PlatUdp *u); // the port it is bound to; 0 on failure
// One datagram to *to; never blocks. 1 when it was sent or the OS dropped it (a full send buffer, no route, an ICMP
// error: a datagram may be lost anyway), 0 when it can never be sent (too long, an address the socket cannot reach).
int plat_udp_send(PlatUdp *u, const PlatAddr *to, const void *p, size_t n);
// The next datagram into buf, waiting at most ms for it; *from (may be NULL) gets its sender. Bytes received (a datagram
// longer than cap is cut to cap), 0 when none came in time, -1 when the socket is unusable.
int plat_udp_recv(PlatUdp *u, void *buf, size_t cap, PlatAddr *from, uint32_t ms);
void plat_udp_close(PlatUdp *u);
// Address helpers that need no OS (common.c); the back ends build addresses with plat_addr_set.
void plat_addr_set(PlatAddr *a, const uint8_t ip[16], uint16_t port, uint32_t scope);
void plat_addr_get(const PlatAddr *a, uint8_t ip[16], uint32_t *scope);
uint16_t plat_addr_port(const PlatAddr *a);
int plat_addr_eq(const PlatAddr *a, const PlatAddr *b);
// "1.2.3.4:5" or "[::1]:5" ("%scope" after a link-local address) into out; its length, 0 when it does not fit cap.
int plat_addr_str(const PlatAddr *a, char *out, size_t cap);
// ---- restart: this program again, in a new process that starts once this one is gone
// Starts this executable again with the same command line, environment and working folder, and no handle of this
// process. 1 when it started; the caller then exits.