r5sdk/r5dev/engine/sys_utils.cpp
Kawe Mazidjatari a618990937 Detour code refactor
This change was planned for a long time. This moves all REGISTER calls to a single translation unit, this is required as we currently added a very dirty workaround for not registering duplicates by checking if VFTable pointer was already present in the vector... Registering from single translation unit prevents duplicate instances that gets created if header is included by more cpp files.
Reworking this reduced 100kb+ of compiled code. This commit also reworked the way functions/variables/constant gets logged with their addresses; the new code formats them on the fly, and allows for resize at any time. Formatting is no longer required by programmer.

TODO: currently there are some compile errors for dedicated and client dll's. These will be resolved very soon as they need to be properly worked out still (server & client only stuff needs to be properly split). Use the 'main' (stable) branch for the time being if you need to compile these dll's.
2023-01-25 02:26:52 +01:00

119 lines
3.1 KiB
C++

//=============================================================================//
//
// Purpose: General system utilities.
//
//=============================================================================//
#include "core/stdafx.h"
#include "core/logdef.h"
#include "tier0/commandline.h"
#include "tier1/cvar.h"
#include "engine/sys_utils.h"
#ifdef DEDICATED
#include "engine/server/sv_rcon.h"
#else
#include "vgui/vgui_debugpanel.h"
#endif // !DEDICATED
//-----------------------------------------------------------------------------
// Purpose: Exit engine with error
// Input : *error -
// ... -
// Output : void Sys_Error
//-----------------------------------------------------------------------------
void HSys_Error(char* fmt, ...)
{
static char buf[1024] = {};
va_list args{};
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
buf[sizeof(buf) -1] = 0;
va_end(args);
Error(eDLL_T::ENGINE, NO_ERROR, "%s", buf);
return v_Sys_Error(buf);
}
//-----------------------------------------------------------------------------
// Purpose: Show warning in the console, exit engine with error when level 5
// Input : level -
// *error - ... -
// Output : void* Sys_Warning
//-----------------------------------------------------------------------------
void* HSys_Warning(int level, char* fmt, ...)
{
static char buf[1024] = {};
{/////////////////////////////
va_list args{};
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
buf[sizeof(buf) - 1] = '\0';
va_end(args);
}/////////////////////////////
Warning(eDLL_T::COMMON, "Warning(%d):%s", level, buf);
return v_Sys_Warning(level, buf);
}
#ifndef DEDICATED
//-----------------------------------------------------------------------------
// Purpose: Builds log to be displayed on the screen
// Input : pos -
// *fmt - ... -
// Output : void NPrintf
//-----------------------------------------------------------------------------
void HCon_NPrintf(int pos, const char* fmt, ...)
{
if (cl_showhoststats->GetBool())
{
static char buf[1024] = {};
{/////////////////////////////
va_list args{};
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
buf[sizeof(buf) - 1] = '\0';
va_end(args);
}/////////////////////////////
snprintf(g_pOverlay->m_pszCon_NPrintf_Buf,
sizeof(g_pOverlay->m_pszCon_NPrintf_Buf), buf);
}
}
#endif // !DEDICATED
//-----------------------------------------------------------------------------
// Purpose: Gets the process up time (input buffer should be at least 4096 bytes in size)
// Input : *szBuffer -
// Output : snprintf_s ret val
//-----------------------------------------------------------------------------
int Sys_GetProcessUpTime(char* szBuffer)
{
return v_Sys_GetProcessUpTime(szBuffer);
}
void VSys_Utils::Attach() const
{
//DetourAttach(&Sys_Error, &HSys_Error);
DetourAttach(&v_Sys_Warning, &HSys_Warning);
#ifndef DEDICATED
DetourAttach(&v_Con_NPrintf, &HCon_NPrintf);
#endif // !DEDICATED
}
void VSys_Utils::Detach() const
{
//DetourDetach(&Sys_Error, &HSys_Error);
DetourDetach(&v_Sys_Warning, &HSys_Warning);
#ifndef DEDICATED
DetourDetach(&v_Con_NPrintf, &HCon_NPrintf);
#endif // !DEDICATED
}