Properly implement ConCommand callbacks

COMMAND_COMPLETION_MAXITEMS and COMMAND_COMPLETION_ITEM_LENGTH are confirmed to be 128 (original code is 64).
New register func sets the callback bit fields accordingly (no longer hardcoded).
This commit is contained in:
Kawe Mazidjatari 2022-08-04 17:37:30 +02:00
parent ea5f17b4ca
commit 8f513a519f
3 changed files with 91 additions and 38 deletions

View File

@ -85,6 +85,35 @@ dq offset sub_1404701A0
dq offset RegisterConVar; #STR: "Convar '%s' is flagged as both FCVAR_ARCHIVE and FCVAR_ARC
*/
//-----------------------------------------------------------------------------
// Called when a ConCommand needs to execute
//-----------------------------------------------------------------------------
typedef void (*FnCommandCallbackV1_t)(void);
typedef void (*FnCommandCallback_t)(const CCommand& command);
#define COMMAND_COMPLETION_MAXITEMS 128
#define COMMAND_COMPLETION_ITEM_LENGTH 128
//-----------------------------------------------------------------------------
// Returns 0 to COMMAND_COMPLETION_MAXITEMS worth of completion strings
//-----------------------------------------------------------------------------
typedef int (*FnCommandCompletionCallback)(const char* partial, char commands[COMMAND_COMPLETION_MAXITEMS][COMMAND_COMPLETION_ITEM_LENGTH]);
//-----------------------------------------------------------------------------
// Interface version
//-----------------------------------------------------------------------------
class ICommandCallback
{
public:
virtual void CommandCallback(const CCommand& command) = 0;
};
class ICommandCompletionCallback
{
public:
//virtual int CommandCompletionCallback(const char* pPartial, CUtlVector< CUtlString > &commands) = 0;
};
//-----------------------------------------------------------------------------
// Called when a ConVar changes value
// NOTE: For FCVAR_NEVER_AS_STRING ConVars, pOldValue == NULL

View File

@ -268,30 +268,40 @@ void CCommand::Reset()
m_nQueuedVal = cmd_source_t::kCommandSrcInvalid;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
ConCommand::ConCommand()
: m_nNullCallBack(nullptr)
, m_pSubCallback(nullptr)
, m_fnCommandCallbackV1(nullptr)
, m_fnCompletionCallback(nullptr)
, m_bHasCompletionCallback(false)
, m_bUsingNewCommandCallback(false)
, m_bUsingCommandCallbackInterface(false)
{
}
//-----------------------------------------------------------------------------
// Purpose: construct/allocate
//-----------------------------------------------------------------------------
ConCommand::ConCommand(const char* pszName, const char* pszHelpString, int nFlags, void* pCallback, void* pCommandCompletionCallback)
ConCommand::ConCommand(const char* pszName, const char* pszHelpString, int nFlags, FnCommandCallback_t pCallback, FnCommandCompletionCallback pCompletionFunc)
{
ConCommand* pCommand = MemAllocSingleton()->Alloc<ConCommand>(sizeof(ConCommand));
memset(pCommand, '\0', sizeof(ConCommand));
pCommand->m_pConCommandBaseVFTable = g_pConCommandVtable.RCast<IConCommandBase*>();
pCommand->m_pszName = pszName;
pCommand->m_pszHelpString = pszHelpString;
pCommand->m_nFlags = nFlags;
pCommand->m_nNullCallBack = NullSub;
pCommand->m_pCommandCallback = pCallback;
pCommand->m_nCallbackFlags = 2;
if (pCommandCompletionCallback)
{
pCommand->m_pCompletionCallback = pCommandCompletionCallback;
}
else
{
pCommand->m_pCompletionCallback = CallbackStub;
}
ConCommand_RegisterConCommand(pCommand);
pCommand->m_pszName = pszName;
pCommand->m_pszHelpString = pszHelpString;
pCommand->m_nFlags = nFlags;
pCommand->m_nNullCallBack = NullSub;
pCommand->m_fnCommandCallback = pCallback;
pCommand->m_bHasCompletionCallback = pCompletionFunc != nullptr ? true : false;
pCommand->m_bUsingNewCommandCallback = true;
pCommand->m_bUsingCommandCallbackInterface = false;
pCommand->m_fnCompletionCallback = pCompletionFunc ? pCompletionFunc : CallbackStub;
ConCommandBase_Init(pCommand);
*this = *pCommand;
}
@ -358,7 +368,7 @@ void ConCommand::InitShipped(void)
#ifndef DEDICATED
//-------------------------------------------------------------------------
// MATERIAL SYSTEM
g_pCVar->FindCommand("mat_crosshair")->m_pCommandCallback = Mat_CrossHair_f; // Patch callback function to working callback.
g_pCVar->FindCommand("mat_crosshair")->m_fnCommandCallback = Mat_CrossHair_f; // Patch callback function to working callback.
#endif // !DEDICATED
}

