[Clantags] Optimize code and get rid of memory leaks

This commit is contained in:
momo5502 2017-05-31 09:58:36 +02:00
parent e99894f692
commit 5c91a8c451
2 changed files with 42 additions and 54 deletions

View File

@ -2,98 +2,81 @@
namespace Components namespace Components
{ {
std::vector < std::string > Clantags::Tags; std::string Clantags::Tags[18];
const char* CurrentName;
void Clantags::ParseClantags(const char* infoString) void Clantags::ParseClantags(const char* infoString)
{ {
const char* clantag;
for (int i = 0; i < 18; i++) for (int i = 0; i < 18; i++)
{ {
clantag = Game::Info_ValueForKey(infoString, std::to_string(i).c_str()); const char* clantag = Game::Info_ValueForKey(infoString, std::to_string(i).data());
if (clantag != nullptr) if (clantag) Tags[i] = clantag;
{ else Tags[i].clear();
Tags[i] = _strdup(clantag);
}
else
{
Tags[i] = "";
}
} }
} }
void Clantags::SendClantagsToClients() void Clantags::SendClantagsToClients()
{ {
const char* list = ""; std::string list;
for (int i = 0; i < 18; i++) for (int i = 0; i < 18; i++)
{ {
char clantag[5]; char clantag[5] = { 0 };
if (Game::svs_clients[i].state >= 3) if (Game::svs_clients[i].state >= 3)
{ {
strncpy_s(clantag, Game::Info_ValueForKey(Game::svs_clients[i].connectInfoString, "iw4x_clantag"), 4); strncpy_s(clantag, Game::Info_ValueForKey(Game::svs_clients[i].connectInfoString, "iw4x_clantag"), 4);
} }
else
list.append(Utils::String::VA("\\%s\\%s", std::to_string(i).data(), clantag));
}
std::string command = Utils::String::VA("%c clantags \"%s\"", 22, list.data());
Game::SV_GameSendServerCommand(-1, 0, command.data());
}
const char* Clantags::GetUserClantag(std::uint32_t clientnum, const char* playername)
{ {
memset(clantag, 0, 5); if (Tags[clientnum].empty()) return playername;
} return Utils::String::VA("[%s] %s", Tags[clientnum].data(), playername);
list = Utils::String::VA("%s\\%s\\%s", list, std::to_string(i).c_str(), clantag);
}
list = Utils::String::VA("%c clantags \"%s\"", 22, list);
Game::SV_GameSendServerCommand(-1, 0, list);
}
void Clantags::GetUserClantag(std::uint32_t clientnum, const char* playername)
{
if (Tags[clientnum].empty())
{
CurrentName = playername;
return;
}
CurrentName = Utils::String::VA("[%s] %s", Tags[clientnum].data(), playername);
} }
void __declspec(naked) Clantags::DrawPlayerNameOnScoreboard() void __declspec(naked) Clantags::DrawPlayerNameOnScoreboard()
{ {
static DWORD drawstringfunc = 0x5909E0;
__asm __asm
{ {
pushad; push eax
pushad
push edi; push edi
push [ebp]; push [ebp]
call GetUserClantag; call Clantags::GetUserClantag
add esp, 8; add esp, 8
popad; mov [esp + 20h], eax
mov edi, CurrentName; popad
pop edi
call drawstringfunc; push 591247h // Return address
push 5909E0h // Draw string func
push 591247h; retn
retn;
} }
} }
Clantags::Clantags() Clantags::Clantags()
{ {
// Create clantag dvar // Create clantag dvar
Dvar::OnInit([]() { Dvar::OnInit([]()
CardTitles::CustomTitleDvar = Game::Dvar_RegisterString("iw4x_clantag", "", Game::dvar_flag::DVAR_FLAG_USERINFO | Game::dvar_flag::DVAR_FLAG_SAVED, "If set, your clantag will be shown on the scoreboard."); {
Dvar::Register<const char*>("iw4x_clantag", "", Game::dvar_flag::DVAR_FLAG_USERINFO | Game::dvar_flag::DVAR_FLAG_SAVED, "If set, your clantag will be shown on the scoreboard.");
}); });
// Servercommand hook // Servercommand hook
ServerCommands::OnCommand(22, [](Command::Params* params) { ServerCommands::OnCommand(22, [](Command::Params* params)
{
if (params->get(1) == "clantags"s && !Flags::HasFlag("dedicated")) if (params->get(1) == "clantags"s && !Dedicated::IsEnabled())
{ {
if (params->length() == 3) if (params->length() == 3)
{ {
@ -103,18 +86,22 @@ namespace Components
} }
return false; return false;
}); });
// Resize clantags array for (int i = 0; i < ARRAYSIZE(Clantags::Tags); ++i)
Tags.resize(18); {
Clantags::Tags[i].clear();
}
// Draw clantag before playername // Draw clantag before playername
Utils::Hook(0x591242, DrawPlayerNameOnScoreboard).install()->quick(); Utils::Hook(0x591242, Clantags::DrawPlayerNameOnScoreboard).install()->quick();
} }
Clantags::~Clantags() Clantags::~Clantags()
{ {
Tags.clear(); for (int i = 0; i < ARRAYSIZE(Clantags::Tags); ++i)
{
Clantags::Tags[i].clear();
}
} }
} }

View File

@ -5,7 +5,6 @@ namespace Components
class Clantags : public Component class Clantags : public Component
{ {
public: public:
static std::vector < std::string > Clantags::Tags;
static void ParseClantags(const char * infoString); static void ParseClantags(const char * infoString);
static void SendClantagsToClients(); static void SendClantagsToClients();
@ -13,7 +12,9 @@ namespace Components
~Clantags(); ~Clantags();
private: private:
static void GetUserClantag(std::uint32_t clientnum, const char * playername); static std::string Clantags::Tags[18];
static const char* GetUserClantag(std::uint32_t clientnum, const char * playername);
static void DrawPlayerNameOnScoreboard(); static void DrawPlayerNameOnScoreboard();
}; };