From 95d8b0692fc817bef1dce93403a946f4af3b4c05 Mon Sep 17 00:00:00 2001 From: fgsfds Date: Sun, 24 Dec 2023 17:31:57 +0100 Subject: [PATCH] port: net: add some config vars --- port/include/config.h | 1 + port/include/net/net.h | 7 +++++- port/src/net/net.c | 14 ++++++++++- port/src/net/netmenu.c | 57 ++++++++++++++++++++++++++++++++++++++---- 4 files changed, 72 insertions(+), 7 deletions(-) diff --git a/port/include/config.h b/port/include/config.h index 5bc12cf11..ed0a0df43 100644 --- a/port/include/config.h +++ b/port/include/config.h @@ -16,5 +16,6 @@ s32 configSave(const char *fname); // registers a variable in the config file // this should be done before configInit() is called, preferably in a module constructor void configRegisterInt(const char *key, s32 *var, s32 min, s32 max); +void configRegisterUInt(const char *key, u32 *var, u32 min, u32 max); void configRegisterFloat(const char *key, f32 *var, f32 min, f32 max); void configRegisterString(const char *key, char *var, u32 maxstr); diff --git a/port/include/net/net.h b/port/include/net/net.h index b8e3258b0..51fa9ceea 100644 --- a/port/include/net/net.h +++ b/port/include/net/net.h @@ -9,6 +9,7 @@ #define NET_MAX_CLIENTS MAX_PLAYERS #define NET_MAX_NAME MAX_PLAYERNAME +#define NET_MAX_ADDR 256 #define NET_BUFSIZE 1440 @@ -98,8 +99,12 @@ extern s32 g_NetMode; // net frame, ticks at 60 fps, starts at 0 when the server is started extern u32 g_NetTick; -extern u32 g_NetInterpTicks; extern u32 g_NetNextSyncId; + +extern u32 g_NetInterpTicks; +extern u32 g_NetServerPort; +extern char g_NetLastJoinAddr[NET_MAX_ADDR + 1]; + extern s32 g_NetDebugDraw; extern s32 g_NetMaxClients; diff --git a/port/src/net/net.c b/port/src/net/net.c index 7f86418d3..8c3d99280 100644 --- a/port/src/net/net.c +++ b/port/src/net/net.c @@ -24,9 +24,11 @@ s32 g_NetMode = NETMODE_NONE; -u32 g_NetTick = 0; u32 g_NetInterpTicks = 6; +u32 g_NetServerPort = NET_DEFAULT_PORT; +char g_NetLastJoinAddr[NET_MAX_ADDR + 1] = "127.0.0.1:27100"; +u32 g_NetTick = 0; u32 g_NetNextSyncId = 1; s32 g_NetSimPacketLoss = 0; @@ -325,6 +327,9 @@ s32 netStartClient(const char *addr) return -3; } + // save the address since it appears to be valid + strncpy(g_NetLastJoinAddr, addr, NET_MAX_ADDR); + // we'll use the whole array to store what we know of other clients netClientResetAll(); @@ -786,3 +791,10 @@ Gfx *netDebugRender(Gfx *gdl) return gdl; } + +PD_CONSTRUCTOR static void netConfigInit(void) +{ + configRegisterString("Net.LastJoinAddr", g_NetLastJoinAddr, NET_MAX_ADDR); + configRegisterUInt("Net.ServerPort", &g_NetServerPort, 0, 0xFFFF); + configRegisterUInt("Net.LerpTicks", &g_NetInterpTicks, 0, 600); +} diff --git a/port/src/net/netmenu.c b/port/src/net/netmenu.c index 560014004..c9e944c41 100644 --- a/port/src/net/netmenu.c +++ b/port/src/net/netmenu.c @@ -19,7 +19,7 @@ extern MenuItemHandlerResult menuhandlerMpAdvancedSetup(s32 operation, struct me static s32 g_NetMenuMaxPlayers = NET_MAX_CLIENTS; static s32 g_NetMenuPort = NET_DEFAULT_PORT; -static char g_NetJoinAddr[256] = "127.0.0.1:27100"; +static char g_NetJoinAddr[NET_MAX_ADDR + 1]; static s32 g_NetJoinAddrPtr = 0; /* host */ @@ -40,6 +40,22 @@ static MenuItemHandlerResult menuhandlerHostMaxPlayers(s32 operation, struct men return 0; } +static MenuItemHandlerResult menuhandlerHostPort(s32 operation, struct menuitem *item, union handlerdata *data) +{ + if (operation == MENUOP_SET) { + + } + + return 0; +} + +static char *menuhandlerHostPortValue(struct menuitem *item) +{ + static char tmp[16]; + snprintf(tmp, sizeof(tmp), "%u\n", g_NetMenuPort); + return tmp; +} + static MenuItemHandlerResult menuhandlerHostStart(s32 operation, struct menuitem *item, union handlerdata *data) { if (operation == MENUOP_SET) { @@ -61,6 +77,14 @@ struct menuitem g_NetHostMenuItems[] = { NET_MAX_CLIENTS, menuhandlerHostMaxPlayers, }, + { + MENUITEMTYPE_SELECTABLE, + 0, + MENUITEMFLAG_LITERAL_TEXT, + (uintptr_t)"Port\n", + (uintptr_t)&menuhandlerHostPortValue, + menuhandlerHostPort, + }, { MENUITEMTYPE_SELECTABLE, 0, @@ -326,22 +350,45 @@ struct menudialogdef g_NetJoinMenuDialog = { /* main */ +static MenuItemHandlerResult menuhandlerHostGame(s32 operation, struct menuitem *item, union handlerdata *data) +{ + if (operation == MENUOP_SET) { + g_NetMenuPort = g_NetServerPort; + menuPushDialog(&g_NetHostMenuDialog); + } + + return 0; +} + +static MenuItemHandlerResult menuhandlerJoinGame(s32 operation, struct menuitem *item, union handlerdata *data) +{ + if (operation == MENUOP_SET) { + if (g_NetJoinAddr[0] == '\0') { + strncpy(g_NetJoinAddr, g_NetLastJoinAddr, NET_MAX_ADDR); + g_NetJoinAddrPtr = strlen(g_NetJoinAddr); + } + menuPushDialog(&g_NetJoinMenuDialog); + } + + return 0; +} + struct menuitem g_NetMenuItems[] = { { MENUITEMTYPE_SELECTABLE, 0, - MENUITEMFLAG_SELECTABLE_OPENSDIALOG | MENUITEMFLAG_LITERAL_TEXT, + MENUITEMFLAG_LITERAL_TEXT, (uintptr_t)"Host Game\n", 0, - (void *)&g_NetHostMenuDialog, + menuhandlerHostGame, }, { MENUITEMTYPE_SELECTABLE, 0, - MENUITEMFLAG_SELECTABLE_OPENSDIALOG | MENUITEMFLAG_LITERAL_TEXT, + MENUITEMFLAG_LITERAL_TEXT, (uintptr_t)"Join Game\n", 0, - (void *)&g_NetJoinMenuDialog, + menuhandlerJoinGame, }, { MENUITEMTYPE_SEPARATOR,