[Chat] Improve code

This commit is contained in:
Diavolo 2022-08-16 11:10:01 +02:00
parent 7f853c557e
commit 22c633f4be
No known key found for this signature in database
GPG Key ID: FA77F074E98D98A5
2 changed files with 40 additions and 38 deletions

View File

@ -11,8 +11,7 @@ namespace Components
bool Chat::SendChat; bool Chat::SendChat;
std::mutex Chat::AccessMutex; Utils::Concurrency::Container<Chat::muteList> Chat::MutedList;
std::unordered_set<std::uint64_t> Chat::MuteList;
bool Chat::CanAddCallback = true; bool Chat::CanAddCallback = true;
std::vector<Scripting::Function> Chat::SayCallbacks; std::vector<Scripting::Function> Chat::SayCallbacks;
@ -36,21 +35,13 @@ namespace Components
++text; ++text;
} }
std::unique_lock lock(AccessMutex); if (IsMuted(player))
if (MuteList.contains(Game::svs_clients[player->s.number].steamID))
{ {
lock.unlock();
SendChat = false; SendChat = false;
Game::SV_GameSendServerCommand(player->s.number, Game::SV_CMD_CAN_IGNORE, Game::SV_GameSendServerCommand(player->s.number, Game::SV_CMD_CAN_IGNORE,
Utils::String::VA("%c \"You are muted\"", 0x65)); 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) for (const auto& callback : SayCallbacks)
{ {
if (!ChatCallback(player, callback.getPos(), (text + 1), mode)) if (!ChatCallback(player, callback.getPos(), (text + 1), mode))
@ -243,25 +234,34 @@ namespace Components
} }
} }
void Chat::MuteClient(const Game::client_t* client) bool Chat::IsMuted(const Game::gentity_s* ent)
{ {
std::unique_lock lock(AccessMutex); auto result = false;
const auto clientNum = ent->s.number;
const auto xuid = Game::svs_clients[clientNum].steamID;
if (!MuteList.contains(client->steamID)) MutedList.access([&](muteList& clients)
{ {
MuteList.insert(client->steamID); if (clients.contains(xuid))
lock.unlock(); {
result = true;
}
});
Logger::Print("{} was muted\n", client->name); return result;
Game::SV_GameSendServerCommand(client->gentity->s.number, Game::SV_CMD_CAN_IGNORE,
Utils::String::VA("%c \"You were muted\"", 0x65));
return;
} }
lock.unlock(); void Chat::MuteClient(const Game::client_t* client)
Logger::Print("{} is already muted\n", client->name); {
Game::SV_GameSendServerCommand(-1, Game::SV_CMD_CAN_IGNORE, const auto xuid = client->steamID;
Utils::String::VA("%c \"%s is already muted\"", 0x65, client->name)); MutedList.access([&](muteList& clients)
{
clients.insert(xuid);
});
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) void Chat::UnmuteClient(const Game::client_t* client)
@ -269,18 +269,19 @@ namespace Components
UnmuteInternal(client->steamID); UnmuteInternal(client->steamID);
Logger::Print("{} was unmuted\n", client->name); 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)); Utils::String::VA("%c \"You were unmuted\"", 0x65));
} }
void Chat::UnmuteInternal(const std::uint64_t id, bool everyone) void Chat::UnmuteInternal(const std::uint64_t id, bool everyone)
{ {
std::unique_lock lock(AccessMutex); MutedList.access([&](muteList& clients)
{
if (everyone) if (everyone)
MuteList.clear(); clients.clear();
else else
MuteList.erase(id); clients.erase(id);
});
} }
void Chat::AddChatCommands() void Chat::AddChatCommands()
@ -360,7 +361,7 @@ namespace Components
const auto* result = &Game::scrVmPub->top[1 - Game::scrVmPub->outparamcount]; 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 // Garbage was returned
return 1; return 1;
@ -378,7 +379,7 @@ namespace Components
Game::Scr_AddString(message); Game::Scr_AddString(message);
Game::VariableValue value; Game::VariableValue value;
value.type = Game::scrParamType_t::VAR_OBJECT; value.type = Game::VAR_OBJECT;
value.u.uintValue = entityId; value.u.uintValue = entityId;
Game::AddRefToValue(value.type, value.u); Game::AddRefToValue(value.type, value.u);

View File

@ -18,8 +18,8 @@ namespace Components
static bool SendChat; static bool SendChat;
static std::mutex AccessMutex; using muteList = std::unordered_set<std::uint64_t>;
static std::unordered_set<std::uint64_t> MuteList; static Utils::Concurrency::Container<muteList> MutedList;
static bool CanAddCallback; // ClientCommand & GSC thread are the same static bool CanAddCallback; // ClientCommand & GSC thread are the same
static std::vector<Scripting::Function> SayCallbacks; static std::vector<Scripting::Function> SayCallbacks;
@ -33,9 +33,10 @@ namespace Components
static void CG_AddToTeamChat(const char* text); static void CG_AddToTeamChat(const char* text);
static void CG_AddToTeamChat_Stub(); static void CG_AddToTeamChat_Stub();
static bool IsMuted(const Game::gentity_s* ent);
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 UnmuteInternal(std::uint64_t id, bool everyone = false);
static void AddChatCommands(); static void AddChatCommands();
static int GetCallbackReturn(); static int GetCallbackReturn();