View File

@ -1,5 +1,6 @@
#pragma once
#include "tier1/characterset.h"
#include "public/include/iconvar.h"
#include "public/include/iconcommand.h"
//-----------------------------------------------------------------------------
@ -123,19 +124,32 @@ class ConCommand : public ConCommandBase
{
friend class CCVar;
public:
ConCommand(void) {};
ConCommand(const char* szName, const char* szHelpString, int nFlags, void* pCallback, void* pCommandCompletionCallback);
ConCommand(void);
ConCommand(const char* szName, const char* szHelpString, int nFlags, FnCommandCallback_t pCallback, FnCommandCompletionCallback pCommandCompletionCallback);
void Init(void);
void InitShipped(void);
void PurgeShipped(void) const;
bool IsCommand(void) const;
void* m_nNullCallBack {}; //0x0040
char m_nPad48[8] {}; //0x0048
void* m_pCommandCallback {}; //0x0050
void* m_pCompletionCallback{}; //0x0058
int m_nCallbackFlags {}; //0x0060
char m_nPad68[4] {}; //0x0068
void* m_nNullCallBack; //0x0040
void* m_pSubCallback; //0x0048
// Call this function when executing the command
union
{
FnCommandCallbackV1_t m_fnCommandCallbackV1;
FnCommandCallback_t m_fnCommandCallback;
ICommandCallback* m_pCommandCallback;
};
union
{
FnCommandCompletionCallback m_fnCompletionCallback;
ICommandCompletionCallback* m_pCommandCompletionCallback;
};
bool m_bHasCompletionCallback : 1;
bool m_bUsingNewCommandCallback : 1;
bool m_bUsingCommandCallbackInterface : 1;
};
/* ==== COMMAND_BUFFER ================================================================================================================================================== */
@ -146,20 +160,20 @@ inline CMemory p_Cbuf_Execute;
inline auto Cbuf_Execute = p_Cbuf_Execute.RCast<void (*)(void)>();
/* ==== CONCOMMAND ====================================================================================================================================================== */
inline CMemory p_ConCommandBase_Init;
inline auto ConCommandBase_Init = p_ConCommandBase_Init.RCast<void* (*)(ConCommand* pCommand)>();
inline CMemory p_ConCommandBase_IsFlagSet;
inline auto ConCommandBase_IsFlagSet = p_ConCommandBase_IsFlagSet.RCast<bool (*)(ConCommandBase* pCommand, int nFlag)>();
inline CMemory p_ConCommand_CMaterialSystemCmdInit;
inline auto ConCommand_CMaterialSystemCmdInit = p_ConCommand_CMaterialSystemCmdInit.RCast<ConCommand* (*)(void)>();
inline CMemory p_ConCommand_RegisterConCommand;
inline auto ConCommand_RegisterConCommand = p_ConCommand_RegisterConCommand.RCast<void* (*)(ConCommand* pCommand)>();
inline CMemory p_NullSub;
inline auto NullSub = p_NullSub.RCast<void(*)(void)>();
inline CMemory p_CallbackStub;
inline auto CallbackStub = p_CallbackStub.RCast<void* (*)(struct _exception* _exc)>();
inline FnCommandCompletionCallback CallbackStub = p_CallbackStub.RCast<FnCommandCompletionCallback>();
inline CMemory g_pConCommandVtable;
@ -179,9 +193,9 @@ class VConCommand : public IDetour
spdlog::debug("| FUN: Cbuf_AddText : {:#18x} |\n", p_Cbuf_AddText.GetPtr());
spdlog::debug("| FUN: Cbuf_Execute : {:#18x} |\n", p_Cbuf_Execute.GetPtr());
spdlog::debug("+----------------------------------------------------------------+\n");
spdlog::debug("| FUN: ConCommandBase::Init : {:#18x} |\n", p_ConCommandBase_Init.GetPtr());
spdlog::debug("| FUN: ConCommandBase::IsFlagSet : {:#18x} |\n", p_ConCommandBase_IsFlagSet.GetPtr());
spdlog::debug("| FUN: ConCommand::CMaterialSystemCmdInit : {:#18x} |\n", p_ConCommand_CMaterialSystemCmdInit.GetPtr());
spdlog::debug("| FUN: ConCommand::RegisterConCommand : {:#18x} |\n", p_ConCommand_RegisterConCommand.GetPtr());
spdlog::debug("+----------------------------------------------------------------+\n");
spdlog::debug("| FUN: CallbackStub : {:#18x} |\n", p_CallbackStub.GetPtr());
spdlog::debug("| FUN: NullSub : {:#18x} |\n", p_NullSub.GetPtr());
@ -193,19 +207,19 @@ class VConCommand : public IDetour
{
p_Cbuf_AddText = g_mGameDll.FindPatternSIMD(reinterpret_cast<rsig_t>("\x48\x89\x5C\x24\x00\x48\x89\x74\x24\x00\x57\x48\x83\xEC\x20\x48\x63\xD9\x41\x8B\xF8\x48\x8D\x0D\x00\x00\x00\x00\x48\x8B\xF2\xFF\x15\x00\x00\x00\x00\x48\x8D\x05\x00\x00\x00\x00\x41\xB9\x00\x00\x00\x00"), "xxxx?xxxx?xxxxxxxxxxxxxx????xxxxx????xxx????xx????");
p_Cbuf_Execute = g_mGameDll.FindPatternSIMD(reinterpret_cast<rsig_t>("\x48\x89\x5C\x24\x00\x48\x89\x6C\x24\x00\x48\x89\x74\x24\x00\x57\x48\x83\xEC\x20\xFF\x15\x00\x00\x00\x00"), "xxxx?xxxx?xxxx?xxxxxxx????");
p_ConCommandBase_Init = g_mGameDll.FindPatternSIMD(reinterpret_cast<rsig_t>("\x48\x8B\xD1\x48\x8B\x0D\x00\x00\x00\x00\x48\x85\xC9\x74\x06"), "xxxxxx????xxxxx");
p_ConCommandBase_IsFlagSet = g_mGameDll.FindPatternSIMD(reinterpret_cast<rsig_t>("\x85\x51\x38\x0F\x95\xC0\xC3"), "xxxxxxx");
p_ConCommand_CMaterialSystemCmdInit = g_mGameDll.FindPatternSIMD(reinterpret_cast<rsig_t>("\x48\x89\x5C\x24\x00\x48\x89\x74\x24\x00\x48\x89\x7C\x24\x00\x55\x41\x54\x41\x55\x41\x56\x41\x57\x48\x8B\xEC\x48\x83\xEC\x50\x48\x8B\x15\x00\x00\x00\x00"), "xxxx?xxxx?xxxx?xxxxxxxxxxxxxxxxxxx????");
p_ConCommand_RegisterConCommand = g_mGameDll.FindPatternSIMD(reinterpret_cast<rsig_t>("\x48\x8B\xD1\x48\x8B\x0D\x00\x00\x00\x00\x48\x85\xC9\x74\x06"), "xxxxxx????xxxxx");
p_NullSub = g_mGameDll.FindPatternSIMD(reinterpret_cast<rsig_t>("\xC2\x00\x00\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\x40\x53\x48\x83\xEC\x20\x48\x8D\x05\x00\x00\x00\x00"), "xxxxxxxxxxxxxxxxxxxxxxxxx????");
p_CallbackStub = g_mGameDll.FindPatternSIMD(reinterpret_cast<rsig_t>("\x33\xC0\xC3\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\x80\x49\x68\x08"), "xxxxxxxxxxxxxxxxxxxx");
Cbuf_AddText = p_Cbuf_AddText.RCast<void (*)(ECommandTarget_t eTarget, const char* pText, cmd_source_t cmdSource)>(); /*48 89 5C 24 ?? 48 89 74 24 ?? 57 48 83 EC 20 48 63 D9 41 8B F8 48 8D 0D ?? ?? ?? ?? 48 8B F2 FF 15 ?? ?? ?? ?? 48 8D 05 ?? ?? ?? ?? 41 B9 ?? ?? ?? ??*/
Cbuf_Execute = p_Cbuf_Execute.RCast<void (*)(void)>(); /*48 89 5C 24 ?? 48 89 6C 24 ?? 48 89 74 24 ?? 57 48 83 EC 20 FF 15 ?? ?? ?? ??*/
ConCommandBase_IsFlagSet = p_ConCommandBase_IsFlagSet.RCast<bool (*)(ConCommandBase* pCommand, int nFlag)>(); /*85 51 38 0F 95 C0 C3*/
ConCommand_CMaterialSystemCmdInit = p_ConCommand_CMaterialSystemCmdInit.RCast<ConCommand* (*)(void)>(); /*48 89 5C 24 ?? 48 89 74 24 ?? 48 89 7C 24 ?? 55 41 54 41 55 41 56 41 57 48 8B EC 48 83 EC 50 48 8B 15 ?? ?? ?? ??*/
ConCommand_RegisterConCommand = p_ConCommand_RegisterConCommand.RCast<void* (*)(ConCommand* pCommand)>(); /*48 8B D1 48 8B 0D ?? ?? ?? ?? 48 85 C9 74 06*/
NullSub = p_NullSub.RCast<void(*)(void)>(); /*C2 00 00 CC CC CC CC CC CC CC CC CC CC CC CC CC 40 53 48 83 EC 20 48 8D 05 ?? ?? ?? ??*/
CallbackStub = p_CallbackStub.RCast<void* (*)(struct _exception* _exc)>(); /*33 C0 C3 CC CC CC CC CC CC CC CC CC CC CC CC CC 80 49 68 08*/ /*UserMathErrorFunction*/
Cbuf_AddText = p_Cbuf_AddText.RCast<void (*)(ECommandTarget_t, const char*, cmd_source_t)>(); /*48 89 5C 24 ?? 48 89 74 24 ?? 57 48 83 EC 20 48 63 D9 41 8B F8 48 8D 0D ?? ?? ?? ?? 48 8B F2 FF 15 ?? ?? ?? ?? 48 8D 05 ?? ?? ?? ?? 41 B9 ?? ?? ?? ??*/
Cbuf_Execute = p_Cbuf_Execute.RCast<void (*)(void)>(); /*48 89 5C 24 ?? 48 89 6C 24 ?? 48 89 74 24 ?? 57 48 83 EC 20 FF 15 ?? ?? ?? ??*/
ConCommandBase_Init = p_ConCommandBase_Init.RCast<void* (*)(ConCommand*)>(); /*48 8B D1 48 8B 0D ?? ?? ?? ?? 48 85 C9 74 06*/
ConCommandBase_IsFlagSet = p_ConCommandBase_IsFlagSet.RCast<bool (*)(ConCommandBase*, int)>(); /*85 51 38 0F 95 C0 C3*/
ConCommand_CMaterialSystemCmdInit = p_ConCommand_CMaterialSystemCmdInit.RCast<ConCommand* (*)(void)>(); /*48 89 5C 24 ?? 48 89 74 24 ?? 48 89 7C 24 ?? 55 41 54 41 55 41 56 41 57 48 8B EC 48 83 EC 50 48 8B 15 ?? ?? ?? ??*/
NullSub = p_NullSub.RCast<void(*)(void)>(); /*C2 00 00 CC CC CC CC CC CC CC CC CC CC CC CC CC 40 53 48 83 EC 20 48 8D 05 ?? ?? ?? ??*/
CallbackStub = p_CallbackStub.RCast<FnCommandCompletionCallback>(); /*33 C0 C3 CC CC CC CC CC CC CC CC CC CC CC CC CC 80 49 68 08*/ /*UserMathErrorFunction*/
}
virtual void GetVar(void) const
{