Error/Warning/Con_NPrintf log hook refactor

* Warning now only logs if error level < 5.
* Warning buffer size has been increased to '10000' (internal buffer size).
* Error is now hooked as well.
* Error buffer size has been increased to '4096' (internal buffer size).
* Con_NPrintf has been refactored; 'cl_showhoststats' has been removed.
* Con_NPrintf buffer size has been increased to '4096' (MAXPRINTMSG).
* CTextOverlay::Con_NPrintf now checks the first byte in the character buffer before rendering anything, and sets the first byte to a terminating null character after rendering. This fixes the bug causing the host_speeds overlay to be still drawn while the cvar 'host_speeds' was disabled.
This commit is contained in:
Kawe Mazidjatari 2023-02-19 09:51:46 +01:00
parent 9e61db4b60
commit 8965d6f101
7 changed files with 67 additions and 56 deletions

View File

@ -15,16 +15,21 @@
#include "vgui/vgui_debugpanel.h"
#endif // !DEDICATED
#if !defined( _X360 )
#define MAXPRINTMSG 4096
#else
#define MAXPRINTMSG 1024
#endif
//-----------------------------------------------------------------------------
// Purpose: Exit engine with error
// Purpose: Show error in the console
// Input : *error -
// ... -
// Output : void Sys_Error
// Output : void _Error
//-----------------------------------------------------------------------------
void HSys_Error(char* fmt, ...)
void _Error(char* fmt, ...)
{
static char buf[1024] = {};
char buf[4096];
va_list args{};
va_start(args, fmt);
@ -35,18 +40,18 @@ void HSys_Error(char* fmt, ...)
va_end(args);
Error(eDLL_T::ENGINE, NO_ERROR, "%s", buf);
return v_Sys_Error(buf);
v_Error(buf);
}
//-----------------------------------------------------------------------------
// Purpose: Show warning in the console, exit engine with error when level 5
// Input : level -
// *error - ... -
// Output : void* Sys_Warning
// Output : void* _Warning
//-----------------------------------------------------------------------------
void* HSys_Warning(int level, char* fmt, ...)
void* _Warning(int level, char* fmt, ...)
{
static char buf[1024] = {};
char buf[10000];
{/////////////////////////////
va_list args{};
va_start(args, fmt);
@ -57,8 +62,12 @@ void* HSys_Warning(int level, char* fmt, ...)
va_end(args);
}/////////////////////////////
Warning(eDLL_T::COMMON, "Warning(%d):%s", level, buf);
return v_Sys_Warning(level, buf);
if (level < 5)
{
Warning(eDLL_T::COMMON, "Warning(%d):%s", level, buf);
}
v_Warning(level, buf);
}
#ifndef DEDICATED
@ -68,24 +77,22 @@ void* HSys_Warning(int level, char* fmt, ...)
// *fmt - ... -
// Output : void NPrintf
//-----------------------------------------------------------------------------
void HCon_NPrintf(int pos, const char* fmt, ...)
void _Con_NPrintf(int pos, const char* fmt, ...)
{
if (cl_showhoststats->GetBool())
{
static char buf[1024] = {};
{/////////////////////////////
va_list args{};
va_start(args, fmt);
char buf[MAXPRINTMSG];
{/////////////////////////////
va_list args{};
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
vsnprintf(buf, sizeof(buf), fmt, args);
buf[sizeof(buf) - 1] = '\0';
va_end(args);
}/////////////////////////////
buf[sizeof(buf) - 1] = '\0';
va_end(args);
}/////////////////////////////
snprintf(g_pOverlay->m_pszCon_NPrintf_Buf,
sizeof(g_pOverlay->m_pszCon_NPrintf_Buf), buf);
}
g_pOverlay->m_nCon_NPrintf_Idx = pos;
snprintf(g_pOverlay->m_szCon_NPrintf_Buf,
sizeof(g_pOverlay->m_szCon_NPrintf_Buf), buf);
}
#endif // !DEDICATED
@ -101,18 +108,18 @@ int Sys_GetProcessUpTime(char* szBuffer)
void VSys_Utils::Attach() const
{
//DetourAttach(&Sys_Error, &HSys_Error);
DetourAttach(&v_Sys_Warning, &HSys_Warning);
DetourAttach((LPVOID*)&v_Error, &_Error);
DetourAttach((LPVOID*)&v_Warning, &_Warning);
#ifndef DEDICATED
DetourAttach(&v_Con_NPrintf, &HCon_NPrintf);
DetourAttach((LPVOID*)&v_Con_NPrintf, &_Con_NPrintf);
#endif // !DEDICATED
}
void VSys_Utils::Detach() const
{
//DetourDetach(&Sys_Error, &HSys_Error);
DetourDetach(&v_Sys_Warning, &HSys_Warning);
DetourDetach((LPVOID*)&v_Error, &_Error);
DetourDetach((LPVOID*)&v_Warning, &_Warning);
#ifndef DEDICATED
DetourDetach(&v_Con_NPrintf, &HCon_NPrintf);
DetourDetach((LPVOID*)&v_Con_NPrintf, &_Con_NPrintf);
#endif // !DEDICATED
}

