mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-15 21:29: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>
122 lines
5.0 KiB
C++
122 lines
5.0 KiB
C++
// Win32 adapter of the in-game menu: window messages to menu events (menu.h). The only Win32 code of the menu; the SDL3
|
|
// window has its own (ui/menu_sdl.cpp). Window thread.
|
|
//
|
|
// 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_window_msg runs 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 polls the keyboard on its own (input_win.c) and gets nothing while the
|
|
// menu is open (input_menu).
|
|
#include <windows.h>
|
|
#include <windowsx.h>
|
|
|
|
#include "imgui.h"
|
|
|
|
extern "C" {
|
|
#include "input.h"
|
|
#include "input_win.h"
|
|
#include "ui/menu.h"
|
|
}
|
|
|
|
static ImGuiKey key_of(WPARAM vk) {
|
|
switch (vk) {
|
|
case VK_UP: return ImGuiKey_UpArrow;
|
|
case VK_DOWN: return ImGuiKey_DownArrow;
|
|
case VK_LEFT: return ImGuiKey_LeftArrow;
|
|
case VK_RIGHT: return ImGuiKey_RightArrow;
|
|
case VK_RETURN: return ImGuiKey_Enter;
|
|
case VK_SPACE: return ImGuiKey_Space;
|
|
case VK_ESCAPE: return ImGuiKey_Escape;
|
|
case VK_TAB: return ImGuiKey_Tab;
|
|
case VK_BACK: return ImGuiKey_Backspace;
|
|
case VK_DELETE: return ImGuiKey_Delete; // clears a cell of the remapping rows
|
|
case VK_HOME: return ImGuiKey_Home;
|
|
case VK_END: return ImGuiKey_End;
|
|
case VK_PRIOR: return ImGuiKey_PageUp;
|
|
case VK_NEXT: return ImGuiKey_PageDown;
|
|
case VK_SHIFT: return ImGuiKey_LeftShift;
|
|
case VK_CONTROL: return ImGuiKey_LeftCtrl;
|
|
case 'Q': return ImGuiKey_Q; // previous tab
|
|
case 'E': return ImGuiKey_E; // next tab
|
|
default: return ImGuiKey_None;
|
|
}
|
|
}
|
|
|
|
// The mouse buttons pressed on the open menu and not yet released, one bit each. While any is held the window has the
|
|
// mouse capture, so the release reaches the menu where the pointer went outside the window (else ImGui would hold the
|
|
// button down and miss the next click). The game itself only clips the cursor (input_win.c), it takes no capture.
|
|
static unsigned g_held;
|
|
|
|
extern "C" int menu_window_msg(HWND h, UINT m, WPARAM w, LPARAM l, LRESULT *res) {
|
|
switch (m) {
|
|
case WM_KEYDOWN:
|
|
case WM_SYSKEYDOWN:
|
|
// bit 30: auto-repeat
|
|
// Esc with the menu open (opened by F1 too) is not a toggle here: it goes to the menu, which closes itself
|
|
if (!(l & (1 << 30)) && (w == VK_F1 || (w == VK_ESCAPE && !menu_is_open()))) {
|
|
menu_toggle();
|
|
*res = 0;
|
|
return 1;
|
|
}
|
|
if (w == VK_F10) { // also keeps F10 from opening the window menu
|
|
if (!(l & (1 << 30))) menu_perf_cycle();
|
|
*res = 0;
|
|
return 1;
|
|
}
|
|
// a held Esc's repeats: the press that opened the menu is not queued, so its first repeat would close it
|
|
if (w == VK_ESCAPE && (l & (1 << 30))) return 0;
|
|
[[fallthrough]];
|
|
case WM_KEYUP:
|
|
case WM_SYSKEYUP:
|
|
if (!menu_is_open()) return 0;
|
|
if (ImGuiKey k = key_of(w)) menu_event_key(k, m == WM_KEYDOWN || m == WM_SYSKEYDOWN);
|
|
return 0; // F11 and the system keys keep working
|
|
case WM_CHAR: { // UTF-16 units (TranslateMessage); a character outside the BMP comes as a surrogate pair
|
|
static unsigned high;
|
|
unsigned c = (unsigned)w;
|
|
if (c >= 0xd800 && c < 0xdc00) {
|
|
high = c;
|
|
return 0;
|
|
}
|
|
if (c >= 0xdc00 && c < 0xe000) c = high ? 0x10000 + ((high - 0xd800) << 10) + (c - 0xdc00) : 0;
|
|
high = 0;
|
|
if (menu_is_open() && c >= 0x20 && c != 0x7f) menu_event_char(c);
|
|
return 0;
|
|
}
|
|
case WM_MOUSEMOVE:
|
|
if (menu_is_open()) menu_event_mouse_pos((float)GET_X_LPARAM(l), (float)GET_Y_LPARAM(l));
|
|
return 0;
|
|
case WM_LBUTTONDOWN: case WM_LBUTTONUP:
|
|
case WM_RBUTTONDOWN: case WM_RBUTTONUP:
|
|
case WM_MBUTTONDOWN: case WM_MBUTTONUP: {
|
|
int b = m == WM_LBUTTONDOWN || m == WM_LBUTTONUP ? 0 : m == WM_RBUTTONDOWN || m == WM_RBUTTONUP ? 1 : 2;
|
|
int down = m == WM_LBUTTONDOWN || m == WM_RBUTTONDOWN || m == WM_MBUTTONDOWN;
|
|
if (!down && (g_held & 1u << b)) { // also when the menu closed meanwhile: the capture goes either way
|
|
g_held &= ~(1u << b);
|
|
if (!g_held && GetCapture() == h) ReleaseCapture();
|
|
}
|
|
if (!menu_is_open()) return 0;
|
|
if (down) {
|
|
if (!g_held) SetCapture(h);
|
|
g_held |= 1u << b;
|
|
}
|
|
menu_event_mouse_pos((float)GET_X_LPARAM(l), (float)GET_Y_LPARAM(l));
|
|
menu_event_mouse_button(b, down);
|
|
return 0;
|
|
}
|
|
case WM_MOUSEWHEEL:
|
|
if (menu_is_open()) menu_event_wheel(0, (float)GET_WHEEL_DELTA_WPARAM(w) / WHEEL_DELTA);
|
|
return 0;
|
|
case WM_CAPTURECHANGED: // another window took the capture (ReleaseCapture above finds g_held empty)
|
|
if ((HWND)l != h && g_held) {
|
|
for (int b = 0; b < 3; b++)
|
|
if (g_held & 1u << b) menu_event_mouse_button(b, 0);
|
|
g_held = 0;
|
|
}
|
|
return 0;
|
|
case WM_SETFOCUS:
|
|
case WM_KILLFOCUS:
|
|
menu_event_focus(m == WM_SETFOCUS);
|
|
return 0;
|
|
}
|
|
return 0;
|
|
}
|