]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - cvar.c
Implement cvar callback functionality
[xonotic/darkplaces.git] / cvar.c
diff --git a/cvar.c b/cvar.c
index b4c41b7d71139b3a4a72785f08763312bc348e6e..be6ed386f08405660bdf053b7884830703a965fe 100644 (file)
--- a/cvar.c
+++ b/cvar.c
@@ -251,6 +251,14 @@ const char **Cvar_CompleteBuildList(cvar_state_t *cvars, const char *partial, in
        return buf;
 }
 
+void Cvar_PrintHelp(cvar_t *cvar, qboolean full)
+{
+       Con_Printf("^3%s^7 is \"%s\" [\"%s\"] ", cvar->name, ((cvar->flags & CVAR_PRIVATE) ? "********"/*hunter2*/ : cvar->string), cvar->defstring);
+       if (full)
+               Con_Printf("%s", cvar->description);
+       Con_Printf("\n");
+}
+
 // written by LadyHavoc
 void Cvar_CompleteCvarPrint(cvar_state_t *cvars, const char *partial, int neededflags)
 {
@@ -259,7 +267,7 @@ void Cvar_CompleteCvarPrint(cvar_state_t *cvars, const char *partial, int needed
        // Loop through the command list and print all matches
        for (cvar = cvars->vars; cvar; cvar = cvar->next)
                if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
-                       Con_Printf ("^3%s^7 is \"%s\" [\"%s\"] %s\n", cvar->name, cvar->string, cvar->defstring, cvar->description);
+                       Cvar_PrintHelp(cvar, true);
 }
 
 // check if a cvar is held by some progs
@@ -341,21 +349,28 @@ static void Cvar_SetQuick_Internal (cvar_t *var, const char *value)
        qboolean changed;
        size_t valuelen;
        char vabuf[1024];
+       char new_value[MAX_INPUTLINE];
 
        changed = strcmp(var->string, value) != 0;
        // LadyHavoc: don't reallocate when there is no change
        if (!changed)
                return;
 
+       memcpy(new_value,value,MAX_INPUTLINE);
+       
+       // Call the function stored in the cvar for bounds checking, cleanup, etc
+       if (var->callback)
+               var->callback(new_value);
+
        // LadyHavoc: don't reallocate when the buffer is the same size
-       valuelen = strlen(value);
+       valuelen = strlen(new_value);
        if (!var->string || strlen(var->string) != valuelen)
        {
                Z_Free ((char *)var->string);   // free the old value string
 
                var->string = (char *)Z_Malloc (valuelen + 1);
        }
-       memcpy ((char *)var->string, value, valuelen + 1);
+       memcpy ((char *)var->string, new_value, valuelen + 1);
        var->value = atof (var->string);
        var->integer = (int) var->value;
        if ((var->flags & CVAR_NOTIFY) && changed && sv.active && !sv_disablenotify.integer)
@@ -474,6 +489,11 @@ void Cvar_SetValue(cvar_state_t *cvars, const char *var_name, float value)
        Cvar_Set(cvars, var_name, val);
 }
 
+void Cvar_RegisterCallback(cvar_t *variable, void (*callback)(char *))
+{
+       variable->callback = callback;
+}
+
 /*
 ============
 Cvar_RegisterVariable
@@ -700,7 +720,7 @@ qboolean    Cvar_Command (cmd_state_t *cmd)
 // perform a variable print or set
        if (Cmd_Argc(cmd) == 1)
        {
-               Con_Printf("\"%s\" is \"%s\" [\"%s\"]\n", v->name, ((v->flags & CVAR_PRIVATE) ? "********"/*hunter2*/ : v->string), v->defstring);
+               Cvar_PrintHelp(v, true);
                return true;
        }
 
@@ -934,7 +954,7 @@ void Cvar_List_f(cmd_state_t *cmd)
                if (len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp (partial,cvar->name,len)))
                        continue;
 
-               Con_Printf("%s is \"%s\" [\"%s\"] %s\n", cvar->name, ((cvar->flags & CVAR_PRIVATE) ? "********"/*hunter2*/ : cvar->string), cvar->defstring, cvar->description);
+               Cvar_PrintHelp(cvar, true);
                count++;
        }