r5sdk/r5dev/tier0/commandline.cpp
Kawe Mazidjatari d37f12aa4f Don't recreate the command line
Don't recreate it in the engine (LauncherMain) if its already created in the SDK. This solution toggles a hybrid between creating it in the SDK, or in the engine when -noworkerdll is passed to the loader.
2023-07-01 02:11:39 +02:00

100 lines
2.6 KiB
C++

//=============================================================================//
//
// Purpose: Command line utilities
//
//=============================================================================//
#include "tier0/commandline.h"
bool g_bCommandLineCreated = false;
//-----------------------------------------------------------------------------
// Purpose: Create a command line from the passed in string
// Note that if you pass in a @filename, then the routine will read settings
// from a file instead of the command line
//-----------------------------------------------------------------------------
void CCommandLine::StaticCreateCmdLine(CCommandLine* thisptr, const char* pszCommandLine)
{
// The SDK creates the cmdline instead, when loaded. We hook this
// function, and skip the actual creation of subsequent calls.
// This is required as otherwise our own appended parameters will
// get lost when the game recreates it in 'LauncherMain'.
if (!g_bCommandLineCreated)
{
v_CCommandLine__CreateCmdLine(thisptr, pszCommandLine);
}
}
//-----------------------------------------------------------------------------
// Purpose: parses a text file and appends its parameters/values
//-----------------------------------------------------------------------------
void CCommandLine::AppendParametersFromFile(const char* const pszConfig)
{
char szTextBuf[2048];
FILE* pFile = fopen(pszConfig, "r");
if (!pFile)
{
// Try the 'PLATFORM' folder.
snprintf(szTextBuf, sizeof(szTextBuf), "platform/%s", pszConfig);
pFile = fopen(szTextBuf, "r");
if (!pFile)
{
printf("%s: '%s' does not exist!\n",
__FUNCTION__, pszConfig);
return;
}
}
while (fgets(szTextBuf, sizeof(szTextBuf), pFile))
{
// Trim newlines...
size_t nLen = strlen(szTextBuf);
if (nLen > 0 && szTextBuf[nLen - 1] == '\n')
{
szTextBuf[--nLen] = '\0';
}
char* const pSpacePos = strchr(szTextBuf, ' ');
const char* pArgValue = "";
if (pSpacePos)
{
// Skip space and move to value.
*pSpacePos = '\0';
char* pArg = pSpacePos + 1;
// Trim the quotes around the value.
if (*pArg == '\"')
{
pArg++;
char* const pEndQuote = strchr(pArg, '\"');
if (pEndQuote)
{
*pEndQuote = '\0';
}
}
pArgValue = pArg;
}
AppendParm(szTextBuf, pArgValue);
}
fclose(pFile);
}
///////////////////////////////////////////////////////////////////////////////
CCommandLine* g_pCmdLine = nullptr;
void VCommandLine::Attach() const
{
DetourAttach(&v_CCommandLine__CreateCmdLine, &CCommandLine::StaticCreateCmdLine);
}
void VCommandLine::Detach() const
{
DetourDetach(&v_CCommandLine__CreateCmdLine, &CCommandLine::StaticCreateCmdLine);
}