Move playername related patches from TextRenderer to PlayerName component

This commit is contained in:
Jan 2021-09-07 15:06:58 +02:00
parent 1821d9072e
commit 26f2403418
5 changed files with 70 additions and 102 deletions

View File

@ -116,7 +116,7 @@ namespace Components
mov [esp + 100h + 10h], eax mov [esp + 100h + 10h], eax
jmp TextRenderer::CleanStrStub jmp PlayerName::CleanStrStub
} }
} }

View File

@ -2,37 +2,77 @@
namespace Components namespace Components
{ {
std::string PlayerName::PlayerNames[18]; Dvar::Var PlayerName::sv_allowColoredNames;
int PlayerName::GetClientName(int /*localClientNum*/, int index, char *buf, int size) void PlayerName::UserInfoCopy(char* buffer, const char* name, const size_t size)
{ {
if (index < 0 || index >= 18) return 0; if (!sv_allowColoredNames.get<bool>())
return strncpy_s(buf, size, PlayerName::PlayerNames[index].data(), PlayerName::PlayerNames[index].size()) == 0; {
char nameBuffer[64] = { 0 };
TextRenderer::StripColors(name, nameBuffer, sizeof(nameBuffer));
TextRenderer::StripAllTextIcons(nameBuffer, buffer, size);
}
else
{
TextRenderer::StripAllTextIcons(name, buffer, size);
}
std::string readablePlayerName(buffer);
readablePlayerName = Utils::String::Trim(readablePlayerName);
if (readablePlayerName.size() < 3)
{
strncpy(buffer, "Unknown Soldier", size);
}
}
__declspec(naked) void PlayerName::ClientUserinfoChanged()
{
__asm
{
mov eax, [esp + 4h] // length
//sub eax, 1
push eax
push ecx // name
push edx // buffer
call UserInfoCopy
add esp, 0Ch
retn
}
}
char* PlayerName::GetClientName(int localClientNum, int index, char* buf, size_t size)
{
Game::CL_GetClientName(localClientNum, index, buf, size);
// Append clantag to username & remove the colors
strncpy_s(buf, size, TextRenderer::StripColors(ClanTags::GetUserClantag(index, buf)).data(), size);
return buf;
}
char* PlayerName::CleanStrStub(char* string)
{
TextRenderer::StripColors(string, string, strlen(string) + 1);
return string;
} }
PlayerName::PlayerName() PlayerName::PlayerName()
{ {
#if(0) // Disabled for now sv_allowColoredNames = Dvar::Register<bool>("sv_allowColoredNames", true, Game::dvar_flag::DVAR_FLAG_NONE, "Allow colored names on the server");
{
for (int i = 0; i < ARRAYSIZE(PlayerName::PlayerNames); ++i)
{
PlayerName::PlayerNames[i] = "mumu";
}
Utils::Hook(Game::CL_GetClientName, PlayerName::GetClientName, HOOK_JUMP).install()->quick(); // Disable SV_UpdateUserinfo_f, to block changing the name ingame
} Utils::Hook::Set<BYTE>(0x6258D0, 0xC3);
#endif
//const char* clanname = "ZOB"; // Allow colored names ingame
//Utils::Hook::Set<const char*>(0x497656, clanname); Utils::Hook(0x5D8B40, ClientUserinfoChanged, HOOK_JUMP).install()->quick();
//Utils::Hook::Set<const char*>(0x497679, clanname);
}
PlayerName::~PlayerName() // Though, don't apply that to overhead names.
{ Utils::Hook(0x581932, GetClientName, HOOK_CALL).install()->quick();
for (int i = 0; i < ARRAYSIZE(PlayerName::PlayerNames); ++i)
{ // Patch I_CleanStr
PlayerName::PlayerNames[i].clear(); Utils::Hook(0x4AD470, CleanStrStub, HOOK_JUMP).install()->quick();
}
} }
} }

View File

