mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-17 15:49:57 -04:00
Merge co-op, off by default: rooms through a relay, progress sync between the players' own saves, and a Co-op tab.
Without the other players drawn in the world, which are unfinished. No relay address is built in, and with co-op off nothing opens a socket. Built on Windows and tested against a relay on this PC only; two games in one room and any real network have not been tried. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -9,6 +9,8 @@
|
||||
#include <linux/fs.h>
|
||||
#include <linux/futex.h>
|
||||
#include <linux/userfaultfd.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <poll.h>
|
||||
#include <pthread.h>
|
||||
#include <sched.h>
|
||||
@@ -1072,6 +1074,12 @@ int plat_file_set_readonly(const char *path, int on) {
|
||||
return chmod(path, m & 07777) == 0;
|
||||
}
|
||||
|
||||
int plat_file_private(const char *path) {
|
||||
struct stat st;
|
||||
if (stat(path, &st)) return 0;
|
||||
return chmod(path, S_ISDIR(st.st_mode) ? 0700 : 0600) == 0;
|
||||
}
|
||||
|
||||
int plat_file_list(const char *dir, const char *ext, int (*cb)(const char *name, void *ctx), void *ctx) {
|
||||
DIR *d = opendir(dir);
|
||||
if (!d) return 0;
|
||||
@@ -1828,6 +1836,22 @@ int plat_random(void *out, size_t n) {
|
||||
|
||||
int plat_clipboard_set(const char *utf8) { return SDL_SetClipboardText(utf8); }
|
||||
|
||||
int plat_clipboard_get(char *out, size_t cap) {
|
||||
if (!cap) return 0;
|
||||
out[0] = 0;
|
||||
char *t = SDL_GetClipboardText(); // "" when there is none, never NULL but on failure
|
||||
if (!t) return 0;
|
||||
size_t n = strlen(t);
|
||||
if (n >= cap) { // cut where a character starts, not inside one
|
||||
n = cap - 1;
|
||||
while (n && ((unsigned char)t[n] & 0xc0) == 0x80) n--;
|
||||
}
|
||||
memcpy(out, t, n);
|
||||
out[n] = 0;
|
||||
SDL_free(t);
|
||||
return n != 0;
|
||||
}
|
||||
|
||||
static void reap_main(void *arg) {
|
||||
waitpid((pid_t)(intptr_t)arg, NULL, 0);
|
||||
}
|
||||
@@ -2358,6 +2382,142 @@ void plat_ipc_close(PlatIpc *p) {
|
||||
free(p);
|
||||
}
|
||||
|
||||
// ---- UDP sockets (platform.h): the co-op layer's transport
|
||||
struct PlatUdp { int fd, v6; };
|
||||
|
||||
static void addr_from_sockaddr(PlatAddr *out, const struct sockaddr_storage *ss) {
|
||||
uint8_t ip[16] = {0};
|
||||
if (ss->ss_family == AF_INET6) {
|
||||
const struct sockaddr_in6 *a = (const struct sockaddr_in6 *)ss;
|
||||
memcpy(ip, &a->sin6_addr, 16);
|
||||
plat_addr_set(out, ip, ntohs(a->sin6_port), a->sin6_scope_id);
|
||||
} else {
|
||||
const struct sockaddr_in *a = (const struct sockaddr_in *)ss;
|
||||
ip[10] = ip[11] = 0xff;
|
||||
memcpy(ip + 12, &a->sin_addr, 4);
|
||||
plat_addr_set(out, ip, ntohs(a->sin_port), 0);
|
||||
}
|
||||
}
|
||||
|
||||
// The socket address of a for a socket of family v6 (1: AF_INET6, 0: AF_INET); 0 when an IPv4 socket cannot reach it.
|
||||
static int addr_to_sockaddr(const PlatAddr *a, int v6, struct sockaddr_storage *ss, socklen_t *len) {
|
||||
uint8_t ip[16];
|
||||
uint32_t scope;
|
||||
plat_addr_get(a, ip, &scope);
|
||||
memset(ss, 0, sizeof *ss);
|
||||
if (v6) {
|
||||
struct sockaddr_in6 *s = (struct sockaddr_in6 *)ss;
|
||||
s->sin6_family = AF_INET6;
|
||||
s->sin6_port = htons(plat_addr_port(a));
|
||||
s->sin6_scope_id = scope;
|
||||
memcpy(&s->sin6_addr, ip, 16);
|
||||
*len = sizeof *s;
|
||||
return 1;
|
||||
}
|
||||
static const uint8_t mapped[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff};
|
||||
if (memcmp(ip, mapped, sizeof mapped)) return 0;
|
||||
struct sockaddr_in *s = (struct sockaddr_in *)ss;
|
||||
s->sin_family = AF_INET;
|
||||
s->sin_port = htons(plat_addr_port(a));
|
||||
memcpy(&s->sin_addr, ip + 12, 4);
|
||||
*len = sizeof *s;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int plat_net_resolve(const char *host, uint16_t port, PlatAddr *out) {
|
||||
struct addrinfo hints = {.ai_family = AF_UNSPEC, .ai_socktype = SOCK_DGRAM}, *res = NULL, *pick = NULL;
|
||||
if (!host || !*host || getaddrinfo(host, NULL, &hints, &res) != 0) return 0;
|
||||
for (struct addrinfo *r = res; r; r = r->ai_next) {
|
||||
if (r->ai_addrlen > sizeof(struct sockaddr_storage) || (r->ai_family != AF_INET && r->ai_family != AF_INET6)) continue;
|
||||
if (!pick || (r->ai_family == AF_INET && pick->ai_family != AF_INET)) pick = r; // IPv4 first
|
||||
}
|
||||
int ok = pick != NULL;
|
||||
if (ok) {
|
||||
struct sockaddr_storage ss;
|
||||
memset(&ss, 0, sizeof ss);
|
||||
memcpy(&ss, pick->ai_addr, pick->ai_addrlen);
|
||||
addr_from_sockaddr(out, &ss);
|
||||
uint8_t ip[16];
|
||||
uint32_t scope;
|
||||
plat_addr_get(out, ip, &scope);
|
||||
plat_addr_set(out, ip, port, scope);
|
||||
}
|
||||
freeaddrinfo(res);
|
||||
return ok;
|
||||
}
|
||||
|
||||
PlatUdp *plat_udp_open(uint16_t port, int dual_stack) {
|
||||
int v6 = dual_stack;
|
||||
int fd = v6 ? socket(AF_INET6, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0) : -1;
|
||||
if (fd >= 0) {
|
||||
int off = 0;
|
||||
setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof off);
|
||||
struct sockaddr_in6 a = {.sin6_family = AF_INET6, .sin6_addr = in6addr_any, .sin6_port = htons(port)};
|
||||
if (bind(fd, (struct sockaddr *)&a, sizeof a) != 0) close(fd), fd = -1;
|
||||
}
|
||||
if (fd < 0) { // IPv4 only, asked for or all the machine has
|
||||
v6 = 0;
|
||||
fd = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
|
||||
if (fd < 0) return NULL;
|
||||
struct sockaddr_in a = {.sin_family = AF_INET, .sin_port = htons(port)};
|
||||
a.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
if (bind(fd, (struct sockaddr *)&a, sizeof a) != 0) {
|
||||
close(fd);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
PlatUdp *u = malloc(sizeof *u);
|
||||
if (!u) {
|
||||
close(fd);
|
||||
return NULL;
|
||||
}
|
||||
u->fd = fd, u->v6 = v6;
|
||||
return u;
|
||||
}
|
||||
|
||||
uint16_t plat_udp_port(const PlatUdp *u) {
|
||||
struct sockaddr_storage ss;
|
||||
socklen_t len = sizeof ss;
|
||||
if (!u || getsockname(u->fd, (struct sockaddr *)&ss, &len) != 0) return 0;
|
||||
PlatAddr a;
|
||||
addr_from_sockaddr(&a, &ss);
|
||||
return plat_addr_port(&a);
|
||||
}
|
||||
|
||||
int plat_udp_send(PlatUdp *u, const PlatAddr *to, const void *p, size_t n) {
|
||||
struct sockaddr_storage ss;
|
||||
socklen_t len;
|
||||
if (!u || !addr_to_sockaddr(to, u->v6, &ss, &len)) return 0;
|
||||
for (;;) {
|
||||
ssize_t k = sendto(u->fd, p, n, MSG_NOSIGNAL, (struct sockaddr *)&ss, len);
|
||||
if (k >= 0) return 1;
|
||||
if (errno == EINTR) continue;
|
||||
// a full buffer, a missing route or an ICMP error is a lost datagram; a datagram too long or a socket that is not
|
||||
// ours to use can never be sent
|
||||
return errno != EMSGSIZE && errno != EBADF && errno != EINVAL && errno != EAFNOSUPPORT && errno != ENOTSOCK;
|
||||
}
|
||||
}
|
||||
|
||||
int plat_udp_recv(PlatUdp *u, void *buf, size_t cap, PlatAddr *from, uint32_t ms) {
|
||||
if (!u) return -1;
|
||||
struct pollfd pf = {.fd = u->fd, .events = POLLIN};
|
||||
int r = poll(&pf, 1, ms > (uint32_t)INT32_MAX ? INT32_MAX : (int)ms);
|
||||
if (r < 0) return errno == EINTR ? 0 : -1;
|
||||
if (r == 0) return 0;
|
||||
struct sockaddr_storage ss;
|
||||
socklen_t len = sizeof ss;
|
||||
ssize_t k = recvfrom(u->fd, buf, cap, MSG_DONTWAIT, (struct sockaddr *)&ss, &len);
|
||||
if (k < 0) return errno == EAGAIN || errno == EINTR || errno == ECONNREFUSED || errno == EHOSTUNREACH ? 0 : -1;
|
||||
if (from) addr_from_sockaddr(from, &ss);
|
||||
return (int)k;
|
||||
}
|
||||
|
||||
void plat_udp_close(PlatUdp *u) {
|
||||
if (!u) return;
|
||||
close(u->fd);
|
||||
free(u);
|
||||
}
|
||||
|
||||
|
||||
// ---- restart
|
||||
// NX2NAVI_RESTART_WAIT names the process that called plat_restart, as its id and start time (field 22 of /proc/<pid>/stat,
|
||||
|
||||
Reference in New Issue
Block a user