From f0258631b2373cb039f42cc9513479eddd30456b Mon Sep 17 00:00:00 2001 From: Diavolo Date: Wed, 24 Aug 2022 23:46:07 +0200 Subject: [PATCH] [Vote] Improvements and other things --- src/Components/Modules/Gamepad.cpp | 38 ++++++++++++++--------------- src/Components/Modules/Maps.cpp | 10 ++++---- src/Components/Modules/Menus.cpp | 2 +- src/Components/Modules/RawFiles.hpp | 3 ++- src/Components/Modules/UIScript.cpp | 14 +++++------ src/Components/Modules/UIScript.hpp | 11 ++++----- src/Components/Modules/Vote.cpp | 16 ++++++------ src/Components/Modules/Vote.hpp | 2 +- src/Game/Dvars.cpp | 1 + src/Game/Dvars.hpp | 1 + src/Game/Functions.cpp | 5 ++++ src/Game/Functions.hpp | 7 ++++++ src/Game/Structs.hpp | 34 ++++++++++++-------------- src/Utils/IO.cpp | 2 +- 14 files changed, 77 insertions(+), 69 deletions(-) diff --git a/src/Components/Modules/Gamepad.cpp b/src/Components/Modules/Gamepad.cpp index d9d8371c..05b02f7f 100644 --- a/src/Components/Modules/Gamepad.cpp +++ b/src/Components/Modules/Gamepad.cpp @@ -314,7 +314,7 @@ namespace Components bool Gamepad::GPad_Check(const int gamePadIndex, const int portIndex) { - assert((portIndex >= 0) && (portIndex < Game::MAX_GPAD_COUNT)); + AssertIn(gamePadIndex, Game::MAX_GPAD_COUNT); auto& gamePad = gamePads[gamePadIndex]; @@ -422,7 +422,7 @@ namespace Components bool Gamepad::AimAssist_IsLockonActive(const int gamePadIndex) { - assert(gamePadIndex < Game::MAX_GPAD_COUNT); + AssertIn(gamePadIndex, Game::MAX_GPAD_COUNT); auto& aaGlob = Game::aaGlobArray[gamePadIndex]; @@ -827,7 +827,7 @@ namespace Components void Gamepad::CL_GamepadMove(const int gamePadIndex, Game::usercmd_s* cmd, const float frameTimeBase) { - assert(gamePadIndex < Game::MAX_GPAD_COUNT); + AssertIn(gamePadIndex, Game::MAX_GPAD_COUNT); auto& gamePad = gamePads[gamePadIndex]; auto& clientActive = Game::clients[gamePadIndex]; @@ -970,7 +970,7 @@ namespace Components void Gamepad::CL_GamepadResetMenuScrollTime(const int gamePadIndex, const int key, const bool down, const unsigned time) { - assert(gamePadIndex < Game::MAX_GPAD_COUNT); + AssertIn(gamePadIndex, Game::MAX_GPAD_COUNT); auto& gamePadGlobal = gamePadGlobals[gamePadIndex]; @@ -990,7 +990,7 @@ namespace Components void Gamepad::CL_GamepadGenerateAPad(const int gamePadIndex, const Game::GamepadPhysicalAxis physicalAxis, unsigned time) { - assert(gamePadIndex < Game::MAX_GPAD_COUNT); + AssertIn(gamePadIndex, Game::MAX_GPAD_COUNT); assert(physicalAxis >= 0 && physicalAxis < Game::GPAD_PHYSAXIS_COUNT); auto& gamePad = gamePads[gamePadIndex]; @@ -1025,7 +1025,7 @@ namespace Components void Gamepad::CL_GamepadEvent(const int gamePadIndex, const Game::GamepadPhysicalAxis physicalAxis, const float value, const unsigned time) { - assert(gamePadIndex < Game::MAX_GPAD_COUNT); + AssertIn(gamePadIndex, Game::MAX_GPAD_COUNT); assert(physicalAxis >= 0 && physicalAxis < Game::GPAD_PHYSAXIS_COUNT); auto& gamePad = gamePads[gamePadIndex]; @@ -1126,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_GPAD_COUNT); + AssertIn(gamePadInde, Game::MAX_GPAD_COUNT); const auto pressed = buttonEvent == Game::GPAD_BUTTON_PRESSED; const auto pressedOrUpdated = pressed || buttonEvent == Game::GPAD_BUTTON_UPDATE; @@ -1212,9 +1212,9 @@ namespace Components void Gamepad::CL_GamepadButtonEventForPort(const int gamePadIndex, const int key, const Game::GamePadButtonEvent buttonEvent, const unsigned time) { - assert(gamePadIndex < Game::MAX_GPAD_COUNT); - auto& gamePad = gamePads[gamePadIndex]; + AssertIn(gamePadIndex, Game::MAX_GPAD_COUNT); + auto& gamePad = gamePads[gamePadIndex]; gamePad.inUse = true; gpad_in_use.setRaw(true); @@ -1257,7 +1257,7 @@ namespace Components float Gamepad::GPad_GetStick(const int gamePadIndex, const Game::GamePadStick stick) { - assert(gamePadIndex < Game::MAX_GPAD_COUNT); + AssertIn(gamePadIndex, Game::MAX_GPAD_COUNT); assert(stick & Game::GPAD_STICK_MASK); auto& gamePad = gamePads[gamePadIndex]; @@ -1267,7 +1267,7 @@ namespace Components float Gamepad::GPad_GetButton(const int gamePadIndex, Game::GamePadButton button) { - assert(gamePadIndex < Game::MAX_GPAD_COUNT); + AssertIn(gamePadIndex, Game::MAX_GPAD_COUNT); auto& gamePad = gamePads[gamePadIndex]; float value = 0.0f; @@ -1291,7 +1291,7 @@ namespace Components bool Gamepad::GPad_IsButtonPressed(const int gamePadIndex, Game::GamePadButton button) { - assert(gamePadIndex < Game::MAX_GPAD_COUNT); + AssertIn(gamePadIndex, Game::MAX_GPAD_COUNT); assert(button & (Game::GPAD_DIGITAL_MASK | Game::GPAD_ANALOG_MASK)); auto& gamePad = gamePads[gamePadIndex]; @@ -1327,7 +1327,7 @@ namespace Components bool Gamepad::GPad_IsButtonReleased(int gamePadIndex, Game::GamePadButton button) { - assert(gamePadIndex < Game::MAX_GPAD_COUNT); + AssertIn(gamePadIndex, Game::MAX_GPAD_COUNT); auto& gamePad = gamePads[gamePadIndex]; @@ -1358,7 +1358,7 @@ namespace Components void Gamepad::GPad_UpdateSticksDown(const int gamePadIndex) { - assert(gamePadIndex < Game::MAX_GPAD_COUNT); + AssertIn(gamePadIndex, Game::MAX_GPAD_COUNT); auto& gamePad = gamePads[gamePadIndex]; @@ -1390,7 +1390,7 @@ namespace Components void Gamepad::GPad_UpdateSticks(const int gamePadIndex, const XINPUT_GAMEPAD& state) { - assert(gamePadIndex < Game::MAX_GPAD_COUNT); + AssertIn(gamePadIndex, Game::MAX_GPAD_COUNT); auto& gamePad = gamePads[gamePadIndex]; @@ -1422,7 +1422,7 @@ namespace Components void Gamepad::GPad_UpdateDigitals(const int gamePadIndex, const XINPUT_GAMEPAD& state) { - assert(gamePadIndex < Game::MAX_GPAD_COUNT); + AssertIn(gamePadIndex, Game::MAX_GPAD_COUNT); auto& gamePad = gamePads[gamePadIndex]; @@ -1444,7 +1444,7 @@ namespace Components void Gamepad::GPad_UpdateAnalogs(const int gamePadIndex, const XINPUT_GAMEPAD& state) { - assert(gamePadIndex < Game::MAX_GPAD_COUNT); + AssertIn(gamePadIndex, Game::MAX_GPAD_COUNT); auto& gamePad = gamePads[gamePadIndex]; @@ -1559,7 +1559,7 @@ namespace Components void Gamepad::Gamepad_WriteBindings(const int gamePadIndex, const int handle) { - assert(gamePadIndex < Game::MAX_GPAD_COUNT); + AssertIn(gamePadIndex, Game::MAX_GPAD_COUNT); auto& gamePadGlobal = gamePadGlobals[gamePadIndex]; @@ -1612,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_GPAD_COUNT); + AssertIn(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/Maps.cpp b/src/Components/Modules/Maps.cpp index 9f0b04df..13059ad0 100644 --- a/src/Components/Modules/Maps.cpp +++ b/src/Components/Modules/Maps.cpp @@ -96,12 +96,12 @@ namespace Components const char* Maps::LoadArenaFileStub(const char* name, char* buffer, int size) { - std::string data = Game::Scr_AddSourceBuffer(nullptr, name, nullptr, false); + std::string data = RawFiles::ReadRawFile(name, buffer, size); - if(Maps::UserMap.isValid()) + if (Maps::UserMap.isValid()) { - std::string mapname = Maps::UserMap.getName(); - std::string arena = Utils::String::VA("usermaps/%s/%s.arena", mapname.data(), mapname.data()); + const std::string mapname = Maps::UserMap.getName(); + const auto* arena = Utils::String::VA("usermaps/%s/%s.arena", mapname.data(), mapname.data()); if (Utils::IO::FileExists(arena)) { @@ -109,7 +109,7 @@ namespace Components } } - strncpy_s(buffer, size, data.data(), data.size()); + strncpy_s(buffer, size, data.data(), _TRUNCATE); return buffer; } diff --git a/src/Components/Modules/Menus.cpp b/src/Components/Modules/Menus.cpp index ca347a18..ef726aed 100644 --- a/src/Components/Modules/Menus.cpp +++ b/src/Components/Modules/Menus.cpp @@ -806,7 +806,7 @@ namespace Components } // Not quite sure if we want to do this if we're not ingame, but it's only needed for ingame menus. - if (Dvar::Var("cl_ingame").get()) + if ((*Game::cl_ingame)->current.enabled) { Game::Key_SetCatcher(0, 16); } diff --git a/src/Components/Modules/RawFiles.hpp b/src/Components/Modules/RawFiles.hpp index 6dca6efe..1c3fef1f 100644 --- a/src/Components/Modules/RawFiles.hpp +++ b/src/Components/Modules/RawFiles.hpp @@ -7,8 +7,9 @@ namespace Components public: RawFiles(); - private: static char* ReadRawFile(const char* filename, char* buf, int size); + + private: static char* GetMenuBuffer(const char* filename); }; } diff --git a/src/Components/Modules/UIScript.cpp b/src/Components/Modules/UIScript.cpp index a8f5b677..9f00d097 100644 --- a/src/Components/Modules/UIScript.cpp +++ b/src/Components/Modules/UIScript.cpp @@ -2,8 +2,8 @@ namespace Components { - std::unordered_map> UIScript::UIScripts; - std::unordered_map> UIScript::UIOwnerDraws; + std::unordered_map UIScript::UIScripts; + std::unordered_map> UIScript::UIOwnerDraws; template<> int UIScript::Token::get() const { @@ -49,22 +49,22 @@ namespace Components return &Game::uiInfoArray[localClientNum]; } - void UIScript::Add(const std::string& name, const Utils::Slot& callback) + void UIScript::Add(const std::string& name, const UIScript::UIScriptHandler& callback) { UIScript::UIScripts[name] = callback; } - void UIScript::AddOwnerDraw(int ownerdraw, const Utils::Slot& callback) + void UIScript::AddOwnerDraw(int ownerdraw, const std::function& callback) { UIScript::UIOwnerDraws[ownerdraw] = callback; } bool UIScript::RunMenuScript(const char* name, const char** args) { - if (UIScript::UIScripts.contains(name)) + if (const auto got = UIScript::UIScripts.find(name); got != UIScript::UIScripts.end()) { const auto* info = UIScript::UI_GetClientInfo(0); - UIScript::UIScripts[name](UIScript::Token(args), info); + got->second(UIScript::Token(args), info); return true; } @@ -73,7 +73,7 @@ namespace Components void UIScript::OwnerDrawHandleKeyStub(int ownerDraw, int flags, float *special, int key) { - if (key == 200 || key == 201) //mouse buttons + if (key == 200 || key == 201) // mouse buttons { for (auto i = UIScript::UIOwnerDraws.begin(); i != UIScript::UIOwnerDraws.end(); ++i) { diff --git a/src/Components/Modules/UIScript.hpp b/src/Components/Modules/UIScript.hpp index a6f55c98..553e15ab 100644 --- a/src/Components/Modules/UIScript.hpp +++ b/src/Components/Modules/UIScript.hpp @@ -24,20 +24,19 @@ namespace Components void parse(const char** args); }; - typedef void(Callback)(const Token& token, const Game::uiInfo_s* info); - typedef void(CallbackRaw)(); + using UIScriptHandler = std::function; static Game::uiInfo_s* UI_GetClientInfo(int localClientNum); - static void Add(const std::string& name, const Utils::Slot& callback); - static void AddOwnerDraw(int ownerdraw, const Utils::Slot& callback); + static void Add(const std::string& name, const UIScriptHandler& callback); + static void AddOwnerDraw(int ownerdraw, const std::function& callback); private: static void OwnerDrawHandleKeyStub(int ownerDraw, int flags, float *special, int key); static bool RunMenuScript(const char* name, const char** args); static void RunMenuScriptStub(); - static std::unordered_map> UIScripts; - static std::unordered_map> UIOwnerDraws; + static std::unordered_map UIScripts; + static std::unordered_map> UIOwnerDraws; }; } diff --git a/src/Components/Modules/Vote.cpp b/src/Components/Modules/Vote.cpp index 93f53763..67eebbdd 100644 --- a/src/Components/Modules/Vote.cpp +++ b/src/Components/Modules/Vote.cpp @@ -64,9 +64,10 @@ namespace Components strncpy_s(arg2, params->get(2), _TRUNCATE); strncpy_s(arg3, params->get(3), _TRUNCATE); - if (!MapRotation::Contains("gametype", arg2) || - !MapRotation::Contains("map", arg3)) + // This prevents abuse + if (!MapRotation::Contains("map", arg3)) { + Game::SV_GameSendServerCommand(ent - Game::g_entities, Game::SV_CMD_CAN_IGNORE, VA("%c \"GAME_NOTONROTATION\"", 0x65)); return false; } @@ -124,8 +125,10 @@ namespace Components bool Vote::HandleMap([[maybe_unused]] const Game::gentity_s* ent, [[maybe_unused]] const Command::ServerParams* params) { + // This prevents abuse if (!MapRotation::Contains("map", params->get(2))) { + Game::SV_GameSendServerCommand(ent - Game::g_entities, Game::SV_CMD_CAN_IGNORE, VA("%c \"GAME_NOTONROTATION\"", 0x65)); return false; } @@ -136,11 +139,6 @@ namespace Components bool Vote::HandleGametype([[maybe_unused]] const Game::gentity_s* ent, [[maybe_unused]] const Command::ServerParams* params) { - if (!MapRotation::Contains("gametype", params->get(2))) - { - return false; - } - if (!Game::Scr_IsValidGameType(params->get(2))) { Game::SV_GameSendServerCommand(ent - Game::g_entities, Game::SV_CMD_CAN_IGNORE, VA("%c \"GAME_INVALIDGAMETYPE\"", 0x65)); @@ -342,13 +340,13 @@ namespace Components UIScript::Add("voteTypeMap", []([[maybe_unused]] const UIScript::Token& token, [[maybe_unused]] const Game::uiInfo_s* info) { Game::Cbuf_AddText(0, VA("callvote typemap %s %s\n", Game::sharedUiInfo->gameTypes[(*Game::ui_netGameType)->current.integer].gameType, - Game::sharedUiInfo->mapList[(*Game::ui_netGameType)->current.integer].mapName)); + Game::sharedUiInfo->mapList[(*Game::ui_currentMap)->current.integer].mapName)); }); UIScript::Add("voteMap", []([[maybe_unused]] const UIScript::Token& token, [[maybe_unused]] const Game::uiInfo_s* info) { if ((*Game::ui_currentMap)->current.integer >= 0 && - (*Game::ui_currentMap)->current.integer mapCount) + (*Game::ui_currentMap)->current.integer < Game::sharedUiInfo->mapCount) { Game::Cbuf_AddText(0, VA("callvote map %s\n", Game::sharedUiInfo->mapList[(*Game::ui_currentMap)->current.integer].mapName)); } diff --git a/src/Components/Modules/Vote.hpp b/src/Components/Modules/Vote.hpp index e8be3a43..82b35d22 100644 --- a/src/Components/Modules/Vote.hpp +++ b/src/Components/Modules/Vote.hpp @@ -12,7 +12,7 @@ namespace Components static std::unordered_map VoteCommands; static constexpr auto* CallVoteDesc = "%c \"GAME_VOTECOMMANDSARE\x15 map_restart, map_rotate, map , g_gametype , typemap , " - "kick , tempBanUser \""; + "kick , tempBanUser \""; static void DisplayVote(const Game::gentity_s* ent); static bool IsInvalidVoteString(const std::string& input); diff --git a/src/Game/Dvars.cpp b/src/Game/Dvars.cpp index 1b59359f..67bf764d 100644 --- a/src/Game/Dvars.cpp +++ b/src/Game/Dvars.cpp @@ -44,6 +44,7 @@ namespace Game const dvar_t** cl_showSend = reinterpret_cast(0xA1E870); const dvar_t** cl_voice = reinterpret_cast(0xB2BB44); + const dvar_t** cl_ingame = reinterpret_cast(0xB2BB80); const dvar_t** g_cheats = reinterpret_cast(0x1A45D54); const dvar_t** g_deadChat = reinterpret_cast(0x19BD5DC); diff --git a/src/Game/Dvars.hpp b/src/Game/Dvars.hpp index a13a4280..b0439cae 100644 --- a/src/Game/Dvars.hpp +++ b/src/Game/Dvars.hpp @@ -100,6 +100,7 @@ namespace Game extern const dvar_t** cl_showSend; extern const dvar_t** cl_voice; + extern const dvar_t** cl_ingame; extern const dvar_t** g_cheats; extern const dvar_t** g_deadChat; diff --git a/src/Game/Functions.cpp b/src/Game/Functions.cpp index 5f1e276c..3cf775ab 100644 --- a/src/Game/Functions.cpp +++ b/src/Game/Functions.cpp @@ -216,6 +216,7 @@ namespace Game UI_KeyEvent_t UI_KeyEvent = UI_KeyEvent_t(0x4970F0); UI_SafeTranslateString_t UI_SafeTranslateString = UI_SafeTranslateString_t(0x4F1700); UI_ReplaceConversions_t UI_ReplaceConversions = UI_ReplaceConversions_t(0x4E9740); + UI_ParseInfos_t UI_ParseInfos = UI_ParseInfos_t(0x4027A0); Win_GetLanguage_t Win_GetLanguage = Win_GetLanguage_t(0x45CBA0); @@ -418,6 +419,10 @@ namespace Game int* g_largeLocalPos = reinterpret_cast(0x63D97B4); int* g_largeLocalRightPos = reinterpret_cast(0x63D9780); + char** ui_arenaInfos = reinterpret_cast(0x62D2688); + int* ui_numArenas = reinterpret_cast(0x62D2788); + int* ui_arenaBufPos = reinterpret_cast(0x62D278C); + const char* TableLookup(StringTable* stringtable, int row, int column) { if (!stringtable || !stringtable->values || row >= stringtable->rowCount || column >= stringtable->columnCount) return ""; diff --git a/src/Game/Functions.hpp b/src/Game/Functions.hpp index 3f20a99e..f2c1bc0f 100644 --- a/src/Game/Functions.hpp +++ b/src/Game/Functions.hpp @@ -238,6 +238,9 @@ namespace Game typedef void(*UI_ReplaceConversions_t)(const char* sourceString, ConversionArguments* arguments, char* outputString, size_t outputStringSize); extern UI_ReplaceConversions_t UI_ReplaceConversions; + + typedef int(*UI_ParseInfos_t)(const char* buf, int max, char** infos); + extern UI_ParseInfos_t UI_ParseInfos; typedef void(*MSG_Init_t)(msg_t* buf, unsigned char* data, int length); extern MSG_Init_t MSG_Init; @@ -768,6 +771,10 @@ namespace Game extern int* g_largeLocalPos; extern int* g_largeLocalRightPos; + extern char** ui_arenaInfos; + extern int* ui_numArenas; + extern int* ui_arenaBufPos; + ScreenPlacement* ScrPlace_GetFullPlacement(); ScreenPlacement* ScrPlace_GetUnsafeFullPlacement(); diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index 3f1738c3..a6fd4bd8 100644 --- a/src/Game/Structs.hpp +++ b/src/Game/Structs.hpp @@ -5666,18 +5666,16 @@ namespace Game UILOCALVAR_STRING = 0x2, }; - union $B42A88463653BDCDFC5664844B4491DA - { - int integer; - float value; - const char *string; - }; - struct UILocalVar { UILocalVarType type; - const char *name; - $B42A88463653BDCDFC5664844B4491DA u; + const char* name; + union + { + int integer; + float value; + const char* string; + } u; }; struct UILocalVarContext @@ -5685,20 +5683,18 @@ namespace Game UILocalVar table[256]; }; - struct $1942E78D15753E2013144570239460A8 - { - float x; - float y; - int lastMoveTime; - }; - struct UiContext { int localClientNum; float bias; int realTime; int frameTime; - $1942E78D15753E2013144570239460A8 cursor; + struct + { + float x; + float y; + int lastMoveTime; + } cursor; int isCursorVisible; int paintFull; int screenWidth; @@ -5706,9 +5702,9 @@ namespace Game float screenAspect; float FPS; float blurRadiusOut; - menuDef_t *Menus[640]; + menuDef_t* Menus[640]; int menuCount; - menuDef_t *menuStack[16]; + menuDef_t* menuStack[16]; int openMenuCount; UILocalVarContext localVars; }; diff --git a/src/Utils/IO.cpp b/src/Utils/IO.cpp index 78f8d042..d0522fd7 100644 --- a/src/Utils/IO.cpp +++ b/src/Utils/IO.cpp @@ -54,7 +54,7 @@ namespace Utils if (size > -1) { data->resize(static_cast(size)); - stream.read(const_cast(data->data()), size); + stream.read(data->data(), size); stream.close(); return true; }