@ -6,11 +6,14 @@ namespace Components
{ {
public: public:
PlayerName(); PlayerName();
~PlayerName();
static void UserInfoCopy(char* buffer, const char* name, size_t size);
private: private:
static std::string PlayerNames[18]; static Dvar::Var sv_allowColoredNames;
static int GetClientName(int localClientNum, int index, char *buf, int size); static char* CleanStrStub(char* string);
static void ClientUserinfoChanged();
static char* GetClientName(int localClientNum, int index, char* buf, size_t size);
}; };
} }

View File

@ -38,7 +38,6 @@ namespace Components
Dvar::Var TextRenderer::cg_newColors; Dvar::Var TextRenderer::cg_newColors;
Game::dvar_t* TextRenderer::sv_customTextColor; Game::dvar_t* TextRenderer::sv_customTextColor;
Dvar::Var TextRenderer::sv_allowColoredNames;
Dvar::Var TextRenderer::r_colorBlind; Dvar::Var TextRenderer::r_colorBlind;
Game::dvar_t* TextRenderer::g_ColorBlind_MyTeam; Game::dvar_t* TextRenderer::g_ColorBlind_MyTeam;
Game::dvar_t* TextRenderer::g_ColorBlind_EnemyTeam; Game::dvar_t* TextRenderer::g_ColorBlind_EnemyTeam;
@ -786,56 +785,6 @@ namespace Components
return std::string(buffer); return std::string(buffer);
} }
void TextRenderer::UserInfoCopy(char* buffer, const char* name, const size_t size)
{
if (!sv_allowColoredNames.get<bool>())
{
char nameBuffer[64] = {0};
StripColors(name, nameBuffer, sizeof(nameBuffer));
StripAllTextIcons(nameBuffer, buffer, size);
}
else
{
StripAllTextIcons(name, buffer, size);
}
std::string readablePlayerName(buffer);
readablePlayerName = Utils::String::Trim(readablePlayerName);
if(readablePlayerName.size() < 3)
{
strncpy(buffer, "Unknown Soldier", size);
}
}
__declspec(naked) void TextRenderer::ClientUserinfoChanged()
{
__asm
{
mov eax, [esp + 4h] // length
//sub eax, 1
push eax
push ecx // name
push edx // buffer
call UserInfoCopy
add esp, 0Ch
retn
}
}
char* TextRenderer::GetClientName(int localClientNum, int index, char* buf, size_t size)
{
Game::CL_GetClientName(localClientNum, index, buf, size);
// Append clantag to username & remove the colors
strncpy_s(buf, size, StripColors(ClanTags::GetUserClantag(index, buf)).data(), size);
return buf;
}
void TextRenderer::PatchColorLimit(const char limit) void TextRenderer::PatchColorLimit(const char limit)
{ {
Utils::Hook::Set<char>(0x535629, limit); // DrawText2d Utils::Hook::Set<char>(0x535629, limit); // DrawText2d
@ -853,12 +802,6 @@ namespace Components
Utils::Hook::Set<char>(0x5A2733, static_cast<char>(ColorIndexForChar(limit))); // No idea :P Utils::Hook::Set<char>(0x5A2733, static_cast<char>(ColorIndexForChar(limit))); // No idea :P
} }
char* TextRenderer::CleanStrStub(char* string)
{
StripColors(string, string, strlen(string) + 1);
return string;
}
// Patches team overhead normally // Patches team overhead normally
bool TextRenderer::Dvar_GetUnpackedColorByName(const char* name, float* expandedColor) bool TextRenderer::Dvar_GetUnpackedColorByName(const char* name, float* expandedColor)
{ {
@ -931,7 +874,6 @@ namespace Components
cg_newColors = Dvar::Register<bool>("cg_newColors", true, Game::dvar_flag::DVAR_FLAG_SAVED, "Use Warfare 2 color code style."); cg_newColors = Dvar::Register<bool>("cg_newColors", true, Game::dvar_flag::DVAR_FLAG_SAVED, "Use Warfare 2 color code style.");
sv_customTextColor = Game::Dvar_RegisterColor("sv_customTextColor", 1, 0.7f, 0, 1, Game::dvar_flag::DVAR_FLAG_REPLICATED, "Color for the extended color code."); sv_customTextColor = Game::Dvar_RegisterColor("sv_customTextColor", 1, 0.7f, 0, 1, Game::dvar_flag::DVAR_FLAG_REPLICATED, "Color for the extended color code.");
sv_allowColoredNames = Dvar::Register<bool>("sv_allowColoredNames", true, Game::dvar_flag::DVAR_FLAG_NONE, "Allow colored names on the server");
// Replace vanilla text drawing function with a reimplementation with extensions // Replace vanilla text drawing function with a reimplementation with extensions
Utils::Hook(0x535410, DrawText2D, HOOK_JUMP).install()->quick(); Utils::Hook(0x535410, DrawText2D, HOOK_JUMP).install()->quick();
@ -952,18 +894,6 @@ namespace Components
// Replace team colors with colorblind team colors when colorblind is enabled // Replace team colors with colorblind team colors when colorblind is enabled
Utils::Hook(0x406530, GetUnpackedColorByNameStub, HOOK_JUMP).install()->quick(); Utils::Hook(0x406530, GetUnpackedColorByNameStub, HOOK_JUMP).install()->quick();
// Disable SV_UpdateUserinfo_f, to block changing the name ingame
Utils::Hook::Set<BYTE>(0x6258D0, 0xC3);
// Allow colored names ingame
Utils::Hook(0x5D8B40, ClientUserinfoChanged, HOOK_JUMP).install()->quick();
// Though, don't apply that to overhead names.
Utils::Hook(0x581932, GetClientName, HOOK_CALL).install()->quick();
// Patch I_CleanStr
Utils::Hook(0x4AD470, CleanStrStub, HOOK_JUMP).install()->quick();
PatchColorLimit(COLOR_LAST_CHAR); PatchColorLimit(COLOR_LAST_CHAR);
} }
} }

