mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-16 01:49: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>
94 lines
4.3 KiB
C++
94 lines
4.3 KiB
C++
// SDL3 adapter of the in-game menu: the window's events to menu events (menu.h), as ui/menu_win.cpp does for Win32.
|
|
// Window thread (gfx_window_sdl.c's event loop, after input_sdl_event).
|
|
//
|
|
// F1 opens and closes the menu, Esc opens it (with the menu open, Esc goes to the menu, which closes). Esc also releases
|
|
// the cursor (input_sdl.c sees the event first; closing the menu gives the capture back). While the menu is open, its
|
|
// keys, the mouse and focus changes go to it; the game reads the keyboard through input_sdl.c and gets nothing while
|
|
// the menu is open (input_menu). Mouse positions come in window units and go to the menu in pixels.
|
|
#include <SDL3/SDL.h>
|
|
|
|
#include "imgui.h"
|
|
|
|
extern "C" {
|
|
#include "input.h"
|
|
#include "input_sdl.h"
|
|
#include "ui/menu.h"
|
|
}
|
|
|
|
static ImGuiKey key_of(SDL_Scancode sc) {
|
|
switch (sc) {
|
|
case SDL_SCANCODE_UP: return ImGuiKey_UpArrow;
|
|
case SDL_SCANCODE_DOWN: return ImGuiKey_DownArrow;
|
|
case SDL_SCANCODE_LEFT: return ImGuiKey_LeftArrow;
|
|
case SDL_SCANCODE_RIGHT: return ImGuiKey_RightArrow;
|
|
case SDL_SCANCODE_RETURN: case SDL_SCANCODE_KP_ENTER: return ImGuiKey_Enter;
|
|
case SDL_SCANCODE_SPACE: return ImGuiKey_Space;
|
|
case SDL_SCANCODE_ESCAPE: return ImGuiKey_Escape;
|
|
case SDL_SCANCODE_TAB: return ImGuiKey_Tab;
|
|
case SDL_SCANCODE_BACKSPACE: return ImGuiKey_Backspace;
|
|
case SDL_SCANCODE_DELETE: return ImGuiKey_Delete; // clears a cell of the remapping rows
|
|
case SDL_SCANCODE_HOME: return ImGuiKey_Home;
|
|
case SDL_SCANCODE_END: return ImGuiKey_End;
|
|
case SDL_SCANCODE_PAGEUP: return ImGuiKey_PageUp;
|
|
case SDL_SCANCODE_PAGEDOWN: return ImGuiKey_PageDown;
|
|
case SDL_SCANCODE_LSHIFT: case SDL_SCANCODE_RSHIFT: return ImGuiKey_LeftShift;
|
|
case SDL_SCANCODE_LCTRL: case SDL_SCANCODE_RCTRL: return ImGuiKey_LeftCtrl;
|
|
case SDL_SCANCODE_Q: return ImGuiKey_Q; // previous tab
|
|
case SDL_SCANCODE_E: return ImGuiKey_E; // next tab
|
|
default: return ImGuiKey_None;
|
|
}
|
|
}
|
|
|
|
extern "C" void menu_sdl_event(const SDL_Event *e) {
|
|
float d = (float)gfx_window_sdl_density();
|
|
switch (e->type) {
|
|
case SDL_EVENT_KEY_DOWN:
|
|
// Esc with the menu open (opened by F1 too) is not a toggle here: it goes to the menu, which closes itself
|
|
if (!e->key.repeat && (e->key.scancode == SDL_SCANCODE_F1 ||
|
|
(e->key.scancode == SDL_SCANCODE_ESCAPE && !menu_is_open()))) {
|
|
menu_toggle();
|
|
return;
|
|
}
|
|
if (e->key.scancode == SDL_SCANCODE_F10) {
|
|
if (!e->key.repeat) menu_perf_cycle();
|
|
return;
|
|
}
|
|
// a held Esc's repeats: the press that opened the menu is not queued, so its first repeat would close it
|
|
if (e->key.scancode == SDL_SCANCODE_ESCAPE && e->key.repeat) return;
|
|
[[fallthrough]];
|
|
case SDL_EVENT_KEY_UP:
|
|
if (!menu_is_open()) return;
|
|
if (ImGuiKey k = key_of(e->key.scancode)) menu_event_key(k, e->type == SDL_EVENT_KEY_DOWN);
|
|
return;
|
|
case SDL_EVENT_TEXT_INPUT: // UTF-8, between SDL_StartTextInput and SDL_StopTextInput (gfx_window_text_input)
|
|
if (!menu_is_open() || !e->text.text) return;
|
|
for (const unsigned char *p = (const unsigned char *)e->text.text; *p;) {
|
|
unsigned c = *p++, more = c >= 0xf0 ? 3 : c >= 0xe0 ? 2 : c >= 0xc0 ? 1 : 0;
|
|
if (more) c &= 0x3f >> more;
|
|
for (; more && (*p & 0xc0) == 0x80; more--) c = c << 6 | (*p++ & 0x3f);
|
|
if (!more && c >= 0x20 && c != 0x7f) menu_event_char(c);
|
|
}
|
|
return;
|
|
case SDL_EVENT_MOUSE_MOTION:
|
|
if (menu_is_open()) menu_event_mouse_pos(e->motion.x * d, e->motion.y * d);
|
|
return;
|
|
case SDL_EVENT_MOUSE_BUTTON_DOWN:
|
|
case SDL_EVENT_MOUSE_BUTTON_UP: {
|
|
if (!menu_is_open()) return;
|
|
int b = e->button.button == SDL_BUTTON_LEFT ? 0 : e->button.button == SDL_BUTTON_RIGHT ? 1
|
|
: e->button.button == SDL_BUTTON_MIDDLE ? 2 : -1;
|
|
if (b < 0) return;
|
|
menu_event_mouse_pos(e->button.x * d, e->button.y * d);
|
|
menu_event_mouse_button(b, e->type == SDL_EVENT_MOUSE_BUTTON_DOWN);
|
|
return;
|
|
}
|
|
case SDL_EVENT_MOUSE_WHEEL:
|
|
if (menu_is_open()) menu_event_wheel(e->wheel.x, e->wheel.y);
|
|
return;
|
|
case SDL_EVENT_WINDOW_FOCUS_GAINED:
|
|
case SDL_EVENT_WINDOW_FOCUS_LOST:
|
|
menu_event_focus(e->type == SDL_EVENT_WINDOW_FOCUS_GAINED);
|
|
return;
|
|
}
|
|
}
|