r5sdk/r5dev/engine/sys_utils.cpp
Kawe Mazidjatari 4b72afb74f Light refactor for logging
Moved logging functions to dbg.h (tier0) and export them from the dll.
Added additional functions for checking bad pointers (debug only!).
Reduced output code size.
2022-05-25 14:18:29 +02:00

138 lines
3.8 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"
#include "gameui/IConsole.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, "%s\n", buf);
return 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::NONE, "Warning(%d):%s\n", level, buf);
return 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((char*)g_pLogSystem.m_pszCon_NPrintf_Buf, 4096, buf);
}
}
#endif // !DEDICATED
//-----------------------------------------------------------------------------
// Purpose: Load assets from a custom directory if file exists
// Input : *lpFileName -
// a2 - *a3 -
// Output : void* Sys_LoadAssetHelper
//-----------------------------------------------------------------------------
void* HSys_LoadAssetHelper(const CHAR* lpFileName, std::int64_t a2, LARGE_INTEGER* a3)
{
std::string mod_file;
std::string base_file = lpFileName;
static const std::string mod_dir = "paks\\Win32\\";
static const std::string base_dir = "paks\\Win64\\";
if (strstr(lpFileName, base_dir.c_str()))
{
base_file.erase(0, 11); // Erase 'base_dir'.
mod_file = mod_dir + base_file; // Prepend 'mod_dir'.
if (FileExists(mod_file.c_str()))
{
// Load decompressed pak files from 'mod_dir'.
return Sys_LoadAssetHelper(mod_file.c_str(), a2, a3);
}
}
return Sys_LoadAssetHelper(lpFileName, a2, a3);
}
void SysUtils_Attach()
{
//DetourAttach((LPVOID*)&Sys_Error, &HSys_Error);
DetourAttach((LPVOID*)&Sys_Warning, &HSys_Warning);
DetourAttach((LPVOID*)&Sys_LoadAssetHelper, &HSys_LoadAssetHelper);
#ifndef DEDICATED
DetourAttach((LPVOID*)&Con_NPrintf, &HCon_NPrintf);
#endif // !DEDICATED
}
void SysUtils_Detach()
{
//DetourDetach((LPVOID*)&Sys_Error, &HSys_Error);
DetourDetach((LPVOID*)&Sys_Warning, &HSys_Warning);
DetourDetach((LPVOID*)&Sys_LoadAssetHelper, &HSys_LoadAssetHelper);
#ifndef DEDICATED
DetourDetach((LPVOID*)&Con_NPrintf, &HCon_NPrintf);
#endif // !DEDICATED
}