View File

@ -1,11 +1,11 @@
#pragma once
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline CMemory p_Sys_Error;
inline auto v_Sys_Error = p_Sys_Error.RCast<void (*)(char* fmt, ...)>();
inline CMemory p_Error;
inline auto v_Error = p_Error.RCast<void (*)(char* fmt, ...)>();
inline CMemory p_Sys_Warning;
inline auto v_Sys_Warning = p_Sys_Warning.RCast<void* (*)(int, char* fmt, ...)>();
inline CMemory p_Warning;
inline auto v_Warning = p_Warning.RCast<void (*)(int, char* fmt, ...)>();
inline CMemory p_Sys_GetProcessUpTime;
inline auto v_Sys_GetProcessUpTime = p_Sys_GetProcessUpTime.RCast<int (*)(char* szBuffer)>();
@ -16,7 +16,6 @@ inline auto v_Con_NPrintf = p_Con_NPrintf.RCast<void (*)(int pos, const char* fm
/* ==== ------- ========================================================================================================================================================= */
///////////////////////////////////////////////////////////////////////////////
void HSys_Error(char* fmt, ...);
int Sys_GetProcessUpTime(char* szBuffer);
///////////////////////////////////////////////////////////////////////////////
@ -24,8 +23,8 @@ class VSys_Utils : public IDetour
{
virtual void GetAdr(void) const
{
LogFunAdr("Sys_Error", p_Sys_Error.GetPtr());
LogFunAdr("Sys_Warning", p_Sys_Warning.GetPtr());
LogFunAdr("Error", p_Error.GetPtr());
LogFunAdr("Warning", p_Warning.GetPtr());
LogFunAdr("Sys_GetProcessUpTime", p_Sys_GetProcessUpTime.GetPtr());
#ifndef DEDICATED
LogFunAdr("Con_NPrintf", p_Con_NPrintf.GetPtr());
@ -33,14 +32,14 @@ class VSys_Utils : public IDetour
}
virtual void GetFun(void) const
{
p_Sys_Error = g_GameDll.FindPatternSIMD("48 89 4C 24 08 48 89 54 24 10 4C 89 44 24 18 4C 89 4C 24 20 53 55 41 54 41 56 B8 58 10 ?? ?? E8");
p_Sys_Warning = g_GameDll.FindPatternSIMD("48 89 54 24 ?? 4C 89 44 24 ?? 4C 89 4C 24 ?? 48 83 EC 28 4C 8D 44 24 ?? E8 ?? ?? ?? ?? 48 83 C4 28 C3 CC CC CC CC CC CC CC CC CC CC CC CC CC CC 48 89 5C 24 ?? 48 89 74 24 ?? 48 89 7C 24 ?? 8B 05 ?? ?? ?? ??");
p_Error = g_GameDll.FindPatternSIMD("48 89 4C 24 08 48 89 54 24 10 4C 89 44 24 18 4C 89 4C 24 20 53 55 41 54 41 56 B8 58 10 ?? ?? E8");
p_Warning = g_GameDll.FindPatternSIMD("48 89 54 24 ?? 4C 89 44 24 ?? 4C 89 4C 24 ?? 48 83 EC 28 4C 8D 44 24 ?? E8 ?? ?? ?? ?? 48 83 C4 28 C3 CC CC CC CC CC CC CC CC CC CC CC CC CC CC 48 89 5C 24 ?? 48 89 74 24 ?? 48 89 7C 24 ?? 8B 05 ?? ?? ?? ??");
p_Sys_GetProcessUpTime = g_GameDll.FindPatternSIMD("40 57 48 83 EC 30 48 8B F9 8B 0D ?? ?? ?? ??");
#ifndef DEDICATED
p_Con_NPrintf = g_GameDll.FindPatternSIMD("48 89 4C 24 ?? 48 89 54 24 ?? 4C 89 44 24 ?? 4C 89 4C 24 ?? C3");
#endif // !DEDICATED
v_Sys_Error = p_Sys_Error.RCast<void (*)(char*, ...)>(); /*48 89 4C 24 08 48 89 54 24 10 4C 89 44 24 18 4C 89 4C 24 20 53 55 41 54 41 56 B8 58 10 00 00 E8*/
v_Sys_Warning = p_Sys_Warning.RCast<void* (*)(int, char*, ...)>(); /*48 89 54 24 ?? 4C 89 44 24 ?? 4C 89 4C 24 ?? 48 83 EC 28 4C 8D 44 24 ?? E8 ?? ?? ?? ?? 48 83 C4 28 C3 CC CC CC CC CC CC CC CC CC CC CC CC CC CC 48 89 5C 24 ?? 48 89 74 24 ?? 48 89 7C 24 ?? 8B 05 ?? ?? ?? ??*/
v_Error = p_Error.RCast<void (*)(char*, ...)>(); /*48 89 4C 24 08 48 89 54 24 10 4C 89 44 24 18 4C 89 4C 24 20 53 55 41 54 41 56 B8 58 10 00 00 E8*/
v_Warning = p_Warning.RCast<void (*)(int, char*, ...)>(); /*48 89 54 24 ?? 4C 89 44 24 ?? 4C 89 4C 24 ?? 48 83 EC 28 4C 8D 44 24 ?? E8 ?? ?? ?? ?? 48 83 C4 28 C3 CC CC CC CC CC CC CC CC CC CC CC CC CC CC 48 89 5C 24 ?? 48 89 74 24 ?? 48 89 7C 24 ?? 8B 05 ?? ?? ?? ??*/
v_Sys_GetProcessUpTime = p_Sys_GetProcessUpTime.RCast<int (*)(char*)>(); /*40 57 48 83 EC 30 48 8B F9 8B 0D ?? ?? ?? ??*/
#ifndef DEDICATED
v_Con_NPrintf = p_Con_NPrintf.RCast<void (*)(int, const char*, ...)>(); /*48 89 4C 24 ?? 48 89 54 24 ?? 4C 89 44 24 ?? 4C 89 4C 24 ?? C3*/

View File

@ -140,7 +140,6 @@ void ConVar::Init(void)
cl_rcon_request_sendlogs = ConVar::Create("cl_rcon_request_sendlogs", "1" , FCVAR_RELEASE, "Request the rcon server to send console logs on connect.", false, 0.f, false, 0.f, nullptr, nullptr);
cl_quota_stringCmdsPerSecond = ConVar::Create("cl_quota_stringCmdsPerSecond", "16" , FCVAR_RELEASE, "How many string commands per second user is allowed to submit, 0 to allow all submissions.", true, 0.f, false, 0.f, nullptr, nullptr);
cl_showhoststats = ConVar::Create("cl_showhoststats" , "0", FCVAR_DEVELOPMENTONLY, "Host speeds debug overlay.", false, 0.f, false, 0.f, nullptr, nullptr);
cl_hoststats_invert_x = ConVar::Create("cl_hoststats_invert_x", "0", FCVAR_DEVELOPMENTONLY, "Inverts the X offset for host speeds debug overlay.", false, 0.f, false, 0.f, nullptr, nullptr);
cl_hoststats_invert_y = ConVar::Create("cl_hoststats_invert_y", "0", FCVAR_DEVELOPMENTONLY, "Inverts the Y offset for host speeds debug overlay.", false, 0.f, false, 0.f, nullptr, nullptr);
cl_hoststats_offset_x = ConVar::Create("cl_hoststats_offset_x", "10", FCVAR_DEVELOPMENTONLY, "X offset for host speeds debug overlay.", false, 0.f, false, 0.f, nullptr, nullptr);

View File

@ -112,8 +112,7 @@ ConVar* bhit_abs_origin = nullptr;
ConVar* cl_rcon_request_sendlogs = nullptr;
ConVar* cl_quota_stringCmdsPerSecond = nullptr;
ConVar* cl_showhoststats = nullptr;
ConVar* cl_hoststats_invert_x = nullptr;
ConVar* cl_hoststats_invert_x = nullptr; // RENAME!
ConVar* cl_hoststats_invert_y = nullptr;
ConVar* cl_hoststats_offset_x = nullptr;
ConVar* cl_hoststats_offset_y = nullptr;

View File

@ -108,7 +108,6 @@ extern ConVar* bhit_abs_origin;
extern ConVar* cl_rcon_request_sendlogs;
extern ConVar* cl_quota_stringCmdsPerSecond;
extern ConVar* cl_showhoststats;
extern ConVar* cl_hoststats_invert_x;
extern ConVar* cl_hoststats_invert_y;
extern ConVar* cl_hoststats_offset_x;

View File

@ -32,6 +32,7 @@ void CTextOverlay::Update(void)
{
return;
}
Con_NPrintf();
if (con_drawnotify->GetBool())
{
DrawNotify();
@ -44,10 +45,6 @@ void CTextOverlay::Update(void)
{
DrawGPUStats();
}
if (cl_showhoststats->GetBool())
{
DrawHostStats();
}
if (cl_showmaterialinfo->GetBool())
{
DrawCrosshairMaterial();
@ -177,15 +174,23 @@ void CTextOverlay::ShouldDraw(const float flFrameTime)
}
//-----------------------------------------------------------------------------
// Purpose: draws live host stats on screen.
// Purpose: draws console messages on screen (only used for 'host_speeds'!, deprecated!!).
//-----------------------------------------------------------------------------
void CTextOverlay::DrawHostStats(void) const
void CTextOverlay::Con_NPrintf(void)
{
if (!m_szCon_NPrintf_Buf[0])
{
return;
}
static const Color c = { 255, 255, 255, 255 };
const int nWidth = cl_hoststats_invert_x->GetBool() ? g_nWindowRect[0] - cl_hoststats_offset_x->GetInt() : cl_hoststats_offset_x->GetInt();
const int nWidth = cl_hoststats_invert_x->GetBool() ? g_nWindowRect[0] - cl_hoststats_offset_x->GetInt() : cl_hoststats_offset_x->GetInt() + m_nCon_NPrintf_Idx * m_nFontHeight;
const int nHeight = cl_hoststats_invert_y->GetBool() ? g_nWindowRect[1] - cl_hoststats_offset_y->GetInt() : cl_hoststats_offset_y->GetInt();
CMatSystemSurface_DrawColoredText(g_pMatSystemSurface, v_Rui_GetFontFace(), m_nFontHeight, nWidth, nHeight, c.r(), c.g(), c.b(), c.a(), m_pszCon_NPrintf_Buf);
CMatSystemSurface_DrawColoredText(g_pMatSystemSurface, v_Rui_GetFontFace(), m_nFontHeight, nWidth, nHeight, c.r(), c.g(), c.b(), c.a(), m_szCon_NPrintf_Buf);
m_nCon_NPrintf_Idx = 0;
m_szCon_NPrintf_Buf[0] = '\0';
}
//-----------------------------------------------------------------------------

View File

@ -21,7 +21,8 @@ public:
CTextOverlay()
{
m_nFontHeight = 16;
memset(m_pszCon_NPrintf_Buf, '\0', sizeof(m_pszCon_NPrintf_Buf));
m_nCon_NPrintf_Idx = 0;
memset(m_szCon_NPrintf_Buf, '\0', sizeof(m_szCon_NPrintf_Buf));
}
void Update(void);
@ -29,12 +30,13 @@ public:
void DrawNotify(void);
void DrawFormat(const int x, const int y, const Color c, const char* pszFormat, ...) const;
void ShouldDraw(const float flFrameTime);
void DrawHostStats(void) const;
void DrawSimStats(void) const;
void DrawGPUStats(void) const;
void DrawCrosshairMaterial(void) const;
void DrawStreamOverlay(void) const;
void Con_NPrintf(void);
private:
Color GetLogColorForType(const EGlobalContext_t type) const;
vector<CTextNotify> m_vNotifyText;
@ -43,7 +45,8 @@ private:
mutable std::mutex m_Mutex;
public:
char m_pszCon_NPrintf_Buf[4096]{};
int m_nCon_NPrintf_Idx;
char m_szCon_NPrintf_Buf[4096];
};
///////////////////////////////////////////////////////////////////////////////