Move console hook management to own implementation

+ Function renaming
This commit is contained in:
Amos 2021-06-19 12:05:12 -07:00
parent 48766c23d9
commit 7495f1b4fa
7 changed files with 102 additions and 72 deletions

View File

@ -3,11 +3,12 @@
/////////////////////////////////////////////////////////////////////////////
// Initialization
void SetupConsole();
void RemoveCMHooks();
/////////////////////////////////////////////////////////////////////////////
// Hooks
bool Hook_ConVar_IsFlagSet(int** cvar, int flag);
bool Hook_ConCommand_IsFlagSet(int* cmd, int flag);
bool HConVar_IsFlagSet(int** cvar, int flag);
bool HConCommand_IsFlagSet(int* cmd, int flag);
/////////////////////////////////////////////////////////////////////////////
// Globals

View File

@ -2,8 +2,8 @@
/////////////////////////////////////////////////////////////////////////////
// Initialization
void InstallHooks();
void RemoveHooks();
void InstallENHooks();
void RemoveENHooks();
void ToggleDevCommands();
void ToggleNetHooks();

View File

@ -67,7 +67,7 @@ void SetupConsole()
//---------------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////////
bool Hook_ConVar_IsFlagSet(int** cvar, int flag)
bool HConVar_IsFlagSet(int** cvar, int flag)
{
int real_flags = *(*(cvar + (72 / (sizeof(void*)))) + (56 / sizeof(int)));
if (g_bDebugConsole)
@ -89,7 +89,7 @@ bool Hook_ConVar_IsFlagSet(int** cvar, int flag)
else { return false; }
}
bool Hook_ConCommand_IsFlagSet(int* cmd, int flag)
bool HConCommand_IsFlagSet(int* cmd, int flag)
{
int real_flags = *((cmd + (56 / sizeof(int))));
if (g_bDebugConsole)
@ -155,3 +155,69 @@ DWORD __stdcall ProcessConsoleWorker(LPVOID)
return 0;
}
///////////////////////////////////////////////////////////////////////////////////
//---------------------------------------------------------------------------------
// Management
//---------------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////////
void RemoveCMHooks()
{
///////////////////////////////////////////////////////////////////////////////
// Begin the detour transaction, to unhook the the process
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
///////////////////////////////////////////////////////////////////////////////
// Unhook Console functions
DetourDetach((LPVOID*)&ConVar_IsFlagSet, &HConVar_IsFlagSet);
DetourDetach((LPVOID*)&ConCommand_IsFlagSet, &HConCommand_IsFlagSet);
///////////////////////////////////////////////////////////////////////////////
// Commit the transaction
DetourTransactionCommit();
}
///////////////////////////////////////////////////////////////////////////////////
//---------------------------------------------------------------------------------
// Toggles
//---------------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////////
void ToggleDevCommands()
{
static bool g_dev = false;
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
if (!g_dev)
{
DetourAttach((LPVOID*)&ConVar_IsFlagSet, &HConVar_IsFlagSet);
DetourAttach((LPVOID*)&ConCommand_IsFlagSet, &HConCommand_IsFlagSet);
printf("\n");
printf("+--------------------------------------------------------+\n");
printf("|>>>>>>>>>>>>>| DEVONLY COMMANDS ACTIVATED |<<<<<<<<<<<<<|\n");
printf("+--------------------------------------------------------+\n");
printf("\n");
}
else
{
DetourDetach((LPVOID*)&ConVar_IsFlagSet, &HConVar_IsFlagSet);
DetourDetach((LPVOID*)&ConCommand_IsFlagSet, &HConCommand_IsFlagSet);
printf("\n");
printf("+--------------------------------------------------------+\n");
printf("|>>>>>>>>>>>>| DEVONLY COMMANDS DEACTIVATED |<<<<<<<<<<<<|\n");
printf("+--------------------------------------------------------+\n");
printf("\n");
}
if (DetourTransactionCommit() != NO_ERROR)
{
TerminateProcess(GetCurrentProcess(), 0xBAD0C0DE);
}
g_dev = !g_dev;
}

View File

