]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - cvar.c
improved setinfo handling so that quakeworld works much better now (name/topcolor...
[xonotic/darkplaces.git] / cvar.c
diff --git a/cvar.c b/cvar.c
index 27853eec54b3d504feaa968aa5c6a6532e405045..68d98df421f1ebb41d20b2c84cce70f35e3d8207 100644 (file)
--- a/cvar.c
+++ b/cvar.c
@@ -22,6 +22,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #include "quakedef.h"
 
 cvar_t *cvar_vars = NULL;
+cvar_t *cvar_hashtable[65536];
 char *cvar_null_string = "";
 
 /*
@@ -31,9 +32,12 @@ Cvar_FindVar
 */
 cvar_t *Cvar_FindVar (const char *var_name)
 {
+       int hashindex;
        cvar_t *var;
 
-       for (var = cvar_vars;var;var = var->next)
+       // use hash lookup to minimize search time
+       hashindex = CRC_Block((const unsigned char *)var_name, strlen(var_name));
+       for (var = cvar_hashtable[hashindex];var;var = var->nextonhashchain)
                if (!strcasecmp (var_name, var->name))
                        return var;
 
@@ -191,6 +195,18 @@ const char **Cvar_CompleteBuildList (const char *partial)
        return buf;
 }
 
+// written by LordHavoc
+void Cvar_CompleteCvarPrint (const char *partial)
+{
+       cvar_t *cvar;
+       size_t len = strlen(partial);
+       // Loop through the command list and print all matches
+       for (cvar = cvar_vars; cvar; cvar = cvar->next)
+               if (!strncasecmp(partial, cvar->name, len))
+                       Con_Printf ("%c3%s%s : \"%s\" (\"%s\") : %s\n", STRING_COLOR_TAG, cvar->name, STRING_COLOR_DEFAULT_STR, cvar->string, cvar->defstring, cvar->description);
+}
+
+
 /*
 ============
 Cvar_Set
@@ -217,6 +233,21 @@ void Cvar_SetQuick_Internal (cvar_t *var, const char *value)
        var->integer = (int) var->value;
        if ((var->flags & CVAR_NOTIFY) && changed && sv.active)
                SV_BroadcastPrintf("\"%s\" changed to \"%s\"\n", var->name, var->string);
+#if 0
+       // TODO: add infostring support to the server?
+       if ((var->flags & CVAR_SERVERINFO) && changed && sv.active)
+       {
+               InfoString_SetValue(svs.serverinfo, sizeof(svs.serverinfo), var->name, var->string);
+               if (sv.active)
+               {
+                       MSG_WriteByte (&sv.reliable_datagram, svc_serverinfostring);
+                       MSG_WriteString (&sv.reliable_datagram, var->name);
+                       MSG_WriteString (&sv.reliable_datagram, var->string);
+               }
+       }
+#endif
+       if ((var->flags & CVAR_USERINFO) && cls.state != ca_dedicated)
+               CL_SetInfo(var->name, var->string, true, false, false, false);
 }
 
 void Cvar_SetQuick (cvar_t *var, const char *value)
@@ -227,7 +258,7 @@ void Cvar_SetQuick (cvar_t *var, const char *value)
                return;
        }
 
-       if (developer.integer)
+       if (developer.integer >= 100)
                Con_Printf("Cvar_SetQuick({\"%s\", \"%s\", %i, \"%s\"}, \"%s\");\n", var->name, var->string, var->flags, var->defstring, value);
 
        Cvar_SetQuick_Internal(var, value);
@@ -252,7 +283,7 @@ Cvar_SetValue
 */
 void Cvar_SetValueQuick(cvar_t *var, float value)
 {
-       char val[256];
+       char val[MAX_INPUTLINE];
 
        if ((float)((int)value) == value)
                sprintf(val, "%i", (int)value);
@@ -263,7 +294,7 @@ void Cvar_SetValueQuick(cvar_t *var, float value)
 
 void Cvar_SetValue(const char *var_name, float value)
 {
-       char val[256];
+       char val[MAX_INPUTLINE];
 
        if ((float)((int)value) == value)
                sprintf(val, "%i", (int)value);
@@ -281,10 +312,11 @@ Adds a freestanding variable to the variable list.
 */
 void Cvar_RegisterVariable (cvar_t *variable)
 {
+       int hashindex;
        cvar_t *current, *next, *cvar;
        char *oldstr;
 
-       if (developer.integer)
+       if (developer.integer >= 100)
                Con_Printf("Cvar_RegisterVariable({\"%s\", \"%s\", %i});\n", variable->name, variable->string, variable->flags);
 
 // first check to see if it has already been defined
@@ -293,8 +325,8 @@ void Cvar_RegisterVariable (cvar_t *variable)
        {
                if (cvar->flags & CVAR_ALLOCATED)
                {
-                       if (developer.integer)
-                               Con_Printf("...  replacing existing allocated cvar {\"%s\", \"%s\", %i}", cvar->name, cvar->string, cvar->flags);
+                       if (developer.integer >= 100)
+                               Con_Printf("...  replacing existing allocated cvar {\"%s\", \"%s\", %i}\n", cvar->name, cvar->string, cvar->flags);
                        // fixed variables replace allocated ones
                        // (because the engine directly accesses fixed variables)
                        // NOTE: this isn't actually used currently
@@ -356,6 +388,11 @@ void Cvar_RegisterVariable (cvar_t *variable)
                cvar_vars = variable;
        }
        variable->next = next;
+
+       // link to head of list in this hash table index
+       hashindex = CRC_Block((const unsigned char *)variable->name, strlen(variable->name));
+       variable->nextonhashchain = cvar_hashtable[hashindex];
+       cvar_hashtable[hashindex] = variable;
 }
 
 /*
@@ -367,9 +404,10 @@ Adds a newly allocated variable to the variable list or sets its value.
 */
 cvar_t *Cvar_Get (const char *name, const char *value, int flags)
 {
-       cvar_t *cvar;
+       int hashindex;
+       cvar_t *current, *next, *cvar;
 
-       if (developer.integer)
+       if (developer.integer >= 100)
                Con_Printf("Cvar_Get(\"%s\", \"%s\", %i);\n", name, value, flags);
 
 // first check to see if it has already been defined
@@ -409,10 +447,23 @@ cvar_t *Cvar_Get (const char *name, const char *value, int flags)
        strcpy(cvar->defstring, value);
        cvar->value = atof (cvar->string);
        cvar->integer = (int) cvar->value;
+       cvar->description = "custom cvar";
 
 // link the variable in
-       cvar->next = cvar_vars;
-       cvar_vars = cvar;
+// alphanumerical order
+       for( current = NULL, next = cvar_vars ; next && strcmp( next->name, cvar->name ) < 0 ; current = next, next = next->next )
+               ;
+       if( current )
+               current->next = cvar;
+       else
+               cvar_vars = cvar;
+       cvar->next = next;
+
+       // link to head of list in this hash table index
+       hashindex = CRC_Block((const unsigned char *)cvar->name, strlen(cvar->name));
+       cvar->nextonhashchain = cvar_hashtable[hashindex];
+       cvar_hashtable[hashindex] = cvar;
+
        return cvar;
 }
 
@@ -440,7 +491,8 @@ qboolean    Cvar_Command (void)
                return true;
        }
 
-       Con_DPrint("Cvar_Command: ");
+       if (developer.integer >= 100)
+               Con_DPrint("Cvar_Command: ");
 
        if (v->flags & CVAR_READONLY)
        {
@@ -448,6 +500,8 @@ qboolean    Cvar_Command (void)
                return true;
        }
        Cvar_Set (v->name, Cmd_Argv(1));
+       if (developer.integer >= 100)
+               Con_DPrint("\n");
        return true;
 }
 
@@ -501,14 +555,14 @@ void Cvar_List_f (void)
                if (partial && strncasecmp (partial,cvar->name,len))
                        continue;
 
-               Con_Printf("%s is \"%s\" [\"%s\"]\n", cvar->name, cvar->string, cvar->defstring);
+               Con_Printf("%s is \"%s\" [\"%s\"] %s\n", cvar->name, cvar->string, cvar->defstring, cvar->description);
                count++;
        }
 
-       Con_Printf("%i cvar(s)", count);
        if (partial)
-               Con_Printf(" beginning with \"%s\"", partial);
-       Con_Print("\n");
+               Con_Printf("%i cvar(s) beginning with \"%s\"\n", count, partial);
+       else
+               Con_Printf("%i cvar(s)\n", count);
 }
 // 2000-01-09 CvarList command by Maddes