From 9062491f1d00a46b0d739aea2e313b7cce1431eb Mon Sep 17 00:00:00 2001 From: Mr-X-GTA <110748953+Mr-X-GTA@users.noreply.github.com> Date: Thu, 9 May 2024 23:28:11 +0200 Subject: [PATCH] Fix player list crash (#3080) * Fixed get_rockstar_id returning an incorrect type. * Added more OOP usage of get_rockstar_id when applicable. Co-authored-by: gir489returns --- .../commands/player/kick/complaint_kick.cpp | 2 +- src/backend/reactions/interloper_reaction.cpp | 3 +-- src/backend/reactions/reaction.cpp | 6 ++---- .../player_management/assign_physical_index.cpp | 15 ++++++--------- src/hooks/protections/script_event_handler.cpp | 2 +- src/services/friends/friends_service.cpp | 11 +++++++---- .../player_database/player_database_service.cpp | 6 +++--- src/services/players/player.cpp | 10 ++++++++-- src/services/players/player.hpp | 1 + src/util/chat.hpp | 8 ++++---- src/views/players/view_players.cpp | 9 +++------ 11 files changed, 37 insertions(+), 36 deletions(-) diff --git a/src/backend/commands/player/kick/complaint_kick.cpp b/src/backend/commands/player/kick/complaint_kick.cpp index d3c7678f..955c97af 100644 --- a/src/backend/commands/player/kick/complaint_kick.cpp +++ b/src/backend/commands/player/kick/complaint_kick.cpp @@ -19,7 +19,7 @@ namespace big virtual void execute(player_ptr player, const command_arguments& _args, const std::shared_ptr ctx) override { - if (!player) + if (!player || !player->is_valid()) return; if (gta_util::get_network()->m_game_session_ptr->is_host()) { diff --git a/src/backend/reactions/interloper_reaction.cpp b/src/backend/reactions/interloper_reaction.cpp index bb79a30e..718b4d74 100644 --- a/src/backend/reactions/interloper_reaction.cpp +++ b/src/backend/reactions/interloper_reaction.cpp @@ -25,12 +25,11 @@ namespace big if (log) { - uint64_t rockstar_id = attacker->get_net_data() == nullptr ? 0 : attacker->get_net_data()->m_gamer_handle.m_rockstar_id; LOGF(WARNING, "Received {} from {} ({}), victim is {}", m_event_name, attacker->get_name(), - rockstar_id, + attacker->get_rockstar_id(), victim->get_name()); } diff --git a/src/backend/reactions/reaction.cpp b/src/backend/reactions/reaction.cpp index 57952055..160e6592 100644 --- a/src/backend/reactions/reaction.cpp +++ b/src/backend/reactions/reaction.cpp @@ -56,8 +56,7 @@ namespace big if (log) { - uint64_t rockstar_id = player->get_net_data() == nullptr ? 0 : player->get_net_data()->m_gamer_handle.m_rockstar_id; - LOGF(WARNING, "Received {} from {} ({})", m_event_name, player->get_name(), rockstar_id); + LOGF(WARNING, "Received {} from {} ({})", m_event_name, player->get_name(), player->get_rockstar_id()); } if (announce_in_chat) @@ -89,8 +88,7 @@ namespace big if (log) { - uint64_t rockstar_id = player->get_net_data() == nullptr ? 0 : player->get_net_data()->m_gamer_handle.m_rockstar_id; - LOGF(WARNING, "Received {} from {} ({})", m_event_name, player->get_name(), rockstar_id); + LOGF(WARNING, "Received {} from {} ({})", m_event_name, player->get_name(), player->get_rockstar_id()); } if (notify) diff --git a/src/hooks/player_management/assign_physical_index.cpp b/src/hooks/player_management/assign_physical_index.cpp index 68b59fde..e5afe5e8 100644 --- a/src/hooks/player_management/assign_physical_index.cpp +++ b/src/hooks/player_management/assign_physical_index.cpp @@ -35,17 +35,15 @@ namespace big { g_lua_manager->trigger_event(net_player_data->m_name); + auto rockstar_id = net_player_data->m_gamer_handle.m_rockstar_id; + if (g.notifications.player_leave.log) - LOG(INFO) << "Player left '" << net_player_data->m_name << "' freeing slot #" << (int)player->m_player_id - << " with Rockstar ID: " << net_player_data->m_gamer_handle.m_rockstar_id; + LOG(INFO) << "Player left '" << net_player_data->m_name << "' freeing slot #" << (int)player->m_player_id << " with Rockstar ID: " << rockstar_id; if (g.notifications.player_leave.notify) { g_notification_service.push("PLAYER_LEFT"_T.data(), - std::vformat("PLAYER_LEFT_INFO"_T, - std::make_format_args(net_player_data->m_name, - player->m_player_id, - net_player_data->m_gamer_handle.m_rockstar_id))); + std::vformat("PLAYER_LEFT_INFO"_T, std::make_format_args(net_player_data->m_name, player->m_player_id, rockstar_id))); } } @@ -94,10 +92,9 @@ namespace big g_fiber_pool->queue_job([id] { if (auto plyr = g_player_service->get_by_id(id)) { - if (plyr->get_net_data()->m_gamer_handle.m_rockstar_id != 0) + if (auto rockstar_id = plyr->get_rockstar_id(); rockstar_id != 0) { - if (auto entry = g_player_database_service->get_player_by_rockstar_id( - plyr->get_net_data()->m_gamer_handle.m_rockstar_id)) + if (auto entry = g_player_database_service->get_player_by_rockstar_id(rockstar_id)) { plyr->is_trusted = entry->is_trusted; if (!(plyr->is_friend() && g.session.trust_friends)) diff --git a/src/hooks/protections/script_event_handler.cpp b/src/hooks/protections/script_event_handler.cpp index 0e8c14e5..59c65547 100644 --- a/src/hooks/protections/script_event_handler.cpp +++ b/src/hooks/protections/script_event_handler.cpp @@ -460,7 +460,7 @@ namespace big && (!g.debug.logs.script_event.filter_player || g.debug.logs.script_event.player_id == player->m_player_id)) [[unlikely]] { std::stringstream output; - output << "Script Event From: " << player->get_name() << " (" << player->get_net_data()->m_gamer_handle.m_rockstar_id << ") Args: { "; + output << "Script Event From: " << player->get_name() << " (" << plyr->get_rockstar_id() << ") Args: { "; for (int i = 0; i < args_count; i++) { if (i) diff --git a/src/services/friends/friends_service.cpp b/src/services/friends/friends_service.cpp index 93833b9f..b78a5b29 100644 --- a/src/services/friends/friends_service.cpp +++ b/src/services/friends/friends_service.cpp @@ -22,10 +22,13 @@ namespace big if (net_player == nullptr) return false; - const auto rockstar_id = net_player->get_net_data()->m_gamer_handle.m_rockstar_id; - for (uint32_t i = 0; i < g_pointers->m_gta.m_friend_registry->m_friend_count; i++) - if (rockstar_id == g_pointers->m_gta.m_friend_registry->get(i)->m_rockstar_id) - return true; + if (auto net_data = net_player->get_net_data()) + { + const auto rockstar_id = net_data->m_gamer_handle.m_rockstar_id; + for (uint32_t i = 0; i < g_pointers->m_gta.m_friend_registry->m_friend_count; i++) + if (rockstar_id == g_pointers->m_gta.m_friend_registry->get(i)->m_rockstar_id) + return true; + } return false; } diff --git a/src/services/player_database/player_database_service.cpp b/src/services/player_database/player_database_service.cpp index f894009e..04715951 100644 --- a/src/services/player_database/player_database_service.cpp +++ b/src/services/player_database/player_database_service.cpp @@ -255,11 +255,11 @@ namespace big std::shared_ptr player_database_service::get_or_create_player(player_ptr player) { - if (m_players.contains(player->get_net_data()->m_gamer_handle.m_rockstar_id)) - return m_players[player->get_net_data()->m_gamer_handle.m_rockstar_id]; + if (m_players.contains(player->get_rockstar_id())) + return m_players[player->get_rockstar_id()]; else { - auto player_ptr = add_player(player->get_net_data()->m_gamer_handle.m_rockstar_id, player->get_name()); + auto player_ptr = add_player(player->get_rockstar_id(), player->get_name()); save(); return player_ptr; } diff --git a/src/services/players/player.cpp b/src/services/players/player.cpp index 6a055b4f..ea12fa5f 100644 --- a/src/services/players/player.cpp +++ b/src/services/players/player.cpp @@ -38,6 +38,13 @@ namespace big return (m_net_game_player == nullptr || m_net_game_player->m_player_info == nullptr) ? nullptr : m_net_game_player; } + int64_t player::get_rockstar_id() const + { + if (auto net_data = get_net_data()) + return net_data->m_gamer_handle.m_rockstar_id; + return 0; + } + CPed* player::get_ped() const { if (const auto player_info = this->get_player_info(); player_info != nullptr) @@ -73,8 +80,7 @@ namespace big { for (uint32_t i = 0; i < gta_util::get_network()->m_game_session_ptr->m_peer_count; i++) { - if (gta_util::get_network()->m_game_session_ptr->m_peers[i]->m_peer_data.m_gamer_handle.m_rockstar_id - == get_net_data()->m_gamer_handle.m_rockstar_id) + if (gta_util::get_network()->m_game_session_ptr->m_peers[i]->m_peer_data.m_gamer_handle.m_rockstar_id == get_rockstar_id()) { return gta_util::get_network()->m_game_session_ptr->m_peers[i]; } diff --git a/src/services/players/player.hpp b/src/services/players/player.hpp index cb692631..3d7d3275 100644 --- a/src/services/players/player.hpp +++ b/src/services/players/player.hpp @@ -37,6 +37,7 @@ namespace big [[nodiscard]] CVehicle* get_current_vehicle() const; [[nodiscard]] const char* get_name() const; [[nodiscard]] rage::rlGamerInfo* get_net_data() const; + [[nodiscard]] int64_t get_rockstar_id() const; [[nodiscard]] CNetGamePlayer* get_net_game_player() const; [[nodiscard]] CPed* get_ped() const; [[nodiscard]] CPlayerInfo* get_player_info() const; diff --git a/src/util/chat.hpp b/src/util/chat.hpp index 46d3b388..7d7eeeb0 100644 --- a/src/util/chat.hpp +++ b/src/util/chat.hpp @@ -155,8 +155,8 @@ namespace big::chat g_file_manager.get_project_file(spam_reason != SpamReason::NOT_A_SPAMMER ? "./spam.log" : "./chat.log").get_path(), std::ios::app); - auto& data = *player->get_net_data(); - auto ip = player->get_ip_address(); + auto rockstar_id = player->get_rockstar_id(); + auto ip = player->get_ip_address(); auto now = std::chrono::system_clock::now(); auto ms = std::chrono::duration_cast(now.time_since_epoch()) % 1000; @@ -174,10 +174,10 @@ namespace big::chat log << spam_reason_str << "[" << std::put_time(&local_time, "%m/%d/%Y %I:%M:%S") << ":" << std::setfill('0') << std::setw(3) << ms.count() << " " << std::put_time(&local_time, "%p") << "] "; if (ip) - log << player->get_name() << " (" << data.m_gamer_handle.m_rockstar_id << ") <" << (int)ip.value().m_field1 << "." + log << player->get_name() << " (" << rockstar_id << ") <" << (int)ip.value().m_field1 << "." << (int)ip.value().m_field2 << "." << (int)ip.value().m_field3 << "." << (int)ip.value().m_field4 << "> " << ((is_team == true) ? "[TEAM]: " : "[ALL]: ") << msg << std::endl; else - log << player->get_name() << " (" << data.m_gamer_handle.m_rockstar_id << ") " << ((is_team == true) ? "[TEAM]: " : "[ALL]: ") << msg << std::endl; + log << player->get_name() << " (" << rockstar_id << ") " << ((is_team == true) ? "[TEAM]: " : "[ALL]: ") << msg << std::endl; log.close(); } diff --git a/src/views/players/view_players.cpp b/src/views/players/view_players.cpp index 25813c9f..1870000b 100644 --- a/src/views/players/view_players.cpp +++ b/src/views/players/view_players.cpp @@ -16,7 +16,7 @@ namespace big bool has_scrollbar = false; static void player_button(const player_ptr& plyr) { - if (plyr == nullptr) + if (plyr == nullptr || !plyr->is_valid()) return; bool selected_player = plyr == g_player_service->get_selected(); @@ -63,12 +63,9 @@ namespace big g_gui_service->set_selected(tabs::PLAYER); g.window.switched_view = true; } - if (ImGui::IsItemHovered() - && g_player_database_service->get_player_by_rockstar_id(plyr->get_net_data()->m_gamer_handle.m_rockstar_id) != nullptr) + if (auto rockstar_id = plyr->get_rockstar_id(); ImGui::IsItemHovered() && g_player_database_service->get_player_by_rockstar_id(rockstar_id) != nullptr) { - auto sorted_player = - g_player_database_service->get_player_by_rockstar_id(plyr->get_net_data()->m_gamer_handle.m_rockstar_id); - + auto sorted_player = g_player_database_service->get_player_by_rockstar_id(rockstar_id); if (!sorted_player->infractions.empty()) { ImGui::BeginTooltip();