diff --git a/dll/game/LobbyInfo.h b/dll/game/LobbyInfo.h index 42e91ff..a01c2b5 100644 --- a/dll/game/LobbyInfo.h +++ b/dll/game/LobbyInfo.h @@ -106,8 +106,16 @@ class LobbyInfo { } void registerSlot(struct PlayerSlot* slot) { + if (!slot) { + debug("registerSlot: slot is NULL\r\n"); + return; + } std::lock_guard l(player_slot_mutex); int slot_number = slot->slotNumber; + if (slot_number < 0 || slot_number >= MAX_SLOT_NUMBERS) { + debug((char*)("registerSlot: invalid slot_number " + std::to_string(slot_number) + "\r\n").c_str()); + return; + } if (playerSlots[slot_number] == slot) { return; } @@ -131,17 +139,28 @@ class LobbyInfo { struct PlayerSlot* getPlayerSlot(int slot_number) { std::lock_guard l(player_slot_mutex); + if (slot_number < 0 || slot_number >= MAX_SLOT_NUMBERS) { + debug((char*)("getPlayerSlot: invalid slot_number " + std::to_string(slot_number) + "\r\n").c_str()); + return NULL; + } return this->playerSlots[slot_number]; } std::string getSlotNickname(int slot_number) { - // @todo invalid argument exception std::lock_guard l(player_slot_mutex); + if (slot_number < 0 || slot_number >= MAX_SLOT_NUMBERS) { + debug((char*)("getSlotNickname: invalid slot_number " + std::to_string(slot_number) + "\r\n").c_str()); + return ""; + } return this->slotNicknames[slot_number]; } void setSlotNickname(int slot_number, std::string nickname) { std::lock_guard l(player_slot_mutex); + if (slot_number < 0 || slot_number >= MAX_SLOT_NUMBERS) { + debug((char*)("setSlotNickname: invalid slot_number " + std::to_string(slot_number) + "\r\n").c_str()); + return; + } this->slotNicknames[slot_number] = nickname; } diff --git a/dll/game/PlayerInfo/LagabuseDotaPlayerInfo.h b/dll/game/PlayerInfo/LagabuseDotaPlayerInfo.h index ab06976..2546a6c 100644 --- a/dll/game/PlayerInfo/LagabuseDotaPlayerInfo.h +++ b/dll/game/PlayerInfo/LagabuseDotaPlayerInfo.h @@ -14,11 +14,20 @@ class LagabuseDotaPlayerInfo : public PlayerInfo { LagabuseDotaPlayerInfo& operator=(LagabuseDotaPlayerInfo const&) = delete; // disable copy-assignment constructor virtual void applyJsonData(nlohmann::basic_json json) override { - this->rating = json["rating"]; - this->wins = json["wins"]; - this->loses = json["loses"]; - this->gamesCount = this->wins + this->loses; - this->rank = json["rank"]; + try { + this->rating = json.value("rating", 0UL); + this->wins = json.value("wins", 0UL); + this->loses = json.value("loses", 0UL); + this->gamesCount = this->wins + this->loses; + this->rank = json.value("rank", 0UL); + } catch (const nlohmann::json::exception& e) { + debug((char*)("JSON parse error in applyJsonData: " + std::string(e.what()) + "\r\n").c_str()); + this->rating = 0; + this->wins = 0; + this->loses = 0; + this->gamesCount = 0; + this->rank = 0; + } } virtual std::string compileNameString() override { diff --git a/dll/game/PlayerInfoUpdater.h b/dll/game/PlayerInfoUpdater.h index bb33cfe..2d382ed 100644 --- a/dll/game/PlayerInfoUpdater.h +++ b/dll/game/PlayerInfoUpdater.h @@ -128,10 +128,17 @@ class PlayerInfoUpdater { debug((char*)querystring.c_str()); debug("\r\n"); if (res) { - auto player_info_list = json::parse(res->body); - for (json::iterator it = player_info_list.begin(); it != player_info_list.end(); ++it) { - std::string nickname = (*it)["name"]; - player_info_mapped[nickname] = *it; + try { + auto player_info_list = json::parse(res->body); + for (json::iterator it = player_info_list.begin(); it != player_info_list.end(); ++it) { + std::string nickname = (*it).value("name", ""); + if (!nickname.empty()) { + player_info_mapped[nickname] = *it; + } + } + } catch (const nlohmann::json::exception& e) { + debug((char*)("JSON parse error in fetchPlayersInfo: " + std::string(e.what()) + "\r\n").c_str()); + should_reschedule = true; } } else { diff --git a/dll/game/hook-handlers.cpp b/dll/game/hook-handlers.cpp index 564911a..5f8613b 100644 --- a/dll/game/hook-handlers.cpp +++ b/dll/game/hook-handlers.cpp @@ -108,6 +108,9 @@ __declspec(dllexport) void __stdcall onAfterSlotInit(struct PlayerSlot* slot) { } __declspec(dllexport) void __stdcall onSlotChange(struct PlayerSlot* slot, char* nickname) { + if (!slot) { + return; + } std::string str_nickname; if (nickname) { str_nickname = nickname; @@ -136,9 +139,14 @@ __declspec(dllexport) int __stdcall isDefaultUpdatingTeamTextDisabled() { } __declspec(dllexport) void __stdcall afterSlotTextsUpdate(struct PlayerSlot* slot) { + if (!slot) { + return; + } int slotNumber = slot->slotNumber; std::string nickname = lobby_info->getSlotNickname(slotNumber); - lobby_info->getPlayerInfo(nickname)->applyToPlayerSlot(slot); + if (!nickname.empty()) { + lobby_info->getPlayerInfo(nickname)->applyToPlayerSlot(slot); + } } __declspec(dllexport) void init_hook_handlers() { diff --git a/dll/game/player-slot-helpers.cpp b/dll/game/player-slot-helpers.cpp index 8dc883a..546b0b7 100644 --- a/dll/game/player-slot-helpers.cpp +++ b/dll/game/player-slot-helpers.cpp @@ -7,6 +7,9 @@ extern "C" { // PlayerName LPVOID getPlayerNameTextFrame(struct PlayerSlot* slot) { + if (!slot || !slot->playerName || !slot->playerName->comboBox) { + return NULL; + } return slot->playerName->comboBox->firstTextFrame; } @@ -15,11 +18,17 @@ void initPlayerNameFont(struct PlayerSlot* slot, const char* fontPath, float fon } void setPlayerNameText(struct PlayerSlot* slot, const char* text) { - textFrameSetText(getPlayerNameTextFrame(slot), text); + LPVOID textFrame = getPlayerNameTextFrame(slot); + if (textFrame) { + textFrameSetText(textFrame, text); + } } // PlayerRace LPVOID getPlayerRaceTextFrame(struct PlayerSlot* slot) { + if (!slot || !slot->playerRace || !slot->playerRace->comboBox) { + return NULL; + } return slot->playerRace->comboBox->firstTextFrame; } @@ -28,11 +37,17 @@ void initPlayerRaceFont(struct PlayerSlot* slot, const char* fontPath, float fon } void setPlayerRaceText(struct PlayerSlot* slot, const char* text) { - textFrameSetText(getPlayerRaceTextFrame(slot), text); + LPVOID textFrame = getPlayerRaceTextFrame(slot); + if (textFrame) { + textFrameSetText(textFrame, text); + } } // PlayerTeam LPVOID getPlayerTeamTextFrame(struct PlayerSlot* slot) { + if (!slot || !slot->teamButtonTitle) { + return NULL; + } return slot->teamButtonTitle; } @@ -41,11 +56,17 @@ void initPlayerTeamFont(struct PlayerSlot* slot, const char* fontPath, float fon } void setPlayerTeamText(struct PlayerSlot* slot, const char* text) { - textFrameSetText(getPlayerTeamTextFrame(slot), text); + LPVOID textFrame = getPlayerTeamTextFrame(slot); + if (textFrame) { + textFrameSetText(textFrame, text); + } } // PlayerHandicap LPVOID getPlayerHandicapTextFrame(struct PlayerSlot* slot) { + if (!slot || !slot->playerHandicap || !slot->playerHandicap->comboBox) { + return NULL; + } return slot->playerHandicap->comboBox->firstTextFrame; } @@ -54,7 +75,10 @@ void initPlayerHandicapFont(struct PlayerSlot* slot, const char* fontPath, float } void setHandicapText(struct PlayerSlot* slot, const char* text) { - textFrameSetText(getPlayerHandicapTextFrame(slot), text); + LPVOID textFrame = getPlayerHandicapTextFrame(slot); + if (textFrame) { + textFrameSetText(textFrame, text); + } } #ifdef __cplusplus