From 4eca4a1f079506547646fef22528e4d385287ca4 Mon Sep 17 00:00:00 2001 From: fgsfds Date: Sun, 7 Jan 2024 19:54:27 +0100 Subject: [PATCH] port: net: fix a bunch of access violations --- port/src/net/net.c | 8 +++++--- src/game/bondmove.c | 14 ++++++++++++-- src/game/player.c | 24 ++++++++++++++++++++---- src/game/playerreset.c | 6 ++++++ src/include/constants.h | 2 +- src/include/data.h | 1 + src/include/game/player.h | 1 + 7 files changed, 46 insertions(+), 10 deletions(-) diff --git a/port/src/net/net.c b/port/src/net/net.c index 8a8a2252b..1a02722a8 100644 --- a/port/src/net/net.c +++ b/port/src/net/net.c @@ -854,12 +854,14 @@ void netPlayersAllocate(void) cfg->options |= OPTION_FORWARDPITCH; } - cl->player = g_Vars.players[cl->playernum]; cl->config = &g_PlayerConfigsArray[cl->playernum]; cl->config->client = cl; cl->config->handicap = 0x80; - cl->player->client = cl; - cl->player->isremote = (cl != g_NetLocalClient); + cl->player = g_Vars.players[cl->playernum]; + if (cl->player) { + cl->player->client = cl; + cl->player->isremote = (cl != g_NetLocalClient); + } } } diff --git a/src/game/bondmove.c b/src/game/bondmove.c index a27aaf40a..2b6742684 100644 --- a/src/game/bondmove.c +++ b/src/game/bondmove.c @@ -112,6 +112,10 @@ static void bgunProcessInputAltButton(struct movedata *data, s8 contpad, s32 i) static inline void bmoveProcessRemoteInput(const bool allowc1buttons) { struct player *pl = g_Vars.currentplayer; + if (!pl->client) { + return; + } + struct netplayermove *inmove = &pl->client->inmove[0]; struct netplayermove *inmoveprev = &pl->client->inmove[1]; s32 moveticks = inmove->tick - inmoveprev->tick; @@ -899,14 +903,17 @@ void bmoveProcessInput(bool allowc1x, bool allowc1y, bool allowc1buttons, bool i const f32 mlookscale = g_Vars.lvupdate240 ? (4.f / (f32)g_Vars.lvupdate240) : 4.f; const bool allowmlook = (g_Vars.currentplayernum == 0) && (allowc1x || allowc1y); bool allowmcross = false; +#endif - if (g_Vars.currentplayer->isremote) { + controlmode = optionsGetControlMode(g_Vars.currentplayerstats->mpindex); + +#ifndef PLATFORM_N64 + if (g_Vars.currentplayer->isremote || controlmode == CONTROLMODE_NA) { bmoveProcessRemoteInput(allowc1buttons); return; } #endif - controlmode = optionsGetControlMode(g_Vars.currentplayerstats->mpindex); weaponnum = bgunGetWeaponNum(HAND_RIGHT); canmanualzoom = weaponHasAimFlag(weaponnum, INVAIMFLAG_MANUALZOOM); contpad1 = optionsGetContpadNum1(g_Vars.currentplayerstats->mpindex); @@ -1421,6 +1428,9 @@ void bmoveProcessInput(bool allowc1x, bool allowc1y, bool allowc1buttons, bool i aimoffhist[i] = !aimonhist[i]; } +#ifdef AVOID_UB + if (numsamples) +#endif g_Vars.currentplayer->insightaimmode = aimonhist[numsamples - 1]; } diff --git a/src/game/player.c b/src/game/player.c index 3f63da803..4c6c540b1 100644 --- a/src/game/player.c +++ b/src/game/player.c @@ -184,7 +184,10 @@ u32 var80070748 = 0; u32 var8007074c = 0; bool g_PlayersWithControl[] = { + true, true, true, true, +#if MAX_PLAYERS > 4 true, true, true, true +#endif }; bool g_PlayerInvincible = false; @@ -3507,6 +3510,11 @@ void playerTick(bool arg0) bool pause = false; f32 newspeed; +#ifndef PLATFORM_N64 + if (mode == CONTROLMODE_NA) { + // TODO + } else +#endif if (mode == CONTROLMODE_23 || mode == CONTROLMODE_24 || mode == CONTROLMODE_22 @@ -5886,10 +5894,6 @@ f32 playerGetZoomFovMult(s32 playernum) return g_PlayerExtCfg[playernum % MAX_LOCAL_PLAYERS].fovzoommult; } -#endif - -#if MAX_PLAYERS > 4 - s32 playerGetCount(void) { s32 count = 0; @@ -5899,4 +5903,16 @@ s32 playerGetCount(void) return count; } +s32 playerGetLocalCount(void) +{ + if (g_NetMode) { + return 1; + } + const s32 playercount = playerGetCount(); + if (playercount > MAX_LOCAL_PLAYERS) { + return MAX_LOCAL_PLAYERS; + } + return playercount; +} + #endif diff --git a/src/game/playerreset.c b/src/game/playerreset.c index 896601929..6e941dd9c 100644 --- a/src/game/playerreset.c +++ b/src/game/playerreset.c @@ -148,6 +148,12 @@ void playerReset(void) g_PlayersWithControl[1] = true; g_PlayersWithControl[2] = true; g_PlayersWithControl[3] = true; +#if MAX_PLAYERS > 4 + g_PlayersWithControl[4] = true; + g_PlayersWithControl[5] = true; + g_PlayersWithControl[6] = true; + g_PlayersWithControl[7] = true; +#endif g_PlayerInvincible = false; playerSetTickMode(TICKMODE_GE_FADEIN); diff --git a/src/include/constants.h b/src/include/constants.h index 5a514a207..217d26a87 100644 --- a/src/include/constants.h +++ b/src/include/constants.h @@ -4728,7 +4728,7 @@ enum weaponnum { #define NETMODE_SERVER 1 #define NETMODE_CLIENT 2 -#define LOCALPLAYERCOUNT() (g_NetMode ? 1 : PLAYERCOUNT()) +#define LOCALPLAYERCOUNT() playerGetLocalCount() #endif diff --git a/src/include/data.h b/src/include/data.h index 535d3773c..d2d3438c7 100644 --- a/src/include/data.h +++ b/src/include/data.h @@ -562,6 +562,7 @@ extern s32 g_NetMode; #define TEX_FILTER_2D g_TexFilter2D s32 playerGetCount(void); +s32 playerGetLocalCount(void); #else // PLATFORM_N64 diff --git a/src/include/game/player.h b/src/include/game/player.h index 21f12d0cf..5783f579c 100644 --- a/src/include/game/player.h +++ b/src/include/game/player.h @@ -110,6 +110,7 @@ struct sndstate *playerSndStart(s32 arg0, s16 sound, struct sndstate **handle, s #if MAX_PLAYERS > 4 s32 playerGetCount(void); +s32 playerGetLocalCount(void); #endif #ifndef PLATFORM_N64