2010-11-20 10:37:14 +00:00
|
|
|
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
|
|
|
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
2010-05-29 07:25:38 +00:00
|
|
|
#ifndef ENGINE_CONSOLE_H
|
|
|
|
#define ENGINE_CONSOLE_H
|
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
#include "kernel.h"
|
2019-04-25 15:21:35 +00:00
|
|
|
#include <base/color.h>
|
2017-08-11 19:07:20 +00:00
|
|
|
#include <engine/storage.h>
|
2010-05-29 07:25:38 +00:00
|
|
|
|
2022-10-19 21:46:06 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2021-10-23 23:48:00 +00:00
|
|
|
static const ColorRGBA gs_ConsoleDefaultColor(1, 1, 1, 1);
|
|
|
|
|
2022-04-22 23:04:48 +00:00
|
|
|
enum LEVEL : char;
|
2022-01-31 02:11:47 +00:00
|
|
|
struct CChecksumData;
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
class IConsole : public IInterface
|
|
|
|
{
|
2023-11-28 20:46:03 +00:00
|
|
|
MACRO_INTERFACE("console")
|
2010-05-29 07:25:38 +00:00
|
|
|
public:
|
2011-07-14 20:07:21 +00:00
|
|
|
// TODO: rework/cleanup
|
2010-08-17 22:06:00 +00:00
|
|
|
enum
|
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
OUTPUT_LEVEL_STANDARD = 0,
|
2010-08-17 22:06:00 +00:00
|
|
|
OUTPUT_LEVEL_ADDINFO,
|
2011-07-05 19:54:10 +00:00
|
|
|
OUTPUT_LEVEL_DEBUG,
|
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
ACCESS_LEVEL_ADMIN = 0,
|
2011-07-05 19:54:10 +00:00
|
|
|
ACCESS_LEVEL_MOD,
|
2015-10-23 00:33:10 +00:00
|
|
|
ACCESS_LEVEL_HELPER,
|
2011-08-26 07:19:00 +00:00
|
|
|
ACCESS_LEVEL_USER,
|
2011-07-14 20:07:21 +00:00
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
TEMPCMD_NAME_LENGTH = 32,
|
2022-06-26 22:06:06 +00:00
|
|
|
TEMPCMD_HELP_LENGTH = 192,
|
2020-09-26 19:41:58 +00:00
|
|
|
TEMPCMD_PARAMS_LENGTH = 96,
|
2011-07-30 11:40:01 +00:00
|
|
|
|
Refactor config manager, move config variable handling
Move all code for handling of config variables from console to config manager. The console no longer depends on the config manager, instead the config manager now depends on the console.
Add `struct`s to manage config variables of different types (int, color and string). The config manager now keeps a list of all config variables, so usage of the preprocessor can be avoided except for code to initially create all config variables. Additionally, a separate list of just the game config variables (config variables with `CFGFLAG_GAME`) is kept to optimize the `ResetGameSettings` function, as this function is called whenever connecting to a server and should be fast. Previously, this function was even less efficient because it preformed a linear search for every individual game config variable to find the respective command data.
Move console commands that opperate only on config variables (`reset`, `toggle` and `+toggle`) to config manager. Ensure that these commands only opperate on the desired config variables (client or server, respectively) by checking `IConsole::FlagMask`.
Add `IConfigManager::SetReadOnly` function to set/unset config variables as read-only instead of manually overriding the command handlers for the `sv_rescue` and `sv_test_cmds` config variables. This also fixes that read-only config variables could still be changed by using the `reset` command. A console message is now printed when trying to change a read-only config variable. Removing the special handling for these two config variables is additionally useful so the console does not need to keep a pointer to config values and manager.
Use a `CHeap` for the config variables, their help texts and the previous values of string config variables to avoid many separate allocations as well usage of static variables. Also use the heap to store the unknown commands instead of using `std::string`s.
Properly trigger command chain when resetting config variables with the `reset` command and when resetting game settings on map loading. Closes #7461.
Format default value for color variables as RGB/RGBA hex with dollar sign prefix. Closes #5523.
Add log message when using `reset` with a variable that does not exist. Use `log_error` instead of `dbg_msg` when saving config file fails.
Support unlimited number of config save callbacks instead of at most 16. The code also becomes more readable by using an `std::vector` instead of a fixed-size array and a separate num variable.
Consistently name `MACRO_CONFIG_*` parameters when declaring the macros.
Add `IConsole::CMDLINE_LENGTH` constant to represent the maximum length of the console input and thereby reduce usage of magic numbers for buffer sizes.
2023-11-22 22:07:08 +00:00
|
|
|
CMDLINE_LENGTH = 512,
|
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
CLIENT_ID_GAME = -2,
|
|
|
|
CLIENT_ID_NO_GAME = -3,
|
2010-08-17 22:06:00 +00:00
|
|
|
};
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// TODO: rework this interface to reduce the amount of virtual calls
|
|
|
|
class IResult
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
unsigned m_NumArgs;
|
2020-09-26 19:41:58 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
public:
|
|
|
|
IResult() { m_NumArgs = 0; }
|
|
|
|
virtual ~IResult() {}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2022-10-19 21:46:06 +00:00
|
|
|
virtual int GetInteger(unsigned Index) const = 0;
|
|
|
|
virtual float GetFloat(unsigned Index) const = 0;
|
|
|
|
virtual const char *GetString(unsigned Index) const = 0;
|
|
|
|
virtual ColorHSLA GetColor(unsigned Index, bool Light) const = 0;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2020-08-19 09:38:49 +00:00
|
|
|
virtual void RemoveArgument(unsigned Index) = 0;
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
int NumArguments() const { return m_NumArgs; }
|
2011-08-26 18:03:30 +00:00
|
|
|
int m_ClientID;
|
2011-09-25 16:04:29 +00:00
|
|
|
|
|
|
|
// DDRace
|
|
|
|
|
2022-10-19 21:46:06 +00:00
|
|
|
virtual int GetVictim() const = 0;
|
2010-05-29 07:25:38 +00:00
|
|
|
};
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
class CCommandInfo
|
|
|
|
{
|
2011-07-14 20:07:21 +00:00
|
|
|
protected:
|
|
|
|
int m_AccessLevel;
|
2020-09-26 19:41:58 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
public:
|
2015-12-28 15:14:52 +00:00
|
|
|
CCommandInfo() { m_AccessLevel = ACCESS_LEVEL_ADMIN; }
|
2011-07-14 20:07:21 +00:00
|
|
|
virtual ~CCommandInfo() {}
|
2010-05-29 07:25:38 +00:00
|
|
|
const char *m_pName;
|
|
|
|
const char *m_pHelp;
|
|
|
|
const char *m_pParams;
|
2011-04-09 06:41:31 +00:00
|
|
|
|
2011-07-14 20:07:21 +00:00
|
|
|
virtual const CCommandInfo *NextCommandInfo(int AccessLevel, int FlagMask) const = 0;
|
|
|
|
|
|
|
|
int GetAccessLevel() const { return m_AccessLevel; }
|
2010-05-29 07:25:38 +00:00
|
|
|
};
|
|
|
|
|
2017-09-13 20:35:09 +00:00
|
|
|
typedef void (*FTeeHistorianCommandCallback)(int ClientID, int FlagMask, const char *pCmd, IResult *pResult, void *pUser);
|
2021-03-08 00:08:38 +00:00
|
|
|
typedef void (*FPrintCallback)(const char *pStr, void *pUser, ColorRGBA PrintColor);
|
2022-08-09 15:26:57 +00:00
|
|
|
typedef void (*FPossibleCallback)(int Index, const char *pCmd, void *pUser);
|
2011-08-13 00:11:06 +00:00
|
|
|
typedef void (*FCommandCallback)(IResult *pResult, void *pUserData);
|
2010-05-29 07:25:38 +00:00
|
|
|
typedef void (*FChainCommandCallback)(IResult *pResult, void *pUserData, FCommandCallback pfnCallback, void *pCallbackUserData);
|
2022-08-30 20:17:04 +00:00
|
|
|
typedef bool (*FUnknownCommandCallback)(const char *pCommand, void *pUser); // returns true if the callback has handled the argument
|
2010-05-29 07:25:38 +00:00
|
|
|
|
2022-08-09 15:26:57 +00:00
|
|
|
static void EmptyPossibleCommandCallback(int Index, const char *pCmd, void *pUser) {}
|
2022-08-30 20:17:04 +00:00
|
|
|
static bool EmptyUnknownCommandCallback(const char *pCommand, void *pUser) { return false; }
|
2022-08-09 15:26:57 +00:00
|
|
|
|
2021-01-10 13:02:54 +00:00
|
|
|
virtual void Init() = 0;
|
2011-07-14 20:07:21 +00:00
|
|
|
virtual const CCommandInfo *FirstCommandInfo(int AccessLevel, int Flagmask) const = 0;
|
|
|
|
virtual const CCommandInfo *GetCommandInfo(const char *pName, int FlagMask, bool Temp) = 0;
|
2022-08-09 15:26:57 +00:00
|
|
|
virtual int PossibleCommands(const char *pStr, int FlagMask, bool Temp, FPossibleCallback pfnCallback = EmptyPossibleCommandCallback, void *pUser = nullptr) = 0;
|
2010-05-29 07:25:38 +00:00
|
|
|
virtual void ParseArguments(int NumArgs, const char **ppArguments) = 0;
|
|
|
|
|
2015-12-28 15:14:52 +00:00
|
|
|
virtual void Register(const char *pName, const char *pParams, int Flags, FCommandCallback pfnFunc, void *pUser, const char *pHelp) = 0;
|
|
|
|
virtual void RegisterTemp(const char *pName, const char *pParams, int Flags, const char *pHelp) = 0;
|
2011-07-14 20:07:21 +00:00
|
|
|
virtual void DeregisterTemp(const char *pName) = 0;
|
|
|
|
virtual void DeregisterTempAll() = 0;
|
2010-05-29 07:25:38 +00:00
|
|
|
virtual void Chain(const char *pName, FChainCommandCallback pfnChainFunc, void *pUser) = 0;
|
2011-08-13 00:11:06 +00:00
|
|
|
virtual void StoreCommands(bool Store) = 0;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-10-25 16:30:35 +00:00
|
|
|
virtual bool LineIsValid(const char *pStr) = 0;
|
2022-06-30 22:36:32 +00:00
|
|
|
virtual void ExecuteLine(const char *pStr, int ClientID = -1, bool InterpretSemicolons = true) = 0;
|
|
|
|
virtual void ExecuteLineFlag(const char *pStr, int FlasgMask, int ClientID = -1, bool InterpretSemicolons = true) = 0;
|
2017-01-03 13:01:51 +00:00
|
|
|
virtual void ExecuteLineStroked(int Stroke, const char *pStr, int ClientID = -1, bool InterpretSemicolons = true) = 0;
|
2023-05-09 21:18:52 +00:00
|
|
|
virtual bool ExecuteFile(const char *pFilename, int ClientID = -1, bool LogFailure = false, int StorageType = IStorage::TYPE_ALL) = 0;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2019-08-01 18:24:30 +00:00
|
|
|
virtual char *Format(char *pBuf, int Size, const char *pFrom, const char *pStr) = 0;
|
2022-10-19 21:46:06 +00:00
|
|
|
virtual void Print(int Level, const char *pFrom, const char *pStr, ColorRGBA PrintColor = gs_ConsoleDefaultColor) const = 0;
|
2017-09-13 20:35:09 +00:00
|
|
|
virtual void SetTeeHistorianCommandCallback(FTeeHistorianCommandCallback pfnCallback, void *pUser) = 0;
|
2022-08-30 20:17:04 +00:00
|
|
|
virtual void SetUnknownCommandCallback(FUnknownCommandCallback pfnCallback, void *pUser) = 0;
|
2022-01-31 02:11:47 +00:00
|
|
|
virtual void InitChecksum(CChecksumData *pData) const = 0;
|
2011-04-09 06:41:31 +00:00
|
|
|
|
2011-07-05 19:54:10 +00:00
|
|
|
virtual void SetAccessLevel(int AccessLevel) = 0;
|
2011-08-26 21:24:46 +00:00
|
|
|
|
2022-04-22 23:04:48 +00:00
|
|
|
static LEVEL ToLogLevel(int ConsoleLevel);
|
2022-06-13 19:43:05 +00:00
|
|
|
static int ToLogLevelFilter(int ConsoleLevel);
|
2022-04-22 23:04:48 +00:00
|
|
|
|
2011-08-26 21:24:46 +00:00
|
|
|
// DDRace
|
|
|
|
|
2023-11-25 10:27:45 +00:00
|
|
|
virtual bool Cheated() const = 0;
|
2023-11-25 10:24:31 +00:00
|
|
|
|
|
|
|
virtual int FlagMask() const = 0;
|
2011-08-31 13:41:32 +00:00
|
|
|
virtual void SetFlagMask(int FlagMask) = 0;
|
2010-05-29 07:25:38 +00:00
|
|
|
};
|
|
|
|
|
2022-10-19 21:46:06 +00:00
|
|
|
std::unique_ptr<IConsole> CreateConsole(int FlagMask);
|
2010-05-29 07:25:38 +00:00
|
|
|
|
|
|
|
#endif // FILE_ENGINE_CONSOLE_H
|