X-Git-Url: https://git.xonotic.org/?a=blobdiff_plain;f=cvar.c;h=642080e75809623e4a1b95dbe164107b7e810775;hb=b8692b8cd73685794b59b0522e8caa4b9d9a2dac;hp=4adee7458a4f774247f8ec4c32d6a0b3aea071f0;hpb=add200ca17e19a0a85eed9cc78721aa1ae74215b;p=xonotic%2Fdarkplaces.git diff --git a/cvar.c b/cvar.c index 4adee745..642080e7 100644 --- a/cvar.c +++ b/cvar.c @@ -1,5 +1,6 @@ /* Copyright (C) 1996-1997 Id Software, Inc. +Copyright (C) 2000-2021 DarkPlaces contributors This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -43,8 +44,8 @@ cvar_t *Cvar_FindVar(cvar_state_t *cvars, const char *var_name, int neededflags) if (!strcmp (var_name, hash->cvar->name) && (hash->cvar->flags & neededflags)) return hash->cvar; else - for (int i = 0; i < hash->cvar->aliasindex; i++) - if (!strcmp (var_name, hash->cvar->aliases[i]) && (hash->cvar->flags & neededflags)) + for (char **alias = hash->cvar->aliases; alias && *alias; alias++) + if (!strcmp (var_name, *alias) && (hash->cvar->flags & neededflags)) return hash->cvar; return NULL; } @@ -88,8 +89,8 @@ static cvar_t *Cvar_FindVarLink(cvar_state_t *cvars, const char *var_name, cvar_ if (!strcmp (var_name, hash->cvar->name) && (hash->cvar->flags & neededflags)) goto match; else - for (int i = 0; i < hash->cvar->aliasindex; i++) - if (!strcmp (var_name, hash->cvar->aliases[i]) && (hash->cvar->flags & neededflags)) + for (char **alias = hash->cvar->aliases; alias && *alias; alias++) + if (!strcmp (var_name, *alias) && (hash->cvar->flags & neededflags)) goto match; if(parent) *parent = hash->cvar; } @@ -226,8 +227,8 @@ int Cvar_CompleteCountPossible(cvar_state_t *cvars, const char *partial, int nee if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags)) h++; else - for(int i = 0; i < cvar->aliasindex; i++) - if (!strncasecmp(partial, cvar->aliases[i], len) && (cvar->flags & neededflags)) + for (char **alias = cvar->aliases; alias && *alias; alias++) + if (!strncasecmp(partial, *alias, len) && (cvar->flags & neededflags)) h++; return h; @@ -257,9 +258,9 @@ const char **Cvar_CompleteBuildList(cvar_state_t *cvars, const char *partial, in if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags)) buf[bpos++] = cvar->name; else - for(int i = 0; i < cvar->aliasindex; i++) - if (!strncasecmp(partial, cvar->aliases[i], len) && (cvar->flags & neededflags)) - buf[bpos++] = cvar->aliases[i]; + for (char **alias = cvar->aliases; alias && *alias; alias++) + if (!strncasecmp(partial, *alias, len) && (cvar->flags & neededflags)) + buf[bpos++] = *alias; buf[bpos] = NULL; @@ -270,15 +271,13 @@ void Cvar_PrintHelp(cvar_t *cvar, const char *name, qbool full) { // Aliases are purple, cvars are yellow if (strcmp(cvar->name, name)) - Con_Printf("^6"); + Con_Printf("^6%s^7 (alias of ^3%s^7)", name, cvar->name); else - Con_Printf("^3"); - Con_Printf("%s^7 is \"%s\" [\"%s\"]", name, ((cvar->flags & CF_PRIVATE) ? "********"/*hunter2*/ : cvar->string), cvar->defstring); - if (strcmp(cvar->name, name)) - Con_Printf(" (also ^3%s^7)", cvar->name); + Con_Printf("^3%s^7", name); + Con_Printf(" is \"%s^7\" [\"%s^7\"]", ((cvar->flags & CF_PRIVATE) ? "********"/*hunter2*/ : cvar->string), cvar->defstring); if (full) Con_Printf(" %s", cvar->description); - Con_Printf("\n"); + Con_Print("\n"); } // written by LadyHavoc @@ -291,9 +290,9 @@ void Cvar_CompleteCvarPrint(cvar_state_t *cvars, const char *partial, int needed if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags)) Cvar_PrintHelp(cvar, cvar->name, true); else - for (int i = 0; i < cvar->aliasindex; i++) - if (!strncasecmp (partial, cvar->aliases[i], len) && (cvar->flags & neededflags)) - Cvar_PrintHelp(cvar, cvar->aliases[i], true); + for (char **alias = cvar->aliases; alias && *alias; alias++) + if (!strncasecmp (partial, *alias, len) && (cvar->flags & neededflags)) + Cvar_PrintHelp(cvar, *alias, true); } @@ -365,6 +364,18 @@ void Cvar_UpdateAllAutoCvars(cvar_state_t *cvars) Cvar_UpdateAutoCvar(var); } +void Cvar_Callback(cvar_t *var) +{ + if (var == NULL) + { + Con_Print(CON_WARN "Cvar_Callback: var == NULL\n"); + return; + } + + if(var->callback) + var->callback(var); +} + /* ============ Cvar_Set @@ -379,7 +390,7 @@ static void Cvar_SetQuick_Internal (cvar_t *var, const char *value) changed = strcmp(var->string, value) != 0; // LadyHavoc: don't reallocate when there is no change if (!changed) - return; + goto cvar_callback; // LadyHavoc: don't reallocate when the buffer is the same size valuelen = strlen(value); @@ -412,19 +423,25 @@ static void Cvar_SetQuick_Internal (cvar_t *var, const char *value) Cvar_UpdateAutoCvar(var); +cvar_callback: // Call the function stored in the cvar for bounds checking, cleanup, etc - if (var->callback) - var->callback(var); + Cvar_Callback(var); } void Cvar_SetQuick (cvar_t *var, const char *value) { if (var == NULL) { - Con_Print("Cvar_SetQuick: var == NULL\n"); + Con_Print(CON_WARN "Cvar_SetQuick: var == NULL\n"); return; } + if (!(var->flags & CF_REGISTERED) && !(var->flags & CF_ALLOCATED)) + { + Con_Printf(CON_WARN "Warning: Cvar_SetQuick() cannot set unregistered cvar \"%s\"\n", var->name); + return; // setting an unregistered engine cvar crashes + } + if (developer_extra.integer) Con_DPrintf("Cvar_SetQuick({\"%s\", \"%s\", %i, \"%s\"}, \"%s\");\n", var->name, var->string, var->flags, var->defstring, value); @@ -437,7 +454,7 @@ void Cvar_Set(cvar_state_t *cvars, const char *var_name, const char *value) var = Cvar_FindVar(cvars, var_name, ~0); if (var == NULL) { - Con_Printf("Cvar_Set: variable %s not found\n", var_name); + Con_Printf(CON_WARN "Cvar_Set: variable %s not found\n", var_name); return; } Cvar_SetQuick(var, value); @@ -472,24 +489,52 @@ void Cvar_SetValue(cvar_state_t *cvars, const char *var_name, float value) void Cvar_RegisterCallback(cvar_t *variable, void (*callback)(cvar_t *)) { + if (variable == NULL) + { + Con_Print(CON_WARN "Cvar_RegisterCallback: var == NULL\n"); + return; + } variable->callback = callback; } -void Cvar_RegisterAlias(cvar_t *variable, const char *alias ) +void Cvar_RegisterVirtual(cvar_t *variable, const char *name ) { cvar_state_t *cvars = &cvars_all; cvar_hash_t *hash; int hashindex; - variable->aliases = (char **)Mem_Realloc(zonemempool, variable->aliases, sizeof(char *) * (variable->aliasindex + 1)); - // Add to it - variable->aliases[variable->aliasindex] = (char *)Z_Malloc(strlen(alias) + 1); - memcpy(variable->aliases[variable->aliasindex], alias, strlen(alias) + 1); - variable->aliasindex++; + if (cls.state == ca_dedicated && !(variable->flags & CF_SERVER)) + return; + + if(!*name) + { + Con_Printf(CON_WARN "Cvar_RegisterVirtual: invalid virtual cvar name\n"); + return; + } + + // check for overlap with a command + if (Cmd_Exists(cmd_local, name)) + { + Con_Printf(CON_WARN "Cvar_RegisterVirtual: %s is a command\n", name); + return; + } + + if(Cvar_FindVar(&cvars_all, name, 0)) + { + Con_Printf(CON_WARN "Cvar_RegisterVirtual: %s is a cvar\n", name); + return; + } + + // Resize the variable->aliases list to have room for another entry and a null terminator. + // This zero-pads when resizing, so we don't need to write the NULL terminator manually here. + // Also if aliases is NULL this allocates fresh for the correct size, so it's fine to just do this. + variable->aliases = (char **)Z_Realloc(variable->aliases, sizeof(char *) * (variable->aliases_size + 2)); + // Add the new alias, and increment the number of aliases in the list + variable->aliases[variable->aliases_size++] = (char *)Z_strdup(name); // link to head of list in this hash table index hash = (cvar_hash_t *)Z_Malloc(sizeof(cvar_hash_t)); - hashindex = CRC_Block((const unsigned char *)alias, strlen(alias)) % CVAR_HASHSIZE; + hashindex = CRC_Block((const unsigned char *)name, strlen(name)) % CVAR_HASHSIZE; hash->next = cvars->hashtable[hashindex]; cvars->hashtable[hashindex] = hash; hash->cvar = variable; @@ -542,7 +587,9 @@ void Cvar_RegisterVariable (cvar_t *variable) switch (variable->flags & (CF_CLIENT | CF_SERVER)) { - case CF_CLIENT: + case CF_CLIENT: // client-only cvar + if (cls.state == ca_dedicated) + return; case CF_SERVER: case CF_CLIENT | CF_SERVER: cvars = &cvars_all; @@ -570,7 +617,7 @@ void Cvar_RegisterVariable (cvar_t *variable) // (because the engine directly accesses fixed variables) // NOTE: this isn't actually used currently // (all cvars are registered before config parsing) - variable->flags |= (cvar->flags & ~CF_ALLOCATED); + variable->flags &= ~CF_ALLOCATED; // cvar->string is now owned by variable instead variable->string = cvar->string; variable->defstring = cvar->defstring; @@ -605,9 +652,9 @@ void Cvar_RegisterVariable (cvar_t *variable) } // check for overlap with a command - if (Cmd_Exists(&cmd_client, variable->name) || Cmd_Exists(&cmd_server, variable->name)) + if (Cmd_Exists(cmd_local, variable->name)) { - Con_Printf("Cvar_RegisterVariable: %s is a command\n", variable->name); + Con_Printf(CON_WARN "Cvar_RegisterVariable: %s is a command\n", variable->name); return; } @@ -617,12 +664,17 @@ void Cvar_RegisterVariable (cvar_t *variable) variable->defstring = (char *)Mem_strdup(zonemempool, variable->string); variable->value = atof (variable->string); variable->integer = (int) variable->value; - variable->aliasindex = 0; + variable->aliases = NULL; + variable->aliases_size = 0; + variable->initstate = NULL; // Mark it as not an autocvar. for (i = 0;i < PRVM_PROG_MAX;i++) variable->globaldefindex[i] = -1; + // Safe for Cvar_SetQuick() + variable->flags |= CF_REGISTERED; + Cvar_Link(variable, cvars); } @@ -663,14 +715,14 @@ cvar_t *Cvar_Get(cvar_state_t *cvars, const char *name, const char *value, int f // check for pure evil if (!*name) { - Con_Printf("Cvar_Get: invalid variable name\n"); + Con_Printf(CON_WARN "Cvar_Get: invalid variable name\n"); return NULL; } // check for overlap with a command - if (Cmd_Exists(&cmd_client, name) || Cmd_Exists(&cmd_server, name)) + if (Cmd_Exists(cmd_local, name)) { - Con_Printf("Cvar_Get: %s is a command\n", name); + Con_Printf(CON_WARN "Cvar_Get: %s is a command\n", name); return NULL; } @@ -684,8 +736,9 @@ cvar_t *Cvar_Get(cvar_state_t *cvars, const char *name, const char *value, int f cvar->defstring = (char *)Mem_strdup(zonemempool, value); cvar->value = atof (cvar->string); cvar->integer = (int) cvar->value; - cvar->aliases = (char **)Z_Malloc(sizeof(char **)); - memset(cvar->aliases, 0, sizeof(char *)); + cvar->aliases = NULL; + cvar->aliases_size = 0; + cvar->initstate = NULL; if(newdescription && *newdescription) cvar->description = (char *)Mem_strdup(zonemempool, newdescription); @@ -705,10 +758,10 @@ qbool Cvar_Readonly (cvar_t *var, const char *cmd_name) { if (var->flags & CF_READONLY) { + Con_Print(CON_WARN); if(cmd_name) Con_Printf("%s: ",cmd_name); - Con_Printf("%s", var->name); - Con_Printf(" is read-only\n"); + Con_Printf("%s is read-only\n", var->name); return true; } return false; @@ -785,13 +838,8 @@ void Cvar_SaveInitState(cvar_state_t *cvars) cvar_t *c; for (c = cvars->vars;c;c = c->next) { - c->initstate = true; - c->initflags = c->flags; - c->initdefstring = Mem_strdup(zonemempool, c->defstring); - c->initstring = Mem_strdup(zonemempool, c->string); - c->initvalue = c->value; - c->initinteger = c->integer; - VectorCopy(c->vector, c->initvector); + c->initstate = (cvar_t *)Z_Malloc(sizeof(cvar_t)); + memcpy(c->initstate, c, sizeof(cvar_t)); } } @@ -805,22 +853,22 @@ void Cvar_RestoreInitState(cvar_state_t *cvars) if (c->initstate) { // restore this cvar, it existed at init - if (((c->flags ^ c->initflags) & CF_MAXFLAGSVAL) - || strcmp(c->defstring ? c->defstring : "", c->initdefstring ? c->initdefstring : "") - || strcmp(c->string ? c->string : "", c->initstring ? c->initstring : "")) + if (((c->flags ^ c->initstate->flags) & CF_MAXFLAGSVAL) + || strcmp(c->defstring ? c->defstring : "", c->initstate->defstring ? c->initstate->defstring : "") + || strcmp(c->string ? c->string : "", c->initstate->string ? c->initstate->string : "")) { Con_DPrintf("Cvar_RestoreInitState: Restoring cvar \"%s\"\n", c->name); if (c->defstring) Z_Free((char *)c->defstring); - c->defstring = Mem_strdup(zonemempool, c->initdefstring); + c->defstring = Mem_strdup(zonemempool, c->initstate->defstring); if (c->string) Z_Free((char *)c->string); - c->string = Mem_strdup(zonemempool, c->initstring); + c->string = Mem_strdup(zonemempool, c->initstate->string); } - c->flags = c->initflags; - c->value = c->initvalue; - c->integer = c->initinteger; - VectorCopy(c->initvector, c->vector); + c->flags = c->initstate->flags; + c->value = c->initstate->value; + c->integer = c->initstate->integer; + VectorCopy(c->initstate->vector, c->vector); cp = &c->next; } else @@ -970,11 +1018,11 @@ void Cvar_List_f(cmd_state_t *cmd) Cvar_PrintHelp(cvar, cvar->name, true); count++; } - for (int i = 0; i < cvar->aliasindex; i++) + for (char **alias = cvar->aliases; alias && *alias; alias++) { - if (matchpattern_with_separator(cvar->aliases[i], partial, false, "", false)) + if (matchpattern_with_separator(*alias, partial, false, "", false)) { - Cvar_PrintHelp(cvar, cvar->aliases[i], true); + Cvar_PrintHelp(cvar, *alias, true); count++; } }