From f3eb93b83ad3a9ea6351a71e29a71a48c5e4a030 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Tue, 9 Aug 2022 17:26:57 +0200 Subject: [PATCH] Extend `IConsole::PossibleCommands`, add index to callback `PossibleCommands` now passes the item index to the callback and returns the total number of items. Add `EmptyPossibleCommandCallback` as default parameter. --- src/engine/console.h | 6 ++++-- src/engine/shared/console.cpp | 9 +++++++-- src/engine/shared/console.h | 2 +- src/game/client/components/console.cpp | 16 ++++++---------- src/game/client/components/console.h | 5 ++--- 5 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/engine/console.h b/src/engine/console.h index 1a7e41d6d..c7cbe15d4 100644 --- a/src/engine/console.h +++ b/src/engine/console.h @@ -82,14 +82,16 @@ public: typedef void (*FTeeHistorianCommandCallback)(int ClientID, int FlagMask, const char *pCmd, IResult *pResult, void *pUser); typedef void (*FPrintCallback)(const char *pStr, void *pUser, ColorRGBA PrintColor); - typedef void (*FPossibleCallback)(const char *pCmd, void *pUser); + typedef void (*FPossibleCallback)(int Index, const char *pCmd, void *pUser); typedef void (*FCommandCallback)(IResult *pResult, void *pUserData); typedef void (*FChainCommandCallback)(IResult *pResult, void *pUserData, FCommandCallback pfnCallback, void *pCallbackUserData); + static void EmptyPossibleCommandCallback(int Index, const char *pCmd, void *pUser) {} + virtual void Init() = 0; virtual const CCommandInfo *FirstCommandInfo(int AccessLevel, int Flagmask) const = 0; virtual const CCommandInfo *GetCommandInfo(const char *pName, int FlagMask, bool Temp) = 0; - virtual void PossibleCommands(const char *pStr, int FlagMask, bool Temp, FPossibleCallback pfnCallback, void *pUser) = 0; + virtual int PossibleCommands(const char *pStr, int FlagMask, bool Temp, FPossibleCallback pfnCallback = EmptyPossibleCommandCallback, void *pUser = nullptr) = 0; virtual void ParseArguments(int NumArgs, const char **ppArguments) = 0; virtual void Register(const char *pName, const char *pParams, int Flags, FCommandCallback pfnFunc, void *pUser, const char *pHelp) = 0; diff --git a/src/engine/shared/console.cpp b/src/engine/shared/console.cpp index ff7dd27f0..618f2f27b 100644 --- a/src/engine/shared/console.cpp +++ b/src/engine/shared/console.cpp @@ -551,16 +551,21 @@ void CConsole::ExecuteLineStroked(int Stroke, const char *pStr, int ClientID, bo } } -void CConsole::PossibleCommands(const char *pStr, int FlagMask, bool Temp, FPossibleCallback pfnCallback, void *pUser) +int CConsole::PossibleCommands(const char *pStr, int FlagMask, bool Temp, FPossibleCallback pfnCallback, void *pUser) { + int Index = 0; for(CCommand *pCommand = m_pFirstCommand; pCommand; pCommand = pCommand->m_pNext) { if(pCommand->m_Flags & FlagMask && pCommand->m_Temp == Temp) { if(str_find_nocase(pCommand->m_pName, pStr)) - pfnCallback(pCommand->m_pName, pUser); + { + pfnCallback(Index, pCommand->m_pName, pUser); + Index++; + } } } + return Index; } CConsole::CCommand *CConsole::FindCommand(const char *pName, int FlagMask) diff --git a/src/engine/shared/console.h b/src/engine/shared/console.h index 47fd4b3f0..45ce408d6 100644 --- a/src/engine/shared/console.h +++ b/src/engine/shared/console.h @@ -196,7 +196,7 @@ public: void Init() override; const CCommandInfo *FirstCommandInfo(int AccessLevel, int FlagMask) const override; const CCommandInfo *GetCommandInfo(const char *pName, int FlagMask, bool Temp) override; - void PossibleCommands(const char *pStr, int FlagMask, bool Temp, FPossibleCallback pfnCallback, void *pUser) override; + int PossibleCommands(const char *pStr, int FlagMask, bool Temp, FPossibleCallback pfnCallback, void *pUser) override; void ParseArguments(int NumArgs, const char **ppArguments) override; void Register(const char *pName, const char *pParams, int Flags, FCommandCallback pfnFunc, void *pUser, const char *pHelp) override; diff --git a/src/game/client/components/console.cpp b/src/game/client/components/console.cpp index 70d77024b..69d1c426c 100644 --- a/src/game/client/components/console.cpp +++ b/src/game/client/components/console.cpp @@ -161,12 +161,11 @@ void CGameConsole::CInstance::ExecuteLine(const char *pLine) } } -void CGameConsole::CInstance::PossibleCommandsCompleteCallback(const char *pStr, void *pUser) +void CGameConsole::CInstance::PossibleCommandsCompleteCallback(int Index, const char *pStr, void *pUser) { CGameConsole::CInstance *pInstance = (CGameConsole::CInstance *)pUser; - if(pInstance->m_CompletionChosen == pInstance->m_CompletionEnumerationCount) + if(pInstance->m_CompletionChosen == Index) pInstance->m_Input.Set(pStr); - pInstance->m_CompletionEnumerationCount++; } void CGameConsole::CInstance::OnInput(IInput::CEvent Event) @@ -335,16 +334,13 @@ void CGameConsole::CInstance::OnInput(IInput::CEvent Event) m_CompletionChosen--; else if(!m_ReverseTAB) m_CompletionChosen++; - m_CompletionEnumerationCount = 0; - m_pGameConsole->m_pConsole->PossibleCommands(m_aCompletionBuffer, m_CompletionFlagmask, m_Type != CGameConsole::CONSOLETYPE_LOCAL && m_pGameConsole->Client()->RconAuthed() && m_pGameConsole->Client()->UseTempRconCommands(), PossibleCommandsCompleteCallback, this); - + int CompletionEnumerationCount = m_pGameConsole->m_pConsole->PossibleCommands(m_aCompletionBuffer, m_CompletionFlagmask, m_Type != CGameConsole::CONSOLETYPE_LOCAL && m_pGameConsole->Client()->RconAuthed() && m_pGameConsole->Client()->UseTempRconCommands(), PossibleCommandsCompleteCallback, this); m_CompletionUsed = true; // handle wrapping - if(m_CompletionEnumerationCount && (m_CompletionChosen >= m_CompletionEnumerationCount || m_CompletionChosen < 0)) + if(CompletionEnumerationCount && (m_CompletionChosen >= CompletionEnumerationCount || m_CompletionChosen < 0)) { - m_CompletionChosen = (m_CompletionChosen + m_CompletionEnumerationCount) % m_CompletionEnumerationCount; - m_CompletionEnumerationCount = 0; + m_CompletionChosen = (m_CompletionChosen + CompletionEnumerationCount) % CompletionEnumerationCount; m_pGameConsole->m_pConsole->PossibleCommands(m_aCompletionBuffer, m_CompletionFlagmask, m_Type != CGameConsole::CONSOLETYPE_LOCAL && m_pGameConsole->Client()->RconAuthed() && m_pGameConsole->Client()->UseTempRconCommands(), PossibleCommandsCompleteCallback, this); } } @@ -487,7 +483,7 @@ struct CRenderInfo float m_Width; }; -void CGameConsole::PossibleCommandsRenderCallback(const char *pStr, void *pUser) +void CGameConsole::PossibleCommandsRenderCallback(int Index, const char *pStr, void *pUser) { CRenderInfo *pInfo = static_cast(pUser); diff --git a/src/game/client/components/console.h b/src/game/client/components/console.h index 248c19543..e658b0254 100644 --- a/src/game/client/components/console.h +++ b/src/game/client/components/console.h @@ -40,7 +40,6 @@ class CGameConsole : public CComponent CLineInput m_Input; const char *m_pName; int m_Type; - int m_CompletionEnumerationCount; int m_BacklogCurPage; CGameConsole *m_pGameConsole; @@ -74,7 +73,7 @@ class CGameConsole : public CComponent void PrintLine(const char *pLine, int Len, ColorRGBA PrintColor); const char *GetString() const { return m_Input.GetString(); } - static void PossibleCommandsCompleteCallback(const char *pStr, void *pUser); + static void PossibleCommandsCompleteCallback(int Index, const char *pStr, void *pUser); }; class IConsole *m_pConsole; @@ -106,7 +105,7 @@ class CGameConsole : public CComponent void Toggle(int Type); void Dump(int Type); - static void PossibleCommandsRenderCallback(const char *pStr, void *pUser); + static void PossibleCommandsRenderCallback(int Index, const char *pStr, void *pUser); static void ConToggleLocalConsole(IConsole::IResult *pResult, void *pUserData); static void ConToggleRemoteConsole(IConsole::IResult *pResult, void *pUserData); static void ConClearLocalConsole(IConsole::IResult *pResult, void *pUserData);