@ -18,7 +18,7 @@
void InitializeR5Dev()
{
SetupConsole();
InstallHooks();
InstallENHooks();
InstallIPHooks();
InstallDXHooks();
InstallOpcodes();
@ -31,7 +31,8 @@ void InitializeR5Dev()
void TerminateR5Dev()
{
RemoveHooks();
RemoveCMHooks();
RemoveENHooks();
RemoveIPHooks();
RemoveDXHooks();
FreeConsole();

View File

@ -7,7 +7,6 @@
#include "patterns.h"
#include "utility.h"
#include "structs.h"
#include "console.h"
#include "overlay.h"
#include "hooks.h"
@ -17,7 +16,7 @@
//---------------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////////
bool Hook_NET_ReceiveDatagram(int sock, void* inpacket, bool raw)
bool HNET_ReceiveDatagram(int sock, void* inpacket, bool raw)
{
bool result = NET_ReceiveDatagram(sock, inpacket, raw);
if (result)
@ -33,7 +32,7 @@ bool Hook_NET_ReceiveDatagram(int sock, void* inpacket, bool raw)
return result;
}
unsigned int Hook_NET_SendDatagram(SOCKET s, const char* buf, int len, int flags)
unsigned int HNET_SendDatagram(SOCKET s, const char* buf, int len, int flags)
{
unsigned int result = NET_SendDatagram(s, buf, len, flags);
if (result)
@ -52,7 +51,7 @@ unsigned int Hook_NET_SendDatagram(SOCKET s, const char* buf, int len, int flags
//---------------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////////
void* Hook_SQVM_Print(void* sqvm, char* fmt, ...)
void* HSQVM_Print(void* sqvm, char* fmt, ...)
{
char buf[1024];
va_list args;
@ -65,7 +64,7 @@ void* Hook_SQVM_Print(void* sqvm, char* fmt, ...)
return NULL;
}
__int64 Hook_SQVM_LoadRson(const char* rson_name)
__int64 HSQVM_LoadRson(const char* rson_name)
{
printf("\n");
printf("##################################################\n");
@ -75,7 +74,7 @@ __int64 Hook_SQVM_LoadRson(const char* rson_name)
return SQVM_LoadRson(rson_name);
}
bool Hook_SQVM_LoadScript(void* sqvm, const char* script_path, const char* script_name, int flag)
bool HSQVM_LoadScript(void* sqvm, const char* script_path, const char* script_name, int flag)
{
char filepath[MAX_PATH] = { 0 };
sprintf_s(filepath, MAX_PATH, "platform\\%s", script_path);
@ -112,17 +111,19 @@ bool Hook_SQVM_LoadScript(void* sqvm, const char* script_path, const char* scrip
//---------------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////////
void InstallHooks()
void InstallENHooks()
{
///////////////////////////////////////////////////////////////////////////////
// Begin the detour transaction
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
///////////////////////////////////////////////////////////////////////////////
// Hook Engine functions
DetourAttach((LPVOID*)&SQVM_Print, &Hook_SQVM_Print);
DetourAttach((LPVOID*)&SQVM_LoadRson, &Hook_SQVM_LoadRson);
DetourAttach((LPVOID*)&SQVM_LoadScript, &Hook_SQVM_LoadScript);
DetourAttach((LPVOID*)&SQVM_Print, &HSQVM_Print);
DetourAttach((LPVOID*)&SQVM_LoadRson, &HSQVM_LoadRson);
DetourAttach((LPVOID*)&SQVM_LoadScript, &HSQVM_LoadScript);
///////////////////////////////////////////////////////////////////////////////
// Commit the transaction
if (DetourTransactionCommit() != NO_ERROR)
@ -132,25 +133,24 @@ void InstallHooks()
}
}
void RemoveHooks()
void RemoveENHooks()
{
///////////////////////////////////////////////////////////////////////////////
// Begin the detour transaction, to unhook the the process
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
///////////////////////////////////////////////////////////////////////////////
// Unhook Squirrel functions
DetourDetach((LPVOID*)&SQVM_Print, &Hook_SQVM_Print);
DetourDetach((LPVOID*)&SQVM_LoadRson, &Hook_SQVM_LoadRson);
DetourDetach((LPVOID*)&SQVM_LoadScript, &Hook_SQVM_LoadScript);
DetourDetach((LPVOID*)&SQVM_Print, &HSQVM_Print);
DetourDetach((LPVOID*)&SQVM_LoadRson, &HSQVM_LoadRson);
DetourDetach((LPVOID*)&SQVM_LoadScript, &HSQVM_LoadScript);
///////////////////////////////////////////////////////////////////////////////
// Unhook Netchan functions
DetourDetach((LPVOID*)&NET_SendDatagram, &Hook_NET_SendDatagram);
DetourDetach((LPVOID*)&NET_ReceiveDatagram, &Hook_NET_ReceiveDatagram);
///////////////////////////////////////////////////////////////////////////////
// Unhook Console functions
DetourDetach((LPVOID*)&ConVar_IsFlagSet, &Hook_ConVar_IsFlagSet);
DetourDetach((LPVOID*)&ConCommand_IsFlagSet, &Hook_ConCommand_IsFlagSet);
DetourDetach((LPVOID*)&NET_SendDatagram, &HNET_SendDatagram);
DetourDetach((LPVOID*)&NET_ReceiveDatagram, &HNET_ReceiveDatagram);
///////////////////////////////////////////////////////////////////////////////
// Commit the transaction
DetourTransactionCommit();
@ -171,8 +171,8 @@ void ToggleNetHooks()
if (!g_net)
{
DetourAttach((LPVOID*)&NET_SendDatagram, &Hook_NET_SendDatagram);
DetourAttach((LPVOID*)&NET_ReceiveDatagram, &Hook_NET_ReceiveDatagram);
DetourAttach((LPVOID*)&NET_SendDatagram, &HNET_SendDatagram);
DetourAttach((LPVOID*)&NET_ReceiveDatagram, &HNET_ReceiveDatagram);
printf("\n");
printf("+--------------------------------------------------------+\n");
printf("|>>>>>>>>>>>>>| NETCHANNEL TRACE ACTIVATED |<<<<<<<<<<<<<|\n");
@ -181,8 +181,8 @@ void ToggleNetHooks()
}
else
{
DetourDetach((LPVOID*)&NET_SendDatagram, &Hook_NET_SendDatagram);
DetourDetach((LPVOID*)&NET_ReceiveDatagram, &Hook_NET_ReceiveDatagram);
DetourDetach((LPVOID*)&NET_SendDatagram, &HNET_SendDatagram);
DetourDetach((LPVOID*)&NET_ReceiveDatagram, &HNET_ReceiveDatagram);
printf("\n");
printf("+--------------------------------------------------------+\n");
printf("|>>>>>>>>>>>>| NETCHANNEL TRACE DEACTIVATED |<<<<<<<<<<<<|\n");
@ -197,40 +197,3 @@ void ToggleNetHooks()
g_net = !g_net;
}
void ToggleDevCommands()
{
static bool g_dev = false;
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
if (!g_dev)
{
DetourAttach((LPVOID*)&ConVar_IsFlagSet, &Hook_ConVar_IsFlagSet);
DetourAttach((LPVOID*)&ConCommand_IsFlagSet, &Hook_ConCommand_IsFlagSet);
printf("\n");
printf("+--------------------------------------------------------+\n");
printf("|>>>>>>>>>>>>>| DEVONLY COMMANDS ACTIVATED |<<<<<<<<<<<<<|\n");
printf("+--------------------------------------------------------+\n");
printf("\n");
}
else
{
DetourDetach((LPVOID*)&ConVar_IsFlagSet, &Hook_ConVar_IsFlagSet);
DetourDetach((LPVOID*)&ConCommand_IsFlagSet, &Hook_ConCommand_IsFlagSet);
printf("\n");
printf("+--------------------------------------------------------+\n");
printf("|>>>>>>>>>>>>| DEVONLY COMMANDS DEACTIVATED |<<<<<<<<<<<<|\n");
printf("+--------------------------------------------------------+\n");
printf("\n");
}
if (DetourTransactionCommit() != NO_ERROR)
{
TerminateProcess(GetCurrentProcess(), 0xBAD0C0DE);
}
g_dev = !g_dev;
}

View File

@ -61,7 +61,7 @@ LRESULT CALLBACK DXGIMsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
LRESULT CALLBACK hWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
LRESULT CALLBACK HWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_KEYDOWN)
{
@ -370,7 +370,7 @@ HRESULT __stdcall Present(IDXGISwapChain* pSwapChain, UINT nSyncInterval, UINT n
if (g_oWndProc == nullptr)
{ // Only initialize hWndProc pointer once to avoid stack overflow during ResizeBuffers(..)
g_oWndProc = (WNDPROC)SetWindowLongPtr(g_hGameWindow, GWLP_WNDPROC, (LONG_PTR)hWndProc);
g_oWndProc = (WNDPROC)SetWindowLongPtr(g_hGameWindow, GWLP_WNDPROC, (LONG_PTR)HWndProc);
}
g_bInitialized = true;

View File

@ -369,7 +369,6 @@ public:
for (int i = first > 0 ? first : 0; i < History.Size; i++) { AddLog("%3d: %s\n", i, History[i]); }
}
// On command input, we scroll to bottom even if AutoScroll==false
ScrollToBottom = true;
}