XConsole.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #ifndef CRYINCLUDE_CRYSYSTEM_XCONSOLE_H
  9. #define CRYINCLUDE_CRYSYSTEM_XCONSOLE_H
  10. #pragma once
  11. #include <IConsole.h>
  12. #include <CryCommon/StlUtils.h>
  13. #include <CryCommon/TimeValue.h>
  14. #include <AzFramework/Components/ConsoleBus.h>
  15. #include <AzFramework/CommandLine/CommandRegistrationBus.h>
  16. #include <list>
  17. #include <map>
  18. //forward declaration
  19. struct INetwork;
  20. class CSystem;
  21. struct IFFont;
  22. #define MAX_HISTORY_ENTRIES 50
  23. #define LINE_BORDER 10
  24. enum ScrollDir
  25. {
  26. sdDOWN,
  27. sdUP,
  28. sdNONE
  29. };
  30. //////////////////////////////////////////////////////////////////////////
  31. // Console command holds information about commands registered to console.
  32. //////////////////////////////////////////////////////////////////////////
  33. struct CConsoleCommand
  34. {
  35. AZStd::string m_sName; // Console command name
  36. AZStd::string m_sCommand; // lua code that is executed when this command is invoked
  37. AZStd::string m_sHelp; // optional help string - can be shown in the console with "<commandname> ?"
  38. int m_nFlags; // bitmask consist of flag starting with VF_ e.g. VF_CHEAT
  39. ConsoleCommandFunc m_func; // Pointer to console command.
  40. //////////////////////////////////////////////////////////////////////////
  41. CConsoleCommand()
  42. : m_func(nullptr)
  43. , m_nFlags(0) {}
  44. size_t sizeofThis () const {return sizeof(*this) + m_sName.capacity() + 1 + m_sCommand.capacity() + 1; }
  45. };
  46. //////////////////////////////////////////////////////////////////////////
  47. // Implements IConsoleCmdArgs.
  48. //////////////////////////////////////////////////////////////////////////
  49. struct CConsoleCommandArgs
  50. : public IConsoleCmdArgs
  51. {
  52. CConsoleCommandArgs(AZStd::string& line, std::vector<AZStd::string>& args)
  53. : m_line(line)
  54. , m_args(args) {};
  55. int GetArgCount() const override { return static_cast<int>(m_args.size()); };
  56. // Get argument by index, nIndex must be in 0 <= nIndex < GetArgCount()
  57. const char* GetArg(int nIndex) const override
  58. {
  59. assert(nIndex >= 0 && nIndex < GetArgCount());
  60. if (!(nIndex >= 0 && nIndex < GetArgCount()))
  61. {
  62. return nullptr;
  63. }
  64. return m_args[nIndex].c_str();
  65. }
  66. const char* GetCommandLine() const override
  67. {
  68. return m_line.c_str();
  69. }
  70. private:
  71. std::vector<AZStd::string>& m_args;
  72. AZStd::string& m_line;
  73. };
  74. struct string_nocase_lt
  75. {
  76. bool operator()(const AZStd::string& s1, const AZStd::string& s2) const
  77. {
  78. return azstricmp(s1.c_str(), s2.c_str()) < 0;
  79. }
  80. };
  81. //forward declarations
  82. class ITexture;
  83. struct IRenderer;
  84. /*! engine console implementation
  85. @see IConsole
  86. */
  87. class CXConsole
  88. : public IConsole
  89. , public IRemoteConsoleListener
  90. , public AzFramework::ConsoleRequestBus::Handler
  91. , public AzFramework::CommandRegistrationBus::Handler
  92. {
  93. public:
  94. using ConsoleBuffer = std::deque<AZStd::string>;
  95. using ConsoleBufferItor = ConsoleBuffer::iterator;
  96. using ConsoleBufferRItor = ConsoleBuffer::reverse_iterator;
  97. // constructor
  98. CXConsole();
  99. // destructor
  100. virtual ~CXConsole();
  101. void SetStatus(bool bActive){ m_bConsoleActive = bActive; }
  102. bool GetStatus() const { return m_bConsoleActive; }
  103. //
  104. void FreeRenderResources();
  105. //
  106. void Copy();
  107. void Paste();
  108. // interface IConsole ---------------------------------------------------------
  109. void Release() override;
  110. void Init(ISystem* pSystem) override;
  111. ICVar* RegisterString(const char* sName, const char* sValue, int nFlags, const char* help = "", ConsoleVarFunc pChangeFunc = 0) override;
  112. ICVar* RegisterInt(const char* sName, int iValue, int nFlags, const char* help = "", ConsoleVarFunc pChangeFunc = 0) override;
  113. ICVar* RegisterFloat(const char* sName, float fValue, int nFlags, const char* help = "", ConsoleVarFunc pChangeFunc = 0) override;
  114. ICVar* Register(const char* name, float* src, float defaultvalue, int flags = 0, const char* help = "", ConsoleVarFunc pChangeFunc = 0, bool allowModify = true) override;
  115. ICVar* Register(const char* name, int* src, int defaultvalue, int flags = 0, const char* help = "", ConsoleVarFunc pChangeFunc = 0, bool allowModify = true) override;
  116. ICVar* Register(const char* name, const char** src, const char* defaultvalue, int flags = 0, const char* help = "", ConsoleVarFunc pChangeFunc = 0, bool allowModify = true) override;
  117. void UnregisterVariable(const char* sVarName, bool bDelete = false) override;
  118. void SetScrollMax(int value) override;
  119. void AddOutputPrintSink(IOutputPrintSink* inpSink) override;
  120. void RemoveOutputPrintSink(IOutputPrintSink* inpSink) override;
  121. void ShowConsole(bool show, int iRequestScrollMax = -1) override;
  122. void DumpCVars(ICVarDumpSink* pCallback, unsigned int nFlagsFilter = 0) override;
  123. void DumpKeyBinds(IKeyBindDumpSink* pCallback) override;
  124. void CreateKeyBind(const char* sCmd, const char* sRes) override;
  125. const char* FindKeyBind(const char* sCmd) const override;
  126. bool GetLineNo(int indwLineNo, char* outszBuffer, int indwBufferSize) const override;
  127. int GetLineCount() const override;
  128. ICVar* GetCVar(const char* name) override;
  129. char* GetVariable(const char* szVarName, const char* szFileName, const char* def_val) override;
  130. float GetVariable(const char* szVarName, const char* szFileName, float def_val) override;
  131. void PrintLine(AZStd::string_view s) override;
  132. void PrintLineAppendWithPrevLine(AZStd::string_view s) override;
  133. bool GetStatus() override;
  134. void Clear() override;
  135. void Update() override;
  136. bool AddCommand(const char* sCommand, ConsoleCommandFunc func, int nFlags = 0, const char* sHelp = NULL) override;
  137. bool AddCommand(const char* sName, const char* sScriptFunc, int nFlags = 0, const char* sHelp = NULL) override;
  138. void RemoveCommand(const char* sName) override;
  139. void ExecuteString(const char* command, bool bSilentMode, bool bDeferExecution = false) override;
  140. void ExecuteConsoleCommand(const char* command) override;
  141. void ResetCVarsToDefaults() override;
  142. void Exit(const char* command, ...) override PRINTF_PARAMS(2, 3);
  143. bool IsOpened() override;
  144. int GetNumVars() override;
  145. int GetNumVisibleVars() override;
  146. size_t GetSortedVars(AZStd::vector<AZStd::string_view>& pszArray, const char* szPrefix = 0) override;
  147. void FindVar(const char* substr);
  148. const char* AutoComplete(const char* substr) override;
  149. const char* AutoCompletePrev(const char* substr) override;
  150. const char* ProcessCompletion(const char* szInputBuffer) override;
  151. void RegisterAutoComplete(const char* sVarOrCommand, IConsoleArgumentAutoComplete* pArgAutoComplete) override;
  152. void UnRegisterAutoComplete(const char* sVarOrCommand) override;
  153. void ResetAutoCompletion() override;
  154. void AddConsoleVarSink(IConsoleVarSink* pSink) override;
  155. void RemoveConsoleVarSink(IConsoleVarSink* pSink) override;
  156. const char* GetHistoryElement(bool bUpOrDown) override;
  157. void AddCommandToHistory(const char* szCommand) override;
  158. void SetInputLine(const char* szLine) override;
  159. void LoadConfigVar(const char* sVariable, const char* sValue) override;
  160. void EnableActivationKey(bool bEnable) override;
  161. void SetClientDataProbeString(const char* pName, const char* pValue) override;
  162. // interface IRemoteConsoleListener ------------------------------------------------------------------
  163. void OnConsoleCommand(const char* cmd) override;
  164. // interface IConsoleVarSink ----------------------------------------------------------------------
  165. virtual bool OnBeforeVarChange(ICVar* pVar, const char* sNewValue);
  166. virtual void OnAfterVarChange(ICVar* pVar);
  167. // interface CommandRegistration --------------------------------------------------------------------
  168. bool RegisterCommand(AZStd::string_view identifier, AZStd::string_view helpText, AZ::u32 commandFlags, AzFramework::CommandFunction callback) override;
  169. bool UnregisterCommand(AZStd::string_view identifier) override;
  170. void ExecuteRegisteredCommand(IConsoleCmdArgs* pArg);
  171. //////////////////////////////////////////////////////////////////////////
  172. void SetProcessingGroup(bool isGroup) { m_bIsProcessingGroup = isGroup; }
  173. bool GetIsProcessingGroup() const { return m_bIsProcessingGroup; }
  174. protected: // ----------------------------------------------------------------------------------------
  175. void RegisterVar(ICVar* pCVar, ConsoleVarFunc pChangeFunc = nullptr);
  176. void AddLine(AZStd::string_view inputStr);
  177. void AddLineAppendWithPrevLine(AZStd::string_view inputStr);
  178. void AddInputUTF8(const AZStd::string& textUTF8);
  179. void RemoveInputChar(bool bBackSpace);
  180. void ExecuteInputBuffer();
  181. void ExecuteCommand(CConsoleCommand& cmd, AZStd::string& params, bool bIgnoreDevMode = false);
  182. void ScrollConsole();
  183. // CommandRegistration usage
  184. struct CommandRegistrationEntry
  185. {
  186. AzFramework::CommandFunction m_callback;
  187. AZStd::string m_id;
  188. AZStd::string m_helpText;
  189. };
  190. AZStd::unordered_map<AZStd::string, CommandRegistrationEntry> m_commandRegistrationMap;
  191. #if ALLOW_AUDIT_CVARS
  192. void AuditCVars(IConsoleCmdArgs* pArg);
  193. #endif // ALLOW_AUDIT_CVARS
  194. #ifndef _RELEASE
  195. // will be removed once the HTML version is good enough
  196. void DumpCommandsVarsTxt(const char* prefix);
  197. void DumpVarsTxt(const bool includeCheat);
  198. #endif
  199. void ConsoleLogInputResponse(const char* szFormat, ...) PRINTF_PARAMS(2, 3);
  200. void ConsoleLogInput(const char* szFormat, ...) PRINTF_PARAMS(2, 3);
  201. void ConsoleWarning(const char* szFormat, ...) PRINTF_PARAMS(2, 3);
  202. void DisplayHelp(const char* help, const char* name);
  203. void DisplayVarValue(ICVar* pVar);
  204. // Arguments:
  205. // bFromConsole - true=from console, false=from outside
  206. void SplitCommands(const char* line, std::list<AZStd::string>& split);
  207. void ExecuteStringInternal(const char* command, const bool bFromConsole, const bool bSilentMode = false);
  208. void ExecuteDeferredCommands();
  209. static const char* GetFlagsString(const uint32 dwFlags);
  210. private: // ----------------------------------------------------------
  211. typedef std::map<const char*, ICVar*, string_nocase_lt> ConsoleVariablesMap; // key points into string stored in ICVar or in .exe/.dll
  212. typedef ConsoleVariablesMap::iterator ConsoleVariablesMapItor;
  213. using ConsoleVariablesVector = std::vector<std::pair<const char*, ICVar*> >;
  214. void LogChangeMessage(const char* name, const bool isConst, const bool isCheat, const bool isReadOnly, const bool isDeprecated,
  215. const char* oldValue, const char* newValue, const bool isProcessingGroup, const bool allowChange);
  216. void AddCheckedCVar(ConsoleVariablesVector& vector, const ConsoleVariablesVector::value_type& value);
  217. void RemoveCheckedCVar(ConsoleVariablesVector& vector, const ConsoleVariablesVector::value_type& value);
  218. static bool CVarNameLess(const std::pair<const char*, ICVar*>& lhs, const std::pair<const char*, ICVar*>& rhs);
  219. typedef std::map<AZStd::string, CConsoleCommand, string_nocase_lt> ConsoleCommandsMap;
  220. typedef ConsoleCommandsMap::iterator ConsoleCommandsMapItor;
  221. typedef std::map<AZStd::string, AZStd::string> ConsoleBindsMap;
  222. typedef ConsoleBindsMap::iterator ConsoleBindsMapItor;
  223. typedef std::map<AZStd::string, IConsoleArgumentAutoComplete*, stl::less_stricmp<AZStd::string> > ArgumentAutoCompleteMap;
  224. struct SConfigVar
  225. {
  226. AZStd::string m_value;
  227. bool m_partOfGroup;
  228. };
  229. typedef std::map<AZStd::string, SConfigVar, string_nocase_lt> ConfigVars;
  230. struct SDeferredCommand
  231. {
  232. AZStd::string command;
  233. bool silentMode;
  234. SDeferredCommand(const AZStd::string& _command, bool _silentMode)
  235. : command(_command)
  236. , silentMode(_silentMode)
  237. {}
  238. };
  239. using TDeferredCommandList = std::list<SDeferredCommand>;
  240. using ConsoleVarSinks = std::list<IConsoleVarSink*>;
  241. // --------------------------------------------------------------------------------
  242. ConsoleBuffer m_dqConsoleBuffer;
  243. ConsoleBuffer m_dqHistory;
  244. bool m_bStaticBackground;
  245. int m_nLoadingBackTexID;
  246. int m_nProgress;
  247. int m_nProgressRange;
  248. AZStd::string m_sInputBuffer;
  249. AZStd::string m_sReturnString;
  250. AZStd::string m_sPrevTab;
  251. int m_nTabCount;
  252. ConsoleCommandsMap m_mapCommands; //
  253. ConsoleBindsMap m_mapBinds; //
  254. ConsoleVariablesMap m_mapVariables; //
  255. ConsoleVariablesVector m_randomCheckedVariables;
  256. ConsoleVariablesVector m_alwaysCheckedVariables;
  257. std::vector<IOutputPrintSink*> m_OutputSinks; // objects in this vector are not released
  258. template<typename T>
  259. struct FunctorWrapper
  260. {
  261. FunctorWrapper(const char* name, const char* desc, T initialValue)
  262. : proxyValue(AZStd::move(initialValue))
  263. , m_functor(name, desc, AZ::ConsoleFunctorFlags::DontDuplicate, AZ::AzTypeInfo<T>::Uuid(), proxyValue, ArgToValue)
  264. {
  265. }
  266. T proxyValue;
  267. AZ::ConsoleFunctor<T, true> m_functor;
  268. private:
  269. static void ArgToValue(T& outValue, const AZ::ConsoleCommandContainer& arguments)
  270. {
  271. AZ::ConsoleTypeHelpers::ToValue(outValue, arguments);
  272. }
  273. };
  274. AZStd::vector<FunctorWrapper<float>> m_floatWrappers;
  275. AZStd::vector<FunctorWrapper<int>> m_intWrappers;
  276. AZStd::vector<FunctorWrapper<AZStd::string>> m_stringWrappers;
  277. TDeferredCommandList m_deferredCommands; // A fifo of deferred commands
  278. bool m_deferredExecution; // True when deferred commands are processed
  279. int m_waitFrames; // A counter which is used by wait_frames command
  280. CTimeValue m_waitSeconds; // An absolute timestamp which is used by wait_seconds command
  281. int m_blockCounter; // This counter is incremented whenever a blocker command (VF_BLOCKFRAME) is executed.
  282. ArgumentAutoCompleteMap m_mapArgumentAutoComplete;
  283. ConsoleVarSinks m_consoleVarSinks;
  284. ConfigVars m_configVars; // temporary data of cvars that haven't been created yet
  285. int m_nScrollPos;
  286. int m_nTempScrollMax; // for currently opened console, reset to m_nScrollMax
  287. int m_nScrollMax; //
  288. int m_nScrollLine;
  289. int m_nHistoryPos;
  290. size_t m_nCursorPos; // x position in characters
  291. ITexture* m_pImage;
  292. float m_fRepeatTimer; // relative, next repeat even in .. decreses over time, repeats when 0, only valid if m_nRepeatEvent.keyId != eKI_Unknown
  293. float m_fCursorBlinkTimer; // relative, increases over time,
  294. bool m_bDrawCursor;
  295. ScrollDir m_sdScrollDir;
  296. bool m_bConsoleActive;
  297. bool m_bActivationKeyEnable;
  298. bool m_bIsProcessingGroup;
  299. bool m_bIsConsoleKeyPressed;
  300. size_t m_nCheatHashRangeFirst;
  301. size_t m_nCheatHashRangeLast;
  302. bool m_bCheatHashDirty;
  303. uint64 m_nCheatHash;
  304. CSystem* m_pSystem;
  305. ICVar* m_pSysDeactivateConsole;
  306. static int con_display_last_messages;
  307. static int con_line_buffer_size;
  308. static int con_showonload;
  309. static int con_debug;
  310. static int con_restricted;
  311. friend void Command_SetWaitSeconds(IConsoleCmdArgs* Cmd);
  312. friend void Command_SetWaitFrames(IConsoleCmdArgs* Cmd);
  313. #if ALLOW_AUDIT_CVARS
  314. friend void Command_AuditCVars(IConsoleCmdArgs* pArg);
  315. #endif // ALLOW_AUDIT_CVARS
  316. friend void Command_DumpCommandsVars(IConsoleCmdArgs* Cmd);
  317. friend void Command_DumpVars(IConsoleCmdArgs* Cmd);
  318. friend class CConsoleHelpGen;
  319. };
  320. #endif // CRYINCLUDE_CRYSYSTEM_XCONSOLE_H