From 7e7bcb155a5594a6e5955894cc2b8f05d063b31c Mon Sep 17 00:00:00 2001 From: maybegreat48 <96936658+maybegreat48@users.noreply.github.com> Date: Sat, 23 Mar 2024 16:37:51 +0000 Subject: [PATCH] Refactor and fix send chat (#2864) * feat(chat): refactor and fix send chat * fix(chat): fixes * fix(chat): fix team chat Co-authored-by: DayibBaba <79384354+DayibBaba@users.noreply.github.com> --- src/backend/context/chat_command_context.cpp | 15 +- src/backend/reactions/interloper_reaction.cpp | 13 +- src/backend/reactions/reaction.cpp | 14 +- src/hooks/misc/send_chat_message.cpp | 15 +- src/hooks/protections/receive_net_message.cpp | 10 +- src/lua/bindings/network.cpp | 25 +- src/util/chat.hpp | 240 ++++++++++++++++++ src/util/notify.cpp | 86 +++++++ src/util/notify.hpp | 110 +------- src/util/spam.hpp | 128 ---------- src/views/network/view_network.cpp | 30 +-- 11 files changed, 382 insertions(+), 304 deletions(-) create mode 100644 src/util/chat.hpp create mode 100644 src/util/notify.cpp delete mode 100644 src/util/spam.hpp diff --git a/src/backend/context/chat_command_context.cpp b/src/backend/context/chat_command_context.cpp index 75c061b6..5e7f88c9 100644 --- a/src/backend/context/chat_command_context.cpp +++ b/src/backend/context/chat_command_context.cpp @@ -2,7 +2,7 @@ #include "fiber_pool.hpp" #include "hooking/hooking.hpp" -#include "util/notify.hpp" +#include "util/chat.hpp" namespace big { @@ -26,18 +26,7 @@ namespace big void chat_command_context::report_output(const std::string& output) const { - g_fiber_pool->queue_job([this, output] { - char msg[265]{}; - msg[0] = g.session.chat_output_prefix; - msg[1] = ' '; - strncpy(msg + 2, output.c_str(), sizeof(msg) - 2); - - if (g_hooking->get_original()(*g_pointers->m_gta.m_send_chat_ptr, - g_player_service->get_self()->get_net_data(), - msg, - false)) - notify::draw_chat(msg, g_player_service->get_self()->get_name(), false); - }); + chat::send_message(output); } void chat_command_context::report_error(const std::string& error) const diff --git a/src/backend/reactions/interloper_reaction.cpp b/src/backend/reactions/interloper_reaction.cpp index 21d057e6..bb79a30e 100644 --- a/src/backend/reactions/interloper_reaction.cpp +++ b/src/backend/reactions/interloper_reaction.cpp @@ -5,7 +5,7 @@ #include "hooking/hooking.hpp" #include "pointers.hpp" #include "script.hpp" -#include "util/notify.hpp" +#include "util/chat.hpp" namespace big { @@ -36,15 +36,8 @@ namespace big if (announce_in_chat) { - g_fiber_pool->queue_job([attacker, victim, this] { - auto chat = std::format("{} {}", g.session.chat_output_prefix, g_translation_service.get_translation(m_announce_message)); - - if (g_hooking->get_original()(*g_pointers->m_gta.m_send_chat_ptr, - g_player_service->get_self()->get_net_data(), - chat.data(), - is_team_only)) - notify::draw_chat(chat.c_str(), g_player_service->get_self()->get_name(), is_team_only); - }); + auto msg = std::format("{} {}", g.session.chat_output_prefix, g_translation_service.get_translation(m_announce_message)); + chat::send_message(msg); } if (notify) diff --git a/src/backend/reactions/reaction.cpp b/src/backend/reactions/reaction.cpp index 607d2bf3..08603cdf 100644 --- a/src/backend/reactions/reaction.cpp +++ b/src/backend/reactions/reaction.cpp @@ -6,7 +6,7 @@ #include "pointers.hpp" #include "script.hpp" #include "services/player_database/player_database_service.hpp" -#include "util/notify.hpp" +#include "util/chat.hpp" namespace big { @@ -62,15 +62,11 @@ namespace big if (announce_in_chat) { - g_fiber_pool->queue_job([player, this] { - auto chat = std::format("{} {}", g.session.chat_output_prefix, std::vformat(g_translation_service.get_translation(m_announce_message), std::make_format_args(player->get_name()))); + auto msg = std::format("{} {}", + g.session.chat_output_prefix, + std::vformat(g_translation_service.get_translation(m_announce_message), std::make_format_args(player->get_name()))); - if (g_hooking->get_original()(*g_pointers->m_gta.m_send_chat_ptr, - g_player_service->get_self()->get_net_data(), - chat.data(), - is_team_only)) - notify::draw_chat(chat.c_str(), g_player_service->get_self()->get_name(), is_team_only); - }); + chat::send_message(msg); } if (notify) diff --git a/src/hooks/misc/send_chat_message.cpp b/src/hooks/misc/send_chat_message.cpp index e184089d..b80ef10f 100644 --- a/src/hooks/misc/send_chat_message.cpp +++ b/src/hooks/misc/send_chat_message.cpp @@ -5,7 +5,7 @@ #include "hooking/hooking.hpp" #include "packet.hpp" #include "services/players/player_service.hpp" -#include "util/spam.hpp" +#include "util/chat.hpp" namespace big { @@ -22,19 +22,10 @@ namespace big if (g.session.chat_commands && message[0] == g.session.chat_command_prefix) command::process(std::string(message + 1), std::make_shared(g_player_service->get_self())); - packet msg{}; - msg.write_message(rage::eNetMessage::MsgTextMessage); - msg.m_buffer.WriteString(message ? message : "", 256); - gamer_handle_serialize(g_player_service->get_self()->get_net_data()->m_gamer_handle, msg.m_buffer); - msg.write(is_team, 1); + chat::send_message(message, nullptr, false, is_team); if (g.session.log_chat_messages) - spam::log_chat(message, g_player_service->get_self(), SpamReason::NOT_A_SPAMMER, is_team); - - if (*g_pointers->m_gta.m_is_session_started) - for (auto& player : g_player_service->players()) - if (player.second && player.second->is_valid()) - msg.send(player.second->get_net_game_player()->m_msg_id); + chat::log_chat(message, g_player_service->get_self(), SpamReason::NOT_A_SPAMMER, is_team); return true; } diff --git a/src/hooks/protections/receive_net_message.cpp b/src/hooks/protections/receive_net_message.cpp index 2b1f4b25..28507146 100644 --- a/src/hooks/protections/receive_net_message.cpp +++ b/src/hooks/protections/receive_net_message.cpp @@ -10,7 +10,7 @@ #include "script/scriptIdBase.hpp" #include "services/players/player_service.hpp" #include "util/session.hpp" -#include "util/spam.hpp" +#include "util/chat.hpp" #include "gta/enums.hpp" #include @@ -114,11 +114,12 @@ namespace big if (player->is_spammer) return true; - if (auto spam_reason = spam::is_text_spam(message, player)) + if (auto spam_reason = chat::is_text_spam(message, player)) { if (g.session.log_chat_messages) - spam::log_chat(message, player, spam_reason, is_team); + chat::log_chat(message, player, spam_reason, is_team); g_notification_service.push("PROTECTIONS"_T.data(), + std::format("{} {}", player->get_name(), "IS_A_SPAMMER"_T.data())); player->is_spammer = true; if (g.session.kick_chat_spammers @@ -136,7 +137,7 @@ namespace big else { if (g.session.log_chat_messages) - spam::log_chat(message, player, SpamReason::NOT_A_SPAMMER, is_team); + chat::log_chat(message, player, SpamReason::NOT_A_SPAMMER, is_team); if (g.session.chat_commands && message[0] == g.session.chat_command_prefix) command::process(std::string(message + 1), std::make_shared(player)); @@ -147,7 +148,6 @@ namespace big { rage::rlGamerHandle temp{}; gamer_handle_deserialize(temp, buffer); - bool is_team = buffer.Read(1); g_pointers->m_gta.m_handle_chat_message(*g_pointers->m_gta.m_chat_data, nullptr, diff --git a/src/lua/bindings/network.cpp b/src/lua/bindings/network.cpp index 880daf00..3d784f99 100644 --- a/src/lua/bindings/network.cpp +++ b/src/lua/bindings/network.cpp @@ -5,6 +5,7 @@ #include "pointers.hpp" #include "services/player_database/player_database_service.hpp" #include "util/notify.hpp" +#include "util/chat.hpp" #include "util/scripts.hpp" #include "util/session.hpp" #include "util/system.hpp" @@ -183,13 +184,22 @@ namespace lua::network // Sends a message to the in game chat. static void send_chat_message(const std::string& msg, bool team_only) { - big::g_fiber_pool->queue_job([msg, team_only] { - if (big::g_hooking->get_original()(*big::g_pointers->m_gta.m_send_chat_ptr, - big::g_player_service->get_self()->get_net_data(), - (char*)msg.c_str(), - team_only)) - big::notify::draw_chat((char*)msg.data(), big::g_player_service->get_self()->get_name(), team_only); - }); + big::chat::send_message(msg, nullptr, true, team_only); + } + + // Lua API: Function + // Table: network + // Name: send_chat_message_to_player + // Param: player_idx: integer: Index of the player. + // Param: msg: string: Message to be sent. + // Sends a chat message to the specified player. Other players would not be able to see the message + static void send_chat_message_to_player(int player_idx, const std::string& msg) + { + if (auto player = big::g_player_service->get_by_id(player_idx)) + { + big::chat::send_message(msg, player); + + } } void bind(sol::state& state) @@ -229,5 +239,6 @@ namespace lua::network ns["get_flagged_modder_reason"] = get_flagged_modder_reason; ns["force_script_host"] = force_script_host; ns["send_chat_message"] = send_chat_message; + ns["send_chat_message_to_player"] = send_chat_message_to_player; } } \ No newline at end of file diff --git a/src/util/chat.hpp b/src/util/chat.hpp new file mode 100644 index 00000000..db38f0e8 --- /dev/null +++ b/src/util/chat.hpp @@ -0,0 +1,240 @@ +#pragma once +#include "file_manager/file.hpp" +#include "services/players/player_service.hpp" +#include "core/enums.hpp" + +#include "packet.hpp" +#include "natives.hpp" +#include "script.hpp" +#include "fiber_pool.hpp" +#include "core/scr_globals.hpp" + +#include +#include