View File

@ -74,7 +74,6 @@ namespace Components
static Dvar::Var cg_newColors; static Dvar::Var cg_newColors;
static Game::dvar_t* sv_customTextColor; static Game::dvar_t* sv_customTextColor;
static Dvar::Var sv_allowColoredNames;
static Dvar::Var r_colorBlind; static Dvar::Var r_colorBlind;
static Game::dvar_t* g_ColorBlind_MyTeam; static Game::dvar_t* g_ColorBlind_MyTeam;
static Game::dvar_t* g_ColorBlind_EnemyTeam; static Game::dvar_t* g_ColorBlind_EnemyTeam;
@ -89,17 +88,13 @@ namespace Components
static std::string StripMaterialTextIcons(const std::string& in); static std::string StripMaterialTextIcons(const std::string& in);
static void StripAllTextIcons(const char* in, char* out, size_t max); static void StripAllTextIcons(const char* in, char* out, size_t max);
static std::string StripAllTextIcons(const std::string& in); static std::string StripAllTextIcons(const std::string& in);
static void UserInfoCopy(char* buffer, const char* name, size_t size);
TextRenderer(); TextRenderer();
private: private:
static unsigned HsvToRgb(HsvColor hsv); static unsigned HsvToRgb(HsvColor hsv);
static void ClientUserinfoChanged();
static char* GetClientName(int localClientNum, int index, char* buf, size_t size);
static void PatchColorLimit(char limit); static void PatchColorLimit(char limit);
static char* CleanStrStub(char* string);
static bool Dvar_GetUnpackedColorByName(const char* name, float* expandedColor); static bool Dvar_GetUnpackedColorByName(const char* name, float* expandedColor);
static void GetUnpackedColorByNameStub(); static void GetUnpackedColorByNameStub();