[CardTites] Optimize code

This commit is contained in:
momo5502 2017-05-31 09:59:03 +02:00
parent 5c91a8c451
commit 3e1b5df639
2 changed files with 47 additions and 44 deletions

View File

@ -2,8 +2,8 @@
namespace Components namespace Components
{ {
std::vector < std::string > CardTitles::CustomTitles; std::string CardTitles::CustomTitles[18];
Game::dvar_t* CardTitles::CustomTitleDvar; Dvar::Var CardTitles::CustomTitleDvar;
CClient* CardTitles::GetClientByIndex(std::uint32_t index) CClient* CardTitles::GetClientByIndex(std::uint32_t index)
{ {
@ -14,7 +14,6 @@ namespace Components
{ {
std::int32_t returnResult = lookupResult; std::int32_t returnResult = lookupResult;
CClient* c;
std::string username = Dvar::Var("name").get<std::string>(); std::string username = Dvar::Var("name").get<std::string>();
if (data->name == username) if (data->name == username)
@ -25,7 +24,7 @@ namespace Components
{ {
for (auto clientNum = 0; clientNum < 18; clientNum++) for (auto clientNum = 0; clientNum < 18; clientNum++)
{ {
c = GetClientByIndex(clientNum); CClient* c = GetClientByIndex(clientNum);
if (c != nullptr) if (c != nullptr)
{ {
@ -42,22 +41,30 @@ namespace Components
return returnResult; return returnResult;
} }
void __declspec(naked) CardTitles::GetPlayerCardClientInfoStub() void __declspec(naked) CardTitles::GetPlayerCardClientInfoStub()
{ {
__asm __asm
{ {
push esi; push eax
push eax; pushad
call GetPlayerCardClientInfo;
add esp, 8;
pop esi; push esi
pop ebp; push eax
mov[ebx + 4], eax; call GetPlayerCardClientInfo
pop ebx; add esp, 8
push 62EB2Ch; mov [esp + 20h], eax
retn; popad
pop eax
pop esi
pop ebp
mov[ebx + 4], eax
pop ebx
push 62EB2Ch
retn
} }
} }
@ -65,7 +72,6 @@ namespace Components
{ {
std::uint8_t prefix = (request->tableRow >> (8 * 3)) & 0xFF; std::uint8_t prefix = (request->tableRow >> (8 * 3)) & 0xFF;
std::uint8_t data = (request->tableRow >> (8 * 2)) & 0xFF; std::uint8_t data = (request->tableRow >> (8 * 2)) & 0xFF;
const char* title = nullptr;
if (request->tablename == "mp/cardTitleTable.csv"s) if (request->tablename == "mp/cardTitleTable.csv"s)
{ {
@ -77,9 +83,7 @@ namespace Components
if (prefix == 0xFE) if (prefix == 0xFE)
{ {
// 0xFF in front of the title to skip localization. Or else it will wait for a couple of seconds for the asset of type localize // 0xFF in front of the title to skip localization. Or else it will wait for a couple of seconds for the asset of type localize
if (*CustomTitleDvar->current.string) const char* title = Utils::String::VA("\x15%s", CustomTitleDvar.get<const char*>());
{
title = Utils::String::VA("\x15%s", CustomTitleDvar->current.string);
// prepare return value // prepare return value
operand->internals.stringVal.string = title; operand->internals.stringVal.string = title;
@ -87,12 +91,11 @@ namespace Components
return title; return title;
} }
}
else if (prefix == 0xFF) else if (prefix == 0xFF)
{ {
if (!CustomTitles[data].empty()) if (!CustomTitles[data].empty())
{ {
title = Utils::String::VA("\x15%s", CustomTitles[data].data()); const char* title = Utils::String::VA("\x15%s", CustomTitles[data].data());
// prepare return value // prepare return value
operand->internals.stringVal.string = title; operand->internals.stringVal.string = title;
@ -170,31 +173,25 @@ namespace Components
void CardTitles::ParseCustomTitles(const char* msg) void CardTitles::ParseCustomTitles(const char* msg)
{ {
const char* playerTitle; for (int i = 0; i < 18; ++i)
for (int i = 0; i < 18; i++)
{ {
playerTitle = Game::Info_ValueForKey(msg, std::to_string(i).c_str()); const char* playerTitle = Game::Info_ValueForKey(msg, std::to_string(i).c_str());
if (playerTitle != nullptr) if (playerTitle) CustomTitles[i] = playerTitle;
{ else CustomTitles[i].clear();
CustomTitles[i] = playerTitle;
}
else
{
CustomTitles[i] = "";
}
} }
} }
CardTitles::CardTitles() CardTitles::CardTitles()
{ {
Dvar::OnInit([]() { Dvar::OnInit([]()
CardTitles::CustomTitleDvar = Game::Dvar_RegisterString("customtitle", "", Game::dvar_flag::DVAR_FLAG_USERINFO | Game::dvar_flag::DVAR_FLAG_SAVED, "Custom card title"); {
CardTitles::CustomTitleDvar = Dvar::Register<const char*>("customtitle", "", Game::dvar_flag::DVAR_FLAG_USERINFO | Game::dvar_flag::DVAR_FLAG_SAVED, "Custom card title");
}); });
ServerCommands::OnCommand(21, [](Command::Params* params) { ServerCommands::OnCommand(21, [](Command::Params* params)
{
if (params->get(1) == "customTitles"s && !Flags::HasFlag("dedicated")) if (params->get(1) == "customTitles"s && !Dedicated::IsEnabled())
{ {
if (params->length() == 3) if (params->length() == 3)
{ {
@ -207,7 +204,10 @@ namespace Components
}); });
CardTitles::CustomTitles.resize(18); for(int i = 0; i < ARRAYSIZE(CardTitles::CustomTitles); ++i)
{
CardTitles::CustomTitles[i].clear();
}
Utils::Hook(0x62EB26, GetPlayerCardClientInfoStub).install()->quick(); Utils::Hook(0x62EB26, GetPlayerCardClientInfoStub).install()->quick();
@ -218,6 +218,9 @@ namespace Components
CardTitles::~CardTitles() CardTitles::~CardTitles()
{ {
CustomTitles.clear(); for (int i = 0; i < ARRAYSIZE(CardTitles::CustomTitles); ++i)
{
CardTitles::CustomTitles[i].clear();
}
} }
} }

View File

@ -53,8 +53,8 @@ namespace Components
class CardTitles : public Component class CardTitles : public Component
{ {
public: public:
static Game::dvar_t* CustomTitleDvar; static Dvar::Var CustomTitleDvar;
static std::vector < std::string > CustomTitles; static std::string CustomTitles[18];
static void SendCustomTitlesToClients(); static void SendCustomTitlesToClients();
static void ParseCustomTitles(const char * msg); static void ParseCustomTitles(const char * msg);