don't use ssize_t
[xonotic/darkplaces.git] / prvm_edict.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // AK new vm
21
22 #include "quakedef.h"
23 #include "progsvm.h"
24
25 prvm_prog_t *prog;
26
27 static prvm_prog_t prog_list[PRVM_MAXPROGS];
28
29 int             prvm_type_size[8] = {1,sizeof(string_t)/4,1,3,1,1,sizeof(func_t)/4,sizeof(void *)/4};
30
31 ddef_t *PRVM_ED_FieldAtOfs(int ofs);
32 qboolean PRVM_ED_ParseEpair(prvm_edict_t *ent, ddef_t *key, const char *s, qboolean parsebackslash);
33
34 cvar_t prvm_language = {0, "prvm_language", "", "when set, loads progs.dat.LANGUAGENAME.po for string translations; when set to dump, progs.dat.dump.po is written from the strings in the progs"};
35 // LordHavoc: prints every opcode as it executes - warning: this is significant spew
36 cvar_t prvm_traceqc = {0, "prvm_traceqc", "0", "prints every QuakeC statement as it is executed (only for really thorough debugging!)"};
37 // LordHavoc: counts usage of each QuakeC statement
38 cvar_t prvm_statementprofiling = {0, "prvm_statementprofiling", "0", "counts how many times each QuakeC statement has been executed, these counts are displayed in prvm_printfunction output (if enabled)"};
39 cvar_t prvm_backtraceforwarnings = {0, "prvm_backtraceforwarnings", "0", "print a backtrace for warnings too"};
40 cvar_t prvm_leaktest = {0, "prvm_leaktest", "0", "try to detect memory leaks in strings or entities"};
41 cvar_t prvm_leaktest_ignore_classnames = {0, "prvm_leaktest_ignore_classnames", "", "classnames of entities to NOT leak check because they are found by find(world, classname, ...) but are actually spawned by QC code (NOT map entities)"};
42 cvar_t prvm_errordump = {0, "prvm_errordump", "0", "write a savegame on crash to crash-server.dmp"};
43 cvar_t prvm_reuseedicts_startuptime = {0, "prvm_reuseedicts_startuptime", "2", "allows immediate re-use of freed entity slots during start of new level (value in seconds)"};
44 cvar_t prvm_reuseedicts_neverinsameframe = {0, "prvm_reuseedicts_neverinsameframe", "1", "never allows re-use of freed entity slots during same frame"};
45
46 qboolean prvm_runawaycheck = true;
47
48 // LordHavoc: optional runtime bounds checking (speed drain, but worth it for security, on by default - breaks most QCCX features (used by CRMod and others))
49 // enables detection of out of bounds memory access in the QuakeC code being run (in other words, prevents really exceedingly bad QuakeC code from doing nasty things to your computer)
50 qboolean prvm_boundscheck = true;
51
52 extern sizebuf_t vm_tempstringsbuf;
53
54 //============================================================================
55 // mempool handling
56
57 /*
58 ===============
59 PRVM_MEM_Alloc
60 ===============
61 */
62 void PRVM_MEM_Alloc(void)
63 {
64         int i;
65
66         // reserve space for the null entity aka world
67         // check bound of max_edicts
68         prog->max_edicts = bound(1 + prog->reserved_edicts, prog->max_edicts, prog->limit_edicts);
69         prog->num_edicts = bound(1 + prog->reserved_edicts, prog->num_edicts, prog->max_edicts);
70
71         // edictprivate_size has to be min as big prvm_edict_private_t
72         prog->edictprivate_size = max(prog->edictprivate_size,(int)sizeof(prvm_edict_private_t));
73
74         // alloc edicts
75         prog->edicts = (prvm_edict_t *)Mem_Alloc(prog->progs_mempool,prog->limit_edicts * sizeof(prvm_edict_t));
76
77         // alloc edict private space
78         prog->edictprivate = Mem_Alloc(prog->progs_mempool, prog->max_edicts * prog->edictprivate_size);
79
80         // alloc edict fields
81         prog->entityfieldsarea = prog->entityfields * prog->max_edicts;
82         prog->edictsfields = (vec_t *)Mem_Alloc(prog->progs_mempool, prog->entityfieldsarea * sizeof(vec_t));
83
84         // set edict pointers
85         for(i = 0; i < prog->max_edicts; i++)
86         {
87                 prog->edicts[i].priv.required = (prvm_edict_private_t *)((unsigned char  *)prog->edictprivate + i * prog->edictprivate_size);
88                 prog->edicts[i].fields.vp = prog->edictsfields + i * prog->entityfields;
89         }
90 }
91
92 /*
93 ===============
94 PRVM_MEM_IncreaseEdicts
95 ===============
96 */
97 void PRVM_MEM_IncreaseEdicts(void)
98 {
99         int             i;
100
101         if(prog->max_edicts >= prog->limit_edicts)
102                 return;
103
104         PRVM_GCALL(begin_increase_edicts)();
105
106         // increase edicts
107         prog->max_edicts = min(prog->max_edicts + 256, prog->limit_edicts);
108
109         prog->entityfieldsarea = prog->entityfields * prog->max_edicts;
110         prog->edictsfields = (vec_t*)Mem_Realloc(prog->progs_mempool, (void *)prog->edictsfields, prog->entityfieldsarea * sizeof(vec_t));
111         prog->edictprivate = (void *)Mem_Realloc(prog->progs_mempool, (void *)prog->edictprivate, prog->max_edicts * prog->edictprivate_size);
112
113         //set e and v pointers
114         for(i = 0; i < prog->max_edicts; i++)
115         {
116                 prog->edicts[i].priv.required  = (prvm_edict_private_t *)((unsigned char  *)prog->edictprivate + i * prog->edictprivate_size);
117                 prog->edicts[i].fields.vp = prog->edictsfields + i * prog->entityfields;
118         }
119
120         PRVM_GCALL(end_increase_edicts)();
121 }
122
123 //============================================================================
124 // normal prvm
125
126 int PRVM_ED_FindFieldOffset(const char *field)
127 {
128         ddef_t *d;
129         d = PRVM_ED_FindField(field);
130         if (!d)
131                 return -1;
132         return d->ofs;
133 }
134
135 int PRVM_ED_FindGlobalOffset(const char *global)
136 {
137         ddef_t *d;
138         d = PRVM_ED_FindGlobal(global);
139         if (!d)
140                 return -1;
141         return d->ofs;
142 }
143
144 func_t PRVM_ED_FindFunctionOffset(const char *function)
145 {
146         mfunction_t *f;
147         f = PRVM_ED_FindFunction(function);
148         if (!f)
149                 return 0;
150         return (func_t)(f - prog->functions);
151 }
152
153 qboolean PRVM_ProgLoaded(int prognr)
154 {
155         if(prognr < 0 || prognr >= PRVM_MAXPROGS)
156                 return FALSE;
157
158         return (prog_list[prognr].loaded ? TRUE : FALSE);
159 }
160
161 /*
162 =================
163 PRVM_SetProgFromString
164 =================
165 */
166 // perhaps add a return value when the str doesnt exist
167 qboolean PRVM_SetProgFromString(const char *str)
168 {
169         int i = 0;
170         for(; i < PRVM_MAXPROGS ; i++)
171                 if(prog_list[i].name && !strcmp(prog_list[i].name,str))
172                 {
173                         if(prog_list[i].loaded)
174                         {
175                                 prog = &prog_list[i];
176                                 return TRUE;
177                         }
178                         else
179                         {
180                                 Con_Printf("%s not loaded !\n",PRVM_NAME);
181                                 return FALSE;
182                         }
183                 }
184
185         Con_Printf("Invalid program name %s !\n", str);
186         return FALSE;
187 }
188
189 /*
190 =================
191 PRVM_SetProg
192 =================
193 */
194 void PRVM_SetProg(int prognr)
195 {
196         if(0 <= prognr && prognr < PRVM_MAXPROGS)
197         {
198                 if(prog_list[prognr].loaded)
199                         prog = &prog_list[prognr];
200                 else
201                         PRVM_ERROR("%i not loaded !", prognr);
202                 return;
203         }
204         PRVM_ERROR("Invalid program number %i", prognr);
205 }
206
207 /*
208 =================
209 PRVM_ED_ClearEdict
210
211 Sets everything to NULL
212 =================
213 */
214 void PRVM_ED_ClearEdict (prvm_edict_t *e)
215 {
216         memset (e->fields.vp, 0, prog->progs->entityfields * 4);
217         e->priv.required->free = false;
218
219         // AK: Let the init_edict function determine if something needs to be initialized
220         PRVM_GCALL(init_edict)(e);
221 }
222
223 const char *PRVM_AllocationOrigin(void)
224 {
225         char *buf = NULL;
226         if(prog->leaktest_active)
227         if(prog->depth > 0) // actually in QC code and not just parsing the entities block of a map/savegame
228         {
229                 buf = (char *)PRVM_Alloc(128);
230                 PRVM_ShortStackTrace(buf, 128);
231         }
232         return buf;
233 }
234
235 /*
236 =================
237 PRVM_ED_CanAlloc
238
239 Returns if this particular edict could get allocated by PRVM_ED_Alloc
240 =================
241 */
242 qboolean PRVM_ED_CanAlloc(prvm_edict_t *e)
243 {
244         if(!e->priv.required->free)
245                 return false;
246         if(realtime <= e->priv.required->freetime && prvm_reuseedicts_neverinsameframe.integer)
247                 return false; // never allow reuse in same frame (causes networking trouble)
248         if(e->priv.required->freetime < prog->starttime + prvm_reuseedicts_startuptime.value)
249                 return true;
250         if(realtime > e->priv.required->freetime + 1)
251                 return true;
252         return false; // entity slot still blocked because the entity was freed less than one second ago
253 }
254
255 /*
256 =================
257 PRVM_ED_Alloc
258
259 Either finds a free edict, or allocates a new one.
260 Try to avoid reusing an entity that was recently freed, because it
261 can cause the client to think the entity morphed into something else
262 instead of being removed and recreated, which can cause interpolated
263 angles and bad trails.
264 =================
265 */
266 prvm_edict_t *PRVM_ED_Alloc (void)
267 {
268         int                     i;
269         prvm_edict_t            *e;
270
271         // the client qc dont need maxclients
272         // thus it doesnt need to use svs.maxclients
273         // AK:  changed i=svs.maxclients+1
274         // AK:  changed so the edict 0 wont spawn -> used as reserved/world entity
275         //              although the menu/client has no world
276         for (i = prog->reserved_edicts + 1;i < prog->num_edicts;i++)
277         {
278                 e = PRVM_EDICT_NUM(i);
279                 if(PRVM_ED_CanAlloc(e))
280                 {
281                         PRVM_ED_ClearEdict (e);
282                         e->priv.required->allocation_origin = PRVM_AllocationOrigin();
283                         return e;
284                 }
285         }
286
287         if (i == prog->limit_edicts)
288                 PRVM_ERROR ("%s: PRVM_ED_Alloc: no free edicts",PRVM_NAME);
289
290         prog->num_edicts++;
291         if (prog->num_edicts >= prog->max_edicts)
292                 PRVM_MEM_IncreaseEdicts();
293
294         e = PRVM_EDICT_NUM(i);
295         PRVM_ED_ClearEdict (e);
296
297         e->priv.required->allocation_origin = PRVM_AllocationOrigin();
298
299         return e;
300 }
301
302 /*
303 =================
304 PRVM_ED_Free
305
306 Marks the edict as free
307 FIXME: walk all entities and NULL out references to this entity
308 =================
309 */
310 void PRVM_ED_Free (prvm_edict_t *ed)
311 {
312         // dont delete the null entity (world) or reserved edicts
313         if(PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
314                 return;
315
316         PRVM_GCALL(free_edict)(ed);
317
318         ed->priv.required->free = true;
319         ed->priv.required->freetime = realtime;
320         if(ed->priv.required->allocation_origin)
321         {
322                 PRVM_Free((char *)ed->priv.required->allocation_origin);
323                 ed->priv.required->allocation_origin = NULL;
324         }
325 }
326
327 //===========================================================================
328
329 /*
330 ============
331 PRVM_ED_GlobalAtOfs
332 ============
333 */
334 ddef_t *PRVM_ED_GlobalAtOfs (int ofs)
335 {
336         ddef_t          *def;
337         int                     i;
338
339         for (i=0 ; i<prog->progs->numglobaldefs ; i++)
340         {
341                 def = &prog->globaldefs[i];
342                 if (def->ofs == ofs)
343                         return def;
344         }
345         return NULL;
346 }
347
348 /*
349 ============
350 PRVM_ED_FieldAtOfs
351 ============
352 */
353 ddef_t *PRVM_ED_FieldAtOfs (int ofs)
354 {
355         ddef_t          *def;
356         int                     i;
357
358         for (i=0 ; i<prog->progs->numfielddefs ; i++)
359         {
360                 def = &prog->fielddefs[i];
361                 if (def->ofs == ofs)
362                         return def;
363         }
364         return NULL;
365 }
366
367 /*
368 ============
369 PRVM_ED_FindField
370 ============
371 */
372 ddef_t *PRVM_ED_FindField (const char *name)
373 {
374         ddef_t *def;
375         int i;
376
377         for (i=0 ; i<prog->progs->numfielddefs ; i++)
378         {
379                 def = &prog->fielddefs[i];
380                 if (!strcmp(PRVM_GetString(def->s_name), name))
381                         return def;
382         }
383         return NULL;
384 }
385
386 /*
387 ============
388 PRVM_ED_FindGlobal
389 ============
390 */
391 ddef_t *PRVM_ED_FindGlobal (const char *name)
392 {
393         ddef_t *def;
394         int i;
395
396         for (i=0 ; i<prog->progs->numglobaldefs ; i++)
397         {
398                 def = &prog->globaldefs[i];
399                 if (!strcmp(PRVM_GetString(def->s_name), name))
400                         return def;
401         }
402         return NULL;
403 }
404
405
406 /*
407 ============
408 PRVM_ED_FindFunction
409 ============
410 */
411 mfunction_t *PRVM_ED_FindFunction (const char *name)
412 {
413         mfunction_t             *func;
414         int                             i;
415
416         for (i=0 ; i<prog->progs->numfunctions ; i++)
417         {
418                 func = &prog->functions[i];
419                 if (!strcmp(PRVM_GetString(func->s_name), name))
420                         return func;
421         }
422         return NULL;
423 }
424
425
426 /*
427 ============
428 PRVM_ValueString
429
430 Returns a string describing *data in a type specific manner
431 =============
432 */
433 char *PRVM_ValueString (etype_t type, prvm_eval_t *val)
434 {
435         static char line[MAX_INPUTLINE];
436         ddef_t *def;
437         mfunction_t *f;
438         int n;
439
440         type = (etype_t)((int) type & ~DEF_SAVEGLOBAL);
441
442         switch (type)
443         {
444         case ev_string:
445                 strlcpy (line, PRVM_GetString (val->string), sizeof (line));
446                 break;
447         case ev_entity:
448                 n = val->edict;
449                 if (n < 0 || n >= prog->max_edicts)
450                         dpsnprintf (line, sizeof(line), "entity %i (invalid!)", n);
451                 else
452                         dpsnprintf (line, sizeof(line), "entity %i", n);
453                 break;
454         case ev_function:
455                 f = prog->functions + val->function;
456                 dpsnprintf (line, sizeof(line), "%s()", PRVM_GetString(f->s_name));
457                 break;
458         case ev_field:
459                 def = PRVM_ED_FieldAtOfs ( val->_int );
460                 dpsnprintf (line, sizeof(line), ".%s", PRVM_GetString(def->s_name));
461                 break;
462         case ev_void:
463                 dpsnprintf (line, sizeof(line), "void");
464                 break;
465         case ev_float:
466                 // LordHavoc: changed from %5.1f to %10.4f
467                 dpsnprintf (line, sizeof(line), "%10.4f", val->_float);
468                 break;
469         case ev_vector:
470                 // LordHavoc: changed from %5.1f to %10.4f
471                 dpsnprintf (line, sizeof(line), "'%10.4f %10.4f %10.4f'", val->vector[0], val->vector[1], val->vector[2]);
472                 break;
473         case ev_pointer:
474                 dpsnprintf (line, sizeof(line), "pointer");
475                 break;
476         default:
477                 dpsnprintf (line, sizeof(line), "bad type %i", (int) type);
478                 break;
479         }
480
481         return line;
482 }
483
484 /*
485 ============
486 PRVM_UglyValueString
487
488 Returns a string describing *data in a type specific manner
489 Easier to parse than PR_ValueString
490 =============
491 */
492 char *PRVM_UglyValueString (etype_t type, prvm_eval_t *val)
493 {
494         static char line[MAX_INPUTLINE];
495         int i;
496         const char *s;
497         ddef_t *def;
498         mfunction_t *f;
499
500         type = (etype_t)((int)type & ~DEF_SAVEGLOBAL);
501
502         switch (type)
503         {
504         case ev_string:
505                 // Parse the string a bit to turn special characters
506                 // (like newline, specifically) into escape codes,
507                 // this fixes saving games from various mods
508                 s = PRVM_GetString (val->string);
509                 for (i = 0;i < (int)sizeof(line) - 2 && *s;)
510                 {
511                         if (*s == '\n')
512                         {
513                                 line[i++] = '\\';
514                                 line[i++] = 'n';
515                         }
516                         else if (*s == '\r')
517                         {
518                                 line[i++] = '\\';
519                                 line[i++] = 'r';
520                         }
521                         else if (*s == '\\')
522                         {
523                                 line[i++] = '\\';
524                                 line[i++] = '\\';
525                         }
526                         else if (*s == '"')
527                         {
528                                 line[i++] = '\\';
529                                 line[i++] = '"';
530                         }
531                         else
532                                 line[i++] = *s;
533                         s++;
534                 }
535                 line[i] = '\0';
536                 break;
537         case ev_entity:
538                 dpsnprintf (line, sizeof (line), "%i", PRVM_NUM_FOR_EDICT(PRVM_PROG_TO_EDICT(val->edict)));
539                 break;
540         case ev_function:
541                 f = prog->functions + val->function;
542                 strlcpy (line, PRVM_GetString (f->s_name), sizeof (line));
543                 break;
544         case ev_field:
545                 def = PRVM_ED_FieldAtOfs ( val->_int );
546                 dpsnprintf (line, sizeof (line), ".%s", PRVM_GetString(def->s_name));
547                 break;
548         case ev_void:
549                 dpsnprintf (line, sizeof (line), "void");
550                 break;
551         case ev_float:
552                 dpsnprintf (line, sizeof (line), "%f", val->_float);
553                 break;
554         case ev_vector:
555                 dpsnprintf (line, sizeof (line), "%f %f %f", val->vector[0], val->vector[1], val->vector[2]);
556                 break;
557         default:
558                 dpsnprintf (line, sizeof (line), "bad type %i", type);
559                 break;
560         }
561
562         return line;
563 }
564
565 /*
566 ============
567 PRVM_GlobalString
568
569 Returns a string with a description and the contents of a global,
570 padded to 20 field width
571 ============
572 */
573 char *PRVM_GlobalString (int ofs)
574 {
575         char    *s;
576         //size_t        i;
577         ddef_t  *def;
578         void    *val;
579         static char     line[128];
580
581         val = (void *)&prog->globals.generic[ofs];
582         def = PRVM_ED_GlobalAtOfs(ofs);
583         if (!def)
584                 dpsnprintf (line, sizeof(line), "GLOBAL%i", ofs);
585         else
586         {
587                 s = PRVM_ValueString ((etype_t)def->type, (prvm_eval_t *)val);
588                 dpsnprintf (line, sizeof(line), "%s (=%s)", PRVM_GetString(def->s_name), s);
589         }
590
591         //i = strlen(line);
592         //for ( ; i<20 ; i++)
593         //      strcat (line," ");
594         //strcat (line," ");
595
596         return line;
597 }
598
599 char *PRVM_GlobalStringNoContents (int ofs)
600 {
601         //size_t        i;
602         ddef_t  *def;
603         static char     line[128];
604
605         def = PRVM_ED_GlobalAtOfs(ofs);
606         if (!def)
607                 dpsnprintf (line, sizeof(line), "GLOBAL%i", ofs);
608         else
609                 dpsnprintf (line, sizeof(line), "%s", PRVM_GetString(def->s_name));
610
611         //i = strlen(line);
612         //for ( ; i<20 ; i++)
613         //      strcat (line," ");
614         //strcat (line," ");
615
616         return line;
617 }
618
619
620 /*
621 =============
622 PRVM_ED_Print
623
624 For debugging
625 =============
626 */
627 // LordHavoc: optimized this to print out much more quickly (tempstring)
628 // LordHavoc: changed to print out every 4096 characters (incase there are a lot of fields to print)
629 void PRVM_ED_Print(prvm_edict_t *ed, const char *wildcard_fieldname)
630 {
631         size_t  l;
632         ddef_t  *d;
633         int             *v;
634         int             i, j;
635         const char      *name;
636         int             type;
637         char    tempstring[MAX_INPUTLINE], tempstring2[260]; // temporary string buffers
638
639         if (ed->priv.required->free)
640         {
641                 Con_Printf("%s: FREE\n",PRVM_NAME);
642                 return;
643         }
644
645         tempstring[0] = 0;
646         dpsnprintf(tempstring, sizeof(tempstring), "\n%s EDICT %i:\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ed));
647         for (i=1 ; i<prog->progs->numfielddefs ; i++)
648         {
649                 d = &prog->fielddefs[i];
650                 name = PRVM_GetString(d->s_name);
651                 if (name[strlen(name)-2] == '_')
652                         continue;       // skip _x, _y, _z vars
653
654                 // Check Field Name Wildcard
655                 if(wildcard_fieldname)
656                         if( !matchpattern(name, wildcard_fieldname, 1) )
657                                 // Didn't match; skip
658                                 continue;
659
660                 v = (int *)(ed->fields.vp + d->ofs);
661
662         // if the value is still all 0, skip the field
663                 type = d->type & ~DEF_SAVEGLOBAL;
664
665                 for (j=0 ; j<prvm_type_size[type] ; j++)
666                         if (v[j])
667                                 break;
668                 if (j == prvm_type_size[type])
669                         continue;
670
671                 if (strlen(name) > sizeof(tempstring2)-4)
672                 {
673                         memcpy (tempstring2, name, sizeof(tempstring2)-4);
674                         tempstring2[sizeof(tempstring2)-4] = tempstring2[sizeof(tempstring2)-3] = tempstring2[sizeof(tempstring2)-2] = '.';
675                         tempstring2[sizeof(tempstring2)-1] = 0;
676                         name = tempstring2;
677                 }
678                 strlcat(tempstring, name, sizeof(tempstring));
679                 for (l = strlen(name);l < 14;l++)
680                         strlcat(tempstring, " ", sizeof(tempstring));
681                 strlcat(tempstring, " ", sizeof(tempstring));
682
683                 name = PRVM_ValueString((etype_t)d->type, (prvm_eval_t *)v);
684                 if (strlen(name) > sizeof(tempstring2)-4)
685                 {
686                         memcpy (tempstring2, name, sizeof(tempstring2)-4);
687                         tempstring2[sizeof(tempstring2)-4] = tempstring2[sizeof(tempstring2)-3] = tempstring2[sizeof(tempstring2)-2] = '.';
688                         tempstring2[sizeof(tempstring2)-1] = 0;
689                         name = tempstring2;
690                 }
691                 strlcat(tempstring, name, sizeof(tempstring));
692                 strlcat(tempstring, "\n", sizeof(tempstring));
693                 if (strlen(tempstring) >= sizeof(tempstring)/2)
694                 {
695                         Con_Print(tempstring);
696                         tempstring[0] = 0;
697                 }
698         }
699         if (tempstring[0])
700                 Con_Print(tempstring);
701 }
702
703 /*
704 =============
705 PRVM_ED_Write
706
707 For savegames
708 =============
709 */
710 extern cvar_t developer_entityparsing;
711 void PRVM_ED_Write (qfile_t *f, prvm_edict_t *ed)
712 {
713         ddef_t  *d;
714         int             *v;
715         int             i, j;
716         const char      *name;
717         int             type;
718
719         FS_Print(f, "{\n");
720
721         if (ed->priv.required->free)
722         {
723                 FS_Print(f, "}\n");
724                 return;
725         }
726
727         for (i=1 ; i<prog->progs->numfielddefs ; i++)
728         {
729                 d = &prog->fielddefs[i];
730                 name = PRVM_GetString(d->s_name);
731
732                 if(developer_entityparsing.integer)
733                         Con_Printf("PRVM_ED_Write: at entity %d field %s\n", PRVM_NUM_FOR_EDICT(ed), name);
734
735                 if (name[strlen(name)-2] == '_')
736                         continue;       // skip _x, _y, _z vars
737
738                 v = (int *)(ed->fields.vp + d->ofs);
739
740         // if the value is still all 0, skip the field
741                 type = d->type & ~DEF_SAVEGLOBAL;
742                 for (j=0 ; j<prvm_type_size[type] ; j++)
743                         if (v[j])
744                                 break;
745                 if (j == prvm_type_size[type])
746                         continue;
747
748                 FS_Printf(f,"\"%s\" ",name);
749                 prog->statestring = va("PRVM_ED_Write, ent=%d, name=%s", i, name);
750                 FS_Printf(f,"\"%s\"\n", PRVM_UglyValueString((etype_t)d->type, (prvm_eval_t *)v));
751                 prog->statestring = NULL;
752         }
753
754         FS_Print(f, "}\n");
755 }
756
757 void PRVM_ED_PrintNum (int ent, const char *wildcard_fieldname)
758 {
759         PRVM_ED_Print(PRVM_EDICT_NUM(ent), wildcard_fieldname);
760 }
761
762 /*
763 =============
764 PRVM_ED_PrintEdicts_f
765
766 For debugging, prints all the entities in the current server
767 =============
768 */
769 void PRVM_ED_PrintEdicts_f (void)
770 {
771         int             i;
772         const char *wildcard_fieldname;
773
774         if(Cmd_Argc() < 2 || Cmd_Argc() > 3)
775         {
776                 Con_Print("prvm_edicts <program name> <optional field name wildcard>\n");
777                 return;
778         }
779
780         PRVM_Begin;
781         if(!PRVM_SetProgFromString(Cmd_Argv(1)))
782                 return;
783
784         if( Cmd_Argc() == 3)
785                 wildcard_fieldname = Cmd_Argv(2);
786         else
787                 wildcard_fieldname = NULL;
788
789         Con_Printf("%s: %i entities\n", PRVM_NAME, prog->num_edicts);
790         for (i=0 ; i<prog->num_edicts ; i++)
791                 PRVM_ED_PrintNum (i, wildcard_fieldname);
792
793         PRVM_End;
794 }
795
796 /*
797 =============
798 PRVM_ED_PrintEdict_f
799
800 For debugging, prints a single edict
801 =============
802 */
803 void PRVM_ED_PrintEdict_f (void)
804 {
805         int             i;
806         const char      *wildcard_fieldname;
807
808         if(Cmd_Argc() < 3 || Cmd_Argc() > 4)
809         {
810                 Con_Print("prvm_edict <program name> <edict number> <optional field name wildcard>\n");
811                 return;
812         }
813
814         PRVM_Begin;
815         if(!PRVM_SetProgFromString(Cmd_Argv(1)))
816                 return;
817
818         i = atoi (Cmd_Argv(2));
819         if (i >= prog->num_edicts)
820         {
821                 Con_Print("Bad edict number\n");
822                 PRVM_End;
823                 return;
824         }
825         if( Cmd_Argc() == 4)
826                 // Optional Wildcard Provided
827                 wildcard_fieldname = Cmd_Argv(3);
828         else
829                 // Use All
830                 wildcard_fieldname = NULL;
831         PRVM_ED_PrintNum (i, wildcard_fieldname);
832
833         PRVM_End;
834 }
835
836 /*
837 =============
838 PRVM_ED_Count
839
840 For debugging
841 =============
842 */
843 // 2 possibilities : 1. just displaying the active edict count
844 //                                       2. making a function pointer [x]
845 void PRVM_ED_Count_f (void)
846 {
847         int             i;
848         prvm_edict_t    *ent;
849         int             active;
850
851         if(Cmd_Argc() != 2)
852         {
853                 Con_Print("prvm_count <program name>\n");
854                 return;
855         }
856
857         PRVM_Begin;
858         if(!PRVM_SetProgFromString(Cmd_Argv(1)))
859                 return;
860
861         if(prog->count_edicts)
862                 prog->count_edicts();
863         else
864         {
865                 active = 0;
866                 for (i=0 ; i<prog->num_edicts ; i++)
867                 {
868                         ent = PRVM_EDICT_NUM(i);
869                         if (ent->priv.required->free)
870                                 continue;
871                         active++;
872                 }
873
874                 Con_Printf("num_edicts:%3i\n", prog->num_edicts);
875                 Con_Printf("active    :%3i\n", active);
876         }
877
878         PRVM_End;
879 }
880
881 /*
882 ==============================================================================
883
884                                         ARCHIVING GLOBALS
885
886 FIXME: need to tag constants, doesn't really work
887 ==============================================================================
888 */
889
890 /*
891 =============
892 PRVM_ED_WriteGlobals
893 =============
894 */
895 void PRVM_ED_WriteGlobals (qfile_t *f)
896 {
897         ddef_t          *def;
898         int                     i;
899         const char              *name;
900         int                     type;
901
902         FS_Print(f,"{\n");
903         for (i=0 ; i<prog->progs->numglobaldefs ; i++)
904         {
905                 def = &prog->globaldefs[i];
906                 type = def->type;
907                 if ( !(def->type & DEF_SAVEGLOBAL) )
908                         continue;
909                 type &= ~DEF_SAVEGLOBAL;
910
911                 if (type != ev_string && type != ev_float && type != ev_entity)
912                         continue;
913
914                 name = PRVM_GetString(def->s_name);
915
916                 if(developer_entityparsing.integer)
917                         Con_Printf("PRVM_ED_WriteGlobals: at global %s\n", name);
918
919                 prog->statestring = va("PRVM_ED_WriteGlobals, name=%s", name);
920                 FS_Printf(f,"\"%s\" ", name);
921                 FS_Printf(f,"\"%s\"\n", PRVM_UglyValueString((etype_t)type, (prvm_eval_t *)&prog->globals.generic[def->ofs]));
922                 prog->statestring = NULL;
923         }
924         FS_Print(f,"}\n");
925 }
926
927 /*
928 =============
929 PRVM_ED_ParseGlobals
930 =============
931 */
932 void PRVM_ED_ParseGlobals (const char *data)
933 {
934         char keyname[MAX_INPUTLINE];
935         ddef_t *key;
936
937         while (1)
938         {
939                 // parse key
940                 if (!COM_ParseToken_Simple(&data, false, false))
941                         PRVM_ERROR ("PRVM_ED_ParseGlobals: EOF without closing brace");
942                 if (com_token[0] == '}')
943                         break;
944
945                 if (developer_entityparsing.integer)
946                         Con_Printf("Key: \"%s\"", com_token);
947
948                 strlcpy (keyname, com_token, sizeof(keyname));
949
950                 // parse value
951                 if (!COM_ParseToken_Simple(&data, false, true))
952                         PRVM_ERROR ("PRVM_ED_ParseGlobals: EOF without closing brace");
953
954                 if (developer_entityparsing.integer)
955                         Con_Printf(" \"%s\"\n", com_token);
956
957                 if (com_token[0] == '}')
958                         PRVM_ERROR ("PRVM_ED_ParseGlobals: closing brace without data");
959
960                 key = PRVM_ED_FindGlobal (keyname);
961                 if (!key)
962                 {
963                         Con_DPrintf("'%s' is not a global on %s\n", keyname, PRVM_NAME);
964                         continue;
965                 }
966
967                 if (!PRVM_ED_ParseEpair(NULL, key, com_token, true))
968                         PRVM_ERROR ("PRVM_ED_ParseGlobals: parse error");
969         }
970 }
971
972 //============================================================================
973
974
975 /*
976 =============
977 PRVM_ED_ParseEval
978
979 Can parse either fields or globals
980 returns false if error
981 =============
982 */
983 qboolean PRVM_ED_ParseEpair(prvm_edict_t *ent, ddef_t *key, const char *s, qboolean parsebackslash)
984 {
985         int i, l;
986         char *new_p;
987         ddef_t *def;
988         prvm_eval_t *val;
989         mfunction_t *func;
990
991         if (ent)
992                 val = (prvm_eval_t *)(ent->fields.vp + key->ofs);
993         else
994                 val = (prvm_eval_t *)(prog->globals.generic + key->ofs);
995         switch (key->type & ~DEF_SAVEGLOBAL)
996         {
997         case ev_string:
998                 l = (int)strlen(s) + 1;
999                 val->string = PRVM_AllocString(l, &new_p);
1000                 for (i = 0;i < l;i++)
1001                 {
1002                         if (s[i] == '\\' && s[i+1] && parsebackslash)
1003                         {
1004                                 i++;
1005                                 if (s[i] == 'n')
1006                                         *new_p++ = '\n';
1007                                 else if (s[i] == 'r')
1008                                         *new_p++ = '\r';
1009                                 else
1010                                         *new_p++ = s[i];
1011                         }
1012                         else
1013                                 *new_p++ = s[i];
1014                 }
1015                 break;
1016
1017         case ev_float:
1018                 while (*s && ISWHITESPACE(*s))
1019                         s++;
1020                 val->_float = atof(s);
1021                 break;
1022
1023         case ev_vector:
1024                 for (i = 0;i < 3;i++)
1025                 {
1026                         while (*s && ISWHITESPACE(*s))
1027                                 s++;
1028                         if (!*s)
1029                                 break;
1030                         val->vector[i] = atof(s);
1031                         while (!ISWHITESPACE(*s))
1032                                 s++;
1033                         if (!*s)
1034                                 break;
1035                 }
1036                 break;
1037
1038         case ev_entity:
1039                 while (*s && ISWHITESPACE(*s))
1040                         s++;
1041                 i = atoi(s);
1042                 if (i >= prog->limit_edicts)
1043                         Con_Printf("PRVM_ED_ParseEpair: ev_entity reference too large (edict %u >= MAX_EDICTS %u) on %s\n", (unsigned int)i, prog->limit_edicts, PRVM_NAME);
1044                 while (i >= prog->max_edicts)
1045                         PRVM_MEM_IncreaseEdicts();
1046                 // if IncreaseEdicts was called the base pointer needs to be updated
1047                 if (ent)
1048                         val = (prvm_eval_t *)(ent->fields.vp + key->ofs);
1049                 val->edict = PRVM_EDICT_TO_PROG(PRVM_EDICT_NUM((int)i));
1050                 break;
1051
1052         case ev_field:
1053                 if (*s != '.')
1054                 {
1055                         Con_DPrintf("PRVM_ED_ParseEpair: Bogus field name %s in %s\n", s, PRVM_NAME);
1056                         return false;
1057                 }
1058                 def = PRVM_ED_FindField(s + 1);
1059                 if (!def)
1060                 {
1061                         Con_DPrintf("PRVM_ED_ParseEpair: Can't find field %s in %s\n", s, PRVM_NAME);
1062                         return false;
1063                 }
1064                 val->_int = def->ofs;
1065                 break;
1066
1067         case ev_function:
1068                 func = PRVM_ED_FindFunction(s);
1069                 if (!func)
1070                 {
1071                         Con_Printf("PRVM_ED_ParseEpair: Can't find function %s in %s\n", s, PRVM_NAME);
1072                         return false;
1073                 }
1074                 val->function = func - prog->functions;
1075                 break;
1076
1077         default:
1078                 Con_Printf("PRVM_ED_ParseEpair: Unknown key->type %i for key \"%s\" on %s\n", key->type, PRVM_GetString(key->s_name), PRVM_NAME);
1079                 return false;
1080         }
1081         return true;
1082 }
1083
1084 /*
1085 =============
1086 PRVM_GameCommand_f
1087
1088 Console command to send a string to QC function GameCommand of the
1089 indicated progs
1090
1091 Usage:
1092   sv_cmd adminmsg 3 "do not teamkill"
1093   cl_cmd someclientcommand
1094   menu_cmd somemenucommand
1095
1096 All progs can support this extension; sg calls it in server QC, cg in client
1097 QC, mg in menu QC.
1098 =============
1099 */
1100 void PRVM_GameCommand(const char *whichprogs, const char *whichcmd)
1101 {
1102         if(Cmd_Argc() < 1)
1103         {
1104                 Con_Printf("%s text...\n", whichcmd);
1105                 return;
1106         }
1107
1108         PRVM_Begin;
1109         if(!PRVM_SetProgFromString(whichprogs))
1110         // note: this is not PRVM_SetProg because that one aborts "hard" using PRVM_Error
1111         // also, it makes printing error messages easier!
1112         {
1113                 Con_Printf("%s program not loaded.\n", whichprogs);
1114                 return;
1115         }
1116
1117         if(!prog->funcoffsets.GameCommand)
1118         {
1119                 Con_Printf("%s program do not support GameCommand!\n", whichprogs);
1120         }
1121         else
1122         {
1123                 int restorevm_tempstringsbuf_cursize;
1124                 const char *s;
1125
1126                 s = Cmd_Args();
1127
1128                 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
1129                 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(s ? s : "");
1130                 PRVM_ExecuteProgram (prog->funcoffsets.GameCommand, "QC function GameCommand is missing");
1131                 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
1132         }
1133
1134         PRVM_End;
1135 }
1136 void PRVM_GameCommand_Server_f(void)
1137 {
1138         PRVM_GameCommand("server", "sv_cmd");
1139 }
1140 void PRVM_GameCommand_Client_f(void)
1141 {
1142         PRVM_GameCommand("client", "cl_cmd");
1143 }
1144 void PRVM_GameCommand_Menu_f(void)
1145 {
1146         PRVM_GameCommand("menu", "menu_cmd");
1147 }
1148
1149 /*
1150 =============
1151 PRVM_ED_EdictGet_f
1152
1153 Console command to load a field of a specified edict
1154 =============
1155 */
1156 void PRVM_ED_EdictGet_f(void)
1157 {
1158         prvm_edict_t *ed;
1159         ddef_t *key;
1160         const char *s;
1161         prvm_eval_t *v;
1162
1163         if(Cmd_Argc() != 4 && Cmd_Argc() != 5)
1164         {
1165                 Con_Print("prvm_edictget <program name> <edict number> <field> [<cvar>]\n");
1166                 return;
1167         }
1168
1169         PRVM_Begin;
1170         if(!PRVM_SetProgFromString(Cmd_Argv(1)))
1171         {
1172                 Con_Printf("Wrong program name %s !\n", Cmd_Argv(1));
1173                 return;
1174         }
1175
1176         ed = PRVM_EDICT_NUM(atoi(Cmd_Argv(2)));
1177
1178         if((key = PRVM_ED_FindField(Cmd_Argv(3))) == 0)
1179         {
1180                 Con_Printf("Key %s not found !\n", Cmd_Argv(3));
1181                 goto fail;
1182         }
1183
1184         v = (prvm_eval_t *)(ed->fields.vp + key->ofs);
1185         s = PRVM_UglyValueString(key->type, v);
1186         if(Cmd_Argc() == 5)
1187         {
1188                 cvar_t *cvar = Cvar_FindVar(Cmd_Argv(4));
1189                 if (cvar && cvar->flags & CVAR_READONLY)
1190                 {
1191                         Con_Printf("prvm_edictget: %s is read-only\n", cvar->name);
1192                         goto fail;
1193                 }
1194                 Cvar_Get(Cmd_Argv(4), s, 0, NULL);
1195         }
1196         else
1197                 Con_Printf("%s\n", s);
1198
1199 fail:
1200         PRVM_End;
1201 }
1202
1203 void PRVM_ED_GlobalGet_f(void)
1204 {
1205         ddef_t *key;
1206         const char *s;
1207         prvm_eval_t *v;
1208
1209         if(Cmd_Argc() != 3 && Cmd_Argc() != 4)
1210         {
1211                 Con_Print("prvm_globalget <program name> <global> [<cvar>]\n");
1212                 return;
1213         }
1214
1215         PRVM_Begin;
1216         if(!PRVM_SetProgFromString(Cmd_Argv(1)))
1217         {
1218                 Con_Printf("Wrong program name %s !\n", Cmd_Argv(1));
1219                 return;
1220         }
1221
1222         key = PRVM_ED_FindGlobal(Cmd_Argv(2));
1223         if(!key)
1224         {
1225                 Con_Printf( "No global '%s' in %s!\n", Cmd_Argv(2), Cmd_Argv(1) );
1226                 goto fail;
1227         }
1228
1229         v = (prvm_eval_t *) &prog->globals.generic[key->ofs];
1230         s = PRVM_UglyValueString(key->type, v);
1231         if(Cmd_Argc() == 4)
1232         {
1233                 cvar_t *cvar = Cvar_FindVar(Cmd_Argv(3));
1234                 if (cvar && cvar->flags & CVAR_READONLY)
1235                 {
1236                         Con_Printf("prvm_globalget: %s is read-only\n", cvar->name);
1237                         goto fail;
1238                 }
1239                 Cvar_Get(Cmd_Argv(3), s, 0, NULL);
1240         }
1241         else
1242                 Con_Printf("%s\n", s);
1243
1244 fail:
1245         PRVM_End;
1246 }
1247
1248 /*
1249 =============
1250 PRVM_ED_EdictSet_f
1251
1252 Console command to set a field of a specified edict
1253 =============
1254 */
1255 void PRVM_ED_EdictSet_f(void)
1256 {
1257         prvm_edict_t *ed;
1258         ddef_t *key;
1259
1260         if(Cmd_Argc() != 5)
1261         {
1262                 Con_Print("prvm_edictset <program name> <edict number> <field> <value>\n");
1263                 return;
1264         }
1265
1266         PRVM_Begin;
1267         if(!PRVM_SetProgFromString(Cmd_Argv(1)))
1268         {
1269                 Con_Printf("Wrong program name %s !\n", Cmd_Argv(1));
1270                 return;
1271         }
1272
1273         ed = PRVM_EDICT_NUM(atoi(Cmd_Argv(2)));
1274
1275         if((key = PRVM_ED_FindField(Cmd_Argv(3))) == 0)
1276                 Con_Printf("Key %s not found !\n", Cmd_Argv(3));
1277         else
1278                 PRVM_ED_ParseEpair(ed, key, Cmd_Argv(4), true);
1279
1280         PRVM_End;
1281 }
1282
1283 /*
1284 ====================
1285 PRVM_ED_ParseEdict
1286
1287 Parses an edict out of the given string, returning the new position
1288 ed should be a properly initialized empty edict.
1289 Used for initial level load and for savegames.
1290 ====================
1291 */
1292 const char *PRVM_ED_ParseEdict (const char *data, prvm_edict_t *ent)
1293 {
1294         ddef_t *key;
1295         qboolean anglehack;
1296         qboolean init;
1297         char keyname[256];
1298         size_t n;
1299
1300         init = false;
1301
1302 // go through all the dictionary pairs
1303         while (1)
1304         {
1305         // parse key
1306                 if (!COM_ParseToken_Simple(&data, false, false))
1307                         PRVM_ERROR ("PRVM_ED_ParseEdict: EOF without closing brace");
1308                 if (developer_entityparsing.integer)
1309                         Con_Printf("Key: \"%s\"", com_token);
1310                 if (com_token[0] == '}')
1311                         break;
1312
1313                 // anglehack is to allow QuakeEd to write single scalar angles
1314                 // and allow them to be turned into vectors. (FIXME...)
1315                 if (!strcmp(com_token, "angle"))
1316                 {
1317                         strlcpy (com_token, "angles", sizeof(com_token));
1318                         anglehack = true;
1319                 }
1320                 else
1321                         anglehack = false;
1322
1323                 // FIXME: change light to _light to get rid of this hack
1324                 if (!strcmp(com_token, "light"))
1325                         strlcpy (com_token, "light_lev", sizeof(com_token));    // hack for single light def
1326
1327                 strlcpy (keyname, com_token, sizeof(keyname));
1328
1329                 // another hack to fix keynames with trailing spaces
1330                 n = strlen(keyname);
1331                 while (n && keyname[n-1] == ' ')
1332                 {
1333                         keyname[n-1] = 0;
1334                         n--;
1335                 }
1336
1337         // parse value
1338                 if (!COM_ParseToken_Simple(&data, false, false))
1339                         PRVM_ERROR ("PRVM_ED_ParseEdict: EOF without closing brace");
1340                 if (developer_entityparsing.integer)
1341                         Con_Printf(" \"%s\"\n", com_token);
1342
1343                 if (com_token[0] == '}')
1344                         PRVM_ERROR ("PRVM_ED_ParseEdict: closing brace without data");
1345
1346                 init = true;
1347
1348                 // ignore attempts to set key "" (this problem occurs in nehahra neh1m8.bsp)
1349                 if (!keyname[0])
1350                         continue;
1351
1352 // keynames with a leading underscore are used for utility comments,
1353 // and are immediately discarded by quake
1354                 if (keyname[0] == '_')
1355                         continue;
1356
1357                 key = PRVM_ED_FindField (keyname);
1358                 if (!key)
1359                 {
1360                         Con_DPrintf("%s: '%s' is not a field\n", PRVM_NAME, keyname);
1361                         continue;
1362                 }
1363
1364                 if (anglehack)
1365                 {
1366                         char    temp[32];
1367                         strlcpy (temp, com_token, sizeof(temp));
1368                         dpsnprintf (com_token, sizeof(com_token), "0 %s 0", temp);
1369                 }
1370
1371                 if (!PRVM_ED_ParseEpair(ent, key, com_token, strcmp(keyname, "wad") != 0))
1372                         PRVM_ERROR ("PRVM_ED_ParseEdict: parse error");
1373         }
1374
1375         if (!init)
1376                 ent->priv.required->free = true;
1377
1378         return data;
1379 }
1380
1381
1382 /*
1383 ================
1384 PRVM_ED_LoadFromFile
1385
1386 The entities are directly placed in the array, rather than allocated with
1387 PRVM_ED_Alloc, because otherwise an error loading the map would have entity
1388 number references out of order.
1389
1390 Creates a server's entity / program execution context by
1391 parsing textual entity definitions out of an ent file.
1392
1393 Used for both fresh maps and savegame loads.  A fresh map would also need
1394 to call PRVM_ED_CallSpawnFunctions () to let the objects initialize themselves.
1395 ================
1396 */
1397 void PRVM_ED_LoadFromFile (const char *data)
1398 {
1399         prvm_edict_t *ent;
1400         int parsed, inhibited, spawned, died;
1401         const char *funcname;
1402         mfunction_t *func;
1403
1404         parsed = 0;
1405         inhibited = 0;
1406         spawned = 0;
1407         died = 0;
1408
1409
1410 // parse ents
1411         while (1)
1412         {
1413 // parse the opening brace
1414                 if (!COM_ParseToken_Simple(&data, false, false))
1415                         break;
1416                 if (com_token[0] != '{')
1417                         PRVM_ERROR ("PRVM_ED_LoadFromFile: %s: found %s when expecting {", PRVM_NAME, com_token);
1418
1419                 // CHANGED: this is not conform to PR_LoadFromFile
1420                 if(prog->loadintoworld)
1421                 {
1422                         prog->loadintoworld = false;
1423                         ent = PRVM_EDICT_NUM(0);
1424                 }
1425                 else
1426                         ent = PRVM_ED_Alloc();
1427
1428                 // clear it
1429                 if (ent != prog->edicts)        // hack
1430                         memset (ent->fields.vp, 0, prog->progs->entityfields * 4);
1431
1432                 data = PRVM_ED_ParseEdict (data, ent);
1433                 parsed++;
1434
1435                 // remove the entity ?
1436                 if(prog->load_edict && !prog->load_edict(ent))
1437                 {
1438                         PRVM_ED_Free(ent);
1439                         inhibited++;
1440                         continue;
1441                 }
1442
1443                 if (prog->funcoffsets.SV_OnEntityPreSpawnFunction)
1444                 {
1445                         // self = ent
1446                         PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict = PRVM_EDICT_TO_PROG(ent);
1447                         PRVM_ExecuteProgram (prog->funcoffsets.SV_OnEntityPreSpawnFunction, "QC function SV_OnEntityPreSpawnFunction is missing");
1448                 }
1449
1450                 if(ent->priv.required->free)
1451                 {
1452                         inhibited++;
1453                         continue;
1454                 }
1455
1456 //
1457 // immediately call spawn function, but only if there is a self global and a classname
1458 //
1459                 if(!ent->priv.required->free)
1460                 if(prog->globaloffsets.self >= 0 && prog->fieldoffsets.classname >= 0)
1461                 {
1462                         string_t handle =  PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.classname)->string;
1463                         if (!handle)
1464                         {
1465                                 Con_Print("No classname for:\n");
1466                                 PRVM_ED_Print(ent, NULL);
1467                                 PRVM_ED_Free (ent);
1468                                 continue;
1469                         }
1470
1471                         // look for the spawn function
1472                         funcname = PRVM_GetString(handle);
1473                         func = PRVM_ED_FindFunction (va("spawnfunc_%s", funcname));
1474                         if(!func)
1475                                 if(prog->globaloffsets.require_spawnfunc_prefix < 0)
1476                                         func = PRVM_ED_FindFunction (funcname);
1477
1478                         if (!func)
1479                         {
1480                                 // check for OnEntityNoSpawnFunction
1481                                 if (prog->funcoffsets.SV_OnEntityNoSpawnFunction)
1482                                 {
1483                                         // self = ent
1484                                         PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict = PRVM_EDICT_TO_PROG(ent);
1485                                         PRVM_ExecuteProgram (prog->funcoffsets.SV_OnEntityNoSpawnFunction, "QC function SV_OnEntityNoSpawnFunction is missing");
1486                                 }
1487                                 else
1488                                 {
1489                                         if (developer.integer) // don't confuse non-developers with errors
1490                                         {
1491                                                 Con_Print("No spawn function for:\n");
1492                                                 PRVM_ED_Print(ent, NULL);
1493                                         }
1494                                         PRVM_ED_Free (ent);
1495                                         continue; // not included in "inhibited" count
1496                                 }
1497                         }
1498                         else
1499                         {
1500                                 // self = ent
1501                                 PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict = PRVM_EDICT_TO_PROG(ent);
1502                                 PRVM_ExecuteProgram (func - prog->functions, "");
1503                         }
1504                 }
1505
1506                 if(!ent->priv.required->free)
1507                 if (prog->funcoffsets.SV_OnEntityPostSpawnFunction)
1508                 {
1509                         // self = ent
1510                         PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict = PRVM_EDICT_TO_PROG(ent);
1511                         PRVM_ExecuteProgram (prog->funcoffsets.SV_OnEntityPostSpawnFunction, "QC function SV_OnEntityPostSpawnFunction is missing");
1512                 }
1513
1514                 spawned++;
1515                 if (ent->priv.required->free)
1516                         died++;
1517         }
1518
1519         Con_DPrintf("%s: %i new entities parsed, %i new inhibited, %i (%i new) spawned (whereas %i removed self, %i stayed)\n", PRVM_NAME, parsed, inhibited, prog->num_edicts, spawned, died, spawned - died);
1520 }
1521
1522 void PRVM_FindOffsets(void)
1523 {
1524         // field and global searches use -1 for NULL
1525         memset(&prog->fieldoffsets, -1, sizeof(prog->fieldoffsets));
1526         memset(&prog->globaloffsets, -1, sizeof(prog->globaloffsets));
1527         // functions use 0 for NULL
1528         memset(&prog->funcoffsets, 0, sizeof(prog->funcoffsets));
1529
1530         // server and client qc use a lot of similar fields, so this is combined
1531         prog->fieldoffsets.SendEntity                     = PRVM_ED_FindFieldOffset("SendEntity");
1532         prog->fieldoffsets.SendFlags                      = PRVM_ED_FindFieldOffset("SendFlags");
1533         prog->fieldoffsets.Version                        = PRVM_ED_FindFieldOffset("Version");
1534         prog->fieldoffsets.alpha                          = PRVM_ED_FindFieldOffset("alpha");
1535         prog->fieldoffsets.ammo_cells1                    = PRVM_ED_FindFieldOffset("ammo_cells1");
1536         prog->fieldoffsets.ammo_lava_nails                = PRVM_ED_FindFieldOffset("ammo_lava_nails");
1537         prog->fieldoffsets.ammo_multi_rockets             = PRVM_ED_FindFieldOffset("ammo_multi_rockets");
1538         prog->fieldoffsets.ammo_nails1                    = PRVM_ED_FindFieldOffset("ammo_nails1");
1539         prog->fieldoffsets.ammo_plasma                    = PRVM_ED_FindFieldOffset("ammo_plasma");
1540         prog->fieldoffsets.ammo_rockets1                  = PRVM_ED_FindFieldOffset("ammo_rockets1");
1541         prog->fieldoffsets.ammo_shells1                   = PRVM_ED_FindFieldOffset("ammo_shells1");
1542         prog->fieldoffsets.angles                         = PRVM_ED_FindFieldOffset("angles");
1543         prog->fieldoffsets.button3                        = PRVM_ED_FindFieldOffset("button3");
1544         prog->fieldoffsets.button4                        = PRVM_ED_FindFieldOffset("button4");
1545         prog->fieldoffsets.button5                        = PRVM_ED_FindFieldOffset("button5");
1546         prog->fieldoffsets.button6                        = PRVM_ED_FindFieldOffset("button6");
1547         prog->fieldoffsets.button7                        = PRVM_ED_FindFieldOffset("button7");
1548         prog->fieldoffsets.button8                        = PRVM_ED_FindFieldOffset("button8");
1549         prog->fieldoffsets.button9                        = PRVM_ED_FindFieldOffset("button9");
1550         prog->fieldoffsets.button10                       = PRVM_ED_FindFieldOffset("button10");
1551         prog->fieldoffsets.button11                       = PRVM_ED_FindFieldOffset("button11");
1552         prog->fieldoffsets.button12                       = PRVM_ED_FindFieldOffset("button12");
1553         prog->fieldoffsets.button13                       = PRVM_ED_FindFieldOffset("button13");
1554         prog->fieldoffsets.button14                       = PRVM_ED_FindFieldOffset("button14");
1555         prog->fieldoffsets.button15                       = PRVM_ED_FindFieldOffset("button15");
1556         prog->fieldoffsets.button16                       = PRVM_ED_FindFieldOffset("button16");
1557         prog->fieldoffsets.buttonchat                     = PRVM_ED_FindFieldOffset("buttonchat");
1558         prog->fieldoffsets.buttonuse                      = PRVM_ED_FindFieldOffset("buttonuse");
1559         prog->fieldoffsets.chain                          = PRVM_ED_FindFieldOffset("chain");
1560         prog->fieldoffsets.classname                      = PRVM_ED_FindFieldOffset("classname");
1561         prog->fieldoffsets.clientcamera                   = PRVM_ED_FindFieldOffset("clientcamera");
1562         prog->fieldoffsets.clientcolors                   = PRVM_ED_FindFieldOffset("clientcolors");
1563         prog->fieldoffsets.clientstatus                   = PRVM_ED_FindFieldOffset("clientstatus");
1564         prog->fieldoffsets.color                          = PRVM_ED_FindFieldOffset("color");
1565         prog->fieldoffsets.colormod                       = PRVM_ED_FindFieldOffset("colormod");
1566         prog->fieldoffsets.contentstransition             = PRVM_ED_FindFieldOffset("contentstransition");
1567         prog->fieldoffsets.cursor_active                  = PRVM_ED_FindFieldOffset("cursor_active");
1568         prog->fieldoffsets.cursor_screen                  = PRVM_ED_FindFieldOffset("cursor_screen");
1569         prog->fieldoffsets.cursor_trace_endpos            = PRVM_ED_FindFieldOffset("cursor_trace_endpos");
1570         prog->fieldoffsets.cursor_trace_ent               = PRVM_ED_FindFieldOffset("cursor_trace_ent");
1571         prog->fieldoffsets.cursor_trace_start             = PRVM_ED_FindFieldOffset("cursor_trace_start");
1572         prog->fieldoffsets.customizeentityforclient       = PRVM_ED_FindFieldOffset("customizeentityforclient");
1573         prog->fieldoffsets.dimension_hit                  = PRVM_ED_FindFieldOffset("dimension_hit");
1574         prog->fieldoffsets.dimension_solid                = PRVM_ED_FindFieldOffset("dimension_solid");
1575         prog->fieldoffsets.disableclientprediction        = PRVM_ED_FindFieldOffset("disableclientprediction");
1576         prog->fieldoffsets.dphitcontentsmask              = PRVM_ED_FindFieldOffset("dphitcontentsmask");
1577         prog->fieldoffsets.drawonlytoclient               = PRVM_ED_FindFieldOffset("drawonlytoclient");
1578         prog->fieldoffsets.exteriormodeltoclient          = PRVM_ED_FindFieldOffset("exteriormodeltoclient");
1579         prog->fieldoffsets.fatness                        = PRVM_ED_FindFieldOffset("fatness");
1580         prog->fieldoffsets.forceshader                    = PRVM_ED_FindFieldOffset("forceshader");
1581         prog->fieldoffsets.frame                          = PRVM_ED_FindFieldOffset("frame");
1582         prog->fieldoffsets.frame1time                     = PRVM_ED_FindFieldOffset("frame1time");
1583         prog->fieldoffsets.frame2                         = PRVM_ED_FindFieldOffset("frame2");
1584         prog->fieldoffsets.frame2time                     = PRVM_ED_FindFieldOffset("frame2time");
1585         prog->fieldoffsets.frame3                         = PRVM_ED_FindFieldOffset("frame3");
1586         prog->fieldoffsets.frame3time                     = PRVM_ED_FindFieldOffset("frame3time");
1587         prog->fieldoffsets.frame4                         = PRVM_ED_FindFieldOffset("frame4");
1588         prog->fieldoffsets.frame4time                     = PRVM_ED_FindFieldOffset("frame4time");
1589         prog->fieldoffsets.fullbright                     = PRVM_ED_FindFieldOffset("fullbright");
1590         prog->fieldoffsets.glow_color                     = PRVM_ED_FindFieldOffset("glow_color");
1591         prog->fieldoffsets.glow_size                      = PRVM_ED_FindFieldOffset("glow_size");
1592         prog->fieldoffsets.glow_trail                     = PRVM_ED_FindFieldOffset("glow_trail");
1593         prog->fieldoffsets.glowmod                        = PRVM_ED_FindFieldOffset("glowmod");
1594         prog->fieldoffsets.gravity                        = PRVM_ED_FindFieldOffset("gravity");
1595         prog->fieldoffsets.groundentity                   = PRVM_ED_FindFieldOffset("groundentity");
1596         prog->fieldoffsets.hull                           = PRVM_ED_FindFieldOffset("hull");
1597         prog->fieldoffsets.ideal_yaw                      = PRVM_ED_FindFieldOffset("ideal_yaw");
1598         prog->fieldoffsets.idealpitch                     = PRVM_ED_FindFieldOffset("idealpitch");
1599         prog->fieldoffsets.items2                         = PRVM_ED_FindFieldOffset("items2");
1600         prog->fieldoffsets.lerpfrac                       = PRVM_ED_FindFieldOffset("lerpfrac");
1601         prog->fieldoffsets.lerpfrac3                      = PRVM_ED_FindFieldOffset("lerpfrac3");
1602         prog->fieldoffsets.lerpfrac4                      = PRVM_ED_FindFieldOffset("lerpfrac4");
1603         prog->fieldoffsets.light_lev                      = PRVM_ED_FindFieldOffset("light_lev");
1604         prog->fieldoffsets.message                        = PRVM_ED_FindFieldOffset("message");
1605         prog->fieldoffsets.modelflags                     = PRVM_ED_FindFieldOffset("modelflags");
1606         prog->fieldoffsets.movement                       = PRVM_ED_FindFieldOffset("movement");
1607         prog->fieldoffsets.movetypesteplandevent          = PRVM_ED_FindFieldOffset("movetypesteplandevent");
1608         prog->fieldoffsets.netaddress                     = PRVM_ED_FindFieldOffset("netaddress");
1609         prog->fieldoffsets.nextthink                      = PRVM_ED_FindFieldOffset("nextthink");
1610         prog->fieldoffsets.nodrawtoclient                 = PRVM_ED_FindFieldOffset("nodrawtoclient");
1611         prog->fieldoffsets.pflags                         = PRVM_ED_FindFieldOffset("pflags");
1612         prog->fieldoffsets.ping                           = PRVM_ED_FindFieldOffset("ping");
1613         prog->fieldoffsets.packetloss                     = PRVM_ED_FindFieldOffset("ping_packetloss");
1614         prog->fieldoffsets.movementloss                   = PRVM_ED_FindFieldOffset("ping_movementloss");
1615         prog->fieldoffsets.pitch_speed                    = PRVM_ED_FindFieldOffset("pitch_speed");
1616         prog->fieldoffsets.playermodel                    = PRVM_ED_FindFieldOffset("playermodel");
1617         prog->fieldoffsets.playerskin                     = PRVM_ED_FindFieldOffset("playerskin");
1618         prog->fieldoffsets.pmodel                         = PRVM_ED_FindFieldOffset("pmodel");
1619         prog->fieldoffsets.punchvector                    = PRVM_ED_FindFieldOffset("punchvector");
1620         prog->fieldoffsets.renderamt                      = PRVM_ED_FindFieldOffset("renderamt"); // HalfLife support
1621         prog->fieldoffsets.renderflags                    = PRVM_ED_FindFieldOffset("renderflags");
1622         prog->fieldoffsets.rendermode                     = PRVM_ED_FindFieldOffset("rendermode"); // HalfLife support
1623         prog->fieldoffsets.scale                          = PRVM_ED_FindFieldOffset("scale");
1624         prog->fieldoffsets.shadertime                     = PRVM_ED_FindFieldOffset("shadertime");
1625         prog->fieldoffsets.skeletonindex                  = PRVM_ED_FindFieldOffset("skeletonindex");
1626         prog->fieldoffsets.style                          = PRVM_ED_FindFieldOffset("style");
1627         prog->fieldoffsets.tag_entity                     = PRVM_ED_FindFieldOffset("tag_entity");
1628         prog->fieldoffsets.tag_index                      = PRVM_ED_FindFieldOffset("tag_index");
1629         prog->fieldoffsets.think                          = PRVM_ED_FindFieldOffset("think");
1630         prog->fieldoffsets.viewmodelforclient             = PRVM_ED_FindFieldOffset("viewmodelforclient");
1631         prog->fieldoffsets.viewzoom                       = PRVM_ED_FindFieldOffset("viewzoom");
1632         prog->fieldoffsets.yaw_speed                      = PRVM_ED_FindFieldOffset("yaw_speed");
1633         prog->fieldoffsets.bouncefactor                   = PRVM_ED_FindFieldOffset("bouncefactor");
1634         prog->fieldoffsets.bouncestop                     = PRVM_ED_FindFieldOffset("bouncestop");
1635
1636         prog->fieldoffsets.solid                          = PRVM_ED_FindFieldOffset("solid");
1637         prog->fieldoffsets.movetype                       = PRVM_ED_FindFieldOffset("movetype");
1638         prog->fieldoffsets.modelindex                     = PRVM_ED_FindFieldOffset("modelindex");
1639         prog->fieldoffsets.mins                           = PRVM_ED_FindFieldOffset("mins");
1640         prog->fieldoffsets.maxs                           = PRVM_ED_FindFieldOffset("maxs");
1641         prog->fieldoffsets.mass                           = PRVM_ED_FindFieldOffset("mass");
1642         prog->fieldoffsets.origin                         = PRVM_ED_FindFieldOffset("origin");
1643         prog->fieldoffsets.velocity                       = PRVM_ED_FindFieldOffset("velocity");
1644         //prog->fieldoffsets.axis_forward                   = PRVM_ED_FindFieldOffset("axis_forward");
1645         //prog->fieldoffsets.axis_left                      = PRVM_ED_FindFieldOffset("axis_left");
1646         //prog->fieldoffsets.axis_up                        = PRVM_ED_FindFieldOffset("axis_up");
1647         //prog->fieldoffsets.spinvelocity                   = PRVM_ED_FindFieldOffset("spinvelocity");
1648         prog->fieldoffsets.angles                         = PRVM_ED_FindFieldOffset("angles");
1649         prog->fieldoffsets.avelocity                      = PRVM_ED_FindFieldOffset("avelocity");
1650         prog->fieldoffsets.aiment                         = PRVM_ED_FindFieldOffset("aiment");
1651         prog->fieldoffsets.enemy                          = PRVM_ED_FindFieldOffset("enemy");
1652         prog->fieldoffsets.jointtype                      = PRVM_ED_FindFieldOffset("jointtype");
1653         prog->fieldoffsets.movedir                        = PRVM_ED_FindFieldOffset("movedir");
1654
1655         prog->funcoffsets.CSQC_ConsoleCommand             = PRVM_ED_FindFunctionOffset("CSQC_ConsoleCommand");
1656         prog->funcoffsets.CSQC_Ent_Remove                 = PRVM_ED_FindFunctionOffset("CSQC_Ent_Remove");
1657         prog->funcoffsets.CSQC_Ent_Spawn                  = PRVM_ED_FindFunctionOffset("CSQC_Ent_Spawn");
1658         prog->funcoffsets.CSQC_Ent_Update                 = PRVM_ED_FindFunctionOffset("CSQC_Ent_Update");
1659         prog->funcoffsets.CSQC_Event                      = PRVM_ED_FindFunctionOffset("CSQC_Event");
1660         prog->funcoffsets.CSQC_Event_Sound                = PRVM_ED_FindFunctionOffset("CSQC_Event_Sound");
1661         prog->funcoffsets.CSQC_Init                       = PRVM_ED_FindFunctionOffset("CSQC_Init");
1662         prog->funcoffsets.CSQC_InputEvent                 = PRVM_ED_FindFunctionOffset("CSQC_InputEvent");
1663         prog->funcoffsets.CSQC_Parse_CenterPrint          = PRVM_ED_FindFunctionOffset("CSQC_Parse_CenterPrint");
1664         prog->funcoffsets.CSQC_Parse_Print                = PRVM_ED_FindFunctionOffset("CSQC_Parse_Print");
1665         prog->funcoffsets.CSQC_Parse_StuffCmd             = PRVM_ED_FindFunctionOffset("CSQC_Parse_StuffCmd");
1666         prog->funcoffsets.CSQC_Parse_TempEntity           = PRVM_ED_FindFunctionOffset("CSQC_Parse_TempEntity");
1667         prog->funcoffsets.CSQC_Shutdown                   = PRVM_ED_FindFunctionOffset("CSQC_Shutdown");
1668         prog->funcoffsets.CSQC_UpdateView                 = PRVM_ED_FindFunctionOffset("CSQC_UpdateView");
1669         prog->funcoffsets.EndFrame                        = PRVM_ED_FindFunctionOffset("EndFrame");
1670         prog->funcoffsets.GameCommand                     = PRVM_ED_FindFunctionOffset("GameCommand");
1671         prog->funcoffsets.Gecko_Query                     = PRVM_ED_FindFunctionOffset("Gecko_Query");
1672         prog->funcoffsets.RestoreGame                     = PRVM_ED_FindFunctionOffset("RestoreGame");
1673         prog->funcoffsets.SV_ChangeTeam                   = PRVM_ED_FindFunctionOffset("SV_ChangeTeam");
1674         prog->funcoffsets.SV_OnEntityNoSpawnFunction      = PRVM_ED_FindFunctionOffset("SV_OnEntityNoSpawnFunction");
1675         prog->funcoffsets.SV_OnEntityPostSpawnFunction    = PRVM_ED_FindFunctionOffset("SV_OnEntityPostSpawnFunction");
1676         prog->funcoffsets.SV_OnEntityPreSpawnFunction     = PRVM_ED_FindFunctionOffset("SV_OnEntityPreSpawnFunction");
1677         prog->funcoffsets.SV_ParseClientCommand           = PRVM_ED_FindFunctionOffset("SV_ParseClientCommand");
1678         prog->funcoffsets.SV_PausedTic                    = PRVM_ED_FindFunctionOffset("SV_PausedTic");
1679         prog->funcoffsets.SV_PlayerPhysics                = PRVM_ED_FindFunctionOffset("SV_PlayerPhysics");
1680         prog->funcoffsets.SV_Shutdown                     = PRVM_ED_FindFunctionOffset("SV_Shutdown");
1681         prog->funcoffsets.URI_Get_Callback                = PRVM_ED_FindFunctionOffset("URI_Get_Callback");
1682         prog->globaloffsets.SV_InitCmd                    = PRVM_ED_FindGlobalOffset("SV_InitCmd");
1683         prog->globaloffsets.coop                          = PRVM_ED_FindGlobalOffset("coop");
1684         prog->globaloffsets.deathmatch                    = PRVM_ED_FindGlobalOffset("deathmatch");
1685         prog->globaloffsets.dmg_origin                    = PRVM_ED_FindGlobalOffset("dmg_origin");
1686         prog->globaloffsets.dmg_save                      = PRVM_ED_FindGlobalOffset("dmg_save");
1687         prog->globaloffsets.dmg_take                      = PRVM_ED_FindGlobalOffset("dmg_take");
1688         prog->globaloffsets.drawfont                      = PRVM_ED_FindGlobalOffset("drawfont");
1689         prog->globaloffsets.gettaginfo_forward            = PRVM_ED_FindGlobalOffset("gettaginfo_forward");
1690         prog->globaloffsets.gettaginfo_name               = PRVM_ED_FindGlobalOffset("gettaginfo_name");
1691         prog->globaloffsets.gettaginfo_offset             = PRVM_ED_FindGlobalOffset("gettaginfo_offset");
1692         prog->globaloffsets.gettaginfo_parent             = PRVM_ED_FindGlobalOffset("gettaginfo_parent");
1693         prog->globaloffsets.gettaginfo_right              = PRVM_ED_FindGlobalOffset("gettaginfo_right");
1694         prog->globaloffsets.gettaginfo_up                 = PRVM_ED_FindGlobalOffset("gettaginfo_up");
1695         prog->globaloffsets.transparent_offset            = PRVM_ED_FindGlobalOffset("transparent_offset");
1696         prog->globaloffsets.intermission                  = PRVM_ED_FindGlobalOffset("intermission");
1697         prog->globaloffsets.require_spawnfunc_prefix      = PRVM_ED_FindGlobalOffset("require_spawnfunc_prefix");
1698         prog->globaloffsets.sb_showscores                 = PRVM_ED_FindGlobalOffset("sb_showscores");
1699         prog->globaloffsets.self                          = PRVM_ED_FindGlobalOffset("self");
1700         prog->globaloffsets.serverdeltatime               = PRVM_ED_FindGlobalOffset("serverdeltatime");
1701         prog->globaloffsets.serverprevtime                = PRVM_ED_FindGlobalOffset("serverprevtime");
1702         prog->globaloffsets.servertime                    = PRVM_ED_FindGlobalOffset("servertime");
1703         prog->globaloffsets.time                          = PRVM_ED_FindGlobalOffset("time");
1704         prog->globaloffsets.trace_allsolid                = PRVM_ED_FindGlobalOffset("trace_allsolid");
1705         prog->globaloffsets.trace_dphitcontents           = PRVM_ED_FindGlobalOffset("trace_dphitcontents");
1706         prog->globaloffsets.trace_dphitq3surfaceflags     = PRVM_ED_FindGlobalOffset("trace_dphitq3surfaceflags");
1707         prog->globaloffsets.trace_dphittexturename        = PRVM_ED_FindGlobalOffset("trace_dphittexturename");
1708         prog->globaloffsets.trace_dpstartcontents         = PRVM_ED_FindGlobalOffset("trace_dpstartcontents");
1709         prog->globaloffsets.trace_endpos                  = PRVM_ED_FindGlobalOffset("trace_endpos");
1710         prog->globaloffsets.trace_ent                     = PRVM_ED_FindGlobalOffset("trace_ent");
1711         prog->globaloffsets.trace_fraction                = PRVM_ED_FindGlobalOffset("trace_fraction");
1712         prog->globaloffsets.trace_inopen                  = PRVM_ED_FindGlobalOffset("trace_inopen");
1713         prog->globaloffsets.trace_inwater                 = PRVM_ED_FindGlobalOffset("trace_inwater");
1714         prog->globaloffsets.trace_networkentity           = PRVM_ED_FindGlobalOffset("trace_networkentity");
1715         prog->globaloffsets.trace_plane_dist              = PRVM_ED_FindGlobalOffset("trace_plane_dist");
1716         prog->globaloffsets.trace_plane_normal            = PRVM_ED_FindGlobalOffset("trace_plane_normal");
1717         prog->globaloffsets.trace_startsolid              = PRVM_ED_FindGlobalOffset("trace_startsolid");
1718         prog->globaloffsets.v_forward                     = PRVM_ED_FindGlobalOffset("v_forward");
1719         prog->globaloffsets.v_right                       = PRVM_ED_FindGlobalOffset("v_right");
1720         prog->globaloffsets.v_up                          = PRVM_ED_FindGlobalOffset("v_up");
1721         prog->globaloffsets.view_angles                   = PRVM_ED_FindGlobalOffset("view_angles");
1722         prog->globaloffsets.worldstatus                   = PRVM_ED_FindGlobalOffset("worldstatus");
1723
1724         // menu qc only uses some functions, nothing else
1725         prog->funcoffsets.m_draw                          = PRVM_ED_FindFunctionOffset("m_draw");
1726         prog->funcoffsets.m_init                          = PRVM_ED_FindFunctionOffset("m_init");
1727         prog->funcoffsets.m_keydown                       = PRVM_ED_FindFunctionOffset("m_keydown");
1728         prog->funcoffsets.m_keyup                         = PRVM_ED_FindFunctionOffset("m_keyup");
1729         prog->funcoffsets.m_shutdown                      = PRVM_ED_FindFunctionOffset("m_shutdown");
1730         prog->funcoffsets.m_toggle                        = PRVM_ED_FindFunctionOffset("m_toggle");
1731 }
1732
1733 // not used
1734 /*
1735 typedef struct dpfield_s
1736 {
1737         int type;
1738         char *string;
1739 }
1740 dpfield_t;
1741
1742 #define DPFIELDS (sizeof(dpfields) / sizeof(dpfield_t))
1743
1744 dpfield_t dpfields[] =
1745 {
1746 };
1747 */
1748
1749 /*
1750 ===============
1751 PRVM_ResetProg
1752 ===============
1753 */
1754
1755 #define PO_HASHSIZE 16384
1756 typedef struct po_string_s
1757 {
1758         char *key, *value;
1759         struct po_string_s *nextonhashchain;
1760 }
1761 po_string_t;
1762 typedef struct po_s
1763 {
1764         po_string_t *hashtable[PO_HASHSIZE];
1765 }
1766 po_t;
1767 void PRVM_PO_UnparseString(char *out, const char *in, size_t outsize)
1768 {
1769         for(;;)
1770         {
1771                 switch(*in)
1772                 {
1773                         case 0:
1774                                 *out++ = 0;
1775                                 return;
1776                         case '\a': if(outsize >= 2) { *out++ = '\\'; *out++ = 'a'; outsize -= 2; } break;
1777                         case '\b': if(outsize >= 2) { *out++ = '\\'; *out++ = 'b'; outsize -= 2; } break;
1778                         case '\t': if(outsize >= 2) { *out++ = '\\'; *out++ = 't'; outsize -= 2; } break;
1779                         case '\r': if(outsize >= 2) { *out++ = '\\'; *out++ = 'r'; outsize -= 2; } break;
1780                         case '\n': if(outsize >= 2) { *out++ = '\\'; *out++ = 'n'; outsize -= 2; } break;
1781                         case '\\': if(outsize >= 2) { *out++ = '\\'; *out++ = '\\'; outsize -= 2; } break;
1782                         case '"': if(outsize >= 2) { *out++ = '\\'; *out++ = '"'; outsize -= 2; } break;
1783                         default:
1784                                 if(*in >= 0 && *in <= 0x1F)
1785                                 {
1786                                         if(outsize >= 4)
1787                                         {
1788                                                 *out++ = '\\';
1789                                                 *out++ = '0' + ((*in & 0700) >> 6);
1790                                                 *out++ = '0' + ((*in & 0070) >> 3);
1791                                                 *out++ = '0' + ((*in & 0007));
1792                                                 outsize -= 4;
1793                                         }
1794                                 }
1795                                 else
1796                                 {
1797                                         if(outsize >= 1)
1798                                         {
1799                                                 *out++ = *in;
1800                                                 outsize -= 1;
1801                                         }
1802                                 }
1803                                 break;
1804                 }
1805                 ++in;
1806         }
1807 }
1808 void PRVM_PO_ParseString(char *out, const char *in, size_t outsize)
1809 {
1810         for(;;)
1811         {
1812                 switch(*in)
1813                 {
1814                         case 0:
1815                                 *out++ = 0;
1816                                 return;
1817                         case '\\':
1818                                 ++in;
1819                                 switch(*in)
1820                                 {
1821                                         case 'a': if(outsize > 0) { *out++ = '\a'; --outsize; } break;
1822                                         case 'b': if(outsize > 0) { *out++ = '\b'; --outsize; } break;
1823                                         case 't': if(outsize > 0) { *out++ = '\t'; --outsize; } break;
1824                                         case 'r': if(outsize > 0) { *out++ = '\r'; --outsize; } break;
1825                                         case 'n': if(outsize > 0) { *out++ = '\n'; --outsize; } break;
1826                                         case '\\': if(outsize > 0) { *out++ = '\\'; --outsize; } break;
1827                                         case '"': if(outsize > 0) { *out++ = '"'; --outsize; } break;
1828                                         case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7':
1829                                                 if(outsize > 0) 
1830                                                         *out = *in - '0';
1831                                                 ++in;
1832                                                 if(*in >= '0' && *in <= '7')
1833                                                 {
1834                                                         if(outsize > 0)
1835                                                                 *out = (*out << 3) | (*in - '0');
1836                                                         ++in;
1837                                                 }
1838                                                 if(*in >= '0' && *in <= '7')
1839                                                 {
1840                                                         if(outsize > 0)
1841                                                                 *out = (*out << 3) | (*in - '0');
1842                                                         ++in;
1843                                                 }
1844                                                 --in;
1845                                                 if(outsize > 0)
1846                                                 {
1847                                                         ++out;
1848                                                         --outsize;
1849                                                 }
1850                                                 break;
1851                                         default:
1852                                                 if(outsize > 0) { *out++ = *in; --outsize; }
1853                                                 break;
1854                                 }
1855                                 break;
1856                         default:
1857                                 if(outsize > 0)
1858                                 {
1859                                         *out++ = *in;
1860                                         --outsize;
1861                                 }
1862                                 break;
1863                 }
1864                 ++in;
1865         }
1866 }
1867 po_t *PRVM_PO_Load(const char *filename, mempool_t *pool)
1868 {
1869         po_t *po;
1870         const char *p, *q;
1871         int mode;
1872         char inbuf[MAX_INPUTLINE];
1873         char decodedbuf[MAX_INPUTLINE];
1874         size_t decodedpos;
1875         int hashindex;
1876         po_string_t thisstr;
1877         const char *buf = (const char *) FS_LoadFile(filename, pool, true, NULL);
1878
1879         if(!buf)
1880                 return NULL;
1881
1882         po = Mem_Alloc(pool, sizeof(*po));
1883         memset(po, 0, sizeof(*po));
1884
1885         p = buf;
1886         while(*p)
1887         {
1888                 if(*p == '#')
1889                 {
1890                         // skip to newline
1891                         p = strchr(p, '\n');
1892                         if(!p)
1893                                 break;
1894                         ++p;
1895                         continue;
1896                 }
1897                 if(*p == '\r' || *p == '\n')
1898                 {
1899                         ++p;
1900                         continue;
1901                 }
1902                 if(!strncmp(p, "msgid \"", 7))
1903                 {
1904                         mode = 0;
1905                         p += 6;
1906                 }
1907                 else if(!strncmp(p, "msgstr \"", 8))
1908                 {
1909                         mode = 1;
1910                         p += 7;
1911                 }
1912                 else
1913                 {
1914                         p = strchr(p, '\n');
1915                         if(!p)
1916                                 break;
1917                         ++p;
1918                         continue;
1919                 }
1920                 decodedpos = 0;
1921                 while(*p == '"')
1922                 {
1923                         ++p;
1924                         q = strchr(p, '\n');
1925                         if(!q)
1926                                 break;
1927                         if(*(q-1) == '\r')
1928                                 --q;
1929                         if(*(q-1) != '"')
1930                                 break;
1931                         if((size_t)(q - p) >= (size_t) sizeof(inbuf))
1932                                 break;
1933                         strlcpy(inbuf, p, q - p); // not - 1, because this adds a NUL
1934                         PRVM_PO_ParseString(decodedbuf + decodedpos, inbuf, sizeof(decodedbuf) - decodedpos);
1935                         decodedpos += strlen(decodedbuf + decodedpos);
1936                         if(*q == '\r')
1937                                 ++q;
1938                         if(*q == '\n')
1939                                 ++q;
1940                         p = q;
1941                 }
1942                 if(mode == 0)
1943                 {
1944                         if(thisstr.key)
1945                                 Mem_Free(thisstr.key);
1946                         thisstr.key = Mem_Alloc(pool, decodedpos + 1);
1947                         memcpy(thisstr.key, decodedbuf, decodedpos + 1);
1948                 }
1949                 else if(decodedpos > 0 && thisstr.key) // skip empty translation results
1950                 {
1951                         thisstr.value = Mem_Alloc(pool, decodedpos + 1);
1952                         memcpy(thisstr.value, decodedbuf, decodedpos + 1);
1953                         hashindex = CRC_Block((const unsigned char *) thisstr.key, strlen(thisstr.key)) % PO_HASHSIZE;
1954                         thisstr.nextonhashchain = po->hashtable[hashindex];
1955                         po->hashtable[hashindex] = Mem_Alloc(pool, sizeof(thisstr));
1956                         memcpy(po->hashtable[hashindex], &thisstr, sizeof(thisstr));
1957                         memset(&thisstr, 0, sizeof(thisstr));
1958                 }
1959         }
1960         
1961         Mem_Free((char *) buf);
1962         return po;
1963 }
1964 const char *PRVM_PO_Lookup(po_t *po, const char *str)
1965 {
1966         int hashindex = CRC_Block((const unsigned char *) str, strlen(str)) % PO_HASHSIZE;
1967         po_string_t *p = po->hashtable[hashindex];
1968         while(p)
1969         {
1970                 if(!strcmp(str, p->key))
1971                         return p->value;
1972                 p = p->nextonhashchain;
1973         }
1974         return NULL;
1975 }
1976 void PRVM_PO_Destroy(po_t *po)
1977 {
1978         int i;
1979         for(i = 0; i < PO_HASHSIZE; ++i)
1980         {
1981                 po_string_t *p = po->hashtable[i];
1982                 while(p)
1983                 {
1984                         po_string_t *q = p;
1985                         p = p->nextonhashchain;
1986                         Mem_Free(q->key);
1987                         Mem_Free(q->value);
1988                         Mem_Free(q);
1989                 }
1990         }
1991         Mem_Free(po);
1992 }
1993
1994 void PRVM_LeakTest(void);
1995 void PRVM_ResetProg(void)
1996 {
1997         PRVM_LeakTest();
1998         PRVM_GCALL(reset_cmd)();
1999         Mem_FreePool(&prog->progs_mempool);
2000         if(prog->po)
2001                 PRVM_PO_Destroy((po_t *) prog->po);
2002         memset(prog,0,sizeof(prvm_prog_t));
2003         prog->starttime = Sys_DoubleTime();
2004 }
2005
2006 /*
2007 ===============
2008 PRVM_LoadLNO
2009 ===============
2010 */
2011 void PRVM_LoadLNO( const char *progname ) {
2012         fs_offset_t filesize;
2013         unsigned char *lno;
2014         unsigned int *header;
2015         char filename[512];
2016
2017         FS_StripExtension( progname, filename, sizeof( filename ) );
2018         strlcat( filename, ".lno", sizeof( filename ) );
2019
2020         lno = FS_LoadFile( filename, tempmempool, false, &filesize );
2021         if( !lno ) {
2022                 return;
2023         }
2024
2025 /*
2026 <Spike>    SafeWrite (h, &lnotype, sizeof(int));
2027 <Spike>    SafeWrite (h, &version, sizeof(int));
2028 <Spike>    SafeWrite (h, &numglobaldefs, sizeof(int));
2029 <Spike>    SafeWrite (h, &numpr_globals, sizeof(int));
2030 <Spike>    SafeWrite (h, &numfielddefs, sizeof(int));
2031 <Spike>    SafeWrite (h, &numstatements, sizeof(int));
2032 <Spike>    SafeWrite (h, statement_linenums, numstatements*sizeof(int));
2033 */
2034         if( (unsigned) filesize < (6 + prog->progs->numstatements) * sizeof( int ) ) {
2035                 Mem_Free(lno);
2036                 return;
2037         }
2038
2039         header = (unsigned int *) lno;
2040         if( header[ 0 ] == *(unsigned int *) "LNOF" &&
2041                 LittleLong( header[ 1 ] ) == 1 &&
2042                 (unsigned int)LittleLong( header[ 2 ] ) == (unsigned int)prog->progs->numglobaldefs &&
2043                 (unsigned int)LittleLong( header[ 3 ] ) == (unsigned int)prog->progs->numglobals &&
2044                 (unsigned int)LittleLong( header[ 4 ] ) == (unsigned int)prog->progs->numfielddefs &&
2045                 (unsigned int)LittleLong( header[ 5 ] ) == (unsigned int)prog->progs->numstatements )
2046         {
2047                 prog->statement_linenums = (int *)Mem_Alloc(prog->progs_mempool, prog->progs->numstatements * sizeof( int ) );
2048                 memcpy( prog->statement_linenums, (int *) lno + 6, prog->progs->numstatements * sizeof( int ) );
2049         }
2050         Mem_Free( lno );
2051 }
2052
2053 /*
2054 ===============
2055 PRVM_LoadProgs
2056 ===============
2057 */
2058 void PRVM_LoadProgs (const char * filename, int numrequiredfunc, char **required_func, int numrequiredfields, prvm_required_field_t *required_field, int numrequiredglobals, char **required_global)
2059 {
2060         int i;
2061         dstatement_t *st;
2062         ddef_t *infielddefs;
2063         dfunction_t *dfunctions;
2064         fs_offset_t filesize;
2065
2066         if( prog->loaded ) {
2067                 PRVM_ERROR ("PRVM_LoadProgs: there is already a %s program loaded!", PRVM_NAME );
2068         }
2069
2070         prog->progs = (dprograms_t *)FS_LoadFile (filename, prog->progs_mempool, false, &filesize);
2071         if (prog->progs == NULL || filesize < (fs_offset_t)sizeof(dprograms_t))
2072                 PRVM_ERROR ("PRVM_LoadProgs: couldn't load %s for %s", filename, PRVM_NAME);
2073         // TODO bounds check header fields (e.g. numstatements), they must never go behind end of file
2074
2075         Con_DPrintf("%s programs occupy %iK.\n", PRVM_NAME, (int)(filesize/1024));
2076
2077         prog->filecrc = CRC_Block((unsigned char *)prog->progs, filesize);
2078
2079 // byte swap the header
2080         for (i = 0;i < (int) sizeof(*prog->progs) / 4;i++)
2081                 ((int *)prog->progs)[i] = LittleLong ( ((int *)prog->progs)[i] );
2082
2083         if (prog->progs->version != PROG_VERSION)
2084                 PRVM_ERROR ("%s: %s has wrong version number (%i should be %i)", PRVM_NAME, filename, prog->progs->version, PROG_VERSION);
2085         if (prog->progs->crc != prog->headercrc && prog->progs->crc != prog->headercrc2)
2086                 PRVM_ERROR ("%s: %s system vars have been modified (CRC of progs.dat systemvars %i != engine %i), progdefs.h is out of date", PRVM_NAME, filename, prog->progs->crc, prog->headercrc);
2087
2088         //prog->functions = (dfunction_t *)((unsigned char *)progs + progs->ofs_functions);
2089         dfunctions = (dfunction_t *)((unsigned char *)prog->progs + prog->progs->ofs_functions);
2090
2091         if (prog->progs->ofs_strings + prog->progs->numstrings >= (int)filesize)
2092                 PRVM_ERROR ("%s: %s strings go past end of file", PRVM_NAME, filename);
2093         prog->strings = (char *)prog->progs + prog->progs->ofs_strings;
2094         prog->stringssize = prog->progs->numstrings;
2095
2096         prog->numknownstrings = 0;
2097         prog->maxknownstrings = 0;
2098         prog->knownstrings = NULL;
2099         prog->knownstrings_freeable = NULL;
2100
2101         Mem_ExpandableArray_NewArray(&prog->stringbuffersarray, prog->progs_mempool, sizeof(prvm_stringbuffer_t), 64);
2102
2103         prog->globaldefs = (ddef_t *)((unsigned char *)prog->progs + prog->progs->ofs_globaldefs);
2104
2105         // we need to expand the fielddefs list to include all the engine fields,
2106         // so allocate a new place for it
2107         infielddefs = (ddef_t *)((unsigned char *)prog->progs + prog->progs->ofs_fielddefs);
2108         //                                                                                              ( + DPFIELDS                       )
2109         prog->fielddefs = (ddef_t *)Mem_Alloc(prog->progs_mempool, (prog->progs->numfielddefs + numrequiredfields) * sizeof(ddef_t));
2110
2111         prog->statements = (dstatement_t *)((unsigned char *)prog->progs + prog->progs->ofs_statements);
2112
2113         prog->statement_profile = (double *)Mem_Alloc(prog->progs_mempool, prog->progs->numstatements * sizeof(*prog->statement_profile));
2114
2115         //pr_global_struct = (globalvars_t *)((unsigned char *)progs + progs->ofs_globals);
2116         prog->globals.generic = (float *)((unsigned char *)prog->progs + prog->progs->ofs_globals);
2117
2118 // byte swap the lumps
2119         for (i=0 ; i<prog->progs->numstatements ; i++)
2120         {
2121                 prog->statements[i].op = LittleShort(prog->statements[i].op);
2122                 prog->statements[i].a = LittleShort(prog->statements[i].a);
2123                 prog->statements[i].b = LittleShort(prog->statements[i].b);
2124                 prog->statements[i].c = LittleShort(prog->statements[i].c);
2125         }
2126
2127         prog->functions = (mfunction_t *)Mem_Alloc(prog->progs_mempool, sizeof(mfunction_t) * prog->progs->numfunctions);
2128         for (i = 0;i < prog->progs->numfunctions;i++)
2129         {
2130                 prog->functions[i].first_statement = LittleLong (dfunctions[i].first_statement);
2131                 prog->functions[i].parm_start = LittleLong (dfunctions[i].parm_start);
2132                 prog->functions[i].s_name = LittleLong (dfunctions[i].s_name);
2133                 prog->functions[i].s_file = LittleLong (dfunctions[i].s_file);
2134                 prog->functions[i].numparms = LittleLong (dfunctions[i].numparms);
2135                 prog->functions[i].locals = LittleLong (dfunctions[i].locals);
2136                 memcpy(prog->functions[i].parm_size, dfunctions[i].parm_size, sizeof(dfunctions[i].parm_size));
2137                 if(prog->functions[i].first_statement >= prog->progs->numstatements)
2138                         PRVM_ERROR("PRVM_LoadProgs: out of bounds function statement (function %d) in %s", i, PRVM_NAME);
2139                 // TODO bounds check parm_start, s_name, s_file, numparms, locals, parm_size
2140         }
2141
2142         for (i=0 ; i<prog->progs->numglobaldefs ; i++)
2143         {
2144                 prog->globaldefs[i].type = LittleShort (prog->globaldefs[i].type);
2145                 prog->globaldefs[i].ofs = LittleShort (prog->globaldefs[i].ofs);
2146                 prog->globaldefs[i].s_name = LittleLong (prog->globaldefs[i].s_name);
2147                 // TODO bounds check ofs, s_name
2148         }
2149
2150         // copy the progs fields to the new fields list
2151         for (i = 0;i < prog->progs->numfielddefs;i++)
2152         {
2153                 prog->fielddefs[i].type = LittleShort (infielddefs[i].type);
2154                 if (prog->fielddefs[i].type & DEF_SAVEGLOBAL)
2155                         PRVM_ERROR ("PRVM_LoadProgs: prog->fielddefs[i].type & DEF_SAVEGLOBAL in %s", PRVM_NAME);
2156                 prog->fielddefs[i].ofs = LittleShort (infielddefs[i].ofs);
2157                 prog->fielddefs[i].s_name = LittleLong (infielddefs[i].s_name);
2158                 // TODO bounds check ofs, s_name
2159         }
2160
2161         // append the required fields
2162         for (i = 0;i < (int) numrequiredfields;i++)
2163         {
2164                 prog->fielddefs[prog->progs->numfielddefs].type = required_field[i].type;
2165                 prog->fielddefs[prog->progs->numfielddefs].ofs = prog->progs->entityfields;
2166                 prog->fielddefs[prog->progs->numfielddefs].s_name = PRVM_SetEngineString(required_field[i].name);
2167                 // TODO bounds check ofs, s_name
2168                 if (prog->fielddefs[prog->progs->numfielddefs].type == ev_vector)
2169                         prog->progs->entityfields += 3;
2170                 else
2171                         prog->progs->entityfields++;
2172                 prog->progs->numfielddefs++;
2173         }
2174         prog->entityfields = prog->progs->entityfields;
2175
2176         // check required functions
2177         for(i=0 ; i < numrequiredfunc ; i++)
2178                 if(PRVM_ED_FindFunction(required_func[i]) == 0)
2179                         PRVM_ERROR("%s: %s not found in %s",PRVM_NAME, required_func[i], filename);
2180
2181         // check required globals
2182         for(i=0 ; i < numrequiredglobals ; i++)
2183                 if(PRVM_ED_FindGlobal(required_global[i]) == 0)
2184                         PRVM_ERROR("%s: %s not found in %s",PRVM_NAME, required_global[i], filename);
2185
2186         for (i=0 ; i<prog->progs->numglobals ; i++)
2187                 ((int *)prog->globals.generic)[i] = LittleLong (((int *)prog->globals.generic)[i]);
2188
2189         // LordHavoc: bounds check anything static
2190         for (i = 0,st = prog->statements;i < prog->progs->numstatements;i++,st++)
2191         {
2192                 switch (st->op)
2193                 {
2194                 case OP_IF:
2195                 case OP_IFNOT:
2196                         if ((unsigned short) st->a >= prog->progs->numglobals || st->b + i < 0 || st->b + i >= prog->progs->numstatements)
2197                                 PRVM_ERROR("PRVM_LoadProgs: out of bounds IF/IFNOT (statement %d) in %s", i, PRVM_NAME);
2198                         break;
2199                 case OP_GOTO:
2200                         if (st->a + i < 0 || st->a + i >= prog->progs->numstatements)
2201                                 PRVM_ERROR("PRVM_LoadProgs: out of bounds GOTO (statement %d) in %s", i, PRVM_NAME);
2202                         break;
2203                 // global global global
2204                 case OP_ADD_F:
2205                 case OP_ADD_V:
2206                 case OP_SUB_F:
2207                 case OP_SUB_V:
2208                 case OP_MUL_F:
2209                 case OP_MUL_V:
2210                 case OP_MUL_FV:
2211                 case OP_MUL_VF:
2212                 case OP_DIV_F:
2213                 case OP_BITAND:
2214                 case OP_BITOR:
2215                 case OP_GE:
2216                 case OP_LE:
2217                 case OP_GT:
2218                 case OP_LT:
2219                 case OP_AND:
2220                 case OP_OR:
2221                 case OP_EQ_F:
2222                 case OP_EQ_V:
2223                 case OP_EQ_S:
2224                 case OP_EQ_E:
2225                 case OP_EQ_FNC:
2226                 case OP_NE_F:
2227                 case OP_NE_V:
2228                 case OP_NE_S:
2229                 case OP_NE_E:
2230                 case OP_NE_FNC:
2231                 case OP_ADDRESS:
2232                 case OP_LOAD_F:
2233                 case OP_LOAD_FLD:
2234                 case OP_LOAD_ENT:
2235                 case OP_LOAD_S:
2236                 case OP_LOAD_FNC:
2237                 case OP_LOAD_V:
2238                         if ((unsigned short) st->a >= prog->progs->numglobals || (unsigned short) st->b >= prog->progs->numglobals || (unsigned short) st->c >= prog->progs->numglobals)
2239                                 PRVM_ERROR("PRVM_LoadProgs: out of bounds global index (statement %d)", i);
2240                         break;
2241                 // global none global
2242                 case OP_NOT_F:
2243                 case OP_NOT_V:
2244                 case OP_NOT_S:
2245                 case OP_NOT_FNC:
2246                 case OP_NOT_ENT:
2247                         if ((unsigned short) st->a >= prog->progs->numglobals || (unsigned short) st->c >= prog->progs->numglobals)
2248                                 PRVM_ERROR("PRVM_LoadProgs: out of bounds global index (statement %d) in %s", i, PRVM_NAME);
2249                         break;
2250                 // 2 globals
2251                 case OP_STOREP_F:
2252                 case OP_STOREP_ENT:
2253                 case OP_STOREP_FLD:
2254                 case OP_STOREP_S:
2255                 case OP_STOREP_FNC:
2256                 case OP_STORE_F:
2257                 case OP_STORE_ENT:
2258                 case OP_STORE_FLD:
2259                 case OP_STORE_S:
2260                 case OP_STORE_FNC:
2261                 case OP_STATE:
2262                 case OP_STOREP_V:
2263                 case OP_STORE_V:
2264                         if ((unsigned short) st->a >= prog->progs->numglobals || (unsigned short) st->b >= prog->progs->numglobals)
2265                                 PRVM_ERROR("PRVM_LoadProgs: out of bounds global index (statement %d) in %s", i, PRVM_NAME);
2266                         break;
2267                 // 1 global
2268                 case OP_CALL0:
2269                 case OP_CALL1:
2270                 case OP_CALL2:
2271                 case OP_CALL3:
2272                 case OP_CALL4:
2273                 case OP_CALL5:
2274                 case OP_CALL6:
2275                 case OP_CALL7:
2276                 case OP_CALL8:
2277                 case OP_DONE:
2278                 case OP_RETURN:
2279                         if ((unsigned short) st->a >= prog->progs->numglobals)
2280                                 PRVM_ERROR("PRVM_LoadProgs: out of bounds global index (statement %d) in %s", i, PRVM_NAME);
2281                         break;
2282                 default:
2283                         Con_DPrintf("PRVM_LoadProgs: unknown opcode %d at statement %d in %s\n", st->op, i, PRVM_NAME);
2284                         break;
2285                 }
2286         }
2287         if(prog->progs->numstatements < 1)
2288         {
2289                 PRVM_ERROR("PRVM_LoadProgs: empty program in %s", PRVM_NAME);
2290         }
2291         else switch(prog->statements[prog->progs->numstatements - 1].op)
2292         {
2293                 case OP_RETURN:
2294                 case OP_GOTO:
2295                 case OP_DONE:
2296                         break;
2297                 default:
2298                         PRVM_ERROR("PRVM_LoadProgs: program may fall off the edge (does not end with RETURN, GOTO or DONE) in %s", PRVM_NAME);
2299                         break;
2300         }
2301
2302         PRVM_LoadLNO(filename);
2303
2304         PRVM_Init_Exec();
2305
2306         if(*prvm_language.string)
2307         // in CSQC we really shouldn't be able to change how stuff works... sorry for now
2308         // later idea: include a list of authorized .po file checksums with the csprogs
2309         {
2310                 qboolean deftrans = !!strcmp(PRVM_NAME, "client");
2311                 if(!strcmp(prvm_language.string, "dump"))
2312                 {
2313                         qfile_t *f = FS_OpenRealFile(va("%s.%s.po", filename, prvm_language.string), "w", false);
2314                         Con_Printf("Dumping to %s.%s.po\n", filename, prvm_language.string);
2315                         if(f)
2316                         {
2317                                 for (i=0 ; i<prog->progs->numglobaldefs ; i++)
2318                                 {
2319                                         const char *name;
2320                                         name = PRVM_GetString(prog->globaldefs[i].s_name);
2321                                         if(deftrans ? (!name || strncmp(name, "notranslate_", 12)) : (name && !strncmp(name, "dotranslate_", 12)))
2322                                         if((prog->globaldefs[i].type & ~DEF_SAVEGLOBAL) == ev_string)
2323                                         {
2324                                                 prvm_eval_t *val = (prvm_eval_t *)(prog->globals.generic + prog->globaldefs[i].ofs);
2325                                                 const char *value = PRVM_GetString(val->string);
2326                                                 if(*value)
2327                                                 {
2328                                                         char buf[MAX_INPUTLINE];
2329                                                         PRVM_PO_UnparseString(buf, value, sizeof(buf));
2330                                                         FS_Printf(f, "msgid \"%s\"\nmsgstr \"\"\n\n", buf);
2331                                                 }
2332                                         }
2333                                 }
2334                                 FS_Close(f);
2335                         }
2336                 }
2337                 else
2338                 {
2339                         po_t *po = PRVM_PO_Load(va("%s.%s.po", filename, prvm_language.string), prog->progs_mempool);
2340                         if(po)
2341                         {
2342                                 for (i=0 ; i<prog->progs->numglobaldefs ; i++)
2343                                 {
2344                                         const char *name;
2345                                         name = PRVM_GetString(prog->globaldefs[i].s_name);
2346                                         if(deftrans ? (!name || strncmp(name, "notranslate_", 12)) : (name && !strncmp(name, "dotranslate_", 12)))
2347                                         if((prog->globaldefs[i].type & ~DEF_SAVEGLOBAL) == ev_string)
2348                                         {
2349                                                 prvm_eval_t *val = (prvm_eval_t *)(prog->globals.generic + prog->globaldefs[i].ofs);
2350                                                 const char *value = PRVM_GetString(val->string);
2351                                                 if(*value)
2352                                                 {
2353                                                         value = PRVM_PO_Lookup(po, value);
2354                                                         if(value)
2355                                                                 val->string = PRVM_SetEngineString(value);
2356                                                 }
2357                                         }
2358                                 }
2359                         }
2360                 }
2361         }
2362
2363         for (i=0 ; i<prog->progs->numglobaldefs ; i++)
2364         {
2365                 const char *name;
2366                 name = PRVM_GetString(prog->globaldefs[i].s_name);
2367                 //Con_Printf("found var %s\n", name);
2368                 if(name
2369                         && !strncmp(name, "autocvar_", 9)
2370                         && !(strlen(name) > 1 && name[strlen(name)-2] == '_' && (name[strlen(name)-1] == 'x' || name[strlen(name)-1] == 'y' || name[strlen(name)-1] == 'z'))
2371                 )
2372                 {
2373                         prvm_eval_t *val = (prvm_eval_t *)(prog->globals.generic + prog->globaldefs[i].ofs);
2374                         cvar_t *cvar = Cvar_FindVar(name + 9);
2375                         //Con_Printf("PRVM_LoadProgs: autocvar global %s in %s, processing...\n", name, PRVM_NAME);
2376                         if(!cvar)
2377                         {
2378                                 const char *value;
2379                                 char buf[64];
2380                                 Con_Printf("PRVM_LoadProgs: no cvar for autocvar global %s in %s, creating...\n", name, PRVM_NAME);
2381                                 switch(prog->globaldefs[i].type & ~DEF_SAVEGLOBAL)
2382                                 {
2383                                         case ev_float:
2384                                                 if((float)((int)(val->_float)) == val->_float)
2385                                                         dpsnprintf(buf, sizeof(buf), "%i", (int)(val->_float));
2386                                                 else
2387                                                         dpsnprintf(buf, sizeof(buf), "%f", val->_float);
2388                                                 value = buf;
2389                                                 break;
2390                                         case ev_vector:
2391                                                 dpsnprintf(buf, sizeof(buf), "%f %f %f", val->vector[0], val->vector[1], val->vector[2]); value = buf;
2392                                                 break;
2393                                         case ev_string:
2394                                                 value = PRVM_GetString(val->string);
2395                                                 break;
2396                                         default:
2397                                                 Con_Printf("PRVM_LoadProgs: invalid type of autocvar global %s in %s\n", name, PRVM_NAME);
2398                                                 goto fail;
2399                                 }
2400                                 cvar = Cvar_Get(name + 9, value, 0, NULL);
2401                                 if((prog->globaldefs[i].type & ~DEF_SAVEGLOBAL) == ev_string)
2402                                 {
2403                                         val->string = PRVM_SetEngineString(cvar->string);
2404                                         cvar->globaldefindex_stringno[prog - prog_list] = val->string;
2405                                 }
2406                                 if(!cvar)
2407                                         PRVM_ERROR("PRVM_LoadProgs: could not create cvar for autocvar global %s in %s", name, PRVM_NAME);
2408                                 cvar->globaldefindex_progid[prog - prog_list] = prog->id;
2409                                 cvar->globaldefindex[prog - prog_list] = i;
2410                         }
2411                         else if((cvar->flags & CVAR_PRIVATE) == 0)
2412                         {
2413                                 // MUST BE SYNCED WITH cvar.c Cvar_Set
2414                                 int j;
2415                                 const char *s;
2416                                 switch(prog->globaldefs[i].type & ~DEF_SAVEGLOBAL)
2417                                 {
2418                                         case ev_float:
2419                                                 val->_float = cvar->value;
2420                                                 break;
2421                                         case ev_vector:
2422                                                 s = cvar->string;
2423                                                 VectorClear(val->vector);
2424                                                 for (j = 0;j < 3;j++)
2425                                                 {
2426                                                         while (*s && ISWHITESPACE(*s))
2427                                                                 s++;
2428                                                         if (!*s)
2429                                                                 break;
2430                                                         val->vector[j] = atof(s);
2431                                                         while (!ISWHITESPACE(*s))
2432                                                                 s++;
2433                                                         if (!*s)
2434                                                                 break;
2435                                                 }
2436                                                 break;
2437                                         case ev_string:
2438                                                 val->string = PRVM_SetEngineString(cvar->string);
2439                                                 cvar->globaldefindex_stringno[prog - prog_list] = val->string;
2440                                                 break;
2441                                         default:
2442                                                 Con_Printf("PRVM_LoadProgs: invalid type of autocvar global %s in %s\n", name, PRVM_NAME);
2443                                                 goto fail;
2444                                 }
2445                                 cvar->globaldefindex_progid[prog - prog_list] = prog->id;
2446                                 cvar->globaldefindex[prog - prog_list] = i;
2447                         }
2448                         else
2449                                 Con_Printf("PRVM_LoadProgs: private cvar for autocvar global %s in %s\n", name, PRVM_NAME);
2450                 }
2451 fail:
2452                 ;
2453         }
2454
2455         prog->loaded = TRUE;
2456
2457         // set flags & ddef_ts in prog
2458
2459         prog->flag = 0;
2460
2461         PRVM_FindOffsets();
2462
2463         PRVM_GCALL(init_cmd)();
2464
2465         // init mempools
2466         PRVM_MEM_Alloc();
2467 }
2468
2469
2470 void PRVM_Fields_f (void)
2471 {
2472         int i, j, ednum, used, usedamount;
2473         int *counts;
2474         char tempstring[MAX_INPUTLINE], tempstring2[260];
2475         const char *name;
2476         prvm_edict_t *ed;
2477         ddef_t *d;
2478         int *v;
2479
2480         // TODO
2481         /*
2482         if (!sv.active)
2483         {
2484                 Con_Print("no progs loaded\n");
2485                 return;
2486         }
2487         */
2488
2489         if(Cmd_Argc() != 2)
2490         {
2491                 Con_Print("prvm_fields <program name>\n");
2492                 return;
2493         }
2494
2495         PRVM_Begin;
2496         if(!PRVM_SetProgFromString(Cmd_Argv(1)))
2497                 return;
2498
2499         counts = (int *)Mem_Alloc(tempmempool, prog->progs->numfielddefs * sizeof(int));
2500         for (ednum = 0;ednum < prog->max_edicts;ednum++)
2501         {
2502                 ed = PRVM_EDICT_NUM(ednum);
2503                 if (ed->priv.required->free)
2504                         continue;
2505                 for (i = 1;i < prog->progs->numfielddefs;i++)
2506                 {
2507                         d = &prog->fielddefs[i];
2508                         name = PRVM_GetString(d->s_name);
2509                         if (name[strlen(name)-2] == '_')
2510                                 continue;       // skip _x, _y, _z vars
2511                         v = (int *)(ed->fields.vp + d->ofs);
2512                         // if the value is still all 0, skip the field
2513                         for (j = 0;j < prvm_type_size[d->type & ~DEF_SAVEGLOBAL];j++)
2514                         {
2515                                 if (v[j])
2516                                 {
2517                                         counts[i]++;
2518                                         break;
2519                                 }
2520                         }
2521                 }
2522         }
2523         used = 0;
2524         usedamount = 0;
2525         tempstring[0] = 0;
2526         for (i = 0;i < prog->progs->numfielddefs;i++)
2527         {
2528                 d = &prog->fielddefs[i];
2529                 name = PRVM_GetString(d->s_name);
2530                 if (name[strlen(name)-2] == '_')
2531                         continue;       // skip _x, _y, _z vars
2532                 switch(d->type & ~DEF_SAVEGLOBAL)
2533                 {
2534                 case ev_string:
2535                         strlcat(tempstring, "string   ", sizeof(tempstring));
2536                         break;
2537                 case ev_entity:
2538                         strlcat(tempstring, "entity   ", sizeof(tempstring));
2539                         break;
2540                 case ev_function:
2541                         strlcat(tempstring, "function ", sizeof(tempstring));
2542                         break;
2543                 case ev_field:
2544                         strlcat(tempstring, "field    ", sizeof(tempstring));
2545                         break;
2546                 case ev_void:
2547                         strlcat(tempstring, "void     ", sizeof(tempstring));
2548                         break;
2549                 case ev_float:
2550                         strlcat(tempstring, "float    ", sizeof(tempstring));
2551                         break;
2552                 case ev_vector:
2553                         strlcat(tempstring, "vector   ", sizeof(tempstring));
2554                         break;
2555                 case ev_pointer:
2556                         strlcat(tempstring, "pointer  ", sizeof(tempstring));
2557                         break;
2558                 default:
2559                         dpsnprintf (tempstring2, sizeof(tempstring2), "bad type %i ", d->type & ~DEF_SAVEGLOBAL);
2560                         strlcat(tempstring, tempstring2, sizeof(tempstring));
2561                         break;
2562                 }
2563                 if (strlen(name) > sizeof(tempstring2)-4)
2564                 {
2565                         memcpy (tempstring2, name, sizeof(tempstring2)-4);
2566                         tempstring2[sizeof(tempstring2)-4] = tempstring2[sizeof(tempstring2)-3] = tempstring2[sizeof(tempstring2)-2] = '.';
2567                         tempstring2[sizeof(tempstring2)-1] = 0;
2568                         name = tempstring2;
2569                 }
2570                 strlcat(tempstring, name, sizeof(tempstring));
2571                 for (j = (int)strlen(name);j < 25;j++)
2572                         strlcat(tempstring, " ", sizeof(tempstring));
2573                 dpsnprintf(tempstring2, sizeof(tempstring2), "%5d", counts[i]);
2574                 strlcat(tempstring, tempstring2, sizeof(tempstring));
2575                 strlcat(tempstring, "\n", sizeof(tempstring));
2576                 if (strlen(tempstring) >= sizeof(tempstring)/2)
2577                 {
2578                         Con_Print(tempstring);
2579                         tempstring[0] = 0;
2580                 }
2581                 if (counts[i])
2582                 {
2583                         used++;
2584                         usedamount += prvm_type_size[d->type & ~DEF_SAVEGLOBAL];
2585                 }
2586         }
2587         Mem_Free(counts);
2588         Con_Printf("%s: %i entity fields (%i in use), totalling %i bytes per edict (%i in use), %i edicts allocated, %i bytes total spent on edict fields (%i needed)\n", PRVM_NAME, prog->progs->entityfields, used, prog->progs->entityfields * 4, usedamount * 4, prog->max_edicts, prog->progs->entityfields * 4 * prog->max_edicts, usedamount * 4 * prog->max_edicts);
2589
2590         PRVM_End;
2591 }
2592
2593 void PRVM_Globals_f (void)
2594 {
2595         int i;
2596         const char *wildcard;
2597         int numculled;
2598                 numculled = 0;
2599         // TODO
2600         /*if (!sv.active)
2601         {
2602                 Con_Print("no progs loaded\n");
2603                 return;
2604         }*/
2605         if(Cmd_Argc () < 2 || Cmd_Argc() > 3)
2606         {
2607                 Con_Print("prvm_globals <program name> <optional name wildcard>\n");
2608                 return;
2609         }
2610
2611         PRVM_Begin;
2612         if(!PRVM_SetProgFromString (Cmd_Argv (1)))
2613                 return;
2614
2615         if( Cmd_Argc() == 3)
2616                 wildcard = Cmd_Argv(2);
2617         else
2618                 wildcard = NULL;
2619
2620         Con_Printf("%s :", PRVM_NAME);
2621
2622         for (i = 0;i < prog->progs->numglobaldefs;i++)
2623         {
2624                 if(wildcard)
2625                         if( !matchpattern( PRVM_GetString(prog->globaldefs[i].s_name), wildcard, 1) )
2626                         {
2627                                 numculled++;
2628                                 continue;
2629                         }
2630                 Con_Printf("%s\n", PRVM_GetString(prog->globaldefs[i].s_name));
2631         }
2632         Con_Printf("%i global variables, %i culled, totalling %i bytes\n", prog->progs->numglobals, numculled, prog->progs->numglobals * 4);
2633
2634         PRVM_End;
2635 }
2636
2637 /*
2638 ===============
2639 PRVM_Global
2640 ===============
2641 */
2642 void PRVM_Global_f(void)
2643 {
2644         ddef_t *global;
2645         if( Cmd_Argc() != 3 ) {
2646                 Con_Printf( "prvm_global <program name> <global name>\n" );
2647                 return;
2648         }
2649
2650         PRVM_Begin;
2651         if( !PRVM_SetProgFromString( Cmd_Argv(1) ) )
2652                 return;
2653
2654         global = PRVM_ED_FindGlobal( Cmd_Argv(2) );
2655         if( !global )
2656                 Con_Printf( "No global '%s' in %s!\n", Cmd_Argv(2), Cmd_Argv(1) );
2657         else
2658                 Con_Printf( "%s: %s\n", Cmd_Argv(2), PRVM_ValueString( (etype_t)global->type, (prvm_eval_t *) &prog->globals.generic[ global->ofs ] ) );
2659         PRVM_End;
2660 }
2661
2662 /*
2663 ===============
2664 PRVM_GlobalSet
2665 ===============
2666 */
2667 void PRVM_GlobalSet_f(void)
2668 {
2669         ddef_t *global;
2670         if( Cmd_Argc() != 4 ) {
2671                 Con_Printf( "prvm_globalset <program name> <global name> <value>\n" );
2672                 return;
2673         }
2674
2675         PRVM_Begin;
2676         if( !PRVM_SetProgFromString( Cmd_Argv(1) ) )
2677                 return;
2678
2679         global = PRVM_ED_FindGlobal( Cmd_Argv(2) );
2680         if( !global )
2681                 Con_Printf( "No global '%s' in %s!\n", Cmd_Argv(2), Cmd_Argv(1) );
2682         else
2683                 PRVM_ED_ParseEpair( NULL, global, Cmd_Argv(3), true );
2684         PRVM_End;
2685 }
2686
2687 /*
2688 ===============
2689 PRVM_Init
2690 ===============
2691 */
2692 void PRVM_Init (void)
2693 {
2694         Cmd_AddCommand ("prvm_edict", PRVM_ED_PrintEdict_f, "print all data about an entity number in the selected VM (server, client, menu)");
2695         Cmd_AddCommand ("prvm_edicts", PRVM_ED_PrintEdicts_f, "prints all data about all entities in the selected VM (server, client, menu)");
2696         Cmd_AddCommand ("prvm_edictcount", PRVM_ED_Count_f, "prints number of active entities in the selected VM (server, client, menu)");
2697         Cmd_AddCommand ("prvm_profile", PRVM_Profile_f, "prints execution statistics about the most used QuakeC functions in the selected VM (server, client, menu)");
2698         Cmd_AddCommand ("prvm_childprofile", PRVM_ChildProfile_f, "prints execution statistics about the most used QuakeC functions in the selected VM (server, client, menu), sorted by time taken in function with child calls");
2699         Cmd_AddCommand ("prvm_callprofile", PRVM_CallProfile_f, "prints execution statistics about the most time consuming QuakeC calls from the engine in the selected VM (server, client, menu)");
2700         Cmd_AddCommand ("prvm_fields", PRVM_Fields_f, "prints usage statistics on properties (how many entities have non-zero values) in the selected VM (server, client, menu)");
2701         Cmd_AddCommand ("prvm_globals", PRVM_Globals_f, "prints all global variables in the selected VM (server, client, menu)");
2702         Cmd_AddCommand ("prvm_global", PRVM_Global_f, "prints value of a specified global variable in the selected VM (server, client, menu)");
2703         Cmd_AddCommand ("prvm_globalset", PRVM_GlobalSet_f, "sets value of a specified global variable in the selected VM (server, client, menu)");
2704         Cmd_AddCommand ("prvm_edictset", PRVM_ED_EdictSet_f, "changes value of a specified property of a specified entity in the selected VM (server, client, menu)");
2705         Cmd_AddCommand ("prvm_edictget", PRVM_ED_EdictGet_f, "retrieves the value of a specified property of a specified entity in the selected VM (server, client menu) into a cvar or to the console");
2706         Cmd_AddCommand ("prvm_globalget", PRVM_ED_GlobalGet_f, "retrieves the value of a specified global variable in the selected VM (server, client menu) into a cvar or to the console");
2707         Cmd_AddCommand ("prvm_printfunction", PRVM_PrintFunction_f, "prints a disassembly (QuakeC instructions) of the specified function in the selected VM (server, client, menu)");
2708         Cmd_AddCommand ("cl_cmd", PRVM_GameCommand_Client_f, "calls the client QC function GameCommand with the supplied string as argument");
2709         Cmd_AddCommand ("menu_cmd", PRVM_GameCommand_Menu_f, "calls the menu QC function GameCommand with the supplied string as argument");
2710         Cmd_AddCommand ("sv_cmd", PRVM_GameCommand_Server_f, "calls the server QC function GameCommand with the supplied string as argument");
2711
2712         // COMMANDLINEOPTION: PRVM: -noboundscheck disables the bounds checks (security hole if CSQC is in use!)
2713         prvm_boundscheck = !COM_CheckParm("-noboundscheck");
2714
2715         Cvar_RegisterVariable (&prvm_language);
2716         Cvar_RegisterVariable (&prvm_traceqc);
2717         Cvar_RegisterVariable (&prvm_statementprofiling);
2718         Cvar_RegisterVariable (&prvm_backtraceforwarnings);
2719         Cvar_RegisterVariable (&prvm_leaktest);
2720         Cvar_RegisterVariable (&prvm_leaktest_ignore_classnames);
2721         Cvar_RegisterVariable (&prvm_errordump);
2722         Cvar_RegisterVariable (&prvm_reuseedicts_startuptime);
2723         Cvar_RegisterVariable (&prvm_reuseedicts_neverinsameframe);
2724
2725         // COMMANDLINEOPTION: PRVM: -norunaway disables the runaway loop check (it might be impossible to exit DarkPlaces if used!)
2726         prvm_runawaycheck = !COM_CheckParm("-norunaway");
2727
2728         //VM_Cmd_Init();
2729 }
2730
2731 /*
2732 ===============
2733 PRVM_InitProg
2734 ===============
2735 */
2736 void PRVM_InitProg(int prognr)
2737 {
2738         static unsigned int progid = 0;
2739
2740         if(prognr < 0 || prognr >= PRVM_MAXPROGS)
2741                 Sys_Error("PRVM_InitProg: Invalid program number %i",prognr);
2742
2743         prog = &prog_list[prognr];
2744
2745         if(prog->loaded)
2746                 PRVM_ResetProg();
2747
2748         memset(prog, 0, sizeof(prvm_prog_t));
2749         prog->starttime = Sys_DoubleTime();
2750         prog->id = ++progid;
2751
2752         prog->error_cmd = Host_Error;
2753         prog->leaktest_active = prvm_leaktest.integer != 0;
2754 }
2755
2756 int PRVM_GetProgNr(void)
2757 {
2758         return prog - prog_list;
2759 }
2760
2761 void *_PRVM_Alloc(size_t buffersize, const char *filename, int fileline)
2762 {
2763         return _Mem_Alloc(prog->progs_mempool, NULL, buffersize, 16, filename, fileline);
2764 }
2765
2766 void _PRVM_Free(void *buffer, const char *filename, int fileline)
2767 {
2768         _Mem_Free(buffer, filename, fileline);
2769 }
2770
2771 void _PRVM_FreeAll(const char *filename, int fileline)
2772 {
2773         prog->progs = NULL;
2774         prog->fielddefs = NULL;
2775         prog->functions = NULL;
2776         _Mem_EmptyPool(prog->progs_mempool, filename, fileline);
2777 }
2778
2779 // LordHavoc: turned PRVM_EDICT_NUM into a #define for speed reasons
2780 unsigned int PRVM_EDICT_NUM_ERROR(unsigned int n, char *filename, int fileline)
2781 {
2782         PRVM_ERROR ("PRVM_EDICT_NUM: %s: bad number %i (called at %s:%i)", PRVM_NAME, n, filename, fileline);
2783         return 0;
2784 }
2785
2786 sizebuf_t vm_tempstringsbuf;
2787
2788 const char *PRVM_GetString(int num)
2789 {
2790         if (num >= 0)
2791         {
2792                 if (num < prog->stringssize)
2793                         return prog->strings + num;
2794                 else
2795 #if 1
2796                 if (num <= prog->stringssize + vm_tempstringsbuf.maxsize)
2797                 {
2798                         num -= prog->stringssize;
2799                         if (num < vm_tempstringsbuf.cursize)
2800                                 return (char *)vm_tempstringsbuf.data + num;
2801                         else
2802                         {
2803                                 VM_Warning("PRVM_GetString: Invalid temp-string offset (%i >= %i vm_tempstringsbuf.cursize)\n", num, vm_tempstringsbuf.cursize);
2804                                 return "";
2805                         }
2806                 }
2807                 else
2808 #endif
2809                 {
2810                         VM_Warning("PRVM_GetString: Invalid constant-string offset (%i >= %i prog->stringssize)\n", num, prog->stringssize);
2811                         return "";
2812                 }
2813         }
2814         else
2815         {
2816                 num = -1 - num;
2817 #if 0
2818                 if (num >= (1<<30))
2819                 {
2820                         // special range reserved for tempstrings
2821                         num -= (1<<30);
2822                         if (num < vm_tempstringsbuf.cursize)
2823                                 return (char *)vm_tempstringsbuf.data + num;
2824                         else
2825                         {
2826                                 VM_Warning("PRVM_GetString: Invalid temp-string offset (%i >= %i vm_tempstringsbuf.cursize)\n", num, vm_tempstringsbuf.cursize);
2827                                 return "";
2828                         }
2829                 }
2830                 else
2831 #endif
2832                 if (num < prog->numknownstrings)
2833                 {
2834                         if (!prog->knownstrings[num])
2835                         {
2836                                 VM_Warning("PRVM_GetString: Invalid zone-string offset (%i has been freed)\n", num);
2837                                 return "";
2838                         }
2839                         return prog->knownstrings[num];
2840                 }
2841                 else
2842                 {
2843                         VM_Warning("PRVM_GetString: Invalid zone-string offset (%i >= %i)\n", num, prog->numknownstrings);
2844                         return "";
2845                 }
2846         }
2847 }
2848
2849 const char *PRVM_ChangeEngineString(int i, const char *s)
2850 {
2851         const char *old;
2852         i = -1 - i;
2853         if(i < 0 || i >= prog->numknownstrings)
2854                 PRVM_ERROR("PRVM_ChangeEngineString: s is not an engine string");
2855         old = prog->knownstrings[i];
2856         prog->knownstrings[i] = s;
2857         return old;
2858 }
2859
2860 int PRVM_SetEngineString(const char *s)
2861 {
2862         int i;
2863         if (!s)
2864                 return 0;
2865         if (s >= prog->strings && s <= prog->strings + prog->stringssize)
2866                 PRVM_ERROR("PRVM_SetEngineString: s in prog->strings area");
2867         // if it's in the tempstrings area, use a reserved range
2868         // (otherwise we'd get millions of useless string offsets cluttering the database)
2869         if (s >= (char *)vm_tempstringsbuf.data && s < (char *)vm_tempstringsbuf.data + vm_tempstringsbuf.maxsize)
2870 #if 1
2871                 return prog->stringssize + (s - (char *)vm_tempstringsbuf.data);
2872 #else
2873                 return -1 - ((1<<30) + (s - (char *)vm_tempstringsbuf.data));
2874 #endif
2875         // see if it's a known string address
2876         for (i = 0;i < prog->numknownstrings;i++)
2877                 if (prog->knownstrings[i] == s)
2878                         return -1 - i;
2879         // new unknown engine string
2880         if (developer.integer >= 200)
2881                 Con_Printf("new engine string %p = \"%s\"\n", s, s);
2882         for (i = prog->firstfreeknownstring;i < prog->numknownstrings;i++)
2883                 if (!prog->knownstrings[i])
2884                         break;
2885         if (i >= prog->numknownstrings)
2886         {
2887                 if (i >= prog->maxknownstrings)
2888                 {
2889                         const char **oldstrings = prog->knownstrings;
2890                         const unsigned char *oldstrings_freeable = prog->knownstrings_freeable;
2891                         const char **oldstrings_origin = prog->knownstrings_origin;
2892                         prog->maxknownstrings += 128;
2893                         prog->knownstrings = (const char **)PRVM_Alloc(prog->maxknownstrings * sizeof(char *));
2894                         prog->knownstrings_freeable = (unsigned char *)PRVM_Alloc(prog->maxknownstrings * sizeof(unsigned char));
2895                         if(prog->leaktest_active)
2896                                 prog->knownstrings_origin = (const char **)PRVM_Alloc(prog->maxknownstrings * sizeof(char *));
2897                         if (prog->numknownstrings)
2898                         {
2899                                 memcpy((char **)prog->knownstrings, oldstrings, prog->numknownstrings * sizeof(char *));
2900                                 memcpy((char **)prog->knownstrings_freeable, oldstrings_freeable, prog->numknownstrings * sizeof(unsigned char));
2901                                 if(prog->leaktest_active)
2902                                         memcpy((char **)prog->knownstrings_origin, oldstrings_origin, prog->numknownstrings * sizeof(char *));
2903                         }
2904                 }
2905                 prog->numknownstrings++;
2906         }
2907         prog->firstfreeknownstring = i + 1;
2908         prog->knownstrings[i] = s;
2909         prog->knownstrings_freeable[i] = false;
2910         if(prog->leaktest_active)
2911                 prog->knownstrings_origin[i] = NULL;
2912         return -1 - i;
2913 }
2914
2915 // temp string handling
2916
2917 // all tempstrings go into this buffer consecutively, and it is reset
2918 // whenever PRVM_ExecuteProgram returns to the engine
2919 // (technically each PRVM_ExecuteProgram call saves the cursize value and
2920 //  restores it on return, so multiple recursive calls can share the same
2921 //  buffer)
2922 // the buffer size is automatically grown as needed
2923
2924 int PRVM_SetTempString(const char *s)
2925 {
2926         int size;
2927         char *t;
2928         if (!s)
2929                 return 0;
2930         size = (int)strlen(s) + 1;
2931         if (developer.integer >= 300)
2932                 Con_Printf("PRVM_SetTempString: cursize %i, size %i\n", vm_tempstringsbuf.cursize, size);
2933         if (vm_tempstringsbuf.maxsize < vm_tempstringsbuf.cursize + size)
2934         {
2935                 sizebuf_t old = vm_tempstringsbuf;
2936                 if (vm_tempstringsbuf.cursize + size >= 1<<28)
2937                         PRVM_ERROR("PRVM_SetTempString: ran out of tempstring memory!  (refusing to grow tempstring buffer over 256MB, cursize %i, size %i)\n", vm_tempstringsbuf.cursize, size);
2938                 vm_tempstringsbuf.maxsize = max(vm_tempstringsbuf.maxsize, 65536);
2939                 while (vm_tempstringsbuf.maxsize < vm_tempstringsbuf.cursize + size)
2940                         vm_tempstringsbuf.maxsize *= 2;
2941                 if (vm_tempstringsbuf.maxsize != old.maxsize || vm_tempstringsbuf.data == NULL)
2942                 {
2943                         if (developer.integer >= 100)
2944                                 Con_Printf("PRVM_SetTempString: enlarging tempstrings buffer (%iKB -> %iKB)\n", old.maxsize/1024, vm_tempstringsbuf.maxsize/1024);
2945                         vm_tempstringsbuf.data = (unsigned char *) Mem_Alloc(sv_mempool, vm_tempstringsbuf.maxsize);
2946                         if (old.cursize)
2947                                 memcpy(vm_tempstringsbuf.data, old.data, old.cursize);
2948                         if (old.data)
2949                                 Mem_Free(old.data);
2950                 }
2951         }
2952         t = (char *)vm_tempstringsbuf.data + vm_tempstringsbuf.cursize;
2953         memcpy(t, s, size);
2954         vm_tempstringsbuf.cursize += size;
2955         return PRVM_SetEngineString(t);
2956 }
2957
2958 int PRVM_AllocString(size_t bufferlength, char **pointer)
2959 {
2960         int i;
2961         if (!bufferlength)
2962                 return 0;
2963         for (i = prog->firstfreeknownstring;i < prog->numknownstrings;i++)
2964                 if (!prog->knownstrings[i])
2965                         break;
2966         if (i >= prog->numknownstrings)
2967         {
2968                 if (i >= prog->maxknownstrings)
2969                 {
2970                         const char **oldstrings = prog->knownstrings;
2971                         const unsigned char *oldstrings_freeable = prog->knownstrings_freeable;
2972                         const char **oldstrings_origin = prog->knownstrings_origin;
2973                         prog->maxknownstrings += 128;
2974                         prog->knownstrings = (const char **)PRVM_Alloc(prog->maxknownstrings * sizeof(char *));
2975                         prog->knownstrings_freeable = (unsigned char *)PRVM_Alloc(prog->maxknownstrings * sizeof(unsigned char));
2976                         if(prog->leaktest_active)
2977                                 prog->knownstrings_origin = (const char **)PRVM_Alloc(prog->maxknownstrings * sizeof(char *));
2978                         if (prog->numknownstrings)
2979                         {
2980                                 memcpy((char **)prog->knownstrings, oldstrings, prog->numknownstrings * sizeof(char *));
2981                                 memcpy((char **)prog->knownstrings_freeable, oldstrings_freeable, prog->numknownstrings * sizeof(unsigned char));
2982                                 if(prog->leaktest_active)
2983                                         memcpy((char **)prog->knownstrings_origin, oldstrings_origin, prog->numknownstrings * sizeof(char *));
2984                         }
2985                         // TODO why not Mem_Free the old ones?
2986                 }
2987                 prog->numknownstrings++;
2988         }
2989         prog->firstfreeknownstring = i + 1;
2990         prog->knownstrings[i] = (char *)PRVM_Alloc(bufferlength);
2991         prog->knownstrings_freeable[i] = true;
2992         if(prog->leaktest_active)
2993                 prog->knownstrings_origin[i] = PRVM_AllocationOrigin();
2994         if (pointer)
2995                 *pointer = (char *)(prog->knownstrings[i]);
2996         return -1 - i;
2997 }
2998
2999 void PRVM_FreeString(int num)
3000 {
3001         if (num == 0)
3002                 PRVM_ERROR("PRVM_FreeString: attempt to free a NULL string");
3003         else if (num >= 0 && num < prog->stringssize)
3004                 PRVM_ERROR("PRVM_FreeString: attempt to free a constant string");
3005         else if (num < 0 && num >= -prog->numknownstrings)
3006         {
3007                 num = -1 - num;
3008                 if (!prog->knownstrings[num])
3009                         PRVM_ERROR("PRVM_FreeString: attempt to free a non-existent or already freed string");
3010                 if (!prog->knownstrings_freeable[num])
3011                         PRVM_ERROR("PRVM_FreeString: attempt to free a string owned by the engine");
3012                 PRVM_Free((char *)prog->knownstrings[num]);
3013                 if(prog->leaktest_active)
3014                         if(prog->knownstrings_origin[num])
3015                                 PRVM_Free((char *)prog->knownstrings_origin[num]);
3016                 prog->knownstrings[num] = NULL;
3017                 prog->knownstrings_freeable[num] = false;
3018                 prog->firstfreeknownstring = min(prog->firstfreeknownstring, num);
3019         }
3020         else
3021                 PRVM_ERROR("PRVM_FreeString: invalid string offset %i", num);
3022 }
3023
3024 static qboolean PRVM_IsStringReferenced(string_t string)
3025 {
3026         int i, j;
3027
3028         for (i = 0;i < prog->progs->numglobaldefs;i++)
3029         {
3030                 ddef_t *d = &prog->globaldefs[i];
3031                 if((etype_t)((int) d->type & ~DEF_SAVEGLOBAL) != ev_string)
3032                         continue;
3033                 if(string == ((prvm_eval_t *) &prog->globals.generic[d->ofs])->string)
3034                         return true;
3035         }
3036
3037         for(j = 0; j < prog->num_edicts; ++j)
3038         {
3039                 prvm_edict_t *ed = PRVM_EDICT_NUM(j);
3040                 if (ed->priv.required->free)
3041                         continue;
3042                 for (i=0; i<prog->progs->numfielddefs; ++i)
3043                 {
3044                         ddef_t *d = &prog->fielddefs[i];
3045                         if((etype_t)((int) d->type & ~DEF_SAVEGLOBAL) != ev_string)
3046                                 continue;
3047                         if(string == ((prvm_eval_t *) &ed->fields.vp[d->ofs])->string)
3048                                 return true;
3049                 }
3050         }
3051
3052         return false;
3053 }
3054
3055 static qboolean PRVM_IsEdictRelevant(prvm_edict_t *edict)
3056 {
3057         if(PRVM_NUM_FOR_EDICT(edict) <= prog->reserved_edicts)
3058                 return true; // world or clients
3059         switch(prog - prog_list)
3060         {
3061                 case PRVM_SERVERPROG:
3062                         {
3063                                 entvars_t *ev = edict->fields.server;
3064                                 if(ev->solid) // can block other stuff, or is a trigger?
3065                                         return true;
3066                                 if(ev->modelindex) // visible ent?
3067                                         return true;
3068                                 if(ev->effects) // particle effect?
3069                                         return true;
3070                                 if(ev->think) // has a think function?
3071                                         if(ev->nextthink > 0) // that actually will eventually run?
3072                                                 return true;
3073                                 if(ev->takedamage)
3074                                         return true;
3075                                 if(*prvm_leaktest_ignore_classnames.string)
3076                                 {
3077                                         if(strstr(va(" %s ", prvm_leaktest_ignore_classnames.string), va(" %s ", PRVM_GetString(ev->classname))))
3078                                                 return true;
3079                                 }
3080                         }
3081                         break;
3082                 case PRVM_CLIENTPROG:
3083                         {
3084                                 // TODO someone add more stuff here
3085                                 cl_entvars_t *ev = edict->fields.client;
3086                                 if(ev->entnum) // csqc networked
3087                                         return true;
3088                                 if(ev->modelindex) // visible ent?
3089                                         return true;
3090                                 if(ev->effects) // particle effect?
3091                                         return true;
3092                                 if(ev->think) // has a think function?
3093                                         if(ev->nextthink > 0) // that actually will eventually run?
3094                                                 return true;
3095                                 if(*prvm_leaktest_ignore_classnames.string)
3096                                 {
3097                                         if(strstr(va(" %s ", prvm_leaktest_ignore_classnames.string), va(" %s ", PRVM_GetString(ev->classname))))
3098                                                 return true;
3099                                 }
3100                         }
3101                         break;
3102                 case PRVM_MENUPROG:
3103                         // menu prog does not have classnames
3104                         break;
3105         }
3106         return false;
3107 }
3108
3109 static qboolean PRVM_IsEdictReferenced(prvm_edict_t *edict, int mark)
3110 {
3111         int i, j;
3112         int edictnum = PRVM_NUM_FOR_EDICT(edict);
3113         const char *targetname = NULL;
3114
3115         switch(prog - prog_list)
3116         {
3117                 case PRVM_SERVERPROG:
3118                         targetname = PRVM_GetString(edict->fields.server->targetname);
3119                         break;
3120         }
3121
3122         if(targetname)
3123                 if(!*targetname) // ""
3124                         targetname = NULL;
3125
3126         for (i = 0;i < prog->progs->numglobaldefs;i++)
3127         {
3128                 ddef_t *d = &prog->globaldefs[i];
3129                 if((etype_t)((int) d->type & ~DEF_SAVEGLOBAL) != ev_entity)
3130                         continue;
3131                 if(edictnum == ((prvm_eval_t *) &prog->globals.generic[d->ofs])->edict)
3132                         return true;
3133         }
3134
3135         for(j = 0; j < prog->num_edicts; ++j)
3136         {
3137                 prvm_edict_t *ed = PRVM_EDICT_NUM(j);
3138                 if (ed->priv.required->mark < mark)
3139                         continue;
3140                 if(ed == edict)
3141                         continue;
3142                 if(targetname)
3143                 {
3144                         const char *target = PRVM_GetString(ed->fields.server->target);
3145                         if(target)
3146                                 if(!strcmp(target, targetname))
3147                                         return true;
3148                 }
3149                 for (i=0; i<prog->progs->numfielddefs; ++i)
3150                 {
3151                         ddef_t *d = &prog->fielddefs[i];
3152                         if((etype_t)((int) d->type & ~DEF_SAVEGLOBAL) != ev_entity)
3153                                 continue;
3154                         if(edictnum == ((prvm_eval_t *) &ed->fields.vp[d->ofs])->edict)
3155                                 return true;
3156                 }
3157         }
3158
3159         return false;
3160 }
3161
3162 static void PRVM_MarkReferencedEdicts(void)
3163 {
3164         int j;
3165         qboolean found_new;
3166         int stage;
3167
3168         for(j = 0; j < prog->num_edicts; ++j)
3169         {
3170                 prvm_edict_t *ed = PRVM_EDICT_NUM(j);
3171                 if(ed->priv.required->free)
3172                         continue;
3173                 ed->priv.required->mark = PRVM_IsEdictRelevant(ed) ? 1 : 0;
3174         }
3175
3176         stage = 1;
3177         do
3178         {
3179                 found_new = false;
3180                 for(j = 0; j < prog->num_edicts; ++j)
3181                 {
3182                         prvm_edict_t *ed = PRVM_EDICT_NUM(j);
3183                         if(ed->priv.required->free)
3184                                 continue;
3185                         if(ed->priv.required->mark)
3186                                 continue;
3187                         if(PRVM_IsEdictReferenced(ed, stage))
3188                         {
3189                                 ed->priv.required->mark = stage + 1;
3190                                 found_new = true;
3191                         }
3192                 }
3193                 ++stage;
3194         }
3195         while(found_new);
3196         Con_DPrintf("leak check used %d stages to find all references\n", stage);
3197 }
3198
3199 void PRVM_LeakTest(void)
3200 {
3201         int i, j;
3202         qboolean leaked = false;
3203
3204         if(!prog->leaktest_active)
3205                 return;
3206
3207         // 1. Strings
3208         for (i = 0; i < prog->numknownstrings; ++i)
3209         {
3210                 if(prog->knownstrings[i])
3211                 if(prog->knownstrings_freeable[i])
3212                 if(prog->knownstrings_origin[i])
3213                 if(!PRVM_IsStringReferenced(-1 - i))
3214                 {
3215                         Con_Printf("Unreferenced string found!\n  Value: %s\n  Origin: %s\n", prog->knownstrings[i], prog->knownstrings_origin[i]);
3216                         leaked = true;
3217                 }
3218         }
3219
3220         // 2. Edicts
3221         PRVM_MarkReferencedEdicts();
3222         for(j = 0; j < prog->num_edicts; ++j)
3223         {
3224                 prvm_edict_t *ed = PRVM_EDICT_NUM(j);
3225                 if(ed->priv.required->free)
3226                         continue;
3227                 if(!ed->priv.required->mark)
3228                 if(ed->priv.required->allocation_origin)
3229                 {
3230                         Con_Printf("Unreferenced edict found!\n  Allocated at: %s\n", ed->priv.required->allocation_origin);
3231                         PRVM_ED_Print(ed, NULL);
3232                         Con_Print("\n");
3233                         leaked = true;
3234                 }
3235         }
3236
3237         for (i = 0; i < (int)Mem_ExpandableArray_IndexRange(&prog->stringbuffersarray); ++i)
3238         {
3239                 prvm_stringbuffer_t *stringbuffer = (prvm_stringbuffer_t*) Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);
3240                 if(stringbuffer)
3241                 if(stringbuffer->origin)
3242                 {
3243                         Con_Printf("Open string buffer handle found!\n  Allocated at: %s\n", stringbuffer->origin);
3244                         leaked = true;
3245                 }
3246         }
3247
3248         for(i = 0; i < PRVM_MAX_OPENFILES; ++i)
3249         {
3250                 if(prog->openfiles[i])
3251                 if(prog->openfiles_origin[i])
3252                 {
3253                         Con_Printf("Open file handle found!\n  Allocated at: %s\n", prog->openfiles_origin[i]);
3254                         leaked = true;
3255                 }
3256         }
3257
3258         for(i = 0; i < PRVM_MAX_OPENSEARCHES; ++i)
3259         {
3260                 if(prog->opensearches[i])
3261                 if(prog->opensearches_origin[i])
3262                 {
3263                         Con_Printf("Open search handle found!\n  Allocated at: %s\n", prog->opensearches_origin[i]);
3264                         leaked = true;
3265                 }
3266         }
3267
3268         if(!leaked)
3269                 Con_Printf("Congratulations. No leaks found.\n");
3270 }