r5sdk/r5dev/engine/sys_utils.cpp
Kawe Mazidjatari f120354e96 Initial port to CMake
* All libraries have been isolated from each other, and build into separate artifacts.
* Project has been restructured to support isolating libraries.
* CCrashHandler now calls a callback on crash (setup from core/dllmain.cpp, this can be setup in any way for any project. This callback is getting called when the apllication crashes. Useful for flushing buffers before closing handles to logging files for example).
* Tier0 'CoreMsgV' function now calls a callback sink, which could be set by the user (currently setup to the SDK's internal logger in core/dllmain.cpp).

TODO:
* Add a batch file to autogenerate all projects.
* Add support for dedicated server.
* Add support for client dll.

Bugs:
* Game crashes on the title screen after the UI script compiler has finished (root cause unknown).
* Curl error messages are getting logged twice for the dedicated server due to the removal of all "DEDICATED" preprocessor directives to support isolating projects. This has to be fixed properly!
2023-05-10 00:05:38 +02:00

126 lines
3.2 KiB
C++

//=============================================================================//
//
// Purpose: General system utilities.
//
//=============================================================================//
#include "core/stdafx.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
#if !defined( _X360 )
#define MAXPRINTMSG 4096
#else
#define MAXPRINTMSG 1024
#endif
//-----------------------------------------------------------------------------
// Purpose: Show error in the console
// Input : *error -
// ... -
// Output : void _Error
//-----------------------------------------------------------------------------
void _Error(char* fmt, ...)
{
char buf[4096];
{/////////////////////////////
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);
v_Error("%s", buf);
}
//-----------------------------------------------------------------------------
// Purpose: Show warning in the console, exit engine with error when level 5
// Input : level -
// *error - ... -
// Output : void* _Warning
//-----------------------------------------------------------------------------
void _Warning(int level, char* fmt, ...)
{
char buf[10000];
{/////////////////////////////
va_list args;
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
buf[sizeof(buf) - 1] = '\0';
va_end(args);
}/////////////////////////////
if (level < 5)
{
Warning(eDLL_T::COMMON, "Warning(%d):%s", level, buf);
}
v_Warning(level, "%s", buf);
}
#ifndef DEDICATED
//-----------------------------------------------------------------------------
// Purpose: Builds log to be displayed on the screen
// Input : pos -
// *fmt - ... -
// Output : void NPrintf
//-----------------------------------------------------------------------------
void _Con_NPrintf(int pos, const char* fmt, ...)
{
char buf[MAXPRINTMSG];
{/////////////////////////////
va_list args;
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
buf[sizeof(buf) - 1] = '\0';
va_end(args);
}/////////////////////////////
g_pOverlay->m_nCon_NPrintf_Idx = pos;
snprintf(g_pOverlay->m_szCon_NPrintf_Buf,
sizeof(g_pOverlay->m_szCon_NPrintf_Buf), "%s", 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((LPVOID*)&v_Error, &_Error);
DetourAttach((LPVOID*)&v_Warning, &_Warning);
#ifndef DEDICATED
DetourAttach((LPVOID*)&v_Con_NPrintf, &_Con_NPrintf);
#endif // !DEDICATED
}
void VSys_Utils::Detach() const
{
DetourDetach((LPVOID*)&v_Error, &_Error);
DetourDetach((LPVOID*)&v_Warning, &_Warning);
#ifndef DEDICATED
DetourDetach((LPVOID*)&v_Con_NPrintf, &_Con_NPrintf);
#endif // !DEDICATED
}