[Chat]: Skip over '/' (#989)

This commit is contained in:
Edo 2023-04-29 11:11:57 +01:00 committed by GitHub
parent c1809dbf7d
commit ad59acc457
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 38 deletions

View File

@ -8,7 +8,7 @@ namespace Components
// Have only one instance of IW4x read/write the file // Have only one instance of IW4x read/write the file
std::unique_lock<Utils::NamedMutex> Bans::Lock() std::unique_lock<Utils::NamedMutex> Bans::Lock()
{ {
static Utils::NamedMutex mutex{"iw4x-ban-list-lock"}; static Utils::NamedMutex mutex{ "iw4x-ban-list-lock" };
std::unique_lock lock{mutex}; std::unique_lock lock{mutex};
return lock; return lock;
} }

View File

@ -24,7 +24,7 @@ namespace Components
// Have only one instance of IW4x read/write the file // Have only one instance of IW4x read/write the file
std::unique_lock<Utils::NamedMutex> Chat::Lock() std::unique_lock<Utils::NamedMutex> Chat::Lock()
{ {
static Utils::NamedMutex mutex{"iw4x-mute-list-lock"}; static Utils::NamedMutex mutex{ "iw4x-mute-list-lock" };
std::unique_lock lock{mutex}; std::unique_lock lock{mutex};
return lock; return lock;
} }
@ -51,6 +51,7 @@ namespace Components
if (text[msgIndex] == '/') if (text[msgIndex] == '/')
{ {
SendChat = false; SendChat = false;
++msgIndex;
} }
if (IsMuted(player)) if (IsMuted(player))
@ -65,13 +66,14 @@ namespace Components
Game::SV_GameSendServerCommand(player - Game::g_entities, Game::SV_CMD_CAN_IGNORE, Utils::String::VA("%c \"Chat is disabled\"", 0x65)); Game::SV_GameSendServerCommand(player - Game::g_entities, Game::SV_CMD_CAN_IGNORE, Utils::String::VA("%c \"Chat is disabled\"", 0x65));
} }
// Message might be empty after the special characters // Message might be empty after the special characters or '/'
if (text[msgIndex] == '\0') if (text[msgIndex] == '\0')
{ {
SendChat = false; SendChat = false;
return text; return text;
} }
TextRenderer::StripMaterialTextIcons(text, text, std::strlen(text) + 1);
Logger::Print("{}: {}\n", Game::svs_clients[player - Game::g_entities].name, (text + msgIndex)); Logger::Print("{}: {}\n", Game::svs_clients[player - Game::g_entities].name, (text + msgIndex));
for (const auto& callback : SayCallbacks) for (const auto& callback : SayCallbacks)
@ -82,8 +84,6 @@ namespace Components
} }
} }
TextRenderer::StripMaterialTextIcons(text, text, std::strlen(text) + 1);
Game::Scr_AddEntity(player); Game::Scr_AddEntity(player);
Game::Scr_AddString(text + msgIndex); Game::Scr_AddString(text + msgIndex);
Game::Scr_NotifyLevel(static_cast<std::uint16_t>(Game::SL_GetString("say", 0)), 2); Game::Scr_NotifyLevel(static_cast<std::uint16_t>(Game::SL_GetString("say", 0)), 2);

View File

@ -940,7 +940,7 @@ namespace Components
} }
const auto materialNameLen = static_cast<uint8_t>(*text); const auto materialNameLen = static_cast<uint8_t>(*text);
text++; ++text;
for (auto i = 0u; i < materialNameLen; i++) for (auto i = 0u; i < materialNameLen; i++)
{ {
@ -954,8 +954,10 @@ namespace Components
text += materialNameLen; text += materialNameLen;
auto* material = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_MATERIAL, materialName.data()).material; auto* material = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_MATERIAL, materialName.data()).material;
if (material == nullptr || material->techniqueSet == nullptr || material->techniqueSet->name == nullptr || strcmp(material->techniqueSet->name, "2d") != 0) if (material == nullptr || material->techniqueSet == nullptr || material->techniqueSet->name == nullptr || std::strcmp(material->techniqueSet->name, "2d") != 0)
{
material = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_MATERIAL, "default").material; material = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_MATERIAL, "default").material;
}
const auto yy = y - (h + yScale * static_cast<float>(font->pixelHeight)) * 0.5f; const auto yy = y - (h + yScale * static_cast<float>(font->pixelHeight)) * 0.5f;
@ -1091,7 +1093,7 @@ namespace Components
if (renderFlags & Game::TEXT_RENDERFLAG_PADDING) if (renderFlags & Game::TEXT_RENDERFLAG_PADDING)
xa += xScale * padding; xa += xScale * padding;
++count; ++count;
maxLengthRemaining--; --maxLengthRemaining;
continue; continue;
} }
@ -1111,7 +1113,7 @@ namespace Components
if (renderFlags & Game::TEXT_RENDERFLAG_PADDING) if (renderFlags & Game::TEXT_RENDERFLAG_PADDING)
xa += xScale * padding; xa += xScale * padding;
count += (fontIconEnd - curText) + 1; count += (fontIconEnd - curText) + 1;
maxLengthRemaining--; --maxLengthRemaining;
curText = fontIconEnd; curText = fontIconEnd;
continue; continue;
} }
@ -1222,8 +1224,8 @@ namespace Components
if (renderFlags & Game::TEXT_RENDERFLAG_PADDING) if (renderFlags & Game::TEXT_RENDERFLAG_PADDING)
xa += xScale * padding; xa += xScale * padding;
count++; ++count;
maxLengthRemaining--; --maxLengthRemaining;
} }
if (renderFlags & Game::TEXT_RENDERFLAG_CURSOR && count == cursorPos) if (renderFlags & Game::TEXT_RENDERFLAG_CURSOR && count == cursorPos)
@ -1240,10 +1242,14 @@ namespace Components
auto maxWidth = 0; auto maxWidth = 0;
if (maxChars <= 0) if (maxChars <= 0)
{
maxChars = std::numeric_limits<int>::max(); maxChars = std::numeric_limits<int>::max();
}
if (text == nullptr) if (text == nullptr)
{
return 0; return 0;
}
auto count = 0; auto count = 0;
while (text && *text && count < maxChars) while (text && *text && count < maxChars)
@ -1259,7 +1265,7 @@ namespace Components
{ {
if (*text >= COLOR_FIRST_CHAR && *text <= COLOR_LAST_CHAR) if (*text >= COLOR_FIRST_CHAR && *text <= COLOR_LAST_CHAR)
{ {
text++; ++text;
continue; continue;
} }
@ -1309,7 +1315,8 @@ namespace Components
{ {
maxWidth = lineWidth; maxWidth = lineWidth;
} }
count++;
++count;
} }
} }
@ -1328,7 +1335,7 @@ namespace Components
if (!in || !out) return; if (!in || !out) return;
max--; max--;
size_t current = 0; std::size_t current = 0;
while (*in != 0 && current < max) while (*in != 0 && current < max)
{ {
const char index = *(in + 1); const char index = *(in + 1);
@ -1345,6 +1352,7 @@ namespace Components
++in; ++in;
} }
*out = '\0'; *out = '\0';
} }
@ -1352,7 +1360,7 @@ namespace Components
{ {
char buffer[1024]{}; // 1024 is a lucky number in the engine char buffer[1024]{}; // 1024 is a lucky number in the engine
StripColors(in.data(), buffer, sizeof(buffer)); StripColors(in.data(), buffer, sizeof(buffer));
return {buffer}; return std::string{ buffer };
} }
void TextRenderer::StripMaterialTextIcons(const char* in, char* out, std::size_t max) void TextRenderer::StripMaterialTextIcons(const char* in, char* out, std::size_t max)
@ -1399,6 +1407,7 @@ namespace Components
} }
} }
*out = '\0'; *out = '\0';
} }
@ -1406,7 +1415,7 @@ namespace Components
{ {
char buffer[1000]{}; // Should be more than enough char buffer[1000]{}; // Should be more than enough
StripAllTextIcons(in.data(), buffer, sizeof(buffer)); StripAllTextIcons(in.data(), buffer, sizeof(buffer));
return {buffer}; return std::string{ buffer };
} }
void TextRenderer::StripAllTextIcons(const char* in, char* out, std::size_t max) void TextRenderer::StripAllTextIcons(const char* in, char* out, std::size_t max)
@ -1414,7 +1423,7 @@ namespace Components
if (!in || !out) return; if (!in || !out) return;
--max; --max;
size_t current = 0; std::size_t current = 0;
while (*in != 0 && current < max) while (*in != 0 && current < max)
{ {
if (*in == '^' && (in[1] == '\x01' || in[1] == '\x02')) if (*in == '^' && (in[1] == '\x01' || in[1] == '\x02'))
@ -1422,18 +1431,25 @@ namespace Components
in += 2; in += 2;
if (*in) // width if (*in) // width
in++; {
++in;
}
if (*in) // height if (*in) // height
in++; {
++in;
}
if (*in) // material name length + material name characters if (*in) // material name length + material name characters
{ {
const auto materialNameLength = *in; const auto materialNameLength = *in;
in++; ++in;
for (auto i = 0; i < materialNameLength; i++) for (auto i = 0; i < materialNameLength; i++)
{ {
if (*in) if (*in)
in++; {
++in;
}
} }
} }
@ -1486,16 +1502,16 @@ namespace Components
if (c == '^' && *curText >= COLOR_FIRST_CHAR && *curText <= COLOR_LAST_CHAR && !(cursorPos > count && cursorPos < count + 2)) if (c == '^' && *curText >= COLOR_FIRST_CHAR && *curText <= COLOR_LAST_CHAR && !(cursorPos > count && cursorPos < count + 2))
{ {
curText++; ++curText;
count++; ++count;
} }
else if(c != '\r' && c != '\n') else if(c != '\r' && c != '\n')
{ {
len++; ++len;
} }
count++; ++count;
lenWithInvisibleTail++; ++lenWithInvisibleTail;
} }
return lenWithInvisibleTail; return lenWithInvisibleTail;
@ -1524,17 +1540,17 @@ namespace Components
{ {
Utils::Hook::Set<char>(0x535629, limit); // DrawText2d Utils::Hook::Set<char>(0x535629, limit); // DrawText2d
Utils::Hook::Set<char>(0x4C1BE4, limit); // SEH_PrintStrlen Utils::Hook::Set<char>(0x4C1BE4, limit); // SEH_PrintStrlen
Utils::Hook::Set<char>(0x4863DD, limit); // No idea :P Utils::Hook::Set<char>(0x4863DD, limit); // No idea
Utils::Hook::Set<char>(0x486429, limit); // No idea :P Utils::Hook::Set<char>(0x486429, limit); // No idea
Utils::Hook::Set<char>(0x49A5A8, limit); // No idea :P Utils::Hook::Set<char>(0x49A5A8, limit); // No idea
Utils::Hook::Set<char>(0x505721, limit); // R_TextWidth Utils::Hook::Set<char>(0x505721, limit); // R_TextWidth
Utils::Hook::Set<char>(0x505801, limit); // No idea :P Utils::Hook::Set<char>(0x505801, limit); // No idea
Utils::Hook::Set<char>(0x50597F, limit); // No idea :P Utils::Hook::Set<char>(0x50597F, limit); // No idea
Utils::Hook::Set<char>(0x5815DB, limit); // No idea :P Utils::Hook::Set<char>(0x5815DB, limit); // No idea
Utils::Hook::Set<char>(0x592ED0, limit); // No idea :P Utils::Hook::Set<char>(0x592ED0, limit); // No idea
Utils::Hook::Set<char>(0x5A2E2E, limit); // No idea :P Utils::Hook::Set<char>(0x5A2E2E, limit); // No idea
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
} }
// Patches team overhead normally // Patches team overhead normally
@ -1573,8 +1589,8 @@ namespace Components
{ {
__asm __asm
{ {
push[esp + 8h] push [esp + 8h]
push[esp + 8h] push [esp + 8h]
call TextRenderer::Dvar_GetUnpackedColorByName call TextRenderer::Dvar_GetUnpackedColorByName
add esp, 8h add esp, 8h
@ -1654,7 +1670,7 @@ namespace Components
fontIconLookup.emplace(std::make_pair(entry.iconName, entry)); fontIconLookup.emplace(std::make_pair(entry.iconName, entry));
} }
std::sort(fontIconList.begin(), fontIconList.end(), [](const FontIconTableEntry& a, const FontIconTableEntry& b) std::ranges::sort(fontIconList, [](const FontIconTableEntry& a, const FontIconTableEntry& b) -> bool
{ {
return a.iconName < b.iconName; return a.iconName < b.iconName;
}); });