mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-16 18:09:58 -04:00
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>
90 lines
4.1 KiB
C
90 lines
4.1 KiB
C
// runtime/fs_host.c, save_share.c and save_zip.c without the game, driven by tests/save_share_check.py. Each run of
|
|
// this program is one start of the game on the profile folder it is given: the first storage use runs storage_init,
|
|
// which makes a waiting import the save (fs_host.c import_pending). The rest of the runtime is stubbed below: only what
|
|
// the save paths call does anything.
|
|
// save_share_check export PROFILE DIR [bug] save_share_export into DIR; prints "result N path P"
|
|
// save_share_check import PROFILE ZIP save_share_import; prints "result N detail D waiting W"
|
|
// save_share_check start PROFILE a start and nothing else; prints "done D waiting W"
|
|
// save_share_check cancel PROFILE save_share_cancel; prints "result N waiting W"
|
|
// Backups go to saves/ beside this program (save_share_backup_dir), as the game's go beside the game.
|
|
#include <stdarg.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "save_share.h"
|
|
|
|
// fs.h's own declarations of these would clash with the stubs below, which only have to link
|
|
void fs_host_set_profile(const char *dir);
|
|
const char *fs_save_import_done(void);
|
|
int fs_save_export(int (*visit)(void *ctx, const char *rel, const uint8_t *data, size_t n), void *ctx);
|
|
|
|
static int count_file(void *ctx, const char *rel, const uint8_t *data, size_t n) {
|
|
(void)ctx, (void)rel, (void)data, (void)n;
|
|
return 1;
|
|
}
|
|
|
|
// ---- stubs for the runtime around fs_host.c (host.h and the files that define these in the game)
|
|
const int rt_game_version = 111;
|
|
const char rt_port_version[] = "9.9.9-check";
|
|
|
|
void rt_log(const char *fmt, ...) {
|
|
va_list ap;
|
|
va_start(ap, fmt);
|
|
fputs("log: ", stderr);
|
|
vfprintf(stderr, fmt, ap);
|
|
fputc('\n', stderr);
|
|
va_end(ap);
|
|
}
|
|
|
|
void rt_fatal(const char *fmt, ...) {
|
|
va_list ap;
|
|
va_start(ap, fmt);
|
|
fputs("FATAL: ", stderr);
|
|
vfprintf(stderr, fmt, ap);
|
|
fputc('\n', stderr);
|
|
va_end(ap);
|
|
exit(99);
|
|
}
|
|
|
|
const char *rt_log_path(void) { return getenv("SAVE_SHARE_CHECK_LOG"); }
|
|
void *rt_config(void) { return NULL; } // fs_host_set_profile names the profile first
|
|
void account_uid(uint64_t uid[2]) { uid[0] = 0x1111222233334444ull, uid[1] = 0x5555666677778888ull; }
|
|
int folder_downloads(char *out, size_t cap) { (void)out, (void)cap; return 0; }
|
|
|
|
// never reached on the save paths: RomFS, the host function registry, guest memory, the probes
|
|
#define NEVER(name) \
|
|
void name(void) { rt_fatal(#name " called"); }
|
|
NEVER(fs_init) NEVER(hostfn_override) NEVER(hostfn_register) NEVER(mem_kernel_alloc) NEVER(mem_kernel_free)
|
|
NEVER(mem_thaw) NEVER(romfs_close) NEVER(romfs_entry) NEVER(romfs_entry_type) NEVER(romfs_file_count)
|
|
NEVER(romfs_handle) NEVER(romfs_image_path) NEVER(romfs_list) NEVER(romfs_open) NEVER(romfs_overlay)
|
|
NEVER(romfs_overlay_count) NEVER(romfs_read) NEVER(romfs_size) NEVER(rt_call)
|
|
int romfs_owns(const char *path) { (void)path; return 0; }
|
|
void fs_trace(const char *what, const char *path) { (void)what, (void)path; }
|
|
void coop_game_rom_open(const char *path) { (void)path; } // co-op watches for the title screen's start: not here
|
|
|
|
int main(int argc, char **argv) {
|
|
if (argc < 3) return 2;
|
|
fs_host_set_profile(argv[2]);
|
|
const char *mode = argv[1];
|
|
if (!strcmp(mode, "export") && argc >= 4) {
|
|
char path[2048];
|
|
SaveShareResult r = save_share_export(argv[3], argc > 4 && !strcmp(argv[4], "bug"), path, sizeof path);
|
|
printf("result %d path %s\n", (int)r, path);
|
|
} else if (!strcmp(mode, "import") && argc == 4) {
|
|
char detail[256];
|
|
SaveShareResult r = save_share_import(argv[3], detail, sizeof detail);
|
|
printf("result %d detail %s waiting %d\n", (int)r, detail, save_share_pending());
|
|
} else if (!strcmp(mode, "start")) {
|
|
fs_save_export(count_file, NULL); // the first storage use: storage_init and its import_pending
|
|
printf("done %s waiting %d\n", fs_save_import_done(), save_share_pending());
|
|
} else if (!strcmp(mode, "cancel")) {
|
|
int r = save_share_cancel();
|
|
printf("result %d waiting %d\n", r, save_share_pending());
|
|
} else {
|
|
return 2;
|
|
}
|
|
return 0;
|
|
}
|