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_SHARED_CONSOLE_H
|
|
|
|
#define ENGINE_SHARED_CONSOLE_H
|
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
#include "memheap.h"
|
|
|
|
#include <base/math.h>
|
2010-05-29 07:25:38 +00:00
|
|
|
#include <engine/console.h>
|
2017-08-11 19:07:20 +00:00
|
|
|
#include <engine/storage.h>
|
2010-05-29 07:25:38 +00:00
|
|
|
|
|
|
|
class CConsole : public IConsole
|
|
|
|
{
|
|
|
|
class CCommand : public CCommandInfo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CCommand *m_pNext;
|
|
|
|
int m_Flags;
|
2011-07-14 20:07:21 +00:00
|
|
|
bool m_Temp;
|
2010-05-29 07:25:38 +00:00
|
|
|
FCommandCallback m_pfnCallback;
|
|
|
|
void *m_pUserData;
|
2011-07-14 20:07:21 +00:00
|
|
|
|
|
|
|
virtual const CCommandInfo *NextCommandInfo(int AccessLevel, int FlagMask) const;
|
|
|
|
|
2011-08-26 07:19:00 +00:00
|
|
|
void SetAccessLevel(int AccessLevel) { m_AccessLevel = clamp(AccessLevel, (int)(ACCESS_LEVEL_ADMIN), (int)(ACCESS_LEVEL_USER)); }
|
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 CChain
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FChainCommandCallback m_pfnChainCallback;
|
|
|
|
FCommandCallback m_pfnCallback;
|
|
|
|
void *m_pCallbackUserData;
|
|
|
|
void *m_pUserData;
|
2011-04-13 18:37:12 +00:00
|
|
|
};
|
|
|
|
|
2010-06-18 18:32:52 +00:00
|
|
|
int m_FlagMask;
|
2010-08-07 18:22:25 +00:00
|
|
|
bool m_StoreCommands;
|
2020-10-27 17:57:14 +00:00
|
|
|
const char *m_apStrokeStr[2];
|
2010-05-29 07:25:38 +00:00
|
|
|
CCommand *m_pFirstCommand;
|
|
|
|
|
|
|
|
class CExecFile
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
const char *m_pFilename;
|
2011-04-19 09:54:44 +00:00
|
|
|
CExecFile *m_pPrev;
|
2010-05-29 07:25:38 +00:00
|
|
|
};
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
CExecFile *m_pFirstExec;
|
2021-01-10 12:47:07 +00:00
|
|
|
class CConfig *m_pConfig;
|
2010-05-29 07:25:38 +00:00
|
|
|
class IStorage *m_pStorage;
|
2011-07-05 19:54:10 +00:00
|
|
|
int m_AccessLevel;
|
2010-05-29 07:25:38 +00:00
|
|
|
|
2011-07-14 20:07:21 +00:00
|
|
|
CCommand *m_pRecycleList;
|
|
|
|
CHeap m_TempCommands;
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
static void Con_Chain(IResult *pResult, void *pUserData);
|
|
|
|
static void Con_Echo(IResult *pResult, void *pUserData);
|
|
|
|
static void Con_Exec(IResult *pResult, void *pUserData);
|
2012-01-09 00:25:18 +00:00
|
|
|
static void ConToggle(IResult *pResult, void *pUser);
|
|
|
|
static void ConToggleStroke(IResult *pResult, void *pUser);
|
2015-10-23 00:33:10 +00:00
|
|
|
static void ConCommandAccess(IResult *pResult, void *pUser);
|
2015-10-26 23:33:26 +00:00
|
|
|
static void ConCommandStatus(IConsole::IResult *pResult, void *pUser);
|
2010-05-29 07:25:38 +00:00
|
|
|
|
2017-01-03 13:01:51 +00:00
|
|
|
void ExecuteLineStroked(int Stroke, const char *pStr, int ClientID = -1, bool InterpretSemicolons = true);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-07-30 11:40:01 +00:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
int m_OutputLevel;
|
|
|
|
FPrintCallback m_pfnPrintCallback;
|
|
|
|
void *m_pPrintCallbackUserdata;
|
|
|
|
} m_aPrintCB[MAX_PRINT_CB];
|
|
|
|
int m_NumPrintCB;
|
2010-05-29 07:25:38 +00:00
|
|
|
|
2017-09-13 20:35:09 +00:00
|
|
|
FTeeHistorianCommandCallback m_pfnTeeHistorianCommandCallback;
|
|
|
|
void *m_pTeeHistorianCommandUserdata;
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
enum
|
|
|
|
{
|
2014-04-18 22:40:21 +00:00
|
|
|
CONSOLE_MAX_STR_LENGTH = 8192,
|
2020-09-26 19:41:58 +00:00
|
|
|
MAX_PARTS = (CONSOLE_MAX_STR_LENGTH + 1) / 2
|
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 CResult : public IResult
|
|
|
|
{
|
|
|
|
public:
|
2020-09-26 19:41:58 +00:00
|
|
|
char m_aStringStorage[CONSOLE_MAX_STR_LENGTH + 1];
|
2010-05-29 07:25:38 +00:00
|
|
|
char *m_pArgsStart;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
const char *m_pCommand;
|
|
|
|
const char *m_apArgs[MAX_PARTS];
|
2011-01-07 18:33:29 +00:00
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
CResult() :
|
|
|
|
IResult()
|
2011-01-07 18:33:29 +00:00
|
|
|
{
|
|
|
|
mem_zero(m_aStringStorage, sizeof(m_aStringStorage));
|
|
|
|
m_pArgsStart = 0;
|
|
|
|
m_pCommand = 0;
|
|
|
|
mem_zero(m_apArgs, sizeof(m_apArgs));
|
|
|
|
}
|
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
CResult &operator=(const CResult &Other)
|
2011-01-07 18:33:29 +00:00
|
|
|
{
|
|
|
|
if(this != &Other)
|
|
|
|
{
|
|
|
|
IResult::operator=(Other);
|
|
|
|
mem_copy(m_aStringStorage, Other.m_aStringStorage, sizeof(m_aStringStorage));
|
2020-09-26 19:41:58 +00:00
|
|
|
m_pArgsStart = m_aStringStorage + (Other.m_pArgsStart - Other.m_aStringStorage);
|
|
|
|
m_pCommand = m_aStringStorage + (Other.m_pCommand - Other.m_aStringStorage);
|
2011-01-10 23:57:44 +00:00
|
|
|
for(unsigned i = 0; i < Other.m_NumArgs; ++i)
|
2020-09-26 19:41:58 +00:00
|
|
|
m_apArgs[i] = m_aStringStorage + (Other.m_apArgs[i] - Other.m_aStringStorage);
|
2011-01-07 18:33:29 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void AddArgument(const char *pArg)
|
|
|
|
{
|
|
|
|
m_apArgs[m_NumArgs++] = pArg;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const char *GetString(unsigned Index);
|
|
|
|
virtual int GetInteger(unsigned Index);
|
|
|
|
virtual float GetFloat(unsigned Index);
|
2020-06-18 16:47:46 +00:00
|
|
|
virtual ColorHSLA GetColor(unsigned Index, bool Light);
|
2011-09-25 16:04:29 +00:00
|
|
|
|
2020-08-19 09:38:49 +00:00
|
|
|
virtual void RemoveArgument(unsigned Index)
|
|
|
|
{
|
|
|
|
dbg_assert(Index < m_NumArgs, "invalid argument index");
|
|
|
|
for(unsigned i = Index; i < m_NumArgs - 1; i++)
|
|
|
|
m_apArgs[i] = m_apArgs[i + 1];
|
|
|
|
|
|
|
|
m_apArgs[m_NumArgs--] = 0;
|
|
|
|
}
|
|
|
|
|
2011-09-25 16:04:29 +00:00
|
|
|
// DDRace
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
VICTIM_NONE = -3,
|
|
|
|
VICTIM_ME = -2,
|
|
|
|
VICTIM_ALL = -1,
|
2011-09-25 16:04:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int m_Victim;
|
|
|
|
void ResetVictim();
|
|
|
|
bool HasVictim();
|
|
|
|
void SetVictim(int Victim);
|
|
|
|
void SetVictim(const char *pVictim);
|
|
|
|
virtual int GetVictim();
|
2010-05-29 07:25:38 +00:00
|
|
|
};
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
int ParseStart(CResult *pResult, const char *pString, int Length);
|
|
|
|
int ParseArgs(CResult *pResult, const char *pFormat);
|
|
|
|
|
2015-12-28 15:14:52 +00:00
|
|
|
/*
|
|
|
|
this function will set pFormat to the next parameter (i,s,r,v,?) it contains and
|
|
|
|
return the parameter; descriptions in brackets like [file] will be skipped;
|
|
|
|
returns '\0' if there is no next parameter; expects pFormat to point at a
|
|
|
|
parameter
|
|
|
|
*/
|
|
|
|
char NextParam(const char *&pFormat);
|
|
|
|
|
2010-08-07 18:22:25 +00:00
|
|
|
class CExecutionQueue
|
|
|
|
{
|
|
|
|
CHeap m_Queue;
|
|
|
|
|
|
|
|
public:
|
|
|
|
struct CQueueEntry
|
|
|
|
{
|
|
|
|
CQueueEntry *m_pNext;
|
|
|
|
FCommandCallback m_pfnCommandCallback;
|
|
|
|
void *m_pCommandUserData;
|
|
|
|
CResult m_Result;
|
2020-09-26 19:41:58 +00:00
|
|
|
} * m_pFirst, *m_pLast;
|
2010-08-07 18:22:25 +00:00
|
|
|
|
|
|
|
void AddEntry()
|
|
|
|
{
|
|
|
|
CQueueEntry *pEntry = static_cast<CQueueEntry *>(m_Queue.Allocate(sizeof(CQueueEntry)));
|
|
|
|
pEntry->m_pNext = 0;
|
2011-01-07 18:33:29 +00:00
|
|
|
if(!m_pFirst)
|
|
|
|
m_pFirst = pEntry;
|
|
|
|
if(m_pLast)
|
|
|
|
m_pLast->m_pNext = pEntry;
|
2010-08-07 18:22:25 +00:00
|
|
|
m_pLast = pEntry;
|
2011-01-07 18:33:29 +00:00
|
|
|
(void)new(&(pEntry->m_Result)) CResult;
|
2010-08-07 18:22:25 +00:00
|
|
|
}
|
|
|
|
void Reset()
|
|
|
|
{
|
|
|
|
m_Queue.Reset();
|
2011-01-07 18:33:29 +00:00
|
|
|
m_pFirst = m_pLast = 0;
|
2010-08-07 18:22:25 +00:00
|
|
|
}
|
|
|
|
} m_ExecutionQueue;
|
|
|
|
|
2011-07-14 20:07:21 +00:00
|
|
|
void AddCommandSorted(CCommand *pCommand);
|
2010-06-18 18:32:52 +00:00
|
|
|
CCommand *FindCommand(const char *pName, int FlagMask);
|
2010-05-29 07:25:38 +00:00
|
|
|
|
|
|
|
public:
|
2021-12-14 23:26:49 +00:00
|
|
|
CConfig *Config() { return m_pConfig; }
|
|
|
|
|
2010-06-18 18:32:52 +00:00
|
|
|
CConsole(int FlagMask);
|
2017-07-21 13:19:44 +00:00
|
|
|
~CConsole();
|
2010-05-29 07:25:38 +00:00
|
|
|
|
2021-01-10 13:02:54 +00:00
|
|
|
virtual void Init();
|
2011-12-30 18:12:31 +00:00
|
|
|
virtual const CCommandInfo *FirstCommandInfo(int AccessLevel, int FlagMask) const;
|
2011-07-14 20:07:21 +00:00
|
|
|
virtual const CCommandInfo *GetCommandInfo(const char *pName, int FlagMask, bool Temp);
|
|
|
|
virtual void PossibleCommands(const char *pStr, int FlagMask, bool Temp, FPossibleCallback pfnCallback, void *pUser);
|
2010-05-29 07:25:38 +00:00
|
|
|
|
|
|
|
virtual void ParseArguments(int NumArgs, const char **ppArguments);
|
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);
|
|
|
|
virtual void RegisterTemp(const char *pName, const char *pParams, int Flags, const char *pHelp);
|
2011-07-14 20:07:21 +00:00
|
|
|
virtual void DeregisterTemp(const char *pName);
|
|
|
|
virtual void DeregisterTempAll();
|
2010-05-29 07:25:38 +00:00
|
|
|
virtual void Chain(const char *pName, FChainCommandCallback pfnChainFunc, void *pUser);
|
2011-08-13 00:11:06 +00:00
|
|
|
virtual void StoreCommands(bool Store);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-10-25 16:30:35 +00:00
|
|
|
virtual bool LineIsValid(const char *pStr);
|
2017-01-03 13:01:51 +00:00
|
|
|
virtual void ExecuteLine(const char *pStr, int ClientID = -1, bool InterpretSemicolons = true);
|
|
|
|
virtual void ExecuteLineFlag(const char *pStr, int FlagMask, int ClientID = -1, bool InterpretSemicolons = true);
|
2017-08-11 19:07:20 +00:00
|
|
|
virtual void ExecuteFile(const char *pFilename, int ClientID = -1, bool LogFailure = false, int StorageType = IStorage::TYPE_ALL);
|
2010-05-29 07:25:38 +00:00
|
|
|
|
2011-07-30 11:40:01 +00:00
|
|
|
virtual int RegisterPrintCallback(int OutputLevel, FPrintCallback pfnPrintCallback, void *pUserData);
|
|
|
|
virtual void SetPrintOutputLevel(int Index, int OutputLevel);
|
2019-08-01 18:24:30 +00:00
|
|
|
virtual char *Format(char *pBuf, int Size, const char *pFrom, const char *pStr);
|
2021-10-23 23:48:00 +00:00
|
|
|
virtual void Print(int Level, const char *pFrom, const char *pStr, ColorRGBA PrintColor = gs_ConsoleDefaultColor);
|
2017-09-13 20:35:09 +00:00
|
|
|
virtual void SetTeeHistorianCommandCallback(FTeeHistorianCommandCallback pfnCallback, void *pUser);
|
2011-04-09 06:41:31 +00:00
|
|
|
|
2011-08-26 07:19:00 +00:00
|
|
|
void SetAccessLevel(int AccessLevel) { m_AccessLevel = clamp(AccessLevel, (int)(ACCESS_LEVEL_ADMIN), (int)(ACCESS_LEVEL_USER)); }
|
2015-07-15 12:02:21 +00:00
|
|
|
void ResetServerGameSettings();
|
2011-08-26 21:37:10 +00:00
|
|
|
// DDRace
|
|
|
|
|
2011-08-27 12:12:13 +00:00
|
|
|
static void ConUserCommandStatus(IConsole::IResult *pResult, void *pUser);
|
2011-08-31 13:41:32 +00:00
|
|
|
void SetFlagMask(int FlagMask) { m_FlagMask = FlagMask; }
|
2010-05-29 07:25:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|