r5sdk/r5dev/engine/common.cpp
Kawe Mazidjatari c6f2c99619 Improve client-side online authentication error handling and UX
Display the error to the user without having to open the developer console or terminal window. This patch also adds printing to COM_ExplainDisconnection (which has been stripped out of the retail binary).
2024-04-05 16:34:28 +02:00

101 lines
2.0 KiB
C++

//=====================================================================================//
//
// Purpose:
//
//=====================================================================================//
#include <core/stdafx.h>
#include <tier1/strtools.h>
#include <localize/localize.h>
#include <engine/common.h>
/*
==============================
COM_FormatSeconds
==============================
*/
const char* COM_FormatSeconds(int seconds)
{
static char string[64];
int hours = 0;
int minutes = seconds / 60;
if (minutes > 0)
{
seconds -= (minutes * 60);
hours = minutes / 60;
if (hours > 0)
{
minutes -= (hours * 60);
}
}
if (hours > 0)
{
Q_snprintf(string, sizeof(string), "%2i:%02i:%02i", hours, minutes, seconds);
}
else
{
Q_snprintf(string, sizeof(string), "%02i:%02i", minutes, seconds);
}
return string;
}
/*
==============================
COM_ExplainDisconnection
==============================
*/
void COM_ExplainDisconnection(bool bPrint, const char* fmt, ...)
{
char szBuf[1024];
{/////////////////////////////
va_list vArgs;
va_start(vArgs, fmt);
vsnprintf(szBuf, sizeof(szBuf), fmt, vArgs);
szBuf[sizeof(szBuf) - 1] = '\0';
va_end(vArgs);
}/////////////////////////////
if (bPrint)
{
if (szBuf[0] == '#')
{
wchar_t formatStr[1024];
const wchar_t* wpchReason = (*g_ppVGuiLocalize) ? (*g_ppVGuiLocalize)->Find(szBuf) : nullptr;
if (wpchReason)
{
wcsncpy(formatStr, wpchReason, sizeof(formatStr) / sizeof(wchar_t));
char conStr[256];
(*g_ppVGuiLocalize)->ConvertUnicodeToANSI(formatStr, conStr, sizeof(conStr));
Error(eDLL_T::ENGINE, NO_ERROR, "%s\n", conStr);
}
else
Error(eDLL_T::ENGINE, NO_ERROR, "%s\n", szBuf);
}
else
{
Error(eDLL_T::ENGINE, NO_ERROR, "%s\n", szBuf);
}
}
v_COM_ExplainDisconnection(bPrint, szBuf);
}
void VCommon::Attach() const
{
DetourAttach(&v_COM_ExplainDisconnection, COM_ExplainDisconnection);
}
void VCommon::Detach() const
{
DetourDetach(&v_COM_ExplainDisconnection, COM_ExplainDisconnection);
}