From 0146f22e0a8bcd7ec137bb205d117cea49f533dc Mon Sep 17 00:00:00 2001
From: Amos <48657826+Mauler125@users.noreply.github.com>
Date: Mon, 10 Jan 2022 01:20:44 +0100
Subject: [PATCH] Implement CommandLine utilities
---
r5dev/core/init.cpp | 2 +-
r5dev/dedicated.vcxproj | 2 +
r5dev/dedicated.vcxproj.filters | 6 ++
r5dev/r5dev.vcxproj | 2 +
r5dev/r5dev.vcxproj.filters | 6 ++
r5dev/tier0/commandline.cpp | 132 ++++++++++++++++++++++++++++++++
r5dev/tier0/commandline.h | 34 ++++++++
r5dev/tier0/cvar.h | 15 ++--
8 files changed, 192 insertions(+), 7 deletions(-)
create mode 100644 r5dev/tier0/commandline.cpp
create mode 100644 r5dev/tier0/commandline.h
diff --git a/r5dev/core/init.cpp b/r5dev/core/init.cpp
index 8e582944..4679ab0a 100644
--- a/r5dev/core/init.cpp
+++ b/r5dev/core/init.cpp
@@ -6,6 +6,7 @@
#include "core/stdafx.h"
#include "core/init.h"
+#include "tier0/commandline.h"
#include "tier0/ConCommand.h"
#include "tier0/completion.h"
#include "tier0/cvar.h"
@@ -119,7 +120,6 @@ void Systems_Init()
#ifdef DEDICATED
Dedicated_Init();
#endif // DEDICATED
-
}
//////////////////////////////////////////////////////////////////////////
diff --git a/r5dev/dedicated.vcxproj b/r5dev/dedicated.vcxproj
index b3aef01a..49eb0733 100644
--- a/r5dev/dedicated.vcxproj
+++ b/r5dev/dedicated.vcxproj
@@ -339,6 +339,7 @@
+
@@ -485,6 +486,7 @@
NotUsing
NotUsing
+
diff --git a/r5dev/dedicated.vcxproj.filters b/r5dev/dedicated.vcxproj.filters
index 11d9800b..8e9448d3 100644
--- a/r5dev/dedicated.vcxproj.filters
+++ b/r5dev/dedicated.vcxproj.filters
@@ -660,6 +660,9 @@
sdk\mathlib
+
+ sdk\tier0
+
@@ -863,6 +866,9 @@
sdk\mathlib
+
+ sdk\tier0
+
diff --git a/r5dev/r5dev.vcxproj b/r5dev/r5dev.vcxproj
index a808b972..38f74ed2 100644
--- a/r5dev/r5dev.vcxproj
+++ b/r5dev/r5dev.vcxproj
@@ -191,6 +191,7 @@
NotUsing
NotUsing
+
@@ -401,6 +402,7 @@
+
diff --git a/r5dev/r5dev.vcxproj.filters b/r5dev/r5dev.vcxproj.filters
index dc8d19c2..3dcd9cbc 100644
--- a/r5dev/r5dev.vcxproj.filters
+++ b/r5dev/r5dev.vcxproj.filters
@@ -399,6 +399,9 @@
sdk\mathlib
+
+ sdk\tier0
+
@@ -1034,6 +1037,9 @@
sdk\mathlib
+
+ sdk\tier0
+
diff --git a/r5dev/tier0/commandline.cpp b/r5dev/tier0/commandline.cpp
new file mode 100644
index 00000000..ebc2cfe6
--- /dev/null
+++ b/r5dev/tier0/commandline.cpp
@@ -0,0 +1,132 @@
+//=============================================================================//
+//
+// Purpose: Command line utilities
+//
+//=============================================================================//
+
+#include "core/stdafx.h"
+#include "tier0/cvar.h"
+#include "tier0/commandline.h"
+
+//-----------------------------------------------------------------------------
+// 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::CreateCmdLine(const char* pszCommandline)
+{
+ using OriginalFn = void(__thiscall*)(CCommandLine*, const char*);
+ (*reinterpret_cast(this))[0](this, pszCommandline);
+}
+
+//-----------------------------------------------------------------------------
+// purpose: creates a command line from the arguments passed in
+//-----------------------------------------------------------------------------
+void CCommandLine::CreateCmdLine(int argc, char** argv)
+{
+ using OriginalFn = void(__thiscall*)(CCommandLine*, int, char**);
+ return (*reinterpret_cast(this))[1](this, argc, argv);
+}
+
+//-----------------------------------------------------------------------------
+// purpose: allocates a pool for the command line [seems unused]
+//-----------------------------------------------------------------------------
+void CCommandLine::CreatePool(void* pMem)
+{
+ using OriginalFn = void(__thiscall*)(CCommandLine*, void*);
+ (*reinterpret_cast(this))[2](this, pMem);
+}
+
+//-----------------------------------------------------------------------------
+// Purpose: Return current command line
+// Output : const char
+//-----------------------------------------------------------------------------
+const char* CCommandLine::GetCmdLine(void)
+{
+ using OriginalFn = const char* (__thiscall*)(CCommandLine*);
+ return (*reinterpret_cast(this))[3](this);
+}
+
+//-----------------------------------------------------------------------------
+// Purpose: Search for the parameter in the current commandline
+// Input : *psz -
+// **ppszValue -
+// Output : char
+//-----------------------------------------------------------------------------
+const char* CCommandLine::CheckParm(const char* psz, const char** ppszValue)
+{
+ using OriginalFn = const char* (__thiscall*)(CCommandLine*, const char*, const char**);
+ return (*reinterpret_cast(this))[4](this, psz, ppszValue);
+}
+
+//-----------------------------------------------------------------------------
+// Purpose: Remove specified string ( and any args attached to it ) from command line
+// Input : *pszParm -
+//-----------------------------------------------------------------------------
+void CCommandLine::RemoveParm(void)
+{
+ using OriginalFn = void(__thiscall*)(CCommandLine*);
+ return (*reinterpret_cast(this))[5](this);
+}
+
+//-----------------------------------------------------------------------------
+// Purpose: Append parameter and argument values to command line
+// Input : *pszParm -
+// *pszValues -
+//-----------------------------------------------------------------------------
+void CCommandLine::AppendParm(const char* pszParm, const char* pszValues)
+{
+ using OriginalFn = void(__thiscall*)(CCommandLine*, const char*, const char*);
+ return (*reinterpret_cast(this))[6](this, pszParm, pszParm);
+}
+
+//-----------------------------------------------------------------------------
+// purpose: returns the argument after the one specified, or the default if not found
+//-----------------------------------------------------------------------------
+const char* CCommandLine::ParmValue(const char* psz, const char* pDefaultVal)
+{
+ using OriginalFn = const char* (__thiscall*)(CCommandLine*, const char*, const char*);
+ return (*reinterpret_cast(this))[7](this, psz, pDefaultVal);
+}
+
+int CCommandLine::ParmValue(const char* psz, int nDefaultVal)
+{
+ using OriginalFn = int(__thiscall*)(CCommandLine*, const char*, int);
+ return (*reinterpret_cast(this))[8](this, psz, nDefaultVal);
+}
+
+float CCommandLine::ParmValue(const char* psz, float flDefaultVal)
+{
+ using OriginalFn = float(__thiscall*)(CCommandLine*, const char*, float);
+ return (*reinterpret_cast(this))[9](this, psz, flDefaultVal);
+}
+
+//-----------------------------------------------------------------------------
+// purpose: returns individual command line arguments
+//-----------------------------------------------------------------------------
+int CCommandLine::ParmCount(void)
+{
+ using OriginalFn = int(__thiscall*)(CCommandLine*);
+ return (*reinterpret_cast(this))[10](this);
+}
+
+int CCommandLine::FindParm(const char* psz)
+{
+ using OriginalFn = int(__thiscall*)(CCommandLine*, const char*);
+ return (*reinterpret_cast(this))[11](this, psz);
+}
+
+const char* CCommandLine::GetParm(int nIndex)
+{
+ using OriginalFn = const char* (__thiscall*)(CCommandLine*, int);
+ return (*reinterpret_cast(this))[12](this, nIndex);
+}
+
+void CCommandLine::SetParm(int nIndex, char const* pParm)
+{
+ using OriginalFn = void(__thiscall*)(CCommandLine*, int, const char*);
+ return (*reinterpret_cast(this))[13](this, nIndex, pParm);
+}
+
+///////////////////////////////////////////////////////////////////////////////
+CCommandLine* g_pCmdLine = reinterpret_cast(p_CCVar_GetCommandLineValue.FindPatternSelf("48 8D 0D", ADDRESS::Direction::DOWN, 250).ResolveRelativeAddressSelf(0x3, 0x7).GetPtr());
diff --git a/r5dev/tier0/commandline.h b/r5dev/tier0/commandline.h
new file mode 100644
index 00000000..aada769e
--- /dev/null
+++ b/r5dev/tier0/commandline.h
@@ -0,0 +1,34 @@
+#pragma once
+
+class CCommandLine // VTABLE @0x141369C78 in R5pc_r5launch_N1094_CL456479_2019_10_30_05_20_PM
+{
+public:
+ void CreateCmdLine(const char* pszCommandline);
+ void CreateCmdLine(int argc, char** argv);
+ void CreatePool(void* pMem);
+ const char* GetCmdLine(void);
+ const char* CheckParm(const char* psz, const char** ppszValue);
+ void RemoveParm(void);
+ void AppendParm(const char* pszParm, const char* pszValues);
+ const char* ParmValue(const char* psz, const char* pDefaultVal);
+ int ParmValue(const char* psz, int nDefaultVal);
+ float ParmValue(const char* psz, float flDefaultVal);
+ int ParmCount(void);
+ int FindParm(const char* psz);
+ const char* GetParm(int nIndex);
+ void SetParm(int nIndex, char const* pParm);
+};
+extern CCommandLine* g_pCmdLine;
+
+///////////////////////////////////////////////////////////////////////////////
+class HCommandLine : public IDetour
+{
+ virtual void debugp()
+ {
+ std::cout << "| VAR: g_pCmdLine : 0x" << std::hex << std::uppercase << g_pCmdLine << std::setw(0) << " |" << std::endl;
+ std::cout << "+----------------------------------------------------------------+" << std::endl;
+ }
+};
+///////////////////////////////////////////////////////////////////////////////
+
+REGISTER(HCommandLine);
diff --git a/r5dev/tier0/cvar.h b/r5dev/tier0/cvar.h
index 53da011a..b1f052c9 100644
--- a/r5dev/tier0/cvar.h
+++ b/r5dev/tier0/cvar.h
@@ -6,12 +6,14 @@ namespace
{
/* ==== CCVAR =========================================================================================================================================================== */
#if defined (GAMEDLL_S0) || defined (GAMEDLL_S1)
- ADDRESS p_CCvar_Disconnect = g_mGameDll.FindPatternSIMD((std::uint8_t*)"\x40\x57\x41\x56\x48\x83\xEC\x38\x4C\x8B\x35", "xxxxxxxxxxx");
- void* (*CCvar_Disconnect)() = (void* (*)())p_CCvar_Disconnect.GetPtr(); /*40 57 41 56 48 83 EC 38 4C 8B 35 ? ? ? ?*/
+ ADDRESS p_CCVar_Disconnect = g_mGameDll.FindPatternSIMD((std::uint8_t*)"\x40\x57\x41\x56\x48\x83\xEC\x38\x4C\x8B\x35", "xxxxxxxxxxx");
+ void* (*CCVar_Disconnect)() = (void* (*)())p_CCVar_Disconnect.GetPtr(); /*40 57 41 56 48 83 EC 38 4C 8B 35 ? ? ? ?*/
#elif defined (GAMEDLL_S2) || defined (GAMEDLL_S3)
- ADDRESS p_CCvar_Disconnect = g_mGameDll.FindPatternSIMD((std::uint8_t*)"\x48\x83\xEC\x28\x48\x8B\x0D\x00\x00\x00\x00\x48\x85\xC9\x74\x26\x80\x3D\x00\x00\x00\x00\x00\x74\x1D\x48\x8B\x01\x8B\x15\x00\x00\x00\x00\xFF\x50\x58\xC7\x05\x00\x00\x00\x00\x00\x00\x00\x00\xC6\x05\x00\x00\x00\x00\x00\x48\xC7\x05\x00\x00\x00", "xxxxxxx????xxxxxxx?????xxxxxxx????xxxxx????????xx");
- void* (*CCvar_Disconnect)() = (void* (*)())p_CCvar_Disconnect.GetPtr(); /*48 83 EC 28 48 8B 0D ? ? ? ? 48 85 C9 74 26 80 3D ? ? ? ? ? 74 1D 48 8B 01 8B 15 ? ? ? ? FF 50 58 C7 05 ? ? ? ? ? ? ? ? C6 05 ? ? ? ? ? 48 C7 05 ? ? ? ? ? ? ? ?*/
+ ADDRESS p_CCVar_Disconnect = g_mGameDll.FindPatternSIMD((std::uint8_t*)"\x48\x83\xEC\x28\x48\x8B\x0D\x00\x00\x00\x00\x48\x85\xC9\x74\x26\x80\x3D\x00\x00\x00\x00\x00\x74\x1D\x48\x8B\x01\x8B\x15\x00\x00\x00\x00\xFF\x50\x58\xC7\x05\x00\x00\x00\x00\x00\x00\x00\x00\xC6\x05\x00\x00\x00\x00\x00\x48\xC7\x05\x00\x00\x00", "xxxxxxx????xxxxxxx?????xxxxxxx????xxxxx????????xx");
+ void* (*CCVar_Disconnect)() = (void* (*)())p_CCVar_Disconnect.GetPtr(); /*48 83 EC 28 48 8B 0D ? ? ? ? 48 85 C9 74 26 80 3D ? ? ? ? ? 74 1D 48 8B 01 8B 15 ? ? ? ? FF 50 58 C7 05 ? ? ? ? ? ? ? ? C6 05 ? ? ? ? ? 48 C7 05 ? ? ? ? ? ? ? ?*/
#endif
+ ADDRESS p_CCVar_GetCommandLineValue = g_mGameDll.FindPatternSIMD((std::uint8_t*)"\x40\x55\x48\x83\xEC\x20\x48\x8D\x6C\x24\x00\x48\x89\x5D\x10\x49\xC7\xC0\x00\x00\x00\x00", "xxxxxxxxxx?xxxxxxx????");
+ const char* (*CCVar_GetCommandLineValue)(void* thisptr, const char* pVariableName) = (const char* (*)(void*, const char*))p_CCVar_GetCommandLineValue.GetPtr(); /*40 55 48 83 EC 20 48 8D 6C 24 ? 48 89 5D 10 49 C7 C0 ? ? ? ?*/
}
//-------------------------------------------------------------------------
@@ -88,8 +90,9 @@ class HCvar : public IDetour
{
virtual void debugp()
{
- std::cout << "| FUN: CCvar::Disconnect : 0x" << std::hex << std::uppercase << p_CCvar_Disconnect.GetPtr() << std::setw(npad) << " |" << std::endl;
- std::cout << "| VAR: g_pCvar : 0x" << std::hex << std::uppercase << g_pCvar << std::setw(0) << " |" << std::endl;
+ std::cout << "| FUN: CCVar::Disconnect : 0x" << std::hex << std::uppercase << p_CCVar_Disconnect.GetPtr() << std::setw(npad) << " |" << std::endl;
+ std::cout << "| FUN: CCVar::GetCommandLineValue : 0x" << std::hex << std::uppercase << p_CCVar_GetCommandLineValue.GetPtr() << std::setw(npad) << " |" << std::endl;
+ std::cout << "| VAR: g_pCvar : 0x" << std::hex << std::uppercase << g_pCvar << std::setw(0) << " |" << std::endl;
std::cout << "+----------------------------------------------------------------+" << std::endl;
}
};