cvar.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. // cvar.c -- dynamic variable tracking
  16. #include "quakedef.h"
  17. cvar_t *cvar_vars;
  18. char *cvar_null_string = "";
  19. /*
  20. ============
  21. Cvar_FindVar
  22. ============
  23. */
  24. cvar_t *Cvar_FindVar (char *var_name)
  25. {
  26. cvar_t *var;
  27. for (var=cvar_vars ; var ; var=var->next)
  28. if (!Q_strcmp (var_name, var->name))
  29. return var;
  30. return NULL;
  31. }
  32. /*
  33. ============
  34. Cvar_VariableValue
  35. ============
  36. */
  37. float Cvar_VariableValue (char *var_name)
  38. {
  39. cvar_t *var;
  40. var = Cvar_FindVar (var_name);
  41. if (!var)
  42. return 0;
  43. return Q_atof (var->string);
  44. }
  45. /*
  46. ============
  47. Cvar_VariableString
  48. ============
  49. */
  50. char *Cvar_VariableString (char *var_name)
  51. {
  52. cvar_t *var;
  53. var = Cvar_FindVar (var_name);
  54. if (!var)
  55. return cvar_null_string;
  56. return var->string;
  57. }
  58. /*
  59. ============
  60. Cvar_CompleteVariable
  61. ============
  62. */
  63. char *Cvar_CompleteVariable (char *partial)
  64. {
  65. cvar_t *cvar;
  66. int len;
  67. len = Q_strlen(partial);
  68. if (!len)
  69. return NULL;
  70. // check functions
  71. for (cvar=cvar_vars ; cvar ; cvar=cvar->next)
  72. if (!Q_strncmp (partial,cvar->name, len))
  73. return cvar->name;
  74. return NULL;
  75. }
  76. /*
  77. ============
  78. Cvar_Set
  79. ============
  80. */
  81. void Cvar_Set (char *var_name, char *value)
  82. {
  83. cvar_t *var;
  84. qboolean changed;
  85. var = Cvar_FindVar (var_name);
  86. if (!var)
  87. { // there is an error in C code if this happens
  88. Con_Printf ("Cvar_Set: variable %s not found\n", var_name);
  89. return;
  90. }
  91. changed = Q_strcmp(var->string, value);
  92. Z_Free (var->string); // free the old value string
  93. var->string = Z_Malloc (Q_strlen(value)+1);
  94. Q_strcpy (var->string, value);
  95. var->value = Q_atof (var->string);
  96. if (var->server && changed)
  97. {
  98. if (sv.active)
  99. SV_BroadcastPrintf ("\"%s\" changed to \"%s\"\n", var->name, var->string);
  100. }
  101. }
  102. /*
  103. ============
  104. Cvar_SetValue
  105. ============
  106. */
  107. void Cvar_SetValue (char *var_name, float value)
  108. {
  109. char val[32];
  110. sprintf (val, "%f",value);
  111. Cvar_Set (var_name, val);
  112. }
  113. /*
  114. ============
  115. Cvar_RegisterVariable
  116. Adds a freestanding variable to the variable list.
  117. ============
  118. */
  119. void Cvar_RegisterVariable (cvar_t *variable)
  120. {
  121. char *oldstr;
  122. // first check to see if it has allready been defined
  123. if (Cvar_FindVar (variable->name))
  124. {
  125. Con_Printf ("Can't register variable %s, allready defined\n", variable->name);
  126. return;
  127. }
  128. // check for overlap with a command
  129. if (Cmd_Exists (variable->name))
  130. {
  131. Con_Printf ("Cvar_RegisterVariable: %s is a command\n", variable->name);
  132. return;
  133. }
  134. // copy the value off, because future sets will Z_Free it
  135. oldstr = variable->string;
  136. variable->string = Z_Malloc (Q_strlen(variable->string)+1);
  137. Q_strcpy (variable->string, oldstr);
  138. variable->value = Q_atof (variable->string);
  139. // link the variable in
  140. variable->next = cvar_vars;
  141. cvar_vars = variable;
  142. }
  143. /*
  144. ============
  145. Cvar_Command
  146. Handles variable inspection and changing from the console
  147. ============
  148. */
  149. qboolean Cvar_Command (void)
  150. {
  151. cvar_t *v;
  152. // check variables
  153. v = Cvar_FindVar (Cmd_Argv(0));
  154. if (!v)
  155. return false;
  156. // perform a variable print or set
  157. if (Cmd_Argc() == 1)
  158. {
  159. Con_Printf ("\"%s\" is \"%s\"\n", v->name, v->string);
  160. return true;
  161. }
  162. Cvar_Set (v->name, Cmd_Argv(1));
  163. return true;
  164. }
  165. /*
  166. ============
  167. Cvar_WriteVariables
  168. Writes lines containing "set variable value" for all variables
  169. with the archive flag set to true.
  170. ============
  171. */
  172. void Cvar_WriteVariables (FILE *f)
  173. {
  174. cvar_t *var;
  175. for (var = cvar_vars ; var ; var = var->next)
  176. if (var->archive)
  177. fprintf (f, "%s \"%s\"\n", var->name, var->string);
  178. }