Protect list with mutex

This commit is contained in:
FutureRave 2022-01-10 18:42:58 +00:00
parent 7add84442f
commit 2adad67f4f
No known key found for this signature in database
GPG Key ID: E883E2BC9657D955
4 changed files with 25 additions and 8 deletions

View File

@ -71,7 +71,7 @@ namespace Components
{ "9", 33554432 }, { "9", 33554432 },
}; };
unsigned int Bots::GetClientNum(const Game::client_s* cl) unsigned int Bots::GetClientNum(Game::client_s* cl)
{ {
unsigned int num; unsigned int num;

View File

@ -7,7 +7,7 @@ namespace Components
public: public:
Bots(); Bots();
~Bots(); ~Bots();
static unsigned int GetClientNum(const Game::client_s*); static unsigned int GetClientNum(Game::client_s*);
static bool IsValidClientNum(unsigned int); static bool IsValidClientNum(unsigned int);
private: private:

View File

@ -8,10 +8,13 @@ namespace Components
bool Chat::SendChat; bool Chat::SendChat;
std::mutex Chat::AccessMutex;
std::unordered_set<std::uint64_t> Chat::MuteList; std::unordered_set<std::uint64_t> Chat::MuteList;
const char* Chat::EvaluateSay(char* text, Game::gentity_t* player) const char* Chat::EvaluateSay(char* text, Game::gentity_t* player)
{ {
std::lock_guard<std::mutex> _(Chat::AccessMutex);
Chat::SendChat = true; Chat::SendChat = true;
if (text[1] == '/') if (text[1] == '/')
@ -205,12 +208,14 @@ namespace Components
void Chat::MuteClient(const Game::client_t* client) void Chat::MuteClient(const Game::client_t* client)
{ {
std::lock_guard<std::mutex> _(Chat::AccessMutex);
if (Chat::MuteList.find(client->steamID) == Chat::MuteList.end()) if (Chat::MuteList.find(client->steamID) == Chat::MuteList.end())
{ {
Chat::MuteList.insert(client->steamID); Chat::MuteList.insert(client->steamID);
Logger::Print("%s was muted\n", client->name); Logger::Print("%s was muted\n", client->name);
Game::SV_GameSendServerCommand(Bots::GetClientNum(client), 0, Game::SV_GameSendServerCommand(client->gentity->s.number, 0,
Utils::String::VA("%c \"You were muted\"", 0x65)); Utils::String::VA("%c \"You were muted\"", 0x65));
return; return;
} }
@ -222,13 +227,23 @@ namespace Components
void Chat::UnmuteClient(const Game::client_t* client) void Chat::UnmuteClient(const Game::client_t* client)
{ {
Chat::MuteList.erase(client->steamID); Chat::UnmuteInternal(client->steamID);
Logger::Print("%s was unmuted\n", client->name); Logger::Print("%s was unmuted\n", client->name);
Game::SV_GameSendServerCommand(Bots::GetClientNum(client), 0, Game::SV_GameSendServerCommand(client->gentity->s.number, 0,
Utils::String::VA("%c \"You were unmuted\"", 0x65)); Utils::String::VA("%c \"You were unmuted\"", 0x65));
} }
void Chat::UnmuteInternal(const std::uint64_t id, bool everyone)
{
std::lock_guard<std::mutex> _(Chat::AccessMutex);
if (everyone)
Chat::MuteList.clear();
else
Chat::MuteList.erase(id);
}
void Chat::AddChatCommands() void Chat::AddChatCommands()
{ {
Command::AddSV("muteClient", [](Command::Params* params) Command::AddSV("muteClient", [](Command::Params* params)
@ -279,12 +294,12 @@ namespace Components
if (params->get(1) == "all"s) if (params->get(1) == "all"s)
{ {
Logger::Print("All players were unmuted\n"); Logger::Print("All players were unmuted\n");
Chat::MuteList.clear(); Chat::UnmuteInternal(0, true);
} }
else else
{ {
const auto SteamID = std::strtoull(params->get(1), nullptr, 16); const auto steamId = std::strtoull(params->get(1), nullptr, 16);
Chat::MuteList.erase(SteamID); Chat::UnmuteInternal(steamId);
} }
}); });
} }

View File

@ -16,6 +16,7 @@ namespace Components
static bool SendChat; static bool SendChat;
static std::mutex AccessMutex;
static std::unordered_set<std::uint64_t> MuteList; static std::unordered_set<std::uint64_t> MuteList;
static const char* EvaluateSay(char* text, Game::gentity_t* player); static const char* EvaluateSay(char* text, Game::gentity_t* player);
@ -29,6 +30,7 @@ namespace Components
static void MuteClient(const Game::client_t* client); static void MuteClient(const Game::client_t* client);
static void UnmuteClient(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 AddChatCommands(); static void AddChatCommands();
}; };
} }