cvar.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. #ifdef SERVERONLY
  17. #include "qwsvdef.h"
  18. #else
  19. #include "quakedef.h"
  20. #endif
  21. cvar_t *cvar_vars;
  22. char *cvar_null_string = "";
  23. /*
  24. ============
  25. Cvar_FindVar
  26. ============
  27. */
  28. cvar_t *Cvar_FindVar (char *var_name)
  29. {
  30. cvar_t *var;
  31. for (var=cvar_vars ; var ; var=var->next)
  32. if (!Q_strcmp (var_name, var->name))
  33. return var;
  34. return NULL;
  35. }
  36. /*
  37. ============
  38. Cvar_VariableValue
  39. ============
  40. */
  41. float Cvar_VariableValue (char *var_name)
  42. {
  43. cvar_t *var;
  44. var = Cvar_FindVar (var_name);
  45. if (!var)
  46. return 0;
  47. return Q_atof (var->string);
  48. }
  49. /*
  50. ============
  51. Cvar_VariableString
  52. ============
  53. */
  54. char *Cvar_VariableString (char *var_name)
  55. {
  56. cvar_t *var;
  57. var = Cvar_FindVar (var_name);
  58. if (!var)
  59. return cvar_null_string;
  60. return var->string;
  61. }
  62. /*
  63. ============
  64. Cvar_CompleteVariable
  65. ============
  66. */
  67. char *Cvar_CompleteVariable (char *partial)
  68. {
  69. cvar_t *cvar;
  70. int len;
  71. len = Q_strlen(partial);
  72. if (!len)
  73. return NULL;
  74. // check exact match
  75. for (cvar=cvar_vars ; cvar ; cvar=cvar->next)
  76. if (!strcmp (partial,cvar->name))
  77. return cvar->name;
  78. // check partial match
  79. for (cvar=cvar_vars ; cvar ; cvar=cvar->next)
  80. if (!Q_strncmp (partial,cvar->name, len))
  81. return cvar->name;
  82. return NULL;
  83. }
  84. #ifdef SERVERONLY
  85. void SV_SendServerInfoChange(char *key, char *value);
  86. #endif
  87. /*
  88. ============
  89. Cvar_Set
  90. ============
  91. */
  92. void Cvar_Set (char *var_name, char *value)
  93. {
  94. cvar_t *var;
  95. var = Cvar_FindVar (var_name);
  96. if (!var)
  97. { // there is an error in C code if this happens
  98. Con_Printf ("Cvar_Set: variable %s not found\n", var_name);
  99. return;
  100. }
  101. #ifdef SERVERONLY
  102. if (var->info)
  103. {
  104. Info_SetValueForKey (svs.info, var_name, value, MAX_SERVERINFO_STRING);
  105. SV_SendServerInfoChange(var_name, value);
  106. // SV_BroadcastCommand ("fullserverinfo \"%s\"\n", svs.info);
  107. }
  108. #else
  109. if (var->info)
  110. {
  111. Info_SetValueForKey (cls.userinfo, var_name, value, MAX_INFO_STRING);
  112. if (cls.state >= ca_connected)
  113. {
  114. MSG_WriteByte (&cls.netchan.message, clc_stringcmd);
  115. SZ_Print (&cls.netchan.message, va("setinfo \"%s\" \"%s\"\n", var_name, value));
  116. }
  117. }
  118. #endif
  119. Z_Free (var->string); // free the old value string
  120. var->string = Z_Malloc (Q_strlen(value)+1);
  121. Q_strcpy (var->string, value);
  122. var->value = Q_atof (var->string);
  123. }
  124. /*
  125. ============
  126. Cvar_SetValue
  127. ============
  128. */
  129. void Cvar_SetValue (char *var_name, float value)
  130. {
  131. char val[32];
  132. sprintf (val, "%f",value);
  133. Cvar_Set (var_name, val);
  134. }
  135. /*
  136. ============
  137. Cvar_RegisterVariable
  138. Adds a freestanding variable to the variable list.
  139. ============
  140. */
  141. void Cvar_RegisterVariable (cvar_t *variable)
  142. {
  143. char value[512];
  144. // first check to see if it has allready been defined
  145. if (Cvar_FindVar (variable->name))
  146. {
  147. Con_Printf ("Can't register variable %s, allready defined\n", variable->name);
  148. return;
  149. }
  150. // check for overlap with a command
  151. if (Cmd_Exists (variable->name))
  152. {
  153. Con_Printf ("Cvar_RegisterVariable: %s is a command\n", variable->name);
  154. return;
  155. }
  156. // link the variable in
  157. variable->next = cvar_vars;
  158. cvar_vars = variable;
  159. // copy the value off, because future sets will Z_Free it
  160. strcpy (value, variable->string);
  161. variable->string = Z_Malloc (1);
  162. // set it through the function to be consistant
  163. Cvar_Set (variable->name, value);
  164. }
  165. /*
  166. ============
  167. Cvar_Command
  168. Handles variable inspection and changing from the console
  169. ============
  170. */
  171. qboolean Cvar_Command (void)
  172. {
  173. cvar_t *v;
  174. // check variables
  175. v = Cvar_FindVar (Cmd_Argv(0));
  176. if (!v)
  177. return false;
  178. // perform a variable print or set
  179. if (Cmd_Argc() == 1)
  180. {
  181. Con_Printf ("\"%s\" is \"%s\"\n", v->name, v->string);
  182. return true;
  183. }
  184. Cvar_Set (v->name, Cmd_Argv(1));
  185. return true;
  186. }
  187. /*
  188. ============
  189. Cvar_WriteVariables
  190. Writes lines containing "set variable value" for all variables
  191. with the archive flag set to true.
  192. ============
  193. */
  194. void Cvar_WriteVariables (FILE *f)
  195. {
  196. cvar_t *var;
  197. for (var = cvar_vars ; var ; var = var->next)
  198. if (var->archive)
  199. fprintf (f, "%s \"%s\"\n", var->name, var->string);
  200. }