[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;
std::mutex Chat::AccessMutex;
std::unordered_set<std::uint64_t> Chat::MuteList;
Utils::Concurrency::Container<Chat::muteList> Chat::MutedList;
bool Chat::CanAddCallback = true;
std::vector<Scripting::Function> 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
}
}
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);
lock.unlock();
if (clients.contains(xuid))
{
result = true;
}
});
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;
return result;
}
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));
void Chat::MuteClient(const Game::client_t* client)
{
const auto xuid = client->steamID;
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)
@ -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);
MutedList.access([&](muteList& clients)
{
if (everyone)
MuteList.clear();
clients.clear();
else
MuteList.erase(id);
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);

View File

@ -18,8 +18,8 @@ namespace Components
static bool SendChat;
static std::mutex AccessMutex;
static std::unordered_set<std::uint64_t> MuteList;
using muteList = std::unordered_set<std::uint64_t>;
static Utils::Concurrency::Container<muteList> MutedList;
static bool CanAddCallback; // ClientCommand & GSC thread are the same
static std::vector<Scripting::Function> 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();