]> git.rm.cloudns.org Git - xonotic/darkplaces.git/commitdiff
Merge branch 'master' into Cloudwalk/cvar_overhaul Cloudwalk/cvar_overhaul
authorCloudwalk <cloudwalk@icculus.org>
Sat, 18 Sep 2021 21:55:44 +0000 (17:55 -0400)
committerCloudwalk <cloudwalk@icculus.org>
Sat, 18 Sep 2021 21:55:44 +0000 (17:55 -0400)
1  2 
cmd.c
cvar.c
prvm_cmds.c
prvm_edict.c

diff --cc cmd.c
index 3ff1d5f0e59e7415c83ed9421a851acd0410bd05,e8f168896d963c39ed1c9673e3aaaa2f2f4d5c86..a0bc484c33563a5e2d560e953d2492737dc96dc6
--- 1/cmd.c
--- 2/cmd.c
+++ b/cmd.c
@@@ -1580,7 -1579,7 +1580,7 @@@ static void Cmd_Apropos_f(cmd_state_t *
                partial = va(vabuf, sizeof(vabuf), "*%s*", partial);
  
        count = 0;
-       List_For_Each_Entry(cvar, &cmd->cvars->vars->list, list)
 -      for (cvar = cmd->cvars->vars; cvar; cvar = cvar->next)
++      List_For_Each_Entry(cvar, &cmd->cvars->vars->list, cvar_t, list)
        {
                if (matchpattern_with_separator(cvar->name, partial, true, "", false) ||
                    matchpattern_with_separator(cvar->description, partial, true, "", false))
diff --cc cvar.c
index 541ca9efba1341a356ef5df28b95db7da45a4360,7936c92356be359c13748eca7afbec3250df8ac2..0f5a97cbb36d36b2c024b8ef0b4df2e4d2d3b90c
--- 1/cvar.c
--- 2/cvar.c
+++ b/cvar.c
@@@ -55,18 -59,17 +55,18 @@@ cvar_t *Cvar_FindVarAfter(cvar_state_t 
                var = Cvar_FindVar(cvars, prev_var_name, neededflags);
                if (!var)
                        return NULL;
-               var = List_Next_Entry(var->parent ? var->parent : var, list);
 -              var = var->next;
++              var = List_Next_Entry(var->parent ? var->parent : var, cvar_t, list);
        }
        else
                var = cvars->vars;
  
        // search for the next cvar matching the needed flags
-       List_For_Each_Entry_From(var, &cvars->vars->list, list)
 -      while (var)
++      List_For_Each_Entry_From(var, &cvars->vars->list, cvar_t, list)
        {
 +              if (var->parent)
 +                      continue;
                if (var->flags & neededflags)
                        break;
 -              var = var->next;
        }
        return var;
  }
@@@ -158,7 -194,7 +158,7 @@@ const char *Cvar_CompleteVariable(cvar_
                return NULL;
  
  // check functions
-       List_For_Each_Entry(cvar, &cvars->vars->list, list)
 -      for (cvar=cvars->vars ; cvar ; cvar=cvar->next)
++      List_For_Each_Entry(cvar, &cvars->vars->list, cvar_t, list)
                if (!strncasecmp (partial,cvar->name, len) && (cvar->flags & neededflags))
                        return cvar->name;
  
@@@ -186,9 -223,14 +186,9 @@@ int Cvar_CompleteCountPossible(cvar_sta
                return  0;
  
        // Loop through the cvars and count all possible matches
-       List_For_Each_Entry(cvar, &cvars->vars->list, list)
 -      for (cvar = cvars->vars; cvar; cvar = cvar->next)
++      List_For_Each_Entry(cvar, &cvars->vars->list, cvar_t, list)
                if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
                        h++;
 -              else
 -                      for (char **alias = cvar->aliases; alias && *alias; alias++)
 -                              if (!strncasecmp(partial, *alias, len) && (cvar->flags & neededflags))
 -                                      h++;
 -              
        return h;
  }
  
@@@ -212,9 -254,14 +212,9 @@@ const char **Cvar_CompleteBuildList(cva
        len = strlen(partial);
        buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof(const char *));
        // Loop through the alias list and print all matches
-       List_For_Each_Entry(cvar, &cvars->vars->list, list)
 -      for (cvar = cvars->vars; cvar; cvar = cvar->next)
++      List_For_Each_Entry(cvar, &cvars->vars->list, cvar_t, list)
                if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
                        buf[bpos++] = cvar->name;
 -              else
 -                      for (char **alias = cvar->aliases; alias && *alias; alias++)
 -                              if (!strncasecmp(partial, *alias, len) && (cvar->flags & neededflags))
 -                                      buf[bpos++] = *alias;
 -              
  
        buf[bpos] = NULL;
        return buf;
@@@ -241,9 -288,15 +241,9 @@@ void Cvar_CompleteCvarPrint(cvar_state_
        cvar_t *cvar;
        size_t len = strlen(partial);
        // Loop through the command list and print all matches
-       List_For_Each_Entry(cvar, &cvars->vars->list, list)
 -      for (cvar = cvars->vars; cvar; cvar = cvar->next)
++      List_For_Each_Entry(cvar, &cvars->vars->list, cvar_t, list)
                if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
 -                      Cvar_PrintHelp(cvar, cvar->name, true);
 -              else
 -                      for (char **alias = cvar->aliases; alias && *alias; alias++)
 -                              if (!strncasecmp (partial, *alias, len) && (cvar->flags & neededflags))
 -                                      Cvar_PrintHelp(cvar, *alias, true);
 -
 -              
 +                      Cvar_PrintHelp(cvar->parent ? cvar->parent : cvar, cvar->name, true);
  }
  
  // check if a cvar is held by some progs
@@@ -309,12 -362,8 +309,12 @@@ static void Cvar_UpdateAutoCvar(cvar_t 
  void Cvar_UpdateAllAutoCvars(cvar_state_t *cvars)
  {
        cvar_t *var;
-       List_For_Each_Entry(var, &cvars->vars->list, list)
 -      for (var = cvars->vars ; var ; var = var->next)
++      List_For_Each_Entry(var, &cvars->vars->list, cvar_t, list)
 +      {
 +              if(var->parent)
 +                      continue;
                Cvar_UpdateAutoCvar(var);
 +      }
  }
  
  void Cvar_Callback(cvar_t *var)
@@@ -424,32 -482,6 +424,32 @@@ void Cvar_SetValue(cvar_state_t *cvars
        Cvar_Set(cvars, var_name, val);
  }
  
-       List_For_Each_Entry(current, &cvars->vars->list, list)
 +/*
 +============
 +Cvar_Link
 +
 +Links a variable to the variable list and hashtable
 +============
 +*/
 +static void Cvar_Link(cvar_t *variable, cvar_state_t *cvars)
 +{
 +      cvar_t *current;
 +      int hashindex;
 +
 +      // Link the object in alphanumerical order
++      List_For_Each_Entry(current, &cvars->vars->list, cvar_t, list)
 +              if(strcmp(current->name, variable->name) > 0)
 +                      break;
 +
 +      List_Add_Tail(&variable->list, &current->list);
 +
 +      // link to head of list in this hash table index
 +      hashindex = CRC_Block((const unsigned char *)variable->name, strlen(variable->name)) % CVAR_HASHSIZE;
 +      variable->hnext = cvars->hashtable[hashindex];
 +      cvars->hashtable[hashindex] = variable;
 +      variable->hashindex = hashindex;
 +}
 +
  void Cvar_RegisterCallback(cvar_t *variable, void (*callback)(cvar_t *))
  {
        if (variable == NULL)
        variable->callback = callback;
  }
  
 -void Cvar_RegisterVirtual(cvar_t *variable, const char *name )
 +static void Cvar_DeleteVirtual(cvar_t *vcvar)
 +{
 +      List_Delete(&vcvar->vlist);
 +      List_Delete(&vcvar->list);
 +      Mem_Free((char *)vcvar->name);
 +      Mem_Free(vcvar);
 +}
 +
 +static void Cvar_DeleteVirtual_All(cvar_t *var)
 +{
 +      cvar_t *vcvar, *vcvar_next;
 +
-       List_For_Each_Entry_Safe(vcvar, vcvar_next, &var->vlist, vlist)
++      List_For_Each_Entry_Safe(vcvar, vcvar_next, &var->vlist, cvar_t, vlist)
 +              Cvar_DeleteVirtual(vcvar);
 +}
 +
 +void Cvar_RegisterVirtual(cvar_t *variable, const char *name)
  {
        cvar_state_t *cvars = &cvars_all;
 -      cvar_hash_t *hash;
 -      int hashindex;
 +      cvar_t *vcvar;
  
        if(!*name)
        {
@@@ -765,7 -795,7 +765,7 @@@ void Cvar_UnlockDefaults(cmd_state_t *c
        cvar_state_t *cvars = cmd->cvars;
        cvar_t *var;
        // unlock the default values of all cvars
-       List_For_Each_Entry(var, &cvars->vars->list, list)
 -      for (var = cvars->vars ; var ; var = var->next)
++      List_For_Each_Entry(var, &cvars->vars->list, cvar_t, list)
                var->flags &= ~CF_DEFAULTSET;
  }
  
@@@ -774,9 -804,9 +774,9 @@@ void Cvar_LockDefaults_f(cmd_state_t *c
        cvar_state_t *cvars = cmd->cvars;
        cvar_t *var;
        // lock in the default values of all cvars
-       List_For_Each_Entry(var, &cvars->vars->list, list)
 -      for (var = cvars->vars ; var ; var = var->next)
++      List_For_Each_Entry(var, &cvars->vars->list, cvar_t, list)
        {
 -              if (!(var->flags & CF_DEFAULTSET))
 +              if (!var->parent && !(var->flags & CF_DEFAULTSET))
                {
                        size_t alloclen;
  
  
  void Cvar_SaveInitState(cvar_state_t *cvars)
  {
 -      cvar_t *c;
 -      for (c = cvars->vars;c;c = c->next)
 +      cvar_t *cvar, *vcvar;
 +
-       List_For_Each_Entry(cvar, &cvars->vars->list, list)
++      List_For_Each_Entry(cvar, &cvars->vars->list, cvar_t, list)
        {
 -              c->initstate = (cvar_t *)Z_Malloc(sizeof(cvar_t));
 -              memcpy(c->initstate, c, sizeof(cvar_t));
 +              // Get the the virtual cvars separately
 +              if(cvar->parent)
 +                      continue;
 +
 +              cvar->initstate = (cvar_t *)Mem_Alloc(cvars->mempool, sizeof(cvar_t));
 +              memcpy(cvar->initstate, cvar, sizeof(cvar_t));
 +
 +              if(cvar->description == cvar_dummy_description)
 +                      cvar->initstate->description = cvar_dummy_description;
 +              else
 +                      cvar->initstate->description = (char *)Mem_strdup(cvars->mempool, cvar->description);
 +
 +              cvar->initstate->string = (char *)Mem_strdup(cvars->mempool, cvar->string);
 +              cvar->initstate->defstring = (char *)Mem_strdup(cvars->mempool, cvar->defstring);
 +
 +              /*
 +               * Consider any virtual cvar created up to this point as
 +               * existing during init. Use the initstate of the parent cvar.
 +               */
-               List_For_Each_Entry(vcvar, &cvar->vlist, list)
++              List_For_Each_Entry(vcvar, &cvar->vlist, cvar_t, list)
 +                      vcvar->initstate = cvar->initstate;
        }
  }
  
  void Cvar_RestoreInitState(cvar_state_t *cvars)
  {
 -      int hashindex;
 -      cvar_t *c, **cp;
 -      cvar_t *c2, **cp2;
 -      for (cp = &cvars->vars;(c = *cp);)
 +      cvar_t *var, *next;
 +
-       List_For_Each_Entry_Safe(var, next, &cvars->vars->list, list)
++      List_For_Each_Entry_Safe(var, next, &cvars->vars->list, cvar_t, list)
        {
 -              if (c->initstate)
 +              // Destroy all virtual cvars that didn't exist at init
 +              if(var->parent && !var->initstate)
 +              {
 +                      Cvar_DeleteVirtual(var);
 +                      continue;
 +              }
 +
 +              if (var->initstate)
                {
                        // restore this cvar, it existed at init
 -                      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", var->name);
 +                      if(var->flags & CF_ALLOCATED)
                        {
 -                              Con_DPrintf("Cvar_RestoreInitState: Restoring cvar \"%s\"\n", c->name);
 -                              if (c->defstring)
 -                                      Z_Free((char *)c->defstring);
 -                              c->defstring = Mem_strdup(zonemempool, c->initstate->defstring);
 -                              if (c->string)
 -                                      Z_Free((char *)c->string);
 -                              c->string = Mem_strdup(zonemempool, c->initstate->string);
 +                              if(var->flags & CF_ALLOCATED && var->description && var->description != cvar_dummy_description)
 +                                      Z_Free((char *)var->description);
 +                              if(var->initstate->description == cvar_dummy_description)
 +                                      var->description = cvar_dummy_description;
 +                              else
 +                                      var->initstate->description = (char *)Mem_strdup(cvars->mempool, var->description);
                        }
 -                      c->flags = c->initstate->flags;
 -                      c->value = c->initstate->value;
 -                      c->integer = c->initstate->integer;
 -                      VectorCopy(c->initstate->vector, c->vector);
 -                      cp = &c->next;
 +
 +                      if (var->defstring)
 +                              Z_Free((char *)var->defstring);
 +                      var->defstring = Mem_strdup(cvars->mempool, var->initstate->defstring);
 +                      if (var->string)
 +                              Z_Free((char *)var->string);
 +                      var->string = Mem_strdup(cvars->mempool, var->initstate->string);
 +
 +                      var->flags = var->initstate->flags;
 +                      var->value = var->initstate->value;
 +                      var->integer = var->initstate->integer;
 +                      VectorCopy(var->initstate->vector, var->vector);
                }
                else
                {
@@@ -885,9 -912,9 +885,9 @@@ void Cvar_ResetToDefaults_All_f(cmd_sta
        cvar_state_t *cvars = cmd->cvars;
        cvar_t *var;
        // restore the default values of all cvars
-       List_For_Each_Entry(var, &cvars->vars->list, list)
 -      for (var = cvars->vars ; var ; var = var->next)
++      List_For_Each_Entry(var, &cvars->vars->list, cvar_t, list)
        {
 -              if((var->flags & CF_PERSISTENT) == 0)
 +              if(!var->parent && !(var->flags & CF_PERSISTENT))
                        Cvar_SetQuick(var, var->defstring);
        }
  }
@@@ -897,9 -924,9 +897,9 @@@ void Cvar_ResetToDefaults_NoSaveOnly_f(
        cvar_state_t *cvars = cmd->cvars;
        cvar_t *var;
        // restore the default values of all cvars
-       List_For_Each_Entry(var, &cvars->vars->list, list)
 -      for (var = cvars->vars ; var ; var = var->next)
++      List_For_Each_Entry(var, &cvars->vars->list, cvar_t, list)
        {
 -              if ((var->flags & (CF_PERSISTENT | CF_ARCHIVE)) == 0)
 +              if (!var->parent && !(var->flags & (CF_PERSISTENT | CF_ARCHIVE)))
                        Cvar_SetQuick(var, var->defstring);
        }
  }
@@@ -909,9 -937,9 +909,9 @@@ void Cvar_ResetToDefaults_SaveOnly_f(cm
        cvar_state_t *cvars = cmd->cvars;
        cvar_t *var;
        // restore the default values of all cvars
-       List_For_Each_Entry(var, &cvars->vars->list, list)
 -      for (var = cvars->vars ; var ; var = var->next)
++      List_For_Each_Entry(var, &cvars->vars->list, cvar_t, list)
        {
 -              if ((var->flags & (CF_PERSISTENT | CF_ARCHIVE)) == CF_ARCHIVE)
 +              if (!var->parent && (var->flags & (CF_PERSISTENT | CF_ARCHIVE)) == CF_ARCHIVE)
                        Cvar_SetQuick(var, var->defstring);
        }
  }
@@@ -930,10 -958,7 +930,10 @@@ void Cvar_WriteVariables (cvar_state_t 
        char buf1[MAX_INPUTLINE], buf2[MAX_INPUTLINE];
  
        // don't save cvars that match their default value
-       List_For_Each_Entry(var, &cvars->vars->list, list)
 -      for (var = cvars->vars ; var ; var = var->next) {
++      List_For_Each_Entry(var, &cvars->vars->list, cvar_t, list)
 +      {
 +              if(var->parent)
 +                      continue;
                if ((var->flags & CF_ARCHIVE) && (strcmp(var->string, var->defstring) || ((var->flags & CF_ALLOCATED) && !(var->flags & CF_DEFAULTSET))))
                {
                        Cmd_QuoteString(buf1, sizeof(buf1), var->name, "\"\\$", false);
@@@ -973,7 -998,7 +973,7 @@@ void Cvar_List_f(cmd_state_t *cmd
        }
  
        count = 0;
-       List_For_Each_Entry(cvar, &cvars->vars->list, list)
 -      for (cvar = cvars->vars; cvar; cvar = cvar->next)
++      List_For_Each_Entry(cvar, &cvars->vars->list, cvar_t, list)
        {
                if (matchpattern_with_separator(cvar->name, partial, false, "", false))
                {
diff --cc prvm_cmds.c
index c0b50beb0c46b3feb8f7f2f95e3ca4850799ea1a,3ef0b74cf939fc417757249a4e328f1a5d0dcfb8..89a762ef5b612a74a3972ad54b275b71166ca9f9
@@@ -4630,7 -4630,7 +4630,7 @@@ void VM_buf_cvarlist(prvm_prog_t *prog
        antiispattern = antipartial && (strchr(antipartial, '*') || strchr(antipartial, '?'));
  
        n = 0;
-       List_For_Each_Entry(cvar, &prog->console_cmd->cvars->vars->list, list)
 -      for(cvar = prog->console_cmd->cvars->vars; cvar; cvar = cvar->next)
++      List_For_Each_Entry(cvar, &prog->console_cmd->cvars->vars->list, cvar_t, list)
        {
                if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
                        continue;
                stringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(stringbuffer->strings[0]) * stringbuffer->max_strings);
        
        n = 0;
-       List_For_Each_Entry(cvar, &prog->console_cmd->cvars->vars->list, list)
 -      for(cvar = prog->console_cmd->cvars->vars; cvar; cvar = cvar->next)
++      List_For_Each_Entry(cvar, &prog->console_cmd->cvars->vars->list, cvar_t, list)
        {
                if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
                        continue;
diff --cc prvm_edict.c
index fb77fd32087a2720dc1f23857eb2e9f7e8801aeb,ea8392751e9760f2aff73c0881557e1f7c608557..37d35e9a275b60f5566a722eab0d35f8af6c4e73
@@@ -2573,7 -2573,7 +2573,7 @@@ void PRVM_Prog_Load(prvm_prog_t *prog, 
                }
        }
  
-       List_For_Each_Entry(cvar, &prog->console_cmd->cvars->vars->list, list)
 -      for (cvar = prog->console_cmd->cvars->vars; cvar; cvar = cvar->next)
++      List_For_Each_Entry(cvar, &prog->console_cmd->cvars->vars->list, cvar_t, list)
                cvar->globaldefindex[prog - prvm_prog_list] = -1;
  
        for (i=0 ; i<prog->numglobaldefs ; i++)