Skip to content

Commit

Permalink
Client: Console: Add color offset for 9884
Browse files Browse the repository at this point in the history
  • Loading branch information
tmp64 committed Nov 18, 2023
1 parent 7c94c49 commit 33f8124
Showing 1 changed file with 51 additions and 13 deletions.
64 changes: 51 additions & 13 deletions src/game/client/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@ namespace console
// Ambiguity between ::Color (Source SDK) and vgui::Color (VGUI1)
using ::Color;

struct ColorOffset
{
size_t printOffset;
size_t dprintOffset;
};

constexpr ColorOffset COLOR_OFFSETS[] = {
#ifdef PLATFORM_WINDOWS
constexpr size_t PRINT_COLOR_OFFSET = 0x128;
constexpr size_t DPRINT_COLOR_OFFSET = 0x12C;
ColorOffset { 0x128, 0x12C }, // Pre-9884
ColorOffset { 0x12C, 0x130 }, // 9884
#else
constexpr size_t PRINT_COLOR_OFFSET = 0x124;
constexpr size_t DPRINT_COLOR_OFFSET = 0x128;
ColorOffset { 0x124, 0x128 }, // Pre-9884
ColorOffset { 0x128, 0x12C }, // 9884
#endif
};

class CGameConsolePrototype : public IGameConsole
{
Expand Down Expand Up @@ -240,34 +248,64 @@ void console::HookConsoleColor()
return;
}

auto fnHookSpecificColor = [&](size_t offset, Color compare, Color *&target) -> bool {
auto fnTryHookSpecificColor = [&](size_t offset, Color compare, Color **target) -> bool
{
size_t iColorPtr = reinterpret_cast<size_t>(pGameConsole->m_pConsole) + offset;
Color *pColor = reinterpret_cast<Color *>(iColorPtr);

if (*pColor != compare)
{
ConPrintf(ConColor::Red,
"HookConsoleColor: check failed.\n"
" Expected: %d %d %d %d\n"
" Got: %d %d %d %d.\n",
compare.r(), compare.g(), compare.b(), compare.a(),
pColor->r(), pColor->g(), pColor->b(), pColor->a());
if (target)
{
ConPrintf(ConColor::Red,
" Offset: 0x%X\n"
" Expected: %d %d %d %d\n"
" Got: %d %d %d %d.\n",
offset,
compare.r(), compare.g(), compare.b(), compare.a(),
pColor->r(), pColor->g(), pColor->b(), pColor->a());
}
return false;
}

target = pColor;
if (target)
*target = pColor;

return true;
};

if (fnHookSpecificColor(PRINT_COLOR_OFFSET, s_DefaultColor, s_ConColor) && fnHookSpecificColor(DPRINT_COLOR_OFFSET, s_DefaultDColor, s_ConDColor))
Color *pPrintColor = nullptr;
Color *pDPrintColor = nullptr;

// Try different offsets
for (const ColorOffset& offset : COLOR_OFFSETS)
{
if (fnTryHookSpecificColor(offset.printOffset, s_DefaultColor, &pPrintColor) &&
fnTryHookSpecificColor(offset.dprintOffset, s_DefaultDColor, &pDPrintColor))
{
// Found the offset
break;
}
}

if (pPrintColor && pDPrintColor)
{
s_ConColor = pPrintColor;
s_ConDColor = pDPrintColor;
#ifdef _DEBUG
ConPrintf(ConColor::Cyan, "HookConsoleColor: Success!\n");
#endif
}
else
{
ConPrintf(ConColor::Red, "Failed to hook console color.\n");

// Print offsets and values
for (const ColorOffset &offset : COLOR_OFFSETS)
{
fnTryHookSpecificColor(offset.printOffset, s_DefaultColor, nullptr);
fnTryHookSpecificColor(offset.dprintOffset, s_DefaultDColor, nullptr);
}
}
}

Expand Down

0 comments on commit 33f8124

Please sign in to comment.