Enhanced point_servercommand, point_clientcommand: Added reset of cvar values on remove an entity or change level

This commit is contained in:
s1lentq 2019-06-09 02:45:23 +07:00
parent 1146aa56fe
commit cee63d9703
3 changed files with 103 additions and 17 deletions

View File

@ -21,19 +21,18 @@
void CPointBaseCommand::KeyValue(KeyValueData *pkvd) void CPointBaseCommand::KeyValue(KeyValueData *pkvd)
{ {
// add this field to the command list // add this field to the command list
if (m_uiCommandsCount < MAX_POINT_CMDS) if (m_vecCommands.Count() < MAX_POINT_CMDS)
{ {
char command[128]; if (pkvd->szValue[0] != '\0' &&
if (pkvd->szValue[0] != '\0' && Q_strcmp(pkvd->szValue, "-") != 0) Q_strcmp(pkvd->szValue, "-") != 0)
{ {
Q_snprintf(command, sizeof(command), "%s \"%s\"", pkvd->szKeyName, pkvd->szValue); m_vecCommands.AddToTail({ pkvd->szKeyName, pkvd->szValue });
} }
else else
{ {
Q_strlcpy(command, pkvd->szKeyName); m_vecCommands.AddToTail(pkvd->szKeyName);
} }
m_iszCommands[m_uiCommandsCount++] = ALLOC_STRING(command);
pkvd->fHandled = TRUE; pkvd->fHandled = TRUE;
return; return;
} }
@ -61,12 +60,24 @@ void CPointClientCommand::Use(CBaseEntity *pActivator, CBaseEntity *pCaller, USE
if (pClient) if (pClient)
{ {
for (size_t cmd = 0; cmd < m_uiCommandsCount; cmd++) { for (auto &cmd : m_vecCommands) {
CLIENT_COMMAND(pClient, UTIL_VarArgs("%s\n", m_iszCommands[cmd].str())); Execute(pClient, "%s \"%s\"\n", cmd.name, cmd.value);
} }
} }
} }
void CPointClientCommand::Execute(edict_t *pEdict, const char *pszFmt, ...)
{
va_list argptr;
char command[128];
va_start(argptr, pszFmt);
Q_vsnprintf(command, sizeof(command), pszFmt, argptr);
va_end(argptr);
CLIENT_COMMAND(pEdict, command);
}
LINK_ENTITY_TO_CLASS(point_servercommand, CPointServerCommand, CCSPointServerCommand) LINK_ENTITY_TO_CLASS(point_servercommand, CPointServerCommand, CCSPointServerCommand)
void CPointServerCommand::Use(CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value) void CPointServerCommand::Use(CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value)
@ -78,20 +89,66 @@ void CPointServerCommand::Use(CBaseEntity *pActivator, CBaseEntity *pCaller, USE
} }
#endif #endif
for (size_t cmd = 0; cmd < m_uiCommandsCount; cmd++) { for (auto &cmd : m_vecCommands)
Execute(m_iszCommands[cmd]); {
cvar_t *pCVar = CVAR_GET_POINTER(cmd.name);
if (pCVar &&
pCVar->string &&
pCVar->string[0] != '\0' &&
Q_stricmp(cmd.value, pCVar->string) != 0) {
Q_strlcpy(cmd.valueInitial, pCVar->string);
}
if (cmd.value[0] != '\0')
{
Execute(nullptr, "%s \"%s\"\n", cmd.name, cmd.value);
}
else
{
Execute(nullptr, "%s\n", cmd.name);
}
} }
} }
void CPointServerCommand::Execute(const char *command) void CPointServerCommand::Execute(edict_t *pEdict, const char *pszFmt, ...)
{ {
va_list argptr;
char command[128];
va_start(argptr, pszFmt);
Q_vsnprintf(command, sizeof(command), pszFmt, argptr);
va_end(argptr);
if (!IS_DEDICATED_SERVER()) if (!IS_DEDICATED_SERVER())
{ {
// potentially dangerous for untrusted maps // potentially dangerous for untrusted maps
// so try to use it for passing through filtered svc_stufftext // so try to use it for passing through filtered svc_stufftext
CLIENT_COMMAND(INDEXENT(1), UTIL_VarArgs("%s\n", command)); CLIENT_COMMAND(pEdict ? pEdict : INDEXENT(1), command);
return; return;
} }
SERVER_COMMAND(UTIL_VarArgs("%s\n", command)); SERVER_COMMAND(command);
}
void CPointBaseCommand::OnDestroy()
{
if (!(pev->spawnflags & SF_POINT_CMD_NORESET))
{
bool bAtLeastOneCmdReset = false;
for (auto &cmd : m_vecCommands)
{
if (cmd.valueInitial[0] != '\0')
{
Execute(nullptr, "%s \"%s\"\n", cmd.name, cmd.valueInitial);
bAtLeastOneCmdReset = true;
}
}
if (bAtLeastOneCmdReset)
{
SERVER_EXECUTE();
}
}
m_vecCommands.RemoveAll();
} }

View File

@ -18,16 +18,41 @@
#pragma once #pragma once
#include <utlvector.h>
const int MAX_POINT_CMDS = 16; // maximum number of commands a single point_[server/client]command entity may be assigned const int MAX_POINT_CMDS = 16; // maximum number of commands a single point_[server/client]command entity may be assigned
#define SF_POINT_CMD_NORESET BIT(0) // it is not allowed to be resetting to initial value on remove an entity or change level
class CPointBaseCommand: public CPointEntity { class CPointBaseCommand: public CPointEntity {
public: public:
virtual void OnDestroy();
virtual void KeyValue(KeyValueData *pkvd); virtual void KeyValue(KeyValueData *pkvd);
virtual void Use(CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value) = 0; virtual void Use(CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value) = 0;
virtual void Execute(edict_t *pEdict, const char *pszFmt, ...) = 0;
protected: protected:
size_t m_uiCommandsCount;
string_t m_iszCommands[MAX_POINT_CMDS]; template <size_t SIZE>
struct command_t
{
command_t(const char *_name, const char *_value = nullptr)
{
value[0] = '\0';
valueInitial[0] = '\0';
Q_strlcpy(name, _name);
if (_value)
{
Q_strlcpy(value, _value);
}
}
char name[SIZE], value[SIZE], valueInitial[SIZE];
};
CUtlVector<command_t<64u>> m_vecCommands;
}; };
// It issues commands to the client console // It issues commands to the client console
@ -36,7 +61,7 @@ public:
virtual void Use(CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value); virtual void Use(CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value);
private: private:
void Execute(edict_t *pEdict, const char *command); void Execute(edict_t *pEdict, const char *pszFmt, ...);
}; };
// It issues commands to the server console // It issues commands to the server console
@ -45,5 +70,5 @@ public:
virtual void Use(CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value); virtual void Use(CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value);
private: private:
void Execute(const char *command); void Execute(edict_t *pEdict, const char *pszFmt, ...);
}; };

View File

@ -3029,6 +3029,10 @@
@BaseClass base(Targetname) = BaseCommand @BaseClass base(Targetname) = BaseCommand
[ [
spawnflags(flags) =
[
1 : "No reset cmd on remove an entity or change level" : 0
]
] ]
@PointClass base(BaseCommand) size(-8 -8 -8, 8 8 8) = point_servercommand : "It issues commands to the server console" @PointClass base(BaseCommand) size(-8 -8 -8, 8 8 8) = point_servercommand : "It issues commands to the server console"