From 22c633f4be7f1cdb24860088a97d34069a52512a Mon Sep 17 00:00:00 2001 From: Diavolo Date: Tue, 16 Aug 2022 11:10:01 +0200 Subject: [PATCH] [Chat] Improve code --- src/Components/Modules/Chat.cpp | 71 +++++++++++++++++---------------- src/Components/Modules/Chat.hpp | 7 ++-- 2 files changed, 40 insertions(+), 38 deletions(-) diff --git a/src/Components/Modules/Chat.cpp b/src/Components/Modules/Chat.cpp index 163dbafa..7c67009d 100644 --- a/src/Components/Modules/Chat.cpp +++ b/src/Components/Modules/Chat.cpp @@ -11,8 +11,7 @@ namespace Components bool Chat::SendChat; - std::mutex Chat::AccessMutex; - std::unordered_set Chat::MuteList; + Utils::Concurrency::Container Chat::MutedList; bool Chat::CanAddCallback = true; std::vector Chat::SayCallbacks; @@ -36,21 +35,13 @@ namespace Components ++text; } - std::unique_lock lock(AccessMutex); - if (MuteList.contains(Game::svs_clients[player->s.number].steamID)) + if (IsMuted(player)) { - lock.unlock(); SendChat = false; Game::SV_GameSendServerCommand(player->s.number, Game::SV_CMD_CAN_IGNORE, Utils::String::VA("%c \"You are muted\"", 0x65)); } - // Test whether the lock is still locked - if (lock.owns_lock()) - { - lock.unlock(); - } - for (const auto& callback : SayCallbacks) { if (!ChatCallback(player, callback.getPos(), (text + 1), mode)) @@ -243,25 +234,34 @@ namespace Components } } + bool Chat::IsMuted(const Game::gentity_s* ent) + { + auto result = false; + const auto clientNum = ent->s.number; + const auto xuid = Game::svs_clients[clientNum].steamID; + + MutedList.access([&](muteList& clients) + { + if (clients.contains(xuid)) + { + result = true; + } + }); + + return result; + } + void Chat::MuteClient(const Game::client_t* client) { - std::unique_lock lock(AccessMutex); - - if (!MuteList.contains(client->steamID)) + const auto xuid = client->steamID; + MutedList.access([&](muteList& clients) { - MuteList.insert(client->steamID); - lock.unlock(); + clients.insert(xuid); + }); - Logger::Print("{} was muted\n", client->name); - Game::SV_GameSendServerCommand(client->gentity->s.number, Game::SV_CMD_CAN_IGNORE, - Utils::String::VA("%c \"You were muted\"", 0x65)); - return; - } - - lock.unlock(); - Logger::Print("{} is already muted\n", client->name); - Game::SV_GameSendServerCommand(-1, Game::SV_CMD_CAN_IGNORE, - Utils::String::VA("%c \"%s is already muted\"", 0x65, client->name)); + Logger::Print("{} was muted\n", client->name); + Game::SV_GameSendServerCommand(client - Game::svs_clients, Game::SV_CMD_CAN_IGNORE, + Utils::String::VA("%c \"You were muted\"", 0x65)); } void Chat::UnmuteClient(const Game::client_t* client) @@ -269,18 +269,19 @@ namespace Components UnmuteInternal(client->steamID); Logger::Print("{} was unmuted\n", client->name); - Game::SV_GameSendServerCommand(client->gentity->s.number, Game::SV_CMD_CAN_IGNORE, + Game::SV_GameSendServerCommand(client - Game::svs_clients, Game::SV_CMD_CAN_IGNORE, Utils::String::VA("%c \"You were unmuted\"", 0x65)); } void Chat::UnmuteInternal(const std::uint64_t id, bool everyone) { - std::unique_lock lock(AccessMutex); - - if (everyone) - MuteList.clear(); - else - MuteList.erase(id); + MutedList.access([&](muteList& clients) + { + if (everyone) + clients.clear(); + else + clients.erase(id); + }); } void Chat::AddChatCommands() @@ -360,7 +361,7 @@ namespace Components const auto* result = &Game::scrVmPub->top[1 - Game::scrVmPub->outparamcount]; - if (result->type != Game::scrParamType_t::VAR_INTEGER) + if (result->type != Game::VAR_INTEGER) { // Garbage was returned return 1; @@ -378,7 +379,7 @@ namespace Components Game::Scr_AddString(message); Game::VariableValue value; - value.type = Game::scrParamType_t::VAR_OBJECT; + value.type = Game::VAR_OBJECT; value.u.uintValue = entityId; Game::AddRefToValue(value.type, value.u); diff --git a/src/Components/Modules/Chat.hpp b/src/Components/Modules/Chat.hpp index a9bd64d1..dc6b38fa 100644 --- a/src/Components/Modules/Chat.hpp +++ b/src/Components/Modules/Chat.hpp @@ -18,8 +18,8 @@ namespace Components static bool SendChat; - static std::mutex AccessMutex; - static std::unordered_set MuteList; + using muteList = std::unordered_set; + static Utils::Concurrency::Container MutedList; static bool CanAddCallback; // ClientCommand & GSC thread are the same static std::vector SayCallbacks; @@ -33,9 +33,10 @@ namespace Components static void CG_AddToTeamChat(const char* text); static void CG_AddToTeamChat_Stub(); + static bool IsMuted(const Game::gentity_s* ent); static void MuteClient(const Game::client_t* client); static void UnmuteClient(const Game::client_t* client); - static void UnmuteInternal(const std::uint64_t id, bool everyone = false); + static void UnmuteInternal(std::uint64_t id, bool everyone = false); static void AddChatCommands(); static int GetCallbackReturn();