Skip to content

Commit

Permalink
Added cvarhook from latest HLDS build
Browse files Browse the repository at this point in the history
Making mapcyclefile/sv_cheats work in realtime
Resolve #868
  • Loading branch information
s1lentq committed Sep 24, 2023
1 parent de3679f commit 6f03190
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 12 deletions.
12 changes: 12 additions & 0 deletions rehlds/common/cvardef.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,16 @@ struct cvar_listener_t
const char *name;
};

typedef void (*pfnCvar_HookVariable_t) (cvar_t *pCvar);

struct cvarhook_t
{
pfnCvar_HookVariable_t hook;

cvar_t *cvar;
cvarhook_t *next;
};

qboolean Cvar_HookVariable(const char *var_name, cvarhook_t *pHook);

#endif // CVARDEF_H
49 changes: 47 additions & 2 deletions rehlds/engine/cvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
All cvar names are case insensitive! Values not.
*/

cvar_t *cvar_vars;
cvar_t *cvar_vars = NULL;
cvarhook_t *cvar_hooks = NULL;
char cvar_null_string[] = "";

void Cvar_Init(void)
Expand Down Expand Up @@ -319,15 +320,26 @@ void Cvar_DirectSet(struct cvar_s *var, const char *value)

void Cvar_Set(const char *var_name, const char *value)
{
cvar_t *var = Cvar_FindVar(var_name);
cvar_t *var;
cvarhook_t *pHook;

var = Cvar_FindVar(var_name);
if (!var)
{
Con_DPrintf("%s: variable \"%s\" not found\n", __func__, var_name);
return;
}

Cvar_DirectSet(var, value);

for (pHook = cvar_hooks; pHook; pHook = pHook->next)
{
if (pHook->cvar == var)
{
pHook->hook(var);
break;
}
}
}

void Cvar_SetValue(const char *var_name, float value)
Expand Down Expand Up @@ -730,3 +742,36 @@ void Cvar_CmdInit(void)
{
Cmd_AddCommand("cvarlist", Cmd_CvarList_f);
}

qboolean Cvar_HookVariable(const char *var_name, cvarhook_t *pHook)
{
cvar_t *cvar;

if (!pHook || !pHook->hook)
return FALSE;

if (pHook->cvar || pHook->next)
return FALSE;

cvar = Cvar_FindVar(var_name);
if (!cvar)
return FALSE;

cvarhook_t *pCur = cvar_hooks;
pHook->cvar = cvar;

if (pCur)
{
while (pCur->next)
pCur = pCur->next;

pCur->next = pHook;
}
else
{
// First in chain is null, assign pHook to it
cvar_hooks = pHook;
}

return TRUE;
}
31 changes: 29 additions & 2 deletions rehlds/engine/host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ cvar_t deathmatch = { "deathmatch", "0", FCVAR_SERVER, 0.0f, NULL };
cvar_t coop = { "coop", "0", FCVAR_SERVER, 0.0f, NULL };

cvar_t sys_ticrate = { "sys_ticrate", "100.0", 0, 0.0f, NULL };

void sys_timescale_hook_callback(cvar_t *cvar);
cvarhook_t sys_timescale_hook = { sys_timescale_hook_callback, NULL, NULL };
cvar_t sys_timescale = { "sys_timescale", "1.0", 0, 0.0f, NULL };

cvar_t fps_max = { "fps_max", "100.0", FCVAR_ARCHIVE, 0.0f, NULL };
cvar_t host_killtime = { "host_killtime", "0.0", 0, 0.0f, NULL };
cvar_t sv_stats = { "sv_stats", "1", 0, 0.0f, NULL };
Expand Down Expand Up @@ -143,6 +147,9 @@ void Host_InitLocal(void)
Host_InitCommands();
Cvar_RegisterVariable(&host_killtime);
Cvar_RegisterVariable(&sys_ticrate);
Cvar_RegisterVariable(&sys_timescale);
Cvar_HookVariable(sys_timescale.name, &sys_timescale_hook);

Cvar_RegisterVariable(&fps_max);
Cvar_RegisterVariable(&fps_override);
Cvar_RegisterVariable(&host_name);
Expand Down Expand Up @@ -355,7 +362,7 @@ void SV_ClientPrintf(const char *fmt, ...)
{
va_list va;
char string[1024];

va_start(va, fmt);
Q_vsnprintf(string, ARRAYSIZE(string) - 1, fmt, va);
va_end(va);
Expand All @@ -367,7 +374,7 @@ void SV_ClientPrintf(const char *fmt, ...)
void EXT_FUNC SV_ClientPrintf_internal(const char *Dest)
{
char string[1024];

Q_strlcpy(string, Dest, min(strlen(Dest) + 1, sizeof(string)));
MSG_WriteByte(&host_client->netchan.message, svc_print);
MSG_WriteString(&host_client->netchan.message, string);
Expand Down Expand Up @@ -1275,3 +1282,23 @@ void Host_Shutdown(void)
g_psv.time = 0.0f;
g_pcl.time = 0.0f;
}

void sys_timescale_hook_callback(cvar_t *cvar)
{
int i;
client_t *client = NULL;

if (!Host_IsServerActive())
return;

for (i = 0; i < g_psvs.maxclients; i++)
{
client = &g_psvs.clients[i];

if (!client->fakeclient && (client->active || client->spawned || client->connected))
{
MSG_WriteByte(&client->netchan.message, svc_timescale);
MSG_WriteFloat(&client->netchan.message, max(0.1f, sys_timescale.value));
}
}
}
4 changes: 2 additions & 2 deletions rehlds/engine/net_ws.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ void NET_AdjustLag()
}
lasttime = realtime;

if (allow_cheats || fakelag.value == 0.0)
if (sv_cheats.value || fakelag.value == 0.0)
{
if (fakelag.value != gFakeLag)
{
Expand Down Expand Up @@ -689,7 +689,7 @@ qboolean NET_LagPacket(qboolean newdata, netsrc_t sock, netadr_t *from, sizebuf_
{
if (fakeloss.value != 0.0)
{
if (allow_cheats)
if (sv_cheats.value)
{
static int losscount[NS_MAX] = {};
++losscount[sock];
Expand Down
1 change: 0 additions & 1 deletion rehlds/engine/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,6 @@ extern cvar_t sv_proxies;
extern cvar_t sv_outofdatetime;
extern cvar_t mapchangecfgfile;

extern qboolean allow_cheats;
extern cvar_t mp_logecho;
extern cvar_t mp_logfile;
extern cvar_t sv_allow_download;
Expand Down
48 changes: 45 additions & 3 deletions rehlds/engine/sv_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ redirect_t sv_redirected;
netadr_t sv_redirectto;

GameType_e g_eGameType = GT_Unitialized;
qboolean allow_cheats;

char *gNullString = "";
int SV_UPDATE_BACKUP = SINGLEPLAYER_BACKUP;
Expand Down Expand Up @@ -130,6 +129,12 @@ cvar_t sv_waterfriction = { "sv_waterfriction", "1", FCVAR_SERVER, 0.0f, NULL };
cvar_t sv_zmax = { "sv_zmax", "4096", FCVAR_SPONLY, 0.0f, NULL };
cvar_t sv_wateramp = { "sv_wateramp", "0", 0, 0.0f, NULL };

void sv_cheats_hook_callback(cvar_t *cvar);
void mapcyclefile_hook_callback(cvar_t *cvar);

cvarhook_t sv_cheats_hook = { sv_cheats_hook_callback, NULL, NULL };
cvarhook_t mapcyclefile_hook = { mapcyclefile_hook_callback, NULL, NULL };

cvar_t sv_skyname = { "sv_skyname", "desert", 0, 0.0f, NULL };
cvar_t mapcyclefile = { "mapcyclefile", "mapcycle.txt", 0, 0.0f, NULL };
cvar_t motdfile = { "motdfile", "motd.txt", 0, 0.0f, NULL };
Expand Down Expand Up @@ -1173,7 +1178,7 @@ void SV_SendServerinfo_internal(sizebuf_t *msg, client_t *client)

MSG_WriteByte(msg, svc_sendextrainfo);
MSG_WriteString(msg, com_clientfallback);
MSG_WriteByte(msg, allow_cheats);
MSG_WriteByte(msg, sv_cheats.value != 0);

SV_WriteDeltaDescriptionsToClient(msg);
SV_SetMoveVars();
Expand Down Expand Up @@ -6257,7 +6262,6 @@ int SV_SpawnServer(qboolean bIsDemo, char *server, char *startspot)
gGlobalVariables.serverflags = g_psvs.serverflags;
gGlobalVariables.mapname = (size_t)g_psv.name - (size_t)pr_strings;
gGlobalVariables.startspot = (size_t)g_psv.startspot - (size_t)pr_strings;
allow_cheats = sv_cheats.value;
SV_SetMoveVars();

return 1;
Expand Down Expand Up @@ -6576,6 +6580,42 @@ void EXT_FUNC SV_SerializeSteamid(USERID_t* id, USERID_t* serialized)
*serialized = *id;
}

void sv_cheats_hook_callback(cvar_t *cvar)
{
int i;
client_t *client = NULL;

if (!Host_IsServerActive())
return;

for (i = 0; i < g_psvs.maxclients; i++)
{
client = &g_psvs.clients[i];

if (!client->fakeclient && (client->active || client->spawned || client->connected))
{
MSG_WriteByte(&client->netchan.message, svc_sendextrainfo);
MSG_WriteString(&client->netchan.message, "");
MSG_WriteByte(&client->netchan.message, sv_cheats.value != 0);
}
}
}

void mapcyclefile_hook_callback(cvar_t *cvar)
{
char buf[MAX_PATH + 4];

if (!Q_strcmp(COM_FileExtension(cvar->string), "txt"))
return;

Q_snprintf(buf, sizeof(buf) - 3, "%s.txt", cvar->string);

if (!Q_strcmp(COM_FileExtension(buf), "txt"))
Cvar_DirectSet(cvar, buf);
else
Cvar_DirectSet(cvar, "mapcycle.txt");
}

void SV_BanId_f(void)
{
char szreason[256];
Expand Down Expand Up @@ -8014,6 +8054,7 @@ void SV_Init(void)
Cvar_RegisterVariable(&sv_skyname);
Cvar_RegisterVariable(&sv_maxvelocity);
Cvar_RegisterVariable(&sv_cheats);
Cvar_HookVariable(sv_cheats.name, &sv_cheats_hook);
if (COM_CheckParm("-dev"))
Cvar_SetValue("sv_cheats", 1.0);
Cvar_RegisterVariable(&sv_spectatormaxspeed);
Expand All @@ -8025,6 +8066,7 @@ void SV_Init(void)
Cvar_RegisterVariable(&sv_logbans);
Cvar_RegisterVariable(&hpk_maxsize);
Cvar_RegisterVariable(&mapcyclefile);
Cvar_HookVariable(mapcyclefile.name, &mapcyclefile_hook);
Cvar_RegisterVariable(&motdfile);
Cvar_RegisterVariable(&servercfgfile);
Cvar_RegisterVariable(&mapchangecfgfile);
Expand Down
2 changes: 1 addition & 1 deletion rehlds/engine/zone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ void Cache_Force_Flush()

void Cache_Flush()
{
if (g_pcl.maxclients <= 1 || allow_cheats)
if (g_pcl.maxclients <= 1 || sv_cheats.value)
{
Cache_Force_Flush();
}
Expand Down
1 change: 1 addition & 0 deletions rehlds/msvc/ReHLDS.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@
</ProjectReference>
<ProjectReference Include="..\version\msvc\version.vcxproj">
<Project>{6973dca5-253c-4d84-b51e-187e035eae06}</Project>
<Private>false</Private>
</ProjectReference>
</ItemGroup>
<PropertyGroup Label="Globals">
Expand Down
4 changes: 3 additions & 1 deletion rehlds/rehlds/rehlds_api_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ char* EXT_FUNC GetClientFallback_api() {
}

int* EXT_FUNC GetAllowCheats_api() {
return &allow_cheats;
static int sv_cheats_stub = 0;
Con_Printf("WARNING! allow_cheats marked as deprecated! Use sv_cheats cvar directly!\n");
return &sv_cheats_stub;
}

bool EXT_FUNC GSBSecure_api() {
Expand Down

0 comments on commit 6f03190

Please sign in to comment.