From 28b089f0707b1f21ab20ce8b667779326e92e8f8 Mon Sep 17 00:00:00 2001 From: Diavolo Date: Sat, 20 Aug 2022 12:09:41 +0200 Subject: [PATCH 1/3] [Structs] use clientheader --- src/Components/Modules/Bans.cpp | 4 +-- src/Components/Modules/CardTitles.cpp | 2 +- src/Components/Modules/Console.cpp | 2 +- src/Components/Modules/Dedicated.cpp | 4 +-- src/Components/Modules/Download.cpp | 6 ++-- .../Modules/GSC/ScriptExtension.cpp | 2 +- src/Components/Modules/Network.cpp | 2 +- src/Components/Modules/Party.cpp | 2 +- src/Components/Modules/ServerInfo.cpp | 2 +- src/Components/Modules/Voice.cpp | 12 ++++---- src/Game/Functions.cpp | 4 +-- src/Game/Structs.hpp | 29 +++++++++++++------ 12 files changed, 41 insertions(+), 30 deletions(-) diff --git a/src/Components/Modules/Bans.cpp b/src/Components/Modules/Bans.cpp index f1cac387..dd7e0bec 100644 --- a/src/Components/Modules/Bans.cpp +++ b/src/Components/Modules/Bans.cpp @@ -180,7 +180,7 @@ namespace Components SteamID guid; guid.bits = cl->steamID; - InsertBan({guid, cl->netchan.remoteAddress.ip}); + InsertBan({guid, cl->header.netchan.remoteAddress.ip}); Game::SV_DropClient(cl, reason.data(), true); } @@ -257,7 +257,7 @@ namespace Components } const auto* cl = &Game::svs_clients[num]; - if (cl->state == Game::CS_FREE) + if (cl->header.state == Game::CS_FREE) { Logger::Print("Client {} is not active\n", num); return; diff --git a/src/Components/Modules/CardTitles.cpp b/src/Components/Modules/CardTitles.cpp index 7f5d6164..da34aa24 100644 --- a/src/Components/Modules/CardTitles.cpp +++ b/src/Components/Modules/CardTitles.cpp @@ -160,7 +160,7 @@ namespace Components { char playerTitle[18]; - if (Game::svs_clients[i].state >= Game::CS_CONNECTED) + if (Game::svs_clients[i].header.state >= Game::CS_CONNECTED) { strncpy_s(playerTitle, Game::Info_ValueForKey(Game::svs_clients[i].userinfo, "customTitle"), _TRUNCATE); } diff --git a/src/Components/Modules/Console.cpp b/src/Components/Modules/Console.cpp index 9fc90446..bcb5995d 100644 --- a/src/Components/Modules/Console.cpp +++ b/src/Components/Modules/Console.cpp @@ -46,7 +46,7 @@ namespace Components { for (int i = 0; i < maxclientCount; ++i) { - if (Game::svs_clients[i].state >= 3) + if (Game::svs_clients[i].header.state >= Game::CS_CONNECTED) { ++clientCount; } diff --git a/src/Components/Modules/Dedicated.cpp b/src/Components/Modules/Dedicated.cpp index cb1bb132..b8bc3e11 100644 --- a/src/Components/Modules/Dedicated.cpp +++ b/src/Components/Modules/Dedicated.cpp @@ -81,9 +81,9 @@ namespace Components { std::string list = Utils::String::VA("%c", 20); - for (int i = 0; i < 18; ++i) + for (std::size_t i = 0; i < Game::MAX_CLIENTS; ++i) { - if (Game::svs_clients[i].state >= 3) + if (Game::svs_clients[i].header.state >= 3) { list.append(Utils::String::VA(" %llX", Game::svs_clients[i].steamID)); diff --git a/src/Components/Modules/Download.cpp b/src/Components/Modules/Download.cpp index b51d073c..d48b1cda 100644 --- a/src/Components/Modules/Download.cpp +++ b/src/Components/Modules/Download.cpp @@ -388,9 +388,9 @@ namespace Components { Game::client_t* client = &Game::svs_clients[i]; - if (client->state >= 3) + if (client->header.state >= Game::CS_CONNECTED) { - if (address.getIP().full == Network::Address(client->netchan.remoteAddress).getIP().full) + if (address.getIP().full == Network::Address(client->header.netchan.remoteAddress).getIP().full) { return client; } @@ -752,7 +752,7 @@ namespace Components if ((*Game::com_sv_running)->current.enabled) { - if (Game::svs_clients[i].state < 3) continue; + if (Game::svs_clients[i].header.state < Game::CS_CONNECTED) continue; playerInfo["score"] = Game::SV_GameClientNum_Score(i); playerInfo["ping"] = Game::svs_clients[i].ping; diff --git a/src/Components/Modules/GSC/ScriptExtension.cpp b/src/Components/Modules/GSC/ScriptExtension.cpp index efbe790b..3953bc47 100644 --- a/src/Components/Modules/GSC/ScriptExtension.cpp +++ b/src/Components/Modules/GSC/ScriptExtension.cpp @@ -251,7 +251,7 @@ namespace Components const auto* ent = Game::GetPlayerEntity(entref); const auto* client = Script::GetClient(ent); - std::string ip = Game::NET_AdrToString(client->netchan.remoteAddress); + std::string ip = Game::NET_AdrToString(client->header.netchan.remoteAddress); if (const auto pos = ip.find_first_of(":"); pos != std::string::npos) ip.erase(ip.begin() + pos, ip.end()); // Erase port diff --git a/src/Components/Modules/Network.cpp b/src/Components/Modules/Network.cpp index 04e453f1..06f08b12 100644 --- a/src/Components/Modules/Network.cpp +++ b/src/Components/Modules/Network.cpp @@ -270,7 +270,7 @@ namespace Components Logger::Print(Game::conChannel_t::CON_CHANNEL_NETWORK, "Negative reliableAcknowledge from {} - cl->reliableSequence is {}, reliableAcknowledge is {}\n", client->name, client->reliableSequence, client->reliableAcknowledge); client->reliableAcknowledge = client->reliableSequence; - SendCommand(Game::NS_SERVER, client->netchan.remoteAddress, "error", "EXE_LOSTRELIABLECOMMANDS"); + SendCommand(Game::NS_SERVER, client->header.netchan.remoteAddress, "error", "EXE_LOSTRELIABLECOMMANDS"); return; } diff --git a/src/Components/Modules/Party.cpp b/src/Components/Modules/Party.cpp index db2331a8..cb402a2a 100644 --- a/src/Components/Modules/Party.cpp +++ b/src/Components/Modules/Party.cpp @@ -322,7 +322,7 @@ namespace Components { for (int i = 0; i < maxclientCount; ++i) { - if (Game::svs_clients[i].state >= 3) + if (Game::svs_clients[i].header.state >= Game::CS_CONNECTED) { if (Game::svs_clients[i].bIsTestClient) ++botCount; else ++clientCount; diff --git a/src/Components/Modules/ServerInfo.cpp b/src/Components/Modules/ServerInfo.cpp index 0992e4df..f497e7d7 100644 --- a/src/Components/Modules/ServerInfo.cpp +++ b/src/Components/Modules/ServerInfo.cpp @@ -208,7 +208,7 @@ namespace Components if ((*Game::com_sv_running)->current.enabled) { - if (Game::svs_clients[i].state < 3) continue; + if (Game::svs_clients[i].header.state < Game::CS_CONNECTED) continue; score = Game::SV_GameClientNum_Score(i); ping = Game::svs_clients[i].ping; diff --git a/src/Components/Modules/Voice.cpp b/src/Components/Modules/Voice.cpp index 6871d863..c334d33b 100644 --- a/src/Components/Modules/Voice.cpp +++ b/src/Components/Modules/Voice.cpp @@ -42,7 +42,7 @@ namespace Components assert(VoicePacketCount[clientNum] >= 0); - if (client->state == Game::CS_ACTIVE && VoicePacketCount[clientNum]) + if (client->header.state == Game::CS_ACTIVE && VoicePacketCount[clientNum]) { Game::MSG_Init(&msg, msg_buf.get(), 0x10000); @@ -58,7 +58,7 @@ namespace Components } else { - Game::NET_OutOfBandVoiceData(Game::NS_SERVER, client->netchan.remoteAddress, msg.data, msg.cursize, true); + Game::NET_OutOfBandVoiceData(Game::NS_SERVER, client->header.netchan.remoteAddress, msg.data, msg.cursize, true); VoicePacketCount[clientNum] = 0; } } @@ -203,7 +203,7 @@ namespace Components Game::MSG_ReadData(msg, voicePacket.data, voicePacket.dataSize); for (auto otherPlayer = 0; otherPlayer < (*Game::sv_maxclients)->current.integer; ++otherPlayer) { - if (otherPlayer != talker && Game::svs_clients[otherPlayer].state >= Game::CS_CONNECTED && !SV_ServerHasClientMuted(talker)) + if (otherPlayer != talker && Game::svs_clients[otherPlayer].header.state >= Game::CS_CONNECTED && !SV_ServerHasClientMuted(talker)) { SV_QueueVoicePacket(talker, otherPlayer, &voicePacket); } @@ -213,15 +213,15 @@ namespace Components void Voice::SV_VoicePacket(Game::netadr_t from, Game::msg_t* msg) { - auto qport = Game::MSG_ReadShort(msg); + const auto qport = Game::MSG_ReadShort(msg); auto* cl = Game::SV_FindClientByAddress(from, qport, 0); - if (!cl || cl->state == Game::CS_ZOMBIE) + if (!cl || cl->header.state == Game::CS_ZOMBIE) { return; } cl->lastPacketTime = *Game::svs_time; - if (cl->state < Game::CS_ACTIVE) + if (cl->header.state < Game::CS_ACTIVE) { SV_PreGameUserVoice(cl, msg); } diff --git a/src/Game/Functions.cpp b/src/Game/Functions.cpp index 32a75199..c9ddab80 100644 --- a/src/Game/Functions.cpp +++ b/src/Game/Functions.cpp @@ -884,8 +884,8 @@ namespace Game { for (auto i = 0; i < *svs_clientCount; ++i) { - if (svs_clients[i].state != CS_FREE - && svs_clients[i].netchan.remoteAddress.type == NA_BOT) + if (svs_clients[i].header.state != CS_FREE + && svs_clients[i].header.netchan.remoteAddress.type == NA_BOT) { SV_GameDropClient(i, "GAME_GET_TO_COVER"); } diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index 1387aa13..85c6305b 100644 --- a/src/Game/Structs.hpp +++ b/src/Game/Structs.hpp @@ -235,7 +235,7 @@ namespace Game DVAR_TYPE_COUNT = 0xA, } dvar_type; - typedef enum + enum clientState_t { CS_FREE = 0x0, CS_ZOMBIE = 0x1, @@ -243,7 +243,7 @@ namespace Game CS_CONNECTED = 0x3, CS_CLIENTLOADING = 0x4, CS_ACTIVE = 0x5, - } clientState_t; + }; enum serverState_t { @@ -6301,17 +6301,28 @@ namespace Game bool topFire; }; + struct clientHeader_t + { + int state; + int sendAsActive; + int deltaMessage; + int rateDelayed; + int hasAckedBaselineData; + int hugeSnapshotSent; + netchan_t netchan; + float predictedOrigin[3]; + int predictedOriginServerTime; + int migrationState; + }; + + static_assert(sizeof(clientHeader_t) == 1624); + #pragma pack(push, 1) typedef struct client_s { - clientState_t state; // 0 - int sendAsActive; // 4 - int deltaMessage; // 8 - char __pad1[12]; // 12 - netchan_t netchan; // 24 - char __pad2[20]; // 1604 - const char* delayDropReason; // 1624 + clientHeader_t header; + const char* dropReason; // 1624 char userinfo[1024]; // 1628 char __pad3[132096]; // 2652 int reliableSequence; // 134748 From 6276ef2e24c147226b796cb19b9cd0c2f19e7de9 Mon Sep 17 00:00:00 2001 From: Diavolo Date: Sat, 20 Aug 2022 12:30:34 +0200 Subject: [PATCH 2/3] [Structs] Complete client_t --- src/Components/Modules/Bots.cpp | 3 +- src/Components/Modules/Chat.cpp | 2 + src/Components/Modules/Download.cpp | 4 +- .../Modules/GSC/ScriptExtension.cpp | 2 +- src/Components/Modules/SlowMotion.cpp | 2 +- src/Game/Functions.hpp | 2 +- src/Game/Structs.hpp | 58 ++++++++++++++----- 7 files changed, 53 insertions(+), 20 deletions(-) diff --git a/src/Components/Modules/Bots.cpp b/src/Components/Modules/Bots.cpp index 72c3836b..57a334dc 100644 --- a/src/Components/Modules/Bots.cpp +++ b/src/Components/Modules/Bots.cpp @@ -294,7 +294,8 @@ namespace Components Bots::Bots() { - AssertOffset(Game::client_s, bIsTestClient, 0x41AF0); + AssertOffset(Game::client_t, bIsTestClient, 0x41AF0); + AssertOffset(Game::client_t, ping, 0x212C8); // Replace connect string Utils::Hook::Set(0x48ADA6, "connect bot%d \"\\cg_predictItems\\1\\cl_anonymous\\0\\color\\4\\head\\default\\model\\multi\\snaps\\20\\rate\\5000\\name\\%s\\protocol\\%d\\checksum\\%d\\statver\\%d %u\\qport\\%d\""); diff --git a/src/Components/Modules/Chat.cpp b/src/Components/Modules/Chat.cpp index 087d32ed..b2fe70c4 100644 --- a/src/Components/Modules/Chat.cpp +++ b/src/Components/Modules/Chat.cpp @@ -410,6 +410,8 @@ namespace Components Chat::Chat() { + AssertOffset(Game::client_t, steamID, 0x43F00); + cg_chatWidth = Dvar::Register("cg_chatWidth", 52, 1, std::numeric_limits::max(), Game::DVAR_ARCHIVE, "The normalized maximum width of a chat message"); sv_disableChat = Dvar::Register("sv_disableChat", false, Game::DVAR_NONE, "Disable chat messages from clients"); Events::OnSVInit(AddChatCommands); diff --git a/src/Components/Modules/Download.cpp b/src/Components/Modules/Download.cpp index d48b1cda..7561cfe8 100644 --- a/src/Components/Modules/Download.cpp +++ b/src/Components/Modules/Download.cpp @@ -761,13 +761,13 @@ namespace Components else { // Score and ping are irrelevant - const char* namePtr = Game::PartyHost_GetMemberName(reinterpret_cast(0x1081C00), i); + const char* namePtr = Game::PartyHost_GetMemberName(Game::g_lobbyData, i); if (!namePtr || !namePtr[0]) continue; playerInfo["name"] = namePtr; } - players.push_back(playerInfo); + players.emplace_back(playerInfo); } info["players"] = players; diff --git a/src/Components/Modules/GSC/ScriptExtension.cpp b/src/Components/Modules/GSC/ScriptExtension.cpp index 3953bc47..e3c5cbae 100644 --- a/src/Components/Modules/GSC/ScriptExtension.cpp +++ b/src/Components/Modules/GSC/ScriptExtension.cpp @@ -276,7 +276,7 @@ namespace Components const auto* ent = Game::GetPlayerEntity(entref); auto* client = Script::GetClient(ent); - client->ping = static_cast(ping); + client->ping = ping; }); } diff --git a/src/Components/Modules/SlowMotion.cpp b/src/Components/Modules/SlowMotion.cpp index b1247bd9..4c403705 100644 --- a/src/Components/Modules/SlowMotion.cpp +++ b/src/Components/Modules/SlowMotion.cpp @@ -70,7 +70,7 @@ namespace Components // set snapshot num to 1 behind (T6 does this, why shouldn't we?) for (int i = 0; i < *Game::svs_clientCount; ++i) { - Game::svs_clients[i].snapNum = *Game::svs_time - 1; + Game::svs_clients[i].nextSnapshotTime = *Game::svs_time - 1; } } diff --git a/src/Game/Functions.hpp b/src/Game/Functions.hpp index 332062e9..e8e1272d 100644 --- a/src/Game/Functions.hpp +++ b/src/Game/Functions.hpp @@ -954,7 +954,7 @@ namespace Game typedef bool(__cdecl * SV_Loaded_t)(); extern SV_Loaded_t SV_Loaded; - typedef void(__cdecl * SV_ClientThink_t)(client_s*, usercmd_s*); + typedef void(__cdecl * SV_ClientThink_t)(client_t* cl, usercmd_s* cmd); extern SV_ClientThink_t SV_ClientThink; typedef void(__cdecl * SV_DropClient_t)(client_t* drop, const char* reason, bool tellThem); diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index 85c6305b..53da9032 100644 --- a/src/Game/Structs.hpp +++ b/src/Game/Structs.hpp @@ -6317,14 +6317,34 @@ namespace Game static_assert(sizeof(clientHeader_t) == 1624); -#pragma pack(push, 1) + struct svscmd_info_t + { + char cmd[1024]; + int time; + int type; + }; - typedef struct client_s + struct clientSnapshot_t + { + playerState_s ps; + int num_entities; + int num_clients; + int first_entity; + int first_client; + int messageSent; + int messageAcked; + int messageSize; + int serverTime; + int timeDelta; + int baselineSnap; + }; + + struct client_t { clientHeader_t header; const char* dropReason; // 1624 char userinfo[1024]; // 1628 - char __pad3[132096]; // 2652 + svscmd_info_t reliableCommandInfo[128]; // 2652 int reliableSequence; // 134748 int reliableAcknowledge; // 134752 int reliableSent; // 134756 @@ -6339,21 +6359,31 @@ namespace Game int nextReliableTime; // 135860 int lastPacketTime; // 135864 int lastConnectTime; // 135868 - int snapNum; // 135872 - int __pad5; // 135876 - short ping; // 135880 - char __pad6[14]; // 135882 + int nextSnapshotTime; // 135872 + int timeoutCount; // 135876 + int ping; // 135880 + int rate; + int snapshotMsec; + int snapshotBackoffCount; int pureAuthentic; // 135896 - char __pad7[133138]; // 135900 - short scriptID; // 269038 + char netchanOutgoingBuffer[131072]; + char netchanIncomingBuffer[2048]; + char playerGuid[17]; + unsigned short scriptId; // 269038 int bIsTestClient; // 269040 int serverID; // 269044 - char __pad8[9224]; // 269048 + bool usingOnlineStatsOffline; + char stats[8192]; + char statsModifiedFlags[1024]; + bool statsModified; + char statPacketsReceived; + bool steamAuthorized; + char steamAuthFailCount; unsigned __int64 steamID; // 278272 - char __pad9[403592]; // 278280 - } client_t; - -#pragma pack(pop) + bool sendMatchData; + int matchDataSendTime; + clientSnapshot_t frames[32]; + }; static_assert(sizeof(client_t) == 0xA6790); From c3ee95278cb062efb16e94fccdca0023dc553f43 Mon Sep 17 00:00:00 2001 From: Diavolo Date: Sat, 20 Aug 2022 14:06:52 +0200 Subject: [PATCH 3/3] [Gamepad] Re-work assertions --- src/Components/Modules/Gamepad.cpp | 90 ++++++++++++++++++------------ src/Components/Modules/Gamepad.hpp | 4 +- src/Game/Structs.hpp | 2 +- 3 files changed, 58 insertions(+), 38 deletions(-) diff --git a/src/Components/Modules/Gamepad.cpp b/src/Components/Modules/Gamepad.cpp index 2ec0e24d..d9d8371c 100644 --- a/src/Components/Modules/Gamepad.cpp +++ b/src/Components/Modules/Gamepad.cpp @@ -169,8 +169,8 @@ namespace Components {Game::K_APAD_RIGHT, Game::K_RIGHTARROW}, }; - Gamepad::GamePad Gamepad::gamePads[Game::MAX_GAMEPADS]{}; - Gamepad::GamePadGlobals Gamepad::gamePadGlobals[Game::MAX_GAMEPADS]{{}}; + Gamepad::GamePad Gamepad::gamePads[Game::MAX_GPAD_COUNT]{}; + Gamepad::GamePadGlobals Gamepad::gamePadGlobals[Game::MAX_GPAD_COUNT]{{}}; int Gamepad::gamePadBindingsModifiedFlags = 0; Dvar::Var Gamepad::gpad_enabled; @@ -314,7 +314,8 @@ namespace Components bool Gamepad::GPad_Check(const int gamePadIndex, const int portIndex) { - assert(gamePadIndex < Game::MAX_GAMEPADS); + assert((portIndex >= 0) && (portIndex < Game::MAX_GPAD_COUNT)); + auto& gamePad = gamePads[gamePadIndex]; if (XInputGetCapabilities(portIndex, XINPUT_FLAG_GAMEPAD, &gamePad.caps) == ERROR_SUCCESS) @@ -332,7 +333,7 @@ namespace Components { auto currentGamePadNum = 0; - for (auto currentPort = 0; currentPort < XUSER_MAX_COUNT && currentGamePadNum < Game::MAX_GAMEPADS; currentPort++) + for (auto currentPort = 0; currentPort < XUSER_MAX_COUNT && currentGamePadNum < Game::MAX_GPAD_COUNT; currentPort++) { if (GPad_Check(currentGamePadNum, currentPort)) currentGamePadNum++; @@ -366,7 +367,7 @@ namespace Components bool Gamepad::AimAssist_IsPlayerUsingOffhand(Game::AimAssistPlayerState* ps) { // Check offhand flag - if ((ps->weapFlags & 2) == 0) + if ((ps->weapFlags & Game::PWF_USING_OFFHAND) == 0) return false; // If offhand weapon has no id we are not using one @@ -421,7 +422,8 @@ namespace Components bool Gamepad::AimAssist_IsLockonActive(const int gamePadIndex) { - assert(gamePadIndex < Game::MAX_GAMEPADS); + assert(gamePadIndex < Game::MAX_GPAD_COUNT); + auto& aaGlob = Game::aaGlobArray[gamePadIndex]; if (!aim_lockon_enabled.get() || !gpad_lockon_enabled.get()) @@ -439,7 +441,9 @@ namespace Components void Gamepad::AimAssist_ApplyLockOn(const Game::AimInput* input, Game::AimOutput* output) { assert(input); - assert(input->localClientNum < Game::MAX_GAMEPADS); + assert(output); + AssertIn(input->localClientNum, Game::STATIC_MAX_LOCAL_CLIENTS); + auto& aaGlob = Game::aaGlobArray[input->localClientNum]; const auto prevTargetEnt = aaGlob.lockOnTargetEnt; @@ -554,10 +558,11 @@ namespace Components void Gamepad::AimAssist_CalcSlowdown(const Game::AimInput* input, float* pitchScale, float* yawScale) { assert(input); - assert(input->localClientNum < Game::MAX_GAMEPADS); - auto& aaGlob = Game::aaGlobArray[input->localClientNum]; assert(pitchScale); assert(yawScale); + AssertIn(input->localClientNum, Game::STATIC_MAX_LOCAL_CLIENTS); + + auto& aaGlob = Game::aaGlobArray[input->localClientNum]; *pitchScale = 1.0f; *yawScale = 1.0f; @@ -586,7 +591,10 @@ namespace Components void Gamepad::AimAssist_ApplyTurnRates(const Game::AimInput* input, Game::AimOutput* output) { - assert(input->localClientNum < Game::MAX_GAMEPADS); + assert(input); + assert(output); + AssertIn(input->localClientNum, Game::STATIC_MAX_LOCAL_CLIENTS); + auto& aaGlob = Game::aaGlobArray[input->localClientNum]; auto slowdownPitchScale = 0.0f; @@ -649,7 +657,8 @@ namespace Components void Gamepad::AimAssist_UpdateGamePadInput(const Game::AimInput* input, Game::AimOutput* output) { - assert(input->localClientNum < Game::MAX_GAMEPADS); + AssertIn(input->localClientNum, Game::STATIC_MAX_LOCAL_CLIENTS); + auto& aaGlob = Game::aaGlobArray[input->localClientNum]; output->pitch = input->pitch; @@ -784,8 +793,8 @@ namespace Components float Gamepad::CL_GamepadAxisValue(const int gamePadIndex, const Game::GamepadVirtualAxis virtualAxis) { - assert(gamePadIndex < Game::MAX_GAMEPADS); assert(virtualAxis > Game::GPAD_VIRTAXIS_NONE && virtualAxis < Game::GPAD_VIRTAXIS_COUNT); + const auto& gamePadGlobal = gamePadGlobals[gamePadIndex]; const auto& [physicalAxis, mapType] = gamePadGlobal.axes.virtualAxes[virtualAxis]; @@ -818,7 +827,8 @@ namespace Components void Gamepad::CL_GamepadMove(const int gamePadIndex, Game::usercmd_s* cmd, const float frameTimeBase) { - assert(gamePadIndex < Game::MAX_GAMEPADS); + assert(gamePadIndex < Game::MAX_GPAD_COUNT); + auto& gamePad = gamePads[gamePadIndex]; auto& clientActive = Game::clients[gamePadIndex]; @@ -960,7 +970,8 @@ namespace Components void Gamepad::CL_GamepadResetMenuScrollTime(const int gamePadIndex, const int key, const bool down, const unsigned time) { - assert(gamePadIndex < Game::MAX_GAMEPADS); + assert(gamePadIndex < Game::MAX_GPAD_COUNT); + auto& gamePadGlobal = gamePadGlobals[gamePadIndex]; if (!down) @@ -979,8 +990,8 @@ namespace Components void Gamepad::CL_GamepadGenerateAPad(const int gamePadIndex, const Game::GamepadPhysicalAxis physicalAxis, unsigned time) { - assert(gamePadIndex < Game::MAX_GAMEPADS); - assert(physicalAxis < Game::GPAD_PHYSAXIS_COUNT && physicalAxis >= 0); + assert(gamePadIndex < Game::MAX_GPAD_COUNT); + assert(physicalAxis >= 0 && physicalAxis < Game::GPAD_PHYSAXIS_COUNT); auto& gamePad = gamePads[gamePadIndex]; @@ -1014,8 +1025,8 @@ namespace Components void Gamepad::CL_GamepadEvent(const int gamePadIndex, const Game::GamepadPhysicalAxis physicalAxis, const float value, const unsigned time) { - assert(gamePadIndex < Game::MAX_GAMEPADS); - assert(physicalAxis < Game::GPAD_PHYSAXIS_COUNT && physicalAxis >= 0); + assert(gamePadIndex < Game::MAX_GPAD_COUNT); + assert(physicalAxis >= 0 && physicalAxis < Game::GPAD_PHYSAXIS_COUNT); auto& gamePad = gamePads[gamePadIndex]; auto& gamePadGlobal = gamePadGlobals[gamePadIndex]; @@ -1054,7 +1065,8 @@ namespace Components bool Gamepad::Scoreboard_HandleInput(int gamePadIndex, int key) { - assert(gamePadIndex < Game::MAX_GAMEPADS); + AssertIn(gamePadIndex, Game::STATIC_MAX_LOCAL_CLIENTS); + auto& keyState = Game::playerKeys[gamePadIndex]; if (keyState.keys[key].binding && strcmp(keyState.keys[key].binding, "togglescores") == 0) @@ -1080,7 +1092,8 @@ namespace Components bool Gamepad::CL_CheckForIgnoreDueToRepeat(const int gamePadIndex, const int key, const int repeatCount, const unsigned time) { - assert(gamePadIndex < Game::MAX_GAMEPADS); + AssertIn(gamePadIndex, Game::STATIC_MAX_LOCAL_CLIENTS); + auto& gamePadGlobal = gamePadGlobals[gamePadIndex]; if (Game::Key_IsKeyCatcherActive(gamePadIndex, Game::KEYCATCH_UI)) @@ -1113,7 +1126,7 @@ namespace Components void Gamepad::CL_GamepadButtonEvent(const int gamePadIndex, const int key, const Game::GamePadButtonEvent buttonEvent, const unsigned time) { - assert(gamePadIndex < Game::MAX_GAMEPADS); + assert(gamePadIndex < Game::MAX_GPAD_COUNT); const auto pressed = buttonEvent == Game::GPAD_BUTTON_PRESSED; const auto pressedOrUpdated = pressed || buttonEvent == Game::GPAD_BUTTON_UPDATE; @@ -1199,7 +1212,7 @@ namespace Components void Gamepad::CL_GamepadButtonEventForPort(const int gamePadIndex, const int key, const Game::GamePadButtonEvent buttonEvent, const unsigned time) { - assert(gamePadIndex < Game::MAX_GAMEPADS); + assert(gamePadIndex < Game::MAX_GPAD_COUNT); auto& gamePad = gamePads[gamePadIndex]; gamePad.inUse = true; @@ -1244,7 +1257,9 @@ namespace Components float Gamepad::GPad_GetStick(const int gamePadIndex, const Game::GamePadStick stick) { - assert(gamePadIndex < Game::MAX_GAMEPADS); + assert(gamePadIndex < Game::MAX_GPAD_COUNT); + assert(stick & Game::GPAD_STICK_MASK); + auto& gamePad = gamePads[gamePadIndex]; return gamePad.sticks[stick]; @@ -1252,7 +1267,7 @@ namespace Components float Gamepad::GPad_GetButton(const int gamePadIndex, Game::GamePadButton button) { - assert(gamePadIndex < Game::MAX_GAMEPADS); + assert(gamePadIndex < Game::MAX_GPAD_COUNT); auto& gamePad = gamePads[gamePadIndex]; float value = 0.0f; @@ -1276,7 +1291,9 @@ namespace Components bool Gamepad::GPad_IsButtonPressed(const int gamePadIndex, Game::GamePadButton button) { - assert(gamePadIndex < Game::MAX_GAMEPADS); + assert(gamePadIndex < Game::MAX_GPAD_COUNT); + assert(button & (Game::GPAD_DIGITAL_MASK | Game::GPAD_ANALOG_MASK)); + auto& gamePad = gamePads[gamePadIndex]; bool down = false; @@ -1310,7 +1327,8 @@ namespace Components bool Gamepad::GPad_IsButtonReleased(int gamePadIndex, Game::GamePadButton button) { - assert(gamePadIndex < Game::MAX_GAMEPADS); + assert(gamePadIndex < Game::MAX_GPAD_COUNT); + auto& gamePad = gamePads[gamePadIndex]; bool down = false; @@ -1340,7 +1358,8 @@ namespace Components void Gamepad::GPad_UpdateSticksDown(const int gamePadIndex) { - assert(gamePadIndex < Game::MAX_GAMEPADS); + assert(gamePadIndex < Game::MAX_GPAD_COUNT); + auto& gamePad = gamePads[gamePadIndex]; for (auto stickIndex = 0u; stickIndex < std::extent_v; stickIndex++) @@ -1371,7 +1390,7 @@ namespace Components void Gamepad::GPad_UpdateSticks(const int gamePadIndex, const XINPUT_GAMEPAD& state) { - assert(gamePadIndex < Game::MAX_GAMEPADS); + assert(gamePadIndex < Game::MAX_GPAD_COUNT); auto& gamePad = gamePads[gamePadIndex]; @@ -1403,7 +1422,7 @@ namespace Components void Gamepad::GPad_UpdateDigitals(const int gamePadIndex, const XINPUT_GAMEPAD& state) { - assert(gamePadIndex < Game::MAX_GAMEPADS); + assert(gamePadIndex < Game::MAX_GPAD_COUNT); auto& gamePad = gamePads[gamePadIndex]; @@ -1425,7 +1444,7 @@ namespace Components void Gamepad::GPad_UpdateAnalogs(const int gamePadIndex, const XINPUT_GAMEPAD& state) { - assert(gamePadIndex < Game::MAX_GAMEPADS); + assert(gamePadIndex < Game::MAX_GPAD_COUNT); auto& gamePad = gamePads[gamePadIndex]; @@ -1452,7 +1471,7 @@ namespace Components { GPad_RefreshAll(); - for (auto currentGamePadIndex = 0; currentGamePadIndex < Game::MAX_GAMEPADS; currentGamePadIndex++) + for (auto currentGamePadIndex = 0; currentGamePadIndex < Game::MAX_GPAD_COUNT; currentGamePadIndex++) { const auto& gamePad = gamePads[currentGamePadIndex]; if (!gamePad.enabled) @@ -1477,7 +1496,7 @@ namespace Components const auto time = Game::Sys_Milliseconds(); bool gpadPresent = false; - for (auto gamePadIndex = 0; gamePadIndex < Game::MAX_GAMEPADS; gamePadIndex++) + for (auto gamePadIndex = 0; gamePadIndex < Game::MAX_GPAD_COUNT; gamePadIndex++) { const auto& gamePad = gamePads[gamePadIndex]; @@ -1540,7 +1559,8 @@ namespace Components void Gamepad::Gamepad_WriteBindings(const int gamePadIndex, const int handle) { - assert(gamePadIndex < Game::MAX_GAMEPADS); + assert(gamePadIndex < Game::MAX_GPAD_COUNT); + auto& gamePadGlobal = gamePadGlobals[gamePadIndex]; Game::FS_Printf(handle, "unbindallaxis\n"); @@ -1584,7 +1604,7 @@ namespace Components push 0x60B26E retn - endMethod: + endMethod: push 0x60B298 retn } @@ -1592,7 +1612,7 @@ namespace Components void Gamepad::Gamepad_BindAxis(const int gamePadIndex, const Game::GamepadPhysicalAxis realIndex, const Game::GamepadVirtualAxis axisIndex, const Game::GamepadMapping mapType) { - assert(gamePadIndex < Game::MAX_GAMEPADS); + assert(gamePadIndex < Game::MAX_GPAD_COUNT); assert(realIndex > Game::GPAD_PHYSAXIS_NONE && realIndex < Game::GPAD_PHYSAXIS_COUNT); assert(axisIndex > Game::GPAD_VIRTAXIS_NONE && axisIndex < Game::GPAD_VIRTAXIS_COUNT); assert(mapType > Game::GPAD_MAP_NONE && mapType < Game::GPAD_MAP_COUNT); diff --git a/src/Components/Modules/Gamepad.hpp b/src/Components/Modules/Gamepad.hpp index 11fb17d8..45969762 100644 --- a/src/Components/Modules/Gamepad.hpp +++ b/src/Components/Modules/Gamepad.hpp @@ -62,8 +62,8 @@ namespace Components static Game::keyname_t combinedLocalizedKeyNamesPs3[]; static ControllerMenuKeyMapping controllerMenuKeyMappings[]; - static GamePad gamePads[Game::MAX_GAMEPADS]; - static GamePadGlobals gamePadGlobals[Game::MAX_GAMEPADS]; + static GamePad gamePads[Game::MAX_GPAD_COUNT]; + static GamePadGlobals gamePadGlobals[Game::MAX_GPAD_COUNT]; static int gamePadBindingsModifiedFlags; diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index 53da9032..852aec3b 100644 --- a/src/Game/Structs.hpp +++ b/src/Game/Structs.hpp @@ -7615,7 +7615,7 @@ namespace Game static_assert(sizeof(cg_s) == 0xFD540); - static constexpr auto MAX_GAMEPADS = 1; + static constexpr auto MAX_GPAD_COUNT = 1; static constexpr auto GPAD_VALUE_MASK = 0xFFFFFFFu; static constexpr auto GPAD_DPAD_MASK = XINPUT_GAMEPAD_DPAD_UP | XINPUT_GAMEPAD_DPAD_DOWN | XINPUT_GAMEPAD_DPAD_LEFT | XINPUT_GAMEPAD_DPAD_RIGHT;