From ad8716457419dd127c685341d720525dead64197 Mon Sep 17 00:00:00 2001 From: divverent Date: Fri, 23 Jan 2009 09:34:48 +0000 Subject: [PATCH] +"DP_QC_CVAR_DESCRIPTION " +"DP_QC_STRINGBUFFERS_CVARLIST " allowing menu QC (and other code) to list all cvars, and retrieve their descriptions git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@8663 d7cf8633-e32d-0410-b094-e92efae38249 --- clvm_cmds.c | 4 +-- cvar.c | 51 ++++++++++++++++++++++++++++---- cvar.h | 8 ++++- mvm_cmds.c | 6 ++-- prvm_cmds.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++-- prvm_cmds.h | 3 ++ svvm_cmds.c | 6 ++-- 7 files changed, 148 insertions(+), 15 deletions(-) diff --git a/clvm_cmds.c b/clvm_cmds.c index e9b8c435..beb7db28 100644 --- a/clvm_cmds.c +++ b/clvm_cmds.c @@ -3547,8 +3547,8 @@ VM_uri_get, // #513 float(string uril, float id) uri_get = #512; (DP_QC_URI VM_tokenize_console, // #514 float(string str) tokenize_console = #514; (DP_QC_TOKENIZE_CONSOLE) VM_argv_start_index, // #515 float(float idx) argv_start_index = #515; (DP_QC_TOKENIZE_CONSOLE) VM_argv_end_index, // #516 float(float idx) argv_end_index = #516; (DP_QC_TOKENIZE_CONSOLE) -NULL, // #517 -NULL, // #518 +VM_buf_cvarlist, // #517 void(float buf, string prefix) buf_cvarlist = #517; (DP_QC_STRINGBUFFERS_CVARLIST) +VM_cvar_description, // #518 float(string name) cvar_description = #518; (DP_QC_CVAR_DESCRIPTION) NULL, // #519 VM_keynumtostring, // #520 string keynumtostring(float keynum) VM_findkeysforcommand, // #521 string findkeysforcommand(string command) diff --git a/cvar.c b/cvar.c index 24fb2b2e..3e91f4b1 100644 --- a/cvar.c +++ b/cvar.c @@ -21,6 +21,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "quakedef.h" +char *cvar_dummy_description = "custom cvar"; + cvar_t *cvar_vars = NULL; cvar_t *cvar_hashtable[65536]; char *cvar_null_string = ""; @@ -114,6 +116,21 @@ const char *Cvar_VariableDefString (const char *var_name) return var->defstring; } +/* +============ +Cvar_VariableDescription +============ +*/ +const char *Cvar_VariableDescription (const char *var_name) +{ + cvar_t *var; + + var = Cvar_FindVar (var_name); + if (!var) + return cvar_null_string; + return var->description; +} + /* ============ @@ -434,7 +451,7 @@ Cvar_Get 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_Get (const char *name, const char *value, int flags, const char *newdescription) { int hashindex; cvar_t *current, *next, *cvar; @@ -449,6 +466,20 @@ cvar_t *Cvar_Get (const char *name, const char *value, int flags) { cvar->flags |= flags; Cvar_SetQuick_Internal (cvar, value); + if(newdescription && (cvar->flags & CVAR_ALLOCATED)) + { + if(cvar->description != cvar_dummy_description) + Z_Free(cvar->description); + + if(*newdescription) + { + alloclen = strlen(newdescription) + 1; + cvar->description = (char *)Z_Malloc(alloclen); + memcpy(cvar->description, newdescription, alloclen); + } + else + cvar->description = cvar_dummy_description; + } return cvar; } @@ -474,7 +505,15 @@ cvar_t *Cvar_Get (const char *name, const char *value, int flags) memcpy(cvar->defstring, value, alloclen); cvar->value = atof (cvar->string); cvar->integer = (int) cvar->value; - cvar->description = "custom cvar"; // actually checked by VM_cvar_type + + if(newdescription && *newdescription) + { + alloclen = strlen(newdescription) + 1; + cvar->description = (char *)Z_Malloc(alloclen); + memcpy(cvar->description, newdescription, alloclen); + } + else + cvar->description = cvar_dummy_description; // actually checked by VM_cvar_type // link the variable in // alphanumerical order @@ -660,7 +699,7 @@ void Cvar_Set_f (void) // make sure it's the right number of parameters if (Cmd_Argc() < 3) { - Con_Printf("Set: wrong number of parameters, usage: set \n"); + Con_Printf("Set: wrong number of parameters, usage: set []\n"); return; } @@ -676,7 +715,7 @@ void Cvar_Set_f (void) Con_DPrint("Set: "); // all looks ok, create/modify the cvar - Cvar_Get(Cmd_Argv(1), Cmd_Argv(2), 0); + Cvar_Get(Cmd_Argv(1), Cmd_Argv(2), 0, Cmd_Argc() >= 3 ? Cmd_Argv(3) : NULL); } void Cvar_SetA_f (void) @@ -686,7 +725,7 @@ void Cvar_SetA_f (void) // make sure it's the right number of parameters if (Cmd_Argc() < 3) { - Con_Printf("SetA: wrong number of parameters, usage: seta \n"); + Con_Printf("SetA: wrong number of parameters, usage: seta []\n"); return; } @@ -702,7 +741,7 @@ void Cvar_SetA_f (void) Con_DPrint("SetA: "); // all looks ok, create/modify the cvar - Cvar_Get(Cmd_Argv(1), Cmd_Argv(2), CVAR_SAVE); + Cvar_Get(Cmd_Argv(1), Cmd_Argv(2), CVAR_SAVE, Cmd_Argc() >= 3 ? Cmd_Argv(3) : NULL); } diff --git a/cvar.h b/cvar.h index 63b1b5e4..bc30eb6f 100644 --- a/cvar.h +++ b/cvar.h @@ -158,6 +158,9 @@ const char *Cvar_VariableString (const char *var_name); const char *Cvar_VariableDefString (const char *var_name); // returns an empty string if not defined +const char *Cvar_VariableDescription (const char *var_name); +// returns an empty string if not defined + const char *Cvar_CompleteVariable (const char *partial); // attempts to match a partial variable name for command line completion // returns NULL if nothing fits @@ -199,9 +202,12 @@ void Cvar_SetA_f (void); // commands to create new cvars (or set existing ones) // seta creates an archived cvar (saved to config) -cvar_t *Cvar_Get (const char *name, const char *value, int flags); +cvar_t *Cvar_Get (const char *name, const char *value, int flags, const char *newdescription); // allocates a cvar by name and returns its address, // or merely sets its value if it already exists. +char *cvar_dummy_description; // ALWAYS the same pointer +cvar_t *cvar_vars; // used to list all cvars + #endif diff --git a/mvm_cmds.c b/mvm_cmds.c index 28fa492d..af39e50a 100644 --- a/mvm_cmds.c +++ b/mvm_cmds.c @@ -17,9 +17,11 @@ char *vm_m_extensions = "DP_QC_CMD " "DP_QC_CRC16 " "DP_QC_CVAR_TYPE " +"DP_QC_CVAR_DESCRIPTION " "DP_QC_RENDER_SCENE " "DP_QC_STRFTIME " "DP_QC_STRINGBUFFERS " +"DP_QC_STRINGBUFFERS_CVARLIST " "DP_QC_STRINGCOLORFUNCTIONS " "DP_QC_STRING_CASE_FUNCTIONS " "DP_QC_STRREPLACE " @@ -1336,8 +1338,8 @@ VM_uri_get, // #513 float(string uril, float id) uri_get = #513; (DP_QC_URI VM_tokenize_console, // #514 float(string str) tokenize_console = #514; (DP_QC_TOKENIZE_CONSOLE) VM_argv_start_index, // #515 float(float idx) argv_start_index = #515; (DP_QC_TOKENIZE_CONSOLE) VM_argv_end_index, // #516 float(float idx) argv_end_index = #516; (DP_QC_TOKENIZE_CONSOLE) -NULL, // #517 -NULL, // #518 +VM_buf_cvarlist, // #517 void(float buf, string prefix) buf_cvarlist = #517; (DP_QC_STRINGBUFFERS_CVARLIST) +VM_cvar_description, // #518 float(string name) cvar_description = #518; (DP_QC_CVAR_DESCRIPTION) NULL, // #519 NULL, // #520 NULL, // #521 diff --git a/prvm_cmds.c b/prvm_cmds.c index 94318af1..3356fda5 100644 --- a/prvm_cmds.c +++ b/prvm_cmds.c @@ -466,7 +466,7 @@ void VM_cvar_type (void) ret |= 4; // CVAR_TYPE_PRIVATE if(!(cvar->flags & CVAR_ALLOCATED)) ret |= 8; // CVAR_TYPE_ENGINE - if(strcmp(cvar->description, "custom cvar")) // has to match Cvar_Get's placeholder string + if(cvar->description != cvar_dummy_description) ret |= 16; // CVAR_TYPE_HASDESCRIPTION PRVM_G_FLOAT(OFS_RETURN) = ret; @@ -504,6 +504,22 @@ void VM_cvar_defstring (void) VM_CheckEmptyString(string); PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDefString(string)); } + +/* +======================== +VM_cvar_defstring + +const string VM_cvar_description (string, ...) +======================== +*/ +void VM_cvar_description (void) +{ + char string[VM_STRINGTEMP_LENGTH]; + VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_description); + VM_VarString(0, string, sizeof(string)); + VM_CheckEmptyString(string); + PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDescription(string)); +} /* ================= VM_cvar_set @@ -1439,7 +1455,7 @@ void VM_registercvar (void) return; } - Cvar_Get(name, value, flags); + Cvar_Get(name, value, flags, NULL); PRVM_G_FLOAT(OFS_RETURN) = 1; // success } @@ -4366,6 +4382,71 @@ void VM_bufstr_free (void) BufStr_Shrink(stringbuffer); } + + + + + + +void VM_buf_cvarlist(void) +{ + cvar_t *cvar; + const char *partial; + size_t len; + size_t alloclen; + int n; + prvm_stringbuffer_t *stringbuffer; + VM_SAFEPARMCOUNT(2, VM_bufstr_free); + + stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0)); + if(!stringbuffer) + { + VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME); + return; + } + + partial = PRVM_G_STRING(OFS_PARM1); + if(!partial) + len = 0; + else + len = strlen(partial); + + for (n = 0;n < stringbuffer->num_strings;n++) + if (stringbuffer->strings[n]) + Mem_Free(stringbuffer->strings[n]); + if (stringbuffer->strings) + Mem_Free(stringbuffer->strings); + stringbuffer->strings = NULL; + + n = 0; + for(cvar = cvar_vars; cvar; cvar = cvar->next) + { + if(partial && strncasecmp(partial, cvar->name, len)) + continue; + ++n; + } + + stringbuffer->max_strings = stringbuffer->num_strings = n; + if (stringbuffer->max_strings) + stringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(stringbuffer->strings[0]) * stringbuffer->max_strings); + + n = 0; + for(cvar = cvar_vars; cvar; cvar = cvar->next) + { + if(partial && strncasecmp(partial, cvar->name, len)) + continue; + + alloclen = strlen(cvar->name) + 1; + stringbuffer->strings[n] = (char *)Mem_Alloc(prog->progs_mempool, alloclen); + memcpy(stringbuffer->strings[n], cvar->name, alloclen); + + ++n; + } +} + + + + //============= /* diff --git a/prvm_cmds.h b/prvm_cmds.h index 59377cef..535c7681 100644 --- a/prvm_cmds.h +++ b/prvm_cmds.h @@ -436,3 +436,6 @@ void VM_netaddress_resolve (void); void VM_tokenize_console (void); void VM_argv_start_index (void); void VM_argv_end_index (void); + +void VM_buf_cvarlist(void); +void VM_cvar_description(void); diff --git a/svvm_cmds.c b/svvm_cmds.c index b16f16a0..08440e65 100644 --- a/svvm_cmds.c +++ b/svvm_cmds.c @@ -62,6 +62,7 @@ char *vm_sv_extensions = "DP_QC_COPYENTITY " "DP_QC_CRC16 " "DP_QC_CVAR_DEFSTRING " +"DP_QC_CVAR_DESCRIPTION " "DP_QC_CVAR_STRING " "DP_QC_CVAR_TYPE " "DP_QC_EDICT_NUM " @@ -84,6 +85,7 @@ char *vm_sv_extensions = "DP_QC_SINCOSSQRTPOW " "DP_QC_STRFTIME " "DP_QC_STRINGBUFFERS " +"DP_QC_STRINGBUFFERS_CVARLIST " "DP_QC_STRINGCOLORFUNCTIONS " "DP_QC_STRING_CASE_FUNCTIONS " "DP_QC_STRREPLACE " @@ -3420,8 +3422,8 @@ VM_uri_get, // #513 float(string uril, float id) uri_get = #513; (DP_QC_URI VM_tokenize_console, // #514 float(string str) tokenize_console = #514; (DP_QC_TOKENIZE_CONSOLE) VM_argv_start_index, // #515 float(float idx) argv_start_index = #515; (DP_QC_TOKENIZE_CONSOLE) VM_argv_end_index, // #516 float(float idx) argv_end_index = #516; (DP_QC_TOKENIZE_CONSOLE) -NULL, // #517 -NULL, // #518 +VM_buf_cvarlist, // #517 void(float buf, string prefix) buf_cvarlist = #517; (DP_QC_STRINGBUFFERS_CVARLIST) +VM_cvar_description, // #518 float(string name) cvar_description = #518; (DP_QC_CVAR_DESCRIPTION) NULL, // #519 }; -- 2.39.2