2004-08-23 01:38:21 +00:00
# define PROGSUSED
struct edict_s ;
# include "progsint.h"
2005-05-22 13:47:54 +00:00
//#include "crc.h"
2004-08-23 01:38:21 +00:00
/*int maxedicts;
evalc_t spawnflagscache ;
*/
# ifdef _WIN32
//this is windows all files are written with this endian standard. we do this to try to get a little more speed.
# define NOENDIAN
# endif
vec3_t vec3_origin ;
//edictrun_t *sv_edicts;
//int sv_num_edicts;
//int pr_edict_size; // in bytes
//int pr_max_edict_size;
//unsigned short pr_crc;
2007-08-08 02:37:59 +00:00
const unsigned int type_size [ 12 ] = { 1 , //void
2005-04-02 18:00:45 +00:00
sizeof ( string_t ) / 4 , //string
1 , //float
3 , //vector
1 , //entity
1 , //field
sizeof ( func_t ) / 4 , //function
sizeof ( void * ) / 4 , //pointer
1 , //integer
1 , //fixme: how big should a variant be?
0 , //ev_struct. variable sized.
0 //ev_union. variable sized.
} ;
2004-08-23 01:38:21 +00:00
fdef_t * ED_FieldAtOfs ( progfuncs_t * progfuncs , unsigned int ofs ) ;
pbool ED_ParseEpair ( progfuncs_t * progfuncs , void * base , ddefXX_t * key , char * s , int bits ) ;
/*
# define MAX_FIELD_LEN 64
# define GEFV_CACHESIZE 5
typedef struct {
ddef_t * pcache ;
char field [ MAX_FIELD_LEN ] ;
} gefv_cache ;
static gefv_cache gefvCache [ GEFV_CACHESIZE ] = { { NULL , " " } , { NULL , " " } } ;
*/
/*
= = = = = = = = = = = = = = = = =
ED_ClearEdict
Sets everything to NULL
= = = = = = = = = = = = = = = = =
*/
void ED_ClearEdict ( progfuncs_t * progfuncs , edictrun_t * e )
{
int num = e - > entnum ;
2005-03-28 00:11:59 +00:00
memset ( e - > fields , 0 , fields_size ) ;
2004-08-23 01:38:21 +00:00
e - > isfree = false ;
e - > entnum = num ;
}
/*
= = = = = = = = = = = = = = = = =
ED_Alloc
Either finds a free edict , or allocates a new one .
Try to avoid reusing an entity that was recently freed , because it
can cause the client to think the entity morphed into something else
instead of being removed and recreated , which can cause interpolated
angles and bad trails .
= = = = = = = = = = = = = = = = =
*/
struct edict_s * ED_Alloc ( progfuncs_t * progfuncs )
{
2005-10-07 16:27:20 +00:00
unsigned int i ;
2004-08-23 01:38:21 +00:00
edictrun_t * e ;
for ( i = 0 ; i < sv_num_edicts ; i + + )
{
e = ( edictrun_t * ) EDICT_NUM ( progfuncs , i ) ;
// the first couple seconds of server time can involve a lot of
// freeing and allocating, so relax the replacement policy
if ( ! e | | ( e - > isfree & & ( e - > freetime < 2 | | * externs - > gametime - e - > freetime > 0.5 ) ) )
{
if ( ! e )
{
2005-03-28 00:11:59 +00:00
prinst - > edicttable [ i ] = * ( struct edict_s * * ) & e = ( void * ) memalloc ( externs - > edictsize ) ;
e - > fields = PRAddressableAlloc ( progfuncs , fields_size ) ;
2004-08-23 01:38:21 +00:00
e - > entnum = i ;
}
ED_ClearEdict ( progfuncs , e ) ;
if ( externs - > entspawn )
externs - > entspawn ( ( struct edict_s * ) e ) ;
return ( struct edict_s * ) e ;
}
}
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
if ( i > = maxedicts - 1 ) //try again, but use timed out ents.
{
for ( i = 0 ; i < sv_num_edicts ; i + + )
{
e = ( edictrun_t * ) EDICT_NUM ( progfuncs , i ) ;
// the first couple seconds of server time can involve a lot of
// freeing and allocating, so relax the replacement policy
if ( ! e | | ( e - > isfree ) )
{
if ( ! e )
{
2005-03-28 00:11:59 +00:00
prinst - > edicttable [ i ] = * ( struct edict_s * * ) & e = ( void * ) memalloc ( externs - > edictsize ) ;
e - > fields = PRAddressableAlloc ( progfuncs , fields_size ) ;
2004-08-23 01:38:21 +00:00
e - > entnum = i ;
}
ED_ClearEdict ( progfuncs , e ) ;
if ( externs - > entspawn )
externs - > entspawn ( ( struct edict_s * ) e ) ;
return ( struct edict_s * ) e ;
}
}
2006-09-18 22:56:14 +00:00
if ( i > = maxedicts - 2 )
{
printf ( " Running out of edicts \n " ) ;
pr_trace = 1 ; //trip the debugger whilst it's still valid
}
2004-08-23 01:38:21 +00:00
if ( i > = maxedicts - 1 )
2004-12-08 04:14:52 +00:00
{
int size ;
char * buf ;
buf = progfuncs - > save_ents ( progfuncs , NULL , & size , 0 ) ;
progfuncs - > parms - > WriteFile ( " edalloc.dump " , buf , size ) ;
2004-08-23 01:38:21 +00:00
Sys_Error ( " ED_Alloc: no free edicts " ) ;
2004-12-08 04:14:52 +00:00
}
2004-08-23 01:38:21 +00:00
}
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
sv_num_edicts + + ;
e = ( edictrun_t * ) EDICT_NUM ( progfuncs , i ) ;
2004-09-20 23:25:38 +00:00
2004-08-23 01:38:21 +00:00
if ( ! e )
{
2005-03-28 00:11:59 +00:00
prinst - > edicttable [ i ] = * ( struct edict_s * * ) & e = ( void * ) memalloc ( externs - > edictsize ) ;
e - > fields = PRAddressableAlloc ( progfuncs , fields_size ) ;
2004-08-23 01:38:21 +00:00
e - > entnum = i ;
}
2004-09-20 23:25:38 +00:00
2004-08-23 01:38:21 +00:00
ED_ClearEdict ( progfuncs , e ) ;
if ( externs - > entspawn )
externs - > entspawn ( ( struct edict_s * ) e ) ;
2006-09-18 22:56:14 +00:00
2004-08-23 01:38:21 +00:00
return ( struct edict_s * ) e ;
}
/*
= = = = = = = = = = = = = = = = =
ED_Free
Marks the edict as free
FIXME : walk all entities and NULL out references to this entity
= = = = = = = = = = = = = = = = =
*/
void ED_Free ( progfuncs_t * progfuncs , struct edict_s * ed )
{
edictrun_t * e = ( edictrun_t * ) ed ;
// SV_UnlinkEdict (ed); // unlink from world bsp
if ( e - > isfree ) //this happens on start.bsp where an onlyregistered trigger killtargets itself (when all of this sort die after 1 trigger anyway).
{
2004-11-08 11:45:14 +00:00
if ( pr_depth )
2005-05-15 18:49:04 +00:00
printf ( " Tried to free free entity within %s \n " , pr_xfunction - > s_name + progfuncs - > stringtable ) ;
2004-11-08 11:45:14 +00:00
else
printf ( " Engine tried to free free entity \n " ) ;
2004-08-23 01:38:21 +00:00
// if (developer.value == 1)
// pr_trace = true;
return ;
}
if ( externs - > entcanfree )
if ( ! externs - > entcanfree ( ed ) ) //can stop an ent from being freed.
return ;
e - > isfree = true ;
e - > freetime = ( float ) * externs - > gametime ;
/*
ed - > v . model = 0 ;
ed - > v . takedamage = 0 ;
ed - > v . modelindex = 0 ;
ed - > v . colormap = 0 ;
ed - > v . skin = 0 ;
ed - > v . frame = 0 ;
VectorCopy ( vec3_origin , ed - > v . origin ) ;
VectorCopy ( vec3_origin , ed - > v . angles ) ;
ed - > v . nextthink = - 1 ;
ed - > v . solid = 0 ;
2005-10-09 21:59:54 +00:00
*/
2004-08-23 01:38:21 +00:00
}
//===========================================================================
/*
= = = = = = = = = = = =
ED_GlobalAtOfs
= = = = = = = = = = = =
*/
ddef16_t * ED_GlobalAtOfs16 ( progfuncs_t * progfuncs , int ofs )
{
ddef16_t * def ;
unsigned int i ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
for ( i = 0 ; i < pr_progs - > numglobaldefs ; i + + )
{
def = & pr_globaldefs16 [ i ] ;
if ( def - > ofs = = ofs )
return def ;
}
return NULL ;
}
ddef32_t * ED_GlobalAtOfs32 ( progfuncs_t * progfuncs , unsigned int ofs )
{
ddef32_t * def ;
unsigned int i ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
for ( i = 0 ; i < pr_progs - > numglobaldefs ; i + + )
{
def = & pr_globaldefs32 [ i ] ;
if ( def - > ofs = = ofs )
return def ;
}
return NULL ;
}
/*
= = = = = = = = = = = =
ED_FieldAtOfs
= = = = = = = = = = = =
*/
fdef_t * ED_FieldAtOfs ( progfuncs_t * progfuncs , unsigned int ofs )
{
// ddef_t *def;
unsigned int i ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
for ( i = 0 ; i < numfields ; i + + )
{
if ( field [ i ] . ofs = = ofs )
return & field [ i ] ;
}
return NULL ;
}
/*
= = = = = = = = = = = =
ED_FindField
= = = = = = = = = = = =
*/
fdef_t * ED_FindField ( progfuncs_t * progfuncs , char * name )
{
unsigned int i ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
for ( i = 0 ; i < numfields ; i + + )
2005-10-09 21:59:54 +00:00
{
2005-05-15 18:49:04 +00:00
if ( ! strcmp ( field [ i ] . name , name ) )
2004-08-23 01:38:21 +00:00
return & field [ i ] ;
}
return NULL ;
}
/*
= = = = = = = = = = = =
ED_FindGlobal
= = = = = = = = = = = =
*/
ddef16_t * ED_FindGlobal16 ( progfuncs_t * progfuncs , char * name )
{
ddef16_t * def ;
unsigned int i ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
for ( i = 1 ; i < pr_progs - > numglobaldefs ; i + + )
{
def = & pr_globaldefs16 [ i ] ;
2005-05-15 18:49:04 +00:00
if ( ! strcmp ( def - > s_name + progfuncs - > stringtable , name ) )
2004-08-23 01:38:21 +00:00
return def ;
}
return NULL ;
}
ddef32_t * ED_FindGlobal32 ( progfuncs_t * progfuncs , char * name )
{
ddef32_t * def ;
unsigned int i ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
for ( i = 1 ; i < pr_progs - > numglobaldefs ; i + + )
{
def = & pr_globaldefs32 [ i ] ;
2005-05-15 18:49:04 +00:00
if ( ! strcmp ( def - > s_name + progfuncs - > stringtable , name ) )
2004-08-23 01:38:21 +00:00
return def ;
}
return NULL ;
}
unsigned int ED_FindGlobalOfs ( progfuncs_t * progfuncs , char * name )
{
ddef16_t * d16 ;
ddef32_t * d32 ;
switch ( current_progstate - > intsize )
{
case 24 :
case 16 :
d16 = ED_FindGlobal16 ( progfuncs , name ) ;
return d16 ? d16 - > ofs : 0 ;
case 32 :
d32 = ED_FindGlobal32 ( progfuncs , name ) ;
return d32 ? d32 - > ofs : 0 ;
}
Sys_Error ( " ED_FindGlobalOfs - bad intsize " ) ;
return 0 ;
}
ddef16_t * ED_FindGlobalFromProgs16 ( progfuncs_t * progfuncs , char * name , progsnum_t prnum )
{
ddef16_t * def ;
unsigned int i ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
for ( i = 1 ; i < pr_progstate [ prnum ] . progs - > numglobaldefs ; i + + )
{
def = & pr_progstate [ prnum ] . globaldefs16 [ i ] ;
2005-05-15 18:49:04 +00:00
if ( ! strcmp ( def - > s_name + progfuncs - > stringtable , name ) )
2004-08-23 01:38:21 +00:00
return def ;
}
return NULL ;
}
ddef32_t * ED_FindGlobalFromProgs32 ( progfuncs_t * progfuncs , char * name , progsnum_t prnum )
{
ddef32_t * def ;
unsigned int i ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
for ( i = 1 ; i < pr_progstate [ prnum ] . progs - > numglobaldefs ; i + + )
{
def = & pr_progstate [ prnum ] . globaldefs32 [ i ] ;
2005-05-15 18:49:04 +00:00
if ( ! strcmp ( def - > s_name + progfuncs - > stringtable , name ) )
2004-08-23 01:38:21 +00:00
return def ;
}
return NULL ;
}
ddef16_t * ED_FindTypeGlobalFromProgs16 ( progfuncs_t * progfuncs , char * name , progsnum_t prnum , int type )
{
ddef16_t * def ;
unsigned int i ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
for ( i = 1 ; i < pr_progstate [ prnum ] . progs - > numglobaldefs ; i + + )
{
def = & pr_progstate [ prnum ] . globaldefs16 [ i ] ;
2005-05-15 18:49:04 +00:00
if ( ! strcmp ( def - > s_name + progfuncs - > stringtable , name ) )
2004-08-23 01:38:21 +00:00
{
if ( pr_progstate [ prnum ] . types )
{
if ( pr_progstate [ prnum ] . types [ def - > type & ~ DEF_SAVEGLOBAL ] . type ! = type )
continue ;
}
else if ( ( def - > type & ( ~ DEF_SAVEGLOBAL ) ) ! = type )
continue ;
return def ;
}
}
return NULL ;
}
ddef32_t * ED_FindTypeGlobalFromProgs32 ( progfuncs_t * progfuncs , char * name , progsnum_t prnum , int type )
{
ddef32_t * def ;
unsigned int i ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
for ( i = 1 ; i < pr_progstate [ prnum ] . progs - > numglobaldefs ; i + + )
{
def = & pr_progstate [ prnum ] . globaldefs32 [ i ] ;
2005-05-15 18:49:04 +00:00
if ( ! strcmp ( def - > s_name + progfuncs - > stringtable , name ) )
2004-08-23 01:38:21 +00:00
{
if ( pr_progstate [ prnum ] . types )
{
if ( pr_progstate [ prnum ] . types [ def - > type & ~ DEF_SAVEGLOBAL ] . type ! = type )
continue ;
}
else if ( ( def - > type & ( ~ DEF_SAVEGLOBAL ) ) ! = ( unsigned ) type )
continue ;
return def ;
}
}
return NULL ;
}
unsigned int * ED_FindGlobalOfsFromProgs ( progfuncs_t * progfuncs , char * name , progsnum_t prnum , int type )
{
ddef16_t * def16 ;
ddef32_t * def32 ;
static unsigned int pos ;
switch ( pr_progstate [ prnum ] . intsize )
{
case 16 :
case 24 :
def16 = ED_FindTypeGlobalFromProgs16 ( progfuncs , name , prnum , type ) ;
if ( ! def16 )
return NULL ;
pos = def16 - > ofs ;
return & pos ;
case 32 :
def32 = ED_FindTypeGlobalFromProgs32 ( progfuncs , name , prnum , type ) ;
if ( ! def32 )
return NULL ;
2005-10-09 21:59:54 +00:00
return & def32 - > ofs ;
2004-08-23 01:38:21 +00:00
}
Sys_Error ( " ED_FindGlobalOfsFromProgs - bad intsize " ) ;
return 0 ;
}
/*
= = = = = = = = = = = =
ED_FindFunction
= = = = = = = = = = = =
*/
2005-10-09 21:59:54 +00:00
dfunction_t * ED_FindFunction ( progfuncs_t * progfuncs , char * name , progsnum_t * prnum , progsnum_t fromprogs )
2004-08-23 01:38:21 +00:00
{
dfunction_t * func ;
unsigned int i ;
char * sep ;
2005-10-09 21:59:54 +00:00
progsnum_t pnum ;
2004-08-23 01:38:21 +00:00
if ( prnum )
{
sep = strchr ( name , ' : ' ) ;
if ( sep )
{
pnum = atoi ( name ) ;
name = sep + 1 ;
}
else
{
if ( fromprogs > = 0 )
pnum = fromprogs ;
else
pnum = pr_typecurrent ;
}
* prnum = pnum ;
}
else
pnum = pr_typecurrent ;
2005-10-09 21:59:54 +00:00
if ( ( unsigned ) pnum > ( unsigned ) maxprogs )
{
printf ( " Progsnum %i out of bounds \n " , pnum ) ;
return NULL ;
}
2004-08-23 01:38:21 +00:00
if ( ! pr_progstate [ pnum ] . progs )
return NULL ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
for ( i = 1 ; i < pr_progstate [ pnum ] . progs - > numfunctions ; i + + )
{
func = & pr_progstate [ pnum ] . functions [ i ] ;
2005-05-15 18:49:04 +00:00
if ( ! strcmp ( func - > s_name + progfuncs - > stringtable , name ) )
2004-08-23 01:38:21 +00:00
return func ;
}
return NULL ;
}
/*
= = = = = = = = = = = =
PR_ValueString
Returns a string describing * data in a type specific manner
= = = = = = = = = = = = =
*/
char * PR_ValueString ( progfuncs_t * progfuncs , etype_t type , eval_t * val )
{
static char line [ 256 ] ;
fdef_t * fielddef ;
dfunction_t * f ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
# ifdef DEF_SAVEGLOBAL
type & = ~ DEF_SAVEGLOBAL ;
# endif
if ( pr_types )
type = pr_types [ type ] . type ;
switch ( type )
{
case ev_struct :
sprintf ( line , " struct " ) ;
break ;
case ev_union :
sprintf ( line , " union " ) ;
break ;
case ev_string :
2007-03-11 16:51:45 +00:00
sprintf ( line , " %s " , PR_StringToNative ( progfuncs , val - > string ) ) ;
2004-08-23 01:38:21 +00:00
break ;
2005-10-09 21:59:54 +00:00
case ev_entity :
2005-03-28 00:11:59 +00:00
sprintf ( line , " entity %i " , NUM_FOR_EDICT ( progfuncs , ( struct edict_s * ) PROG_TO_EDICT ( progfuncs , val - > edict ) ) ) ;
2004-08-23 01:38:21 +00:00
break ;
case ev_function :
if ( ! val - > function )
sprintf ( line , " NULL function " ) ;
else
{
if ( ( val - > function & 0xff000000 ) > > 24 > = ( unsigned ) maxprogs | | ! pr_progstate [ ( val - > function & 0xff000000 ) > > 24 ] . functions )
sprintf ( line , " Bad function " ) ;
else
{
f = pr_progstate [ ( val - > function & 0xff000000 ) > > 24 ] . functions + ( val - > function & ~ 0xff000000 ) ;
2005-05-15 18:49:04 +00:00
sprintf ( line , " %i:%s() " , ( val - > function & 0xff000000 ) > > 24 , f - > s_name + progfuncs - > stringtable ) ;
2004-08-23 01:38:21 +00:00
}
}
break ;
case ev_field :
fielddef = ED_FieldAtOfs ( progfuncs , val - > _int ) ;
2006-01-11 22:22:14 +00:00
if ( ! fielddef )
sprintf ( line , " .??? (%i) " , val - > _int ) ;
else
sprintf ( line , " .%s (%i) " , fielddef - > name , val - > _int ) ;
2004-08-23 01:38:21 +00:00
break ;
case ev_void :
sprintf ( line , " void type " ) ;
break ;
case ev_float :
sprintf ( line , " %5.1f " , val - > _float ) ;
break ;
case ev_integer :
sprintf ( line , " %i " , val - > _int ) ;
break ;
case ev_vector :
2005-11-29 13:30:20 +00:00
sprintf ( line , " '%5.1f %5.1f %5.1f' " , val - > _vector [ 0 ] , val - > _vector [ 1 ] , val - > _vector [ 2 ] ) ;
2004-08-23 01:38:21 +00:00
break ;
case ev_pointer :
sprintf ( line , " pointer " ) ;
{
2005-03-28 00:11:59 +00:00
// int entnum;
// int valofs;
if ( val - > _int = = 0 )
2004-08-23 01:38:21 +00:00
{
sprintf ( line , " NULL pointer " ) ;
break ;
}
2005-03-28 00:11:59 +00:00
//FIXME: :/
sprintf ( line , " UNKNOWN " ) ;
// entnum = ((qbyte *)val->edict - (qbyte *)sv_edicts) / pr_edict_size;
// valofs = (int *)val->edict - (int *)edvars(EDICT_NUM(progfuncs, entnum));
// fielddef = ED_FieldAtOfs (progfuncs, valofs );
// if (!fielddef)
// sprintf(line, "ent%i.%s", entnum, "UNKNOWN");
// else
// sprintf(line, "ent%i.%s", entnum, fielddef->s_name);
2004-08-23 01:38:21 +00:00
}
break ;
default :
sprintf ( line , " bad type %i " , type ) ;
break ;
}
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
return line ;
}
/*
= = = = = = = = = = = =
PR_UglyValueString
Returns a string describing * data in a type specific manner
Easier to parse than PR_ValueString
= = = = = = = = = = = = =
*/
char * PR_UglyValueString ( progfuncs_t * progfuncs , etype_t type , eval_t * val )
{
static char line [ 256 ] ;
2005-10-09 21:59:54 +00:00
fdef_t * fielddef ;
2004-08-23 01:38:21 +00:00
dfunction_t * f ;
2005-07-01 19:23:00 +00:00
int i , j ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
# ifdef DEF_SAVEGLOBAL
type & = ~ DEF_SAVEGLOBAL ;
# endif
if ( pr_types )
type = pr_types [ type ] . type ;
switch ( type )
{
case ev_struct :
sprintf ( line , " structures cannot yet be saved " ) ;
2005-10-09 21:59:54 +00:00
break ;
2004-08-23 01:38:21 +00:00
case ev_union :
sprintf ( line , " unions cannot yet be saved " ) ;
break ;
case ev_string :
2005-07-01 19:23:00 +00:00
if ( ( unsigned ) val - > _int > ( unsigned ) addressableused )
_snprintf ( line , sizeof ( line ) , " CORRUPT STRING " ) ;
else
2007-03-11 16:51:45 +00:00
_snprintf ( line , sizeof ( line ) , " %s " , PR_StringToNative ( progfuncs , val - > string ) ) ;
2004-08-23 01:38:21 +00:00
break ;
2005-10-09 21:59:54 +00:00
case ev_entity :
2005-05-13 10:42:48 +00:00
sprintf ( line , " %i " , val - > _int ) ;
2004-08-23 01:38:21 +00:00
break ;
case ev_function :
2005-07-01 19:23:00 +00:00
i = ( val - > function & 0xff000000 ) > > 24 ; //progs number
2005-10-07 16:27:20 +00:00
if ( ( unsigned ) i > maxprogs | | ! pr_progstate [ ( unsigned ) i ] . progs )
2004-08-23 01:38:21 +00:00
sprintf ( line , " BAD FUNCTION INDEX: %i " , val - > function ) ;
else
{
2005-07-01 19:23:00 +00:00
j = ( val - > function & ~ 0xff000000 ) ; //function number
2005-10-07 16:27:20 +00:00
if ( ( unsigned ) j > pr_progstate [ ( unsigned ) i ] . progs - > numfunctions )
2005-07-01 19:23:00 +00:00
sprintf ( line , " %i:%s " , i , " CORRUPT FUNCTION POINTER " ) ;
else
{
2005-10-07 16:27:20 +00:00
f = pr_progstate [ ( unsigned ) i ] . functions + j ;
2005-07-01 19:23:00 +00:00
sprintf ( line , " %i:%s " , i , f - > s_name + progfuncs - > stringtable ) ;
}
2004-08-23 01:38:21 +00:00
}
break ;
case ev_field :
fielddef = ED_FieldAtOfs ( progfuncs , val - > _int ) ;
2005-05-15 18:49:04 +00:00
sprintf ( line , " %s " , fielddef - > name ) ;
2004-08-23 01:38:21 +00:00
break ;
case ev_void :
sprintf ( line , " void " ) ;
break ;
case ev_float :
if ( val - > _float = = ( int ) val - > _float )
sprintf ( line , " %i " , ( int ) val - > _float ) ; //an attempt to cut down on the number of .000000 vars..
else
sprintf ( line , " %f " , val - > _float ) ;
break ;
case ev_integer :
sprintf ( line , " %i " , val - > _int ) ;
break ;
case ev_vector :
2005-11-29 13:30:20 +00:00
if ( val - > _vector [ 0 ] = = ( int ) val - > _vector [ 0 ] & & val - > _vector [ 1 ] = = ( int ) val - > _vector [ 1 ] & & val - > _vector [ 2 ] = = ( int ) val - > _vector [ 2 ] )
sprintf ( line , " %i %i %i " , ( int ) val - > _vector [ 0 ] , ( int ) val - > _vector [ 1 ] , ( int ) val - > _vector [ 2 ] ) ;
2004-08-23 01:38:21 +00:00
else
2005-11-29 13:30:20 +00:00
sprintf ( line , " %f %f %f " , val - > _vector [ 0 ] , val - > _vector [ 1 ] , val - > _vector [ 2 ] ) ;
2004-08-23 01:38:21 +00:00
break ;
default :
sprintf ( line , " bad type %i " , type ) ;
break ;
}
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
return line ;
}
//compatable with Q1 (for savegames)
char * PR_UglyOldValueString ( progfuncs_t * progfuncs , etype_t type , eval_t * val )
{
static char line [ 256 ] ;
2005-10-09 21:59:54 +00:00
fdef_t * fielddef ;
2004-08-23 01:38:21 +00:00
dfunction_t * f ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
# ifdef DEF_SAVEGLOBAL
type & = ~ DEF_SAVEGLOBAL ;
# endif
if ( pr_types )
type = pr_types [ type ] . type ;
switch ( type )
{
case ev_struct :
sprintf ( line , " structures cannot yet be saved " ) ;
2005-10-09 21:59:54 +00:00
break ;
2004-08-23 01:38:21 +00:00
case ev_union :
sprintf ( line , " unions cannot yet be saved " ) ;
break ;
case ev_string :
2007-03-11 16:51:45 +00:00
sprintf ( line , " %s " , PR_StringToNative ( progfuncs , val - > string ) ) ;
2004-08-23 01:38:21 +00:00
break ;
2005-10-09 21:59:54 +00:00
case ev_entity :
2005-03-28 00:11:59 +00:00
sprintf ( line , " %i " , NUM_FOR_EDICT ( progfuncs , ( struct edict_s * ) PROG_TO_EDICT ( progfuncs , val - > edict ) ) ) ;
2004-08-23 01:38:21 +00:00
break ;
case ev_function :
f = pr_progstate [ ( val - > function & 0xff000000 ) > > 24 ] . functions + ( val - > function & ~ 0xff000000 ) ;
2005-05-15 18:49:04 +00:00
sprintf ( line , " %s " , f - > s_name + progfuncs - > stringtable ) ;
2004-08-23 01:38:21 +00:00
break ;
case ev_field :
fielddef = ED_FieldAtOfs ( progfuncs , val - > _int ) ;
2005-05-15 18:49:04 +00:00
sprintf ( line , " %s " , fielddef - > name ) ;
2004-08-23 01:38:21 +00:00
break ;
case ev_void :
sprintf ( line , " void " ) ;
break ;
case ev_float :
if ( val - > _float = = ( int ) val - > _float )
sprintf ( line , " %i " , ( int ) val - > _float ) ; //an attempt to cut down on the number of .000000 vars..
else
sprintf ( line , " %f " , val - > _float ) ;
break ;
case ev_integer :
sprintf ( line , " %i " , val - > _int ) ;
break ;
case ev_vector :
2005-11-29 13:30:20 +00:00
if ( val - > _vector [ 0 ] = = ( int ) val - > _vector [ 0 ] & & val - > _vector [ 1 ] = = ( int ) val - > _vector [ 1 ] & & val - > _vector [ 2 ] = = ( int ) val - > _vector [ 2 ] )
sprintf ( line , " %i %i %i " , ( int ) val - > _vector [ 0 ] , ( int ) val - > _vector [ 1 ] , ( int ) val - > _vector [ 2 ] ) ;
2004-08-23 01:38:21 +00:00
else
2005-11-29 13:30:20 +00:00
sprintf ( line , " %f %f %f " , val - > _vector [ 0 ] , val - > _vector [ 1 ] , val - > _vector [ 2 ] ) ;
2005-10-09 21:59:54 +00:00
break ;
2004-08-23 01:38:21 +00:00
break ;
default :
sprintf ( line , " bad type %i " , type ) ;
break ;
}
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
return line ;
}
char * PR_TypeString ( progfuncs_t * progfuncs , etype_t type )
{
# ifdef DEF_SAVEGLOBAL
type & = ~ DEF_SAVEGLOBAL ;
# endif
if ( pr_types )
type = pr_types [ type ] . type ;
switch ( type )
{
case ev_struct :
2005-10-09 21:59:54 +00:00
return " struct " ;
2004-08-23 01:38:21 +00:00
case ev_union :
2005-10-09 21:59:54 +00:00
return " union " ;
2004-08-23 01:38:21 +00:00
case ev_string :
return " string " ;
2005-10-09 21:59:54 +00:00
case ev_entity :
2004-08-23 01:38:21 +00:00
return " entity " ;
case ev_function :
return " function " ;
case ev_field :
return " field " ;
case ev_void :
return " void " ;
case ev_float :
return " float " ;
case ev_vector :
return " vector " ;
case ev_integer :
return " integer " ;
default :
return " BAD TYPE " ;
2005-10-09 21:59:54 +00:00
}
2004-08-23 01:38:21 +00:00
}
/*
= = = = = = = = = = = =
PR_GlobalString
Returns a string with a description and the contents of a global ,
padded to 20 field width
= = = = = = = = = = = =
*/
char * PR_GlobalString ( progfuncs_t * progfuncs , int ofs )
{
char * s ;
int i ;
ddef16_t * def16 ;
ddef32_t * def32 ;
void * val ;
static char line [ 128 ] ;
switch ( current_progstate - > intsize )
{
case 16 :
case 24 :
val = ( void * ) & pr_globals [ ofs ] ;
def16 = ED_GlobalAtOfs16 ( progfuncs , ofs ) ;
if ( ! def16 )
sprintf ( line , " %i(? " " ? " " ?) " , ofs ) ;
else
{
s = PR_ValueString ( progfuncs , def16 - > type , val ) ;
2005-05-15 18:49:04 +00:00
sprintf ( line , " %i(%s)%s " , ofs , def16 - > s_name + progfuncs - > stringtable , s ) ;
2004-08-23 01:38:21 +00:00
}
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
i = strlen ( line ) ;
for ( ; i < 20 ; i + + )
strcat ( line , " " ) ;
strcat ( line , " " ) ;
return line ;
case 32 :
val = ( void * ) & pr_globals [ ofs ] ;
def32 = ED_GlobalAtOfs32 ( progfuncs , ofs ) ;
if ( ! def32 )
sprintf ( line , " %i(? " " ? " " ?) " , ofs ) ;
else
{
s = PR_ValueString ( progfuncs , def32 - > type , val ) ;
2005-05-15 18:49:04 +00:00
sprintf ( line , " %i(%s)%s " , ofs , def32 - > s_name + progfuncs - > stringtable , s ) ;
2004-08-23 01:38:21 +00:00
}
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
i = strlen ( line ) ;
for ( ; i < 20 ; i + + )
strcat ( line , " " ) ;
strcat ( line , " " ) ;
return line ;
}
Sys_Error ( " Bad offset size in PR_GlobalString " ) ;
return " " ;
}
char * PR_GlobalStringNoContents ( progfuncs_t * progfuncs , int ofs )
{
int i ;
ddef16_t * def16 ;
ddef32_t * def32 ;
static char line [ 128 ] ;
switch ( current_progstate - > intsize )
{
case 16 :
case 24 :
def16 = ED_GlobalAtOfs16 ( progfuncs , ofs ) ;
if ( ! def16 )
sprintf ( line , " %i(? " " ? " " ?) " , ofs ) ;
else
2005-05-15 18:49:04 +00:00
sprintf ( line , " %i(%s) " , ofs , def16 - > s_name + progfuncs - > stringtable ) ;
2004-08-23 01:38:21 +00:00
break ;
case 32 :
def32 = ED_GlobalAtOfs32 ( progfuncs , ofs ) ;
if ( ! def32 )
sprintf ( line , " %i(? " " ? " " ?) " , ofs ) ;
else
2005-05-15 18:49:04 +00:00
sprintf ( line , " %i(%s) " , ofs , def32 - > s_name + progfuncs - > stringtable ) ;
2004-08-23 01:38:21 +00:00
break ;
default :
Sys_Error ( " Bad offset size in PR_GlobalStringNoContents " ) ;
}
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
i = strlen ( line ) ;
for ( ; i < 20 ; i + + )
strcat ( line , " " ) ;
strcat ( line , " " ) ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
return line ;
}
/*
= = = = = = = = = = = = =
ED_Print
For debugging
= = = = = = = = = = = = =
*/
void ED_Print ( progfuncs_t * progfuncs , struct edict_s * ed )
{
int l ;
fdef_t * d ;
int * v ;
2007-08-08 02:37:59 +00:00
unsigned int i ; unsigned int j ;
2004-08-23 01:38:21 +00:00
char * name ;
int type ;
if ( ( ( edictrun_t * ) ed ) - > isfree )
{
printf ( " FREE \n " ) ;
return ;
}
printf ( " \n EDICT %i: \n " , NUM_FOR_EDICT ( progfuncs , ( struct edict_s * ) ed ) ) ;
for ( i = 1 ; i < numfields ; i + + )
{
d = & field [ i ] ;
2005-05-15 18:49:04 +00:00
name = d - > name ;
2005-06-04 17:38:32 +00:00
l = strlen ( name ) ;
if ( l > = 2 & & name [ l - 2 ] = = ' _ ' )
2004-08-23 01:38:21 +00:00
continue ; // skip _x, _y, _z vars
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
v = ( int * ) ( ( char * ) edvars ( ed ) + d - > ofs * 4 ) ;
// if the value is still all 0, skip the field
# ifdef DEF_SAVEGLOBAL
type = d - > type & ~ DEF_SAVEGLOBAL ;
# else
type = d - > type ;
# endif
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
for ( j = 0 ; j < type_size [ type ] ; j + + )
if ( v [ j ] )
break ;
if ( j = = type_size [ type ] )
continue ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
printf ( " %s " , name ) ;
l = strlen ( name ) ;
while ( l + + < 15 )
printf ( " " ) ;
2005-10-09 21:59:54 +00:00
printf ( " %s \n " , PR_ValueString ( progfuncs , d - > type , ( eval_t * ) v ) ) ;
2004-08-23 01:38:21 +00:00
}
}
void ED_PrintNum ( progfuncs_t * progfuncs , int ent )
{
ED_Print ( progfuncs , EDICT_NUM ( progfuncs , ent ) ) ;
}
/*
= = = = = = = = = = = = =
ED_PrintEdicts
For debugging , prints all the entities in the current server
= = = = = = = = = = = = =
*/
void ED_PrintEdicts ( progfuncs_t * progfuncs )
{
2005-10-07 16:27:20 +00:00
unsigned int i ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
printf ( " %i entities \n " , sv_num_edicts ) ;
for ( i = 0 ; i < sv_num_edicts ; i + + )
ED_PrintNum ( progfuncs , i ) ;
}
/*
= = = = = = = = = = = = =
ED_Count
For debugging
= = = = = = = = = = = = =
*/
void ED_Count ( progfuncs_t * progfuncs )
{
2005-10-07 16:27:20 +00:00
unsigned int i ;
2004-08-23 01:38:21 +00:00
edictrun_t * ent ;
2005-10-07 16:27:20 +00:00
unsigned int active , models , solid , step ;
2004-08-23 01:38:21 +00:00
active = models = solid = step = 0 ;
for ( i = 0 ; i < sv_num_edicts ; i + + )
{
ent = ( edictrun_t * ) EDICT_NUM ( progfuncs , i ) ;
if ( ent - > isfree )
continue ;
active + + ;
// if (ent->v.solid)
// solid++;
// if (ent->v.model)
// models++;
// if (ent->v.movetype == MOVETYPE_STEP)
// step++;
}
printf ( " num_edicts:%3i \n " , sv_num_edicts ) ;
printf ( " active :%3i \n " , active ) ;
// Con_Printf ("view :%3i\n", models);
// Con_Printf ("touch :%3i\n", solid);
// Con_Printf ("step :%3i\n", step);
}
//============================================================================
/*
= = = = = = = = = = = = =
ED_NewString
= = = = = = = = = = = = =
*/
2005-05-17 02:36:54 +00:00
char * ED_NewString ( progfuncs_t * progfuncs , char * string , int minlength )
2004-08-23 01:38:21 +00:00
{
2006-06-02 17:42:36 +00:00
char * newc , * new_p ;
2004-08-23 01:38:21 +00:00
int i , l ;
2005-05-17 02:36:54 +00:00
minlength + + ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
l = strlen ( string ) + 1 ;
2005-08-01 16:40:23 +00:00
2006-06-02 17:42:36 +00:00
newc = PRAddressableAlloc ( progfuncs , l < minlength ? minlength : l ) ;
new_p = newc ;
2004-08-23 01:38:21 +00:00
for ( i = 0 ; i < l ; i + + )
{
2005-08-01 16:40:23 +00:00
if ( string [ i ] = = ' \\ ' & & i < l - 1 & & string [ i + 1 ] ! = 0 )
2004-08-23 01:38:21 +00:00
{
i + + ;
if ( string [ i ] = = ' n ' )
* new_p + + = ' \n ' ;
else
* new_p + + = ' \\ ' ;
}
else
* new_p + + = string [ i ] ;
}
2005-10-09 21:59:54 +00:00
2006-06-02 17:42:36 +00:00
return newc ;
2004-08-23 01:38:21 +00:00
}
/*
= = = = = = = = = = = = =
ED_ParseEval
Can parse either fields or globals
returns false if error
= = = = = = = = = = = = =
*/
pbool ED_ParseEpair ( progfuncs_t * progfuncs , void * base , ddefXX_t * key , char * s , int bits )
{
int i ;
char string [ 128 ] ;
fdef_t * def ;
char * v , * w ;
void * d ;
2005-05-15 18:49:04 +00:00
string_t st ;
2004-08-23 01:38:21 +00:00
dfunction_t * func ;
int type ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
switch ( bits )
{
case 16 :
d = ( void * ) ( ( int * ) base + ( ( ddef16_t * ) key ) - > ofs ) ;
if ( pr_types )
type = pr_types [ ( ( ddef16_t * ) key ) - > type & ~ DEF_SAVEGLOBAL ] . type ;
else
type = ( ( ddef16_t * ) key ) - > type & ~ DEF_SAVEGLOBAL ;
break ;
case 32 :
d = ( void * ) ( ( int * ) base + ( ( ddef32_t * ) key ) - > ofs ) ;
if ( pr_types )
type = pr_types [ ( ( ddef32_t * ) key ) - > type & ~ DEF_SAVEGLOBAL ] . type ;
else
type = ( ( ddef32_t * ) key ) - > type & ~ DEF_SAVEGLOBAL ;
break ;
default :
Sys_Error ( " Bad bits in ED_ParseEpair " ) ;
2005-02-06 02:47:36 +00:00
d = 0 ;
2004-08-23 01:38:21 +00:00
}
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
switch ( type )
{
case ev_string :
2007-03-11 16:51:45 +00:00
st = PR_StringToProgs ( progfuncs , ED_NewString ( progfuncs , s , 0 ) ) ;
2005-05-15 18:49:04 +00:00
* ( string_t * ) d = st ;
2004-08-23 01:38:21 +00:00
break ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
case ev_float :
* ( float * ) d = ( float ) atof ( s ) ;
break ;
case ev_integer :
* ( int * ) d = atoi ( s ) ;
break ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
case ev_vector :
strcpy ( string , s ) ;
v = string ;
w = string ;
for ( i = 0 ; i < 3 ; i + + )
{
while ( * v & & * v ! = ' ' )
v + + ;
* v = 0 ;
( ( float * ) d ) [ i ] = ( float ) atof ( w ) ;
w = v = v + 1 ;
}
break ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
case ev_entity :
2005-05-13 10:42:48 +00:00
* ( int * ) d = atoi ( s ) ;
2004-08-23 01:38:21 +00:00
break ;
case ev_field :
def = ED_FindField ( progfuncs , s ) ;
if ( ! def )
{
printf ( " Can't find field %s \n " , s ) ;
return false ;
}
2004-12-02 07:13:55 +00:00
* ( int * ) d = def - > ofs ;
2004-08-23 01:38:21 +00:00
break ;
case ev_function :
if ( s [ 1 ] = = ' : ' & & s [ 2 ] = = ' \0 ' )
{
* ( func_t * ) d = 0 ;
return true ;
}
func = ED_FindFunction ( progfuncs , s , & i , - 1 ) ;
if ( ! func )
{
printf ( " Can't find function %s \n " , s ) ;
return false ;
}
* ( func_t * ) d = ( func - pr_progstate [ i ] . functions ) | ( i < < 24 ) ;
break ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
default :
break ;
}
return true ;
}
/*
= = = = = = = = = = = = = = = = = = = =
ED_ParseEdict
Parses an edict out of the given string , returning the new position
ed should be a properly initialized empty edict .
Used for initial level load and for savegames .
= = = = = = = = = = = = = = = = = = = =
*/
# if 1
char * ED_ParseEdict ( progfuncs_t * progfuncs , char * data , edictrun_t * ent )
{
fdef_t * key ;
pbool init ;
char keyname [ 256 ] ;
int n ;
// eval_t *val;
init = false ;
// clear it
// if (ent != (edictrun_t *)sv_edicts) // hack
// memset (ent+1, 0, pr_edict_size - sizeof(edictrun_t));
// go through all the dictionary pairs
while ( 1 )
2005-10-09 21:59:54 +00:00
{
2004-08-23 01:38:21 +00:00
// parse key
data = QCC_COM_Parse ( data ) ;
if ( qcc_token [ 0 ] = = ' } ' )
break ;
if ( ! data )
Sys_Error ( " ED_ParseEntity: EOF without closing brace " ) ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
strcpy ( keyname , qcc_token ) ;
// another hack to fix heynames with trailing spaces
n = strlen ( keyname ) ;
while ( n & & keyname [ n - 1 ] = = ' ' )
{
keyname [ n - 1 ] = 0 ;
n - - ;
}
2005-10-09 21:59:54 +00:00
// parse value
2004-08-23 01:38:21 +00:00
data = QCC_COM_Parse ( data ) ;
if ( ! data )
Sys_Error ( " ED_ParseEntity: EOF without closing brace " ) ;
if ( qcc_token [ 0 ] = = ' } ' )
Sys_Error ( " ED_ParseEntity: closing brace without data " ) ;
2005-10-09 21:59:54 +00:00
init = true ;
2004-08-23 01:38:21 +00:00
// keynames with a leading underscore are used for utility comments,
// and are immediately discarded by quake
if ( keyname [ 0 ] = = ' _ ' )
continue ;
2005-10-09 21:59:54 +00:00
key = ED_FindField ( progfuncs , keyname ) ;
2004-08-23 01:38:21 +00:00
if ( ! key )
{
if ( ! strcmp ( keyname , " angle " ) ) //Quake anglehack - we've got to leave it in cos it doesn't work for quake otherwise, and this is a QuakeC lib!
{
if ( ( key = ED_FindField ( progfuncs , " angles " ) ) )
{
sprintf ( qcc_token , " 0 %f 0 " , atof ( qcc_token ) ) ; //change it from yaw to 3d angle
goto cont ;
}
}
if ( ! strcmp ( keyname , " light " ) ) //Quake lighthack - allows a field name and a classname to go by the same thing in the level editor
if ( ( key = ED_FindField ( progfuncs , " light_lev " ) ) )
goto cont ;
printf ( " '%s' is not a field \n " , keyname ) ;
continue ;
}
cont :
2005-03-28 00:11:59 +00:00
if ( ! ED_ParseEpair ( progfuncs , ent - > fields , ( ddefXX_t * ) key , qcc_token , 32 ) )
2006-01-12 22:26:32 +00:00
{
continue ;
// Sys_Error ("ED_ParseEdict: parse error on entities");
}
2004-08-23 01:38:21 +00:00
}
if ( ! init )
ent - > isfree = true ;
return data ;
}
# endif
/*
= = = = = = = = = = = = = = = =
ED_LoadFromFile
The entities are directly placed in the array , rather than allocated with
ED_Alloc , because otherwise an error loading the map would have entity
number references out of order .
Creates a server ' s entity / program execution context by
parsing textual entity definitions out of an ent file .
Used for both fresh maps and savegame loads . A fresh map would also need
to call ED_CallSpawnFunctions ( ) to let the objects initialize themselves .
= = = = = = = = = = = = = = = =
*/
char * ED_WriteGlobals ( progfuncs_t * progfuncs , char * buffer ) //switch first.
{
# define AddS(str) strcpy(buffer, str);buffer+=strlen(str);
int * v ;
ddef32_t * def32 ;
ddef16_t * def16 ;
unsigned int i ;
2007-08-08 02:37:59 +00:00
unsigned int j ;
2004-08-23 01:38:21 +00:00
char * name ;
int type ;
int curprogs = pr_typecurrent ;
int len ;
switch ( current_progstate - > intsize )
{
case 16 :
case 24 :
for ( i = 0 ; i < pr_progs - > numglobaldefs ; i + + )
{
2005-10-09 21:59:54 +00:00
def16 = & pr_globaldefs16 [ i ] ;
2005-05-15 18:49:04 +00:00
name = def16 - > s_name + progfuncs - > stringtable ;
2004-08-23 01:38:21 +00:00
len = strlen ( name ) ;
if ( ! * name )
continue ;
if ( name [ len - 2 ] = = ' _ ' & & ( name [ len - 1 ] = = ' x ' | | name [ len - 1 ] = = ' y ' | | name [ len - 1 ] = = ' z ' ) )
continue ; // skip _x, _y, _z vars (vector componants, which are saved as one vector not 3 floats)
type = def16 - > type ;
# ifdef DEF_SAVEGLOBAL
if ( ! ( def16 - > type & DEF_SAVEGLOBAL ) )
continue ;
type & = ~ DEF_SAVEGLOBAL ;
# endif
if ( current_progstate - > types )
type = current_progstate - > types [ type ] . type ;
if ( type = = ev_function )
{
v = ( int * ) & current_progstate - > globals [ def16 - > ofs ] ;
if ( ( v [ 0 ] & 0xff000000 ) > > 24 = = ( unsigned ) curprogs ) //same progs
{
2005-05-15 18:49:04 +00:00
if ( ! progfuncs - > stringtable [ current_progstate - > functions [ v [ 0 ] & 0x00ffffff ] . s_name ] )
2004-08-23 01:38:21 +00:00
continue ;
2005-05-15 18:49:04 +00:00
else if ( ! strcmp ( current_progstate - > functions [ v [ 0 ] & 0x00ffffff ] . s_name + progfuncs - > stringtable , name ) ) //names match. Assume function is at initial value.
2004-08-23 01:38:21 +00:00
continue ;
}
if ( curprogs ! = 0 )
if ( ( v [ 0 ] & 0xff000000 ) > > 24 = = 0 )
if ( ! ED_FindFunction ( progfuncs , name , NULL , curprogs ) ) //defined as extern
{
2005-05-15 18:49:04 +00:00
if ( ! progfuncs - > stringtable [ pr_progstate [ 0 ] . functions [ v [ 0 ] & 0x00ffffff ] . s_name ] )
2004-08-23 01:38:21 +00:00
continue ;
2005-05-15 18:49:04 +00:00
else if ( ! strcmp ( pr_progstate [ 0 ] . functions [ v [ 0 ] & 0x00ffffff ] . s_name + progfuncs - > stringtable , name ) ) //same name.
2004-08-23 01:38:21 +00:00
continue ;
}
//else function has been redirected externally.
goto add16 ;
}
else if ( type ! = ev_string //anything other than these is not saved
& & type ! = ev_float
& & type ! = ev_integer
& & type ! = ev_entity
& & type ! = ev_vector )
continue ;
v = ( int * ) & current_progstate - > globals [ def16 - > ofs ] ;
// make sure the value is not null, where there's no point in saving
for ( j = 0 ; j < type_size [ type ] ; j + + )
if ( v [ j ] )
break ;
if ( j = = type_size [ type ] )
continue ;
add16 :
AddS ( qcva ( " \" %s \" " , name ) ) ;
AddS ( qcva ( " \" %s \" \n " , PR_UglyValueString ( progfuncs , def16 - > type & ~ DEF_SAVEGLOBAL , ( eval_t * ) v ) ) ) ;
}
break ;
case 32 :
for ( i = 0 ; i < pr_progs - > numglobaldefs ; i + + )
{
2005-10-09 21:59:54 +00:00
def32 = & pr_globaldefs32 [ i ] ;
2005-05-15 18:49:04 +00:00
name = def32 - > s_name + progfuncs - > stringtable ;
2004-08-23 01:38:21 +00:00
if ( name [ strlen ( name ) - 2 ] = = ' _ ' )
continue ; // skip _x, _y, _z vars (vector componants, which are saved as one vector not 3 floats)
type = def32 - > type ;
# ifdef DEF_SAVEGLOBAL
if ( ! ( def32 - > type & DEF_SAVEGLOBAL ) )
continue ;
type & = ~ DEF_SAVEGLOBAL ;
# endif
if ( current_progstate - > types )
type = current_progstate - > types [ type ] . type ;
if ( type = = ev_function )
{
v = ( int * ) & current_progstate - > globals [ def32 - > ofs ] ;
if ( ( v [ 0 ] & 0xff000000 ) > > 24 = = ( unsigned ) curprogs ) //same progs
2005-05-15 18:49:04 +00:00
if ( ! strcmp ( current_progstate - > functions [ v [ 0 ] & 0x00ffffff ] . s_name + progfuncs - > stringtable , name ) ) //names match. Assume function is at initial value.
2005-10-09 21:59:54 +00:00
continue ;
2004-08-23 01:38:21 +00:00
if ( curprogs ! = 0 )
if ( ( v [ 0 ] & 0xff000000 ) > > 24 = = 0 )
if ( ! ED_FindFunction ( progfuncs , name , NULL , curprogs ) ) //defined as extern
2005-05-15 18:49:04 +00:00
if ( ! strcmp ( pr_progstate [ 0 ] . functions [ v [ 0 ] & 0x00ffffff ] . s_name + progfuncs - > stringtable , name ) ) //same name.
2004-08-23 01:38:21 +00:00
continue ;
//else function has been redirected externally.
goto add32 ;
}
else if ( type ! = ev_string //anything other than these is not saved
& & type ! = ev_float
& & type ! = ev_integer
& & type ! = ev_entity
& & type ! = ev_vector )
continue ;
v = ( int * ) & current_progstate - > globals [ def32 - > ofs ] ;
// make sure the value is not null, where there's no point in saving
for ( j = 0 ; j < type_size [ type ] ; j + + )
if ( v [ j ] )
break ;
if ( j = = type_size [ type ] )
continue ;
add32 :
AddS ( qcva ( " \" %s \" " , name ) ) ;
AddS ( qcva ( " \" %s \" \n " , PR_UglyValueString ( progfuncs , def32 - > type & ~ DEF_SAVEGLOBAL , ( eval_t * ) v ) ) ) ;
}
break ;
default :
Sys_Error ( " Bad number of bits in SaveEnts " ) ;
}
return buffer ;
}
char * ED_WriteEdict ( progfuncs_t * progfuncs , edictrun_t * ed , char * buffer , pbool q1compatable )
{
fdef_t * d ;
int * v ;
2007-08-08 02:37:59 +00:00
unsigned int i ; unsigned int j ;
2004-08-23 01:38:21 +00:00
char * name ;
int type ;
int len ;
for ( i = 0 ; i < numfields ; i + + )
{
d = & field [ i ] ;
2005-05-15 18:49:04 +00:00
name = d - > name ;
2004-08-23 01:38:21 +00:00
len = strlen ( name ) ;
2005-05-15 18:49:04 +00:00
if ( len > 4 & & ( name [ len - 2 ] = = ' _ ' & & ( name [ len - 1 ] = = ' x ' | | name [ len - 1 ] = = ' y ' | | name [ len - 1 ] = = ' z ' ) ) )
2004-08-23 01:38:21 +00:00
continue ; // skip _x, _y, _z vars
2005-10-09 21:59:54 +00:00
2005-03-28 00:11:59 +00:00
v = ( int * ) ( ( char * ) ed - > fields + d - > ofs * 4 ) ;
2004-08-23 01:38:21 +00:00
// if the value is still all 0, skip the field
# ifdef DEF_SAVEGLOBAL
type = d - > type & ~ DEF_SAVEGLOBAL ;
# else
type = d - > type ;
# endif
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
for ( j = 0 ; j < type_size [ type ] ; j + + )
if ( v [ j ] )
break ;
if ( j = = type_size [ type ] )
continue ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
//add it to the file
AddS ( qcva ( " \" %s \" " , name ) ) ;
AddS ( qcva ( " \" %s \" \n " , ( q1compatable ? PR_UglyOldValueString : PR_UglyValueString ) ( progfuncs , d - > type , ( eval_t * ) v ) ) ) ;
}
return buffer ;
# undef AddS
}
2004-12-08 04:14:52 +00:00
char * SaveCallStack ( progfuncs_t * progfuncs , char * s )
{
# define AddS(str) strcpy(s, str);s+=strlen(str);
char buffer [ 8192 ] ;
dfunction_t * f ;
int i ;
int progs ;
int arg ;
int * globalbase ;
progs = - 1 ;
if ( pr_depth = = 0 )
{
AddS ( " <NO STACK> \n " ) ;
return s ;
}
globalbase = ( int * ) pr_globals + pr_xfunction - > parm_start + pr_xfunction - > locals ;
pr_stack [ pr_depth ] . f = pr_xfunction ;
for ( i = pr_depth ; i > 0 ; i - - )
{
f = pr_stack [ i ] . f ;
2005-10-09 21:59:54 +00:00
2004-12-08 04:14:52 +00:00
if ( ! f )
{
AddS ( " <NO FUNCTION> \n " ) ;
}
else
{
if ( pr_stack [ i ] . progsnum ! = progs )
{
progs = pr_stack [ i ] . progsnum ;
sprintf ( buffer , " //%i %s \n " , progs , pr_progstate [ progs ] . filename ) ;
AddS ( buffer ) ;
}
2005-05-15 18:49:04 +00:00
if ( ! f - > s_file )
sprintf ( buffer , " \t \" %i:%s \" \n " , progs , f - > s_name + progfuncs - > stringtable ) ;
2004-12-08 04:14:52 +00:00
else
2005-05-15 18:49:04 +00:00
sprintf ( buffer , " \t \" %i:%s \" //%s \n " , progs , f - > s_name + progfuncs - > stringtable , f - > s_file + progfuncs - > stringtable ) ;
2004-12-08 04:14:52 +00:00
AddS ( buffer ) ;
AddS ( " \t { \n " ) ;
for ( arg = 0 ; arg < f - > locals ; arg + + )
{
ddef16_t * local ;
local = ED_GlobalAtOfs16 ( progfuncs , f - > parm_start + arg ) ;
if ( ! local )
sprintf ( buffer , " \t \t ofs%i %i // %f \n " , f - > parm_start + arg , * ( int * ) ( globalbase - f - > locals + arg ) , * ( float * ) ( globalbase - f - > locals + arg ) ) ;
else
{
if ( local - > type = = ev_entity )
2005-09-14 04:36:07 +00:00
{
sprintf ( buffer , " \t \t \" %s \" \" entity %i \" \n " , local - > s_name + progfuncs - > stringtable , ( ( eval_t * ) ( globalbase - f - > locals + arg ) ) - > edict ) ;
2004-12-08 04:14:52 +00:00
}
else
2005-05-15 18:49:04 +00:00
sprintf ( buffer , " \t \t \" %s \" \t \" %s \" \n " , local - > s_name + progfuncs - > stringtable , PR_ValueString ( progfuncs , local - > type , ( eval_t * ) ( globalbase - f - > locals + arg ) ) ) ;
2005-09-14 04:36:07 +00:00
2004-12-08 04:14:52 +00:00
if ( local - > type = = ev_vector )
arg + = 2 ;
}
AddS ( buffer ) ;
}
AddS ( " \t } \n " ) ;
if ( i = = pr_depth )
globalbase = localstack + localstack_used - f - > locals ;
else
globalbase - = f - > locals ;
}
}
return s ;
# undef AddS
}
2004-08-23 01:38:21 +00:00
//there are two ways of saving everything.
//0 is to save just the entities.
//1 is to save the entites, and all the progs info so that all the variables are saved off, and it can be reloaded to exactly how it was (provided no files or data has been changed outside, like the progs.dat for example)
char * SaveEnts ( progfuncs_t * progfuncs , char * mem , int * len , int alldata )
{
# define AddS(str) strcpy(s, str);s+=strlen(str);
char * s , * os ;
2005-10-07 16:27:20 +00:00
unsigned int a ;
2004-08-23 01:38:21 +00:00
int oldprogs ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
if ( mem )
{
os = s = mem ;
}
else
os = s = memalloc ( 5 * 1024 * 1024 ) ;
if ( alldata = = 2 )
{ //special Q1 savegame compatability mode.
//engine will need to store references to progs type and will need to preload the progs and inti the ents itself before loading.
//Make sure there is only 1 progs loaded.
for ( a = 1 ; a < maxprogs ; a + + )
{
if ( pr_progstate [ a ] . progs )
break ;
}
if ( ! pr_progstate [ 0 ] . progs | | a ! = maxprogs ) //the state of the progs wasn't Q1 compatable.
{
memfree ( os ) ;
return NULL ;
}
//write the globals
AddS ( " { \n " ) ;
oldprogs = pr_typecurrent ;
PR_SwitchProgs ( progfuncs , 0 ) ;
s = ED_WriteGlobals ( progfuncs , s ) ;
PR_SwitchProgs ( progfuncs , oldprogs ) ;
AddS ( " } \n " ) ;
//write the ents
for ( a = 0 ; a < sv_num_edicts ; a + + )
{
edictrun_t * ed = ( edictrun_t * ) EDICT_NUM ( progfuncs , a ) ;
AddS ( " { \n " ) ;
if ( ! ed - > isfree )
s = ED_WriteEdict ( progfuncs , ed , s , true ) ;
AddS ( " } \n " ) ;
}
* len = s - os ;
return os ;
}
if ( alldata )
{
AddS ( " general { \n " ) ;
AddS ( qcva ( " \" maxprogs \" \" %i \" \n " , maxprogs ) ) ;
// AddS(qcva("\"maxentities\" \"%i\"\n", maxedicts));
// AddS(qcva("\"mem\" \"%i\"\n", hunksize));
// AddS(qcva("\"crc\" \"%i\"\n", header_crc));
AddS ( qcva ( " \" numentities \" \" %i \" \n " , sv_num_edicts ) ) ;
AddS ( " } \n " ) ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
oldprogs = pr_typecurrent ;
for ( a = 0 ; a < maxprogs ; a + + )
{
if ( ! pr_progstate [ a ] . progs )
continue ;
PR_SwitchProgs ( progfuncs , a ) ;
{
AddS ( qcva ( " progs %i { \n " , a ) ) ;
AddS ( qcva ( " \" filename \" \" %s \" \n " , pr_progstate [ a ] . filename ) ) ;
AddS ( qcva ( " \" crc \" \" %i \" \n " , pr_progs - > crc ) ) ;
AddS ( qcva ( " \" numbuiltins \" \" %i \" \n " , current_progstate - > numbuiltins ) ) ;
AddS ( " } \n " ) ;
}
}
2004-12-08 04:14:52 +00:00
if ( alldata = = 3 )
{
//include callstack
AddS ( " stacktrace { \n " ) ;
s = SaveCallStack ( progfuncs , s ) ;
AddS ( " } \n " ) ;
}
2004-08-23 01:38:21 +00:00
for ( a = 0 ; a < maxprogs ; a + + ) //I would mix, but external functions rely on other progs being loaded
{
if ( ! pr_progstate [ a ] . progs )
continue ;
AddS ( qcva ( " globals %i { \n " , a ) ) ;
PR_SwitchProgs ( progfuncs , a ) ;
s = ED_WriteGlobals ( progfuncs , s ) ;
AddS ( " } \n " ) ;
}
PR_SwitchProgs ( progfuncs , oldprogs ) ;
}
for ( a = 0 ; a < sv_num_edicts ; a + + )
{
edictrun_t * ed = ( edictrun_t * ) EDICT_NUM ( progfuncs , a ) ;
if ( ed - > isfree )
continue ;
AddS ( qcva ( " entity %i{ \n " , a ) ) ;
s = ED_WriteEdict ( progfuncs , ed , s , false ) ;
AddS ( " } \n " ) ;
}
* len = s - os ;
return os ;
# undef AddS
}
int header_crc ;
//if 'general' block is found, this is a compleate state, otherwise, we should spawn entities like
int LoadEnts ( progfuncs_t * progfuncs , char * file , float killonspawnflags )
{
eval_t * fulldata ; //this is part of FTE_FULLSPAWNDATA
char * datastart ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
eval_t * var ;
char filename [ 128 ] ;
2005-10-09 21:59:54 +00:00
int num ;
int numbuiltins ;
2004-08-23 01:38:21 +00:00
edictrun_t * ed = NULL ;
ddef16_t * d16 ;
ddef32_t * d32 ;
func_t CheckSpawn = 0 ;
2005-06-14 04:52:10 +00:00
extern edictrun_t tempedict ;
2004-08-23 01:38:21 +00:00
int crc = 1 ;
int entsize = 0 ;
int numents = 0 ;
pbool resethunk = 0 ;
pbool isloadgame ;
if ( ! strncmp ( file , " loadgame " , 8 ) )
{
isloadgame = true ;
numents = - 1 ;
file + = 8 ;
fulldata = NULL ;
}
else
{
isloadgame = false ;
2004-11-13 17:32:03 +00:00
if ( pr_typecurrent > = 0 )
num = ED_FindGlobalOfs ( progfuncs , " __fullspawndata " ) ;
else
num = 0 ;
2004-08-23 01:38:21 +00:00
if ( num )
fulldata = ( eval_t * ) ( ( int * ) pr_globals + num ) ;
else
fulldata = NULL ;
}
while ( 1 )
{
datastart = file ;
file = QCC_COM_Parse ( file ) ;
if ( file = = NULL )
break ; //finished reading file
else if ( ! strcmp ( qcc_token , " entity " ) )
{
if ( entsize = = 0 & & resethunk ) //edicts have not yet been initialized, and this is a compleate load (memsize has been set)
{
entsize = PR_InitEnts ( progfuncs , maxedicts ) ;
// sv_num_edicts = numents;
for ( num = 0 ; num < numents ; num + + )
{
ed = ( edictrun_t * ) EDICT_NUM ( progfuncs , num ) ;
2004-09-20 23:25:38 +00:00
2004-08-23 01:38:21 +00:00
if ( ! ed )
{
2005-03-28 00:11:59 +00:00
prinst - > edicttable [ num ] = * ( struct edict_s * * ) & ed = ( void * ) memalloc ( externs - > edictsize ) ;
ed - > fields = PRAddressableAlloc ( progfuncs , fields_size ) ;
2004-08-23 01:38:21 +00:00
ed - > entnum = num ;
ed - > isfree = true ;
}
}
}
file = QCC_COM_Parse ( file ) ;
num = atoi ( qcc_token ) ;
file = QCC_COM_Parse ( file ) ;
if ( qcc_token [ 0 ] ! = ' { ' )
Sys_Error ( " Progs loading found %s, not '{' " , qcc_token ) ;
if ( ! resethunk )
ed = ( edictrun_t * ) ED_Alloc ( progfuncs ) ;
else
{
ed = ( edictrun_t * ) EDICT_NUM ( progfuncs , num ) ;
2004-09-20 23:25:38 +00:00
2004-08-23 01:38:21 +00:00
if ( ! ed )
{
Sys_Error ( " Edict was not allocated \n " ) ;
2005-03-28 00:11:59 +00:00
prinst - > edicttable [ num ] = * ( struct edict_s * * ) & ed = ( void * ) memalloc ( externs - > edictsize ) ;
ed - > fields = PRAddressableAlloc ( progfuncs , fields_size ) ;
2005-05-17 02:36:54 +00:00
ED_ClearEdict ( progfuncs , ed ) ;
2004-08-23 01:38:21 +00:00
ed - > entnum = num ;
}
}
ed - > isfree = false ;
file = ED_ParseEdict ( progfuncs , file , ed ) ;
if ( killonspawnflags )
{
2004-09-20 23:25:38 +00:00
var = GetEdictFieldValue ( progfuncs , ( struct edict_s * ) & ed , " spawnflags " , & spawnflagscache ) ;
2004-08-23 01:38:21 +00:00
if ( var )
{
if ( ( int ) var - > _float & ( int ) killonspawnflags )
{
ed - > isfree = true ;
continue ;
}
}
}
if ( ! resethunk )
{
dfunction_t * f ;
if ( ( var = GetEdictFieldValue ( progfuncs , ( struct edict_s * ) ed , " classname " , NULL ) ) )
{
2005-05-15 18:49:04 +00:00
f = ED_FindFunction ( progfuncs , var - > string + progfuncs - > stringtable , NULL , - 1 ) ;
2004-08-23 01:38:21 +00:00
if ( f )
{
var = ( eval_t * ) ( ( int * ) pr_globals + ED_FindGlobalOfs ( progfuncs , " self " ) ) ;
2005-03-28 00:11:59 +00:00
var - > edict = EDICT_TO_PROG ( progfuncs , ed ) ;
2004-08-23 01:38:21 +00:00
PR_ExecuteProgram ( progfuncs , f - pr_functions ) ;
}
}
}
}
else if ( ! strcmp ( qcc_token , " progs " ) )
{
file = QCC_COM_Parse ( file ) ;
num = atoi ( qcc_token ) ;
2005-10-09 21:59:54 +00:00
file = QCC_COM_Parse ( file ) ;
2004-08-23 01:38:21 +00:00
if ( qcc_token [ 0 ] ! = ' { ' )
Sys_Error ( " Progs loading found %s, not '{' " , qcc_token ) ;
2005-10-09 21:59:54 +00:00
filename [ 0 ] = ' \0 ' ;
header_crc = 0 ;
2004-08-23 01:38:21 +00:00
numbuiltins = 0 ;
while ( 1 )
{
file = QCC_COM_Parse ( file ) ; //read the key
if ( ! file )
Sys_Error ( " EOF in progs block " ) ;
if ( ! strcmp ( " filename " , qcc_token ) ) //check key get and save values
{ file = QCC_COM_Parse ( file ) ; strcpy ( filename , qcc_token ) ; }
else if ( ! strcmp ( " crc " , qcc_token ) )
{ file = QCC_COM_Parse ( file ) ; header_crc = atoi ( qcc_token ) ; }
else if ( ! strcmp ( " numbuiltins " , qcc_token ) )
{ file = QCC_COM_Parse ( file ) ; numbuiltins = atoi ( qcc_token ) ; }
else if ( qcc_token [ 0 ] = = ' } ' ) //end of block
break ;
else
Sys_Error ( " Bad key \" %s \" in progs block " , qcc_token ) ;
}
PR_ReallyLoadProgs ( progfuncs , filename , header_crc , & pr_progstate [ num ] , true ) ;
if ( ! externs - > builtinsfor )
{
// Sys_Error("Couldn't reset the builtin functions");
current_progstate - > builtins = NULL ; //these are specific, we assume the global ones were set via pr_configure
current_progstate - > numbuiltins = 0 ;
}
else
{
current_progstate - > builtins = externs - > builtinsfor ( num , header_crc ) ;
current_progstate - > numbuiltins = numbuiltins ;
}
}
else if ( ! strcmp ( qcc_token , " globals " ) )
{
if ( entsize = = 0 & & resethunk ) //by the time we parse some globals, we MUST have loaded all progs
{
entsize = PR_InitEnts ( progfuncs , maxedicts ) ;
// sv_num_edicts = numents;
for ( num = 0 ; num < numents ; num + + )
{
ed = ( edictrun_t * ) EDICT_NUM ( progfuncs , num ) ;
2004-09-20 23:25:38 +00:00
2004-08-23 01:38:21 +00:00
if ( ! ed )
{
2005-03-28 00:11:59 +00:00
prinst - > edicttable [ num ] = * ( struct edict_s * * ) & ed = ( void * ) memalloc ( externs - > edictsize ) ;
ed - > fields = PRAddressableAlloc ( progfuncs , fields_size ) ;
2005-05-17 02:36:54 +00:00
ED_ClearEdict ( progfuncs , ed ) ;
2004-08-23 01:38:21 +00:00
ed - > entnum = num ;
ed - > isfree = true ;
}
}
}
file = QCC_COM_Parse ( file ) ;
num = atoi ( qcc_token ) ;
file = QCC_COM_Parse ( file ) ;
if ( qcc_token [ 0 ] ! = ' { ' )
Sys_Error ( " Globals loading found \' %s \' , not '{' " , qcc_token ) ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
PR_SwitchProgs ( progfuncs , num ) ;
while ( 1 )
2005-10-09 21:59:54 +00:00
{
2004-08-23 01:38:21 +00:00
file = QCC_COM_Parse ( file ) ;
if ( qcc_token [ 0 ] = = ' } ' )
break ;
else if ( ! qcc_token [ 0 ] | | ! file )
Sys_Error ( " EOF when parsing global values " ) ;
switch ( current_progstate - > intsize )
{
case 16 :
case 24 :
if ( ! ( d16 = ED_FindGlobal16 ( progfuncs , qcc_token ) ) )
{
file = QCC_COM_Parse ( file ) ;
printf ( " global value %s not found " , qcc_token ) ;
}
else
{
file = QCC_COM_Parse ( file ) ;
ED_ParseEpair ( progfuncs , pr_globals , ( ddefXX_t * ) d16 , qcc_token , 16 ) ;
}
break ;
case 32 :
if ( ! ( d32 = ED_FindGlobal32 ( progfuncs , qcc_token ) ) )
{
file = QCC_COM_Parse ( file ) ;
printf ( " global value %s not found " , qcc_token ) ;
}
else
{
file = QCC_COM_Parse ( file ) ;
ED_ParseEpair ( progfuncs , pr_globals , ( ddefXX_t * ) d32 , qcc_token , 32 ) ;
}
break ;
default :
Sys_Error ( " Bad intsize in LoadEnts " ) ;
}
}
// file = QCC_COM_Parse(file);
// if (com_token[0] != '}')
2005-10-09 21:59:54 +00:00
// Sys_Error("Progs loading found %s, not '}'", qcc_token);
}
2004-08-23 01:38:21 +00:00
else if ( ! strcmp ( qcc_token , " general " ) )
{
QC_StartShares ( progfuncs ) ;
// QC_InitShares(); //forget stuff
// pr_edict_size = 0;
2005-03-28 00:11:59 +00:00
max_fields_size = 0 ;
2004-08-23 01:38:21 +00:00
file = QCC_COM_Parse ( file ) ;
if ( qcc_token [ 0 ] ! = ' { ' )
Sys_Error ( " Progs loading found %s, not '{' " , qcc_token ) ;
while ( 1 )
{
file = QCC_COM_Parse ( file ) ; //read the key
if ( ! file )
Sys_Error ( " EOF in general block " ) ;
if ( ! strcmp ( " maxprogs " , qcc_token ) ) //check key get and save values
{ file = QCC_COM_Parse ( file ) ; maxprogs = atoi ( qcc_token ) ; }
// else if (!strcmp("maxentities", com_token))
// {file = QCC_COM_Parse(file); maxedicts = atoi(qcc_token);}
// else if (!strcmp("mem", com_token))
// {file = QCC_COM_Parse(file); memsize = atoi(qcc_token);}
// else if (!strcmp("crc", com_token))
// {file = QCC_COM_Parse(file); crc = atoi(qcc_token);}
else if ( ! strcmp ( " numentities " , qcc_token ) )
{ file = QCC_COM_Parse ( file ) ; numents = atoi ( qcc_token ) ; }
else if ( qcc_token [ 0 ] = = ' } ' ) //end of block
break ;
else
Sys_Error ( " Bad key \" %s \" in general block " , qcc_token ) ;
}
2005-10-09 21:59:54 +00:00
2005-03-28 00:11:59 +00:00
PRAddressableFlush ( progfuncs , - 1 ) ;
2004-08-23 01:38:21 +00:00
resethunk = true ;
pr_progstate = PRHunkAlloc ( progfuncs , sizeof ( progstate_t ) * maxprogs ) ;
pr_typecurrent = 0 ;
sv_num_edicts = 1 ; //set up a safty buffer so things won't go horribly wrong too often
2005-06-14 04:52:10 +00:00
sv_edicts = ( struct edict_s * ) & tempedict ;
prinst - > edicttable = & sv_edicts ;
2004-08-23 01:38:21 +00:00
sv_num_edicts = numents ; //should be fine
// PR_Configure(crc, NULL, memsize, maxedicts, maxprogs);
2005-10-09 21:59:54 +00:00
}
2004-08-23 01:38:21 +00:00
else if ( ! strcmp ( qcc_token , " { " ) )
{
if ( isloadgame )
{
if ( numents = = - 1 ) //globals
{
while ( 1 )
2005-10-09 21:59:54 +00:00
{
2004-08-23 01:38:21 +00:00
file = QCC_COM_Parse ( file ) ;
if ( qcc_token [ 0 ] = = ' } ' )
break ;
else if ( ! qcc_token [ 0 ] | | ! file )
Sys_Error ( " EOF when parsing global values " ) ;
switch ( current_progstate - > intsize )
{
case 16 :
case 24 :
if ( ! ( d16 = ED_FindGlobal16 ( progfuncs , qcc_token ) ) )
{
file = QCC_COM_Parse ( file ) ;
printf ( " global value %s not found " , qcc_token ) ;
}
else
{
file = QCC_COM_Parse ( file ) ;
ED_ParseEpair ( progfuncs , pr_globals , ( ddefXX_t * ) d16 , qcc_token , 16 ) ;
}
break ;
case 32 :
if ( ! ( d32 = ED_FindGlobal32 ( progfuncs , qcc_token ) ) )
{
file = QCC_COM_Parse ( file ) ;
printf ( " global value %s not found " , qcc_token ) ;
}
else
{
file = QCC_COM_Parse ( file ) ;
ED_ParseEpair ( progfuncs , pr_globals , ( ddefXX_t * ) d32 , qcc_token , 32 ) ;
}
break ;
default :
Sys_Error ( " Bad intsize in LoadEnts " ) ;
}
}
}
else
{
ed = ( edictrun_t * ) EDICT_NUM ( progfuncs , numents ) ;
if ( ! ed )
{
2005-03-28 00:11:59 +00:00
prinst - > edicttable [ numents ] = * ( struct edict_s * * ) & ed = ( void * ) memalloc ( externs - > edictsize ) ;
ed - > fields = PRAddressableAlloc ( progfuncs , fields_size ) ;
2005-05-17 02:36:54 +00:00
ED_ClearEdict ( progfuncs , ed ) ;
2004-08-23 01:38:21 +00:00
ed - > entnum = numents ;
ed - > isfree = true ;
}
2004-09-20 23:25:38 +00:00
2004-08-23 01:38:21 +00:00
sv_num_edicts = numents ;
ed - > isfree = false ;
file = ED_ParseEdict ( progfuncs , file , ed ) ;
}
numents + + ;
continue ;
}
if ( entsize = = 0 & & resethunk ) //edicts have not yet been initialized, and this is a compleate load (memsize has been set)
{
entsize = PR_InitEnts ( progfuncs , maxedicts ) ;
// sv_num_edicts = numents;
for ( num = 0 ; num < numents ; num + + )
{
ed = ( edictrun_t * ) EDICT_NUM ( progfuncs , num ) ;
2004-09-20 23:25:38 +00:00
2004-08-23 01:38:21 +00:00
if ( ! ed )
{
2005-03-28 00:11:59 +00:00
prinst - > edicttable [ num ] = * ( struct edict_s * * ) & ed = ( void * ) memalloc ( externs - > edictsize ) ;
ed - > fields = PRAddressableAlloc ( progfuncs , fields_size ) ;
2005-05-17 02:36:54 +00:00
ED_ClearEdict ( progfuncs , ed ) ;
2004-08-23 01:38:21 +00:00
ed - > entnum = num ;
ed - > isfree = true ;
}
}
}
if ( ! ed ) //first entity
ed = ( edictrun_t * ) EDICT_NUM ( progfuncs , 0 ) ;
else
ed = ( edictrun_t * ) ED_Alloc ( progfuncs ) ;
ed - > i sfree = false ;
file = ED_ParseEdict ( progfuncs , file , ed ) ;
if ( killonspawnflags )
{
var = GetEdictFieldValue ( progfuncs , ( struct edict_s * ) ed , " spawnflags " , & spawnflagscache ) ;
if ( var )
{
if ( ( int ) var - > _float & ( int ) killonspawnflags )
{
ed - > isfree = true ;
ed - > freetime = 0 ;
continue ;
}
}
}
if ( ! resethunk )
2005-10-09 21:59:54 +00:00
{
2004-08-23 01:38:21 +00:00
func_t f ;
if ( ! CheckSpawn )
CheckSpawn = PR_FindFunc ( progfuncs , " CheckSpawn " , - 2 ) ;
var = GetEdictFieldValue ( progfuncs , ( struct edict_s * ) ed , " classname " , NULL ) ;
2007-03-11 16:51:45 +00:00
if ( ! var | | ! var - > string | | ! * PR_StringToNative ( progfuncs , var - > string ) )
2004-08-23 01:38:21 +00:00
{
printf ( " No classname \n " ) ;
ED_Free ( progfuncs , ( struct edict_s * ) ed ) ;
}
else
{
2004-11-06 14:27:53 +00:00
eval_t * selfvar ;
2004-08-23 01:38:21 +00:00
//added by request of Mercury.
if ( fulldata ) //this is a vital part of HL map support!!!
{ //essentually, it passes the ent's spawn info to the ent.
2005-10-09 21:59:54 +00:00
char * nl ; //otherwise it sees only the named fields of
2004-08-23 01:38:21 +00:00
char * spawndata ; //a standard quake ent.
spawndata = PRHunkAlloc ( progfuncs , file - datastart + 1 ) ;
strncpy ( spawndata , datastart , file - datastart ) ;
spawndata [ file - datastart ] = ' \0 ' ;
for ( nl = spawndata ; * nl ; nl + + )
if ( * nl = = ' \n ' )
* nl = ' \t ' ;
fulldata - > string = spawndata - progfuncs - > stringtable ;
}
2004-11-06 14:27:53 +00:00
selfvar = ( eval_t * ) ( ( int * ) pr_globals + ED_FindGlobalOfs ( progfuncs , " self " ) ) ;
2005-03-28 00:11:59 +00:00
selfvar - > edict = EDICT_TO_PROG ( progfuncs , ed ) ;
2004-11-06 14:27:53 +00:00
2007-03-11 16:51:45 +00:00
f = PR_FindFunc ( progfuncs , PR_StringToNative ( progfuncs , var - > string ) , PR_ANYBACK ) ;
2004-08-23 01:38:21 +00:00
if ( f )
{
if ( CheckSpawn )
{
G_INT ( OFS_PARM0 ) = f ;
PR_ExecuteProgram ( progfuncs , CheckSpawn ) ;
//call the spawn func or remove.
}
else
PR_ExecuteProgram ( progfuncs , f ) ;
}
else if ( CheckSpawn )
{
G_INT ( OFS_PARM0 ) = 0 ;
PR_ExecuteProgram ( progfuncs , CheckSpawn ) ;
//the mod is responsible for freeing unrecognised ents.
}
else
{
2007-03-11 16:51:45 +00:00
printf ( " Couldn't find spawn function %s \n " , PR_StringToNative ( progfuncs , var - > string ) ) ;
2004-08-23 01:38:21 +00:00
ED_Free ( progfuncs , ( struct edict_s * ) ed ) ;
}
}
}
}
else
Sys_Error ( " Command %s not recognised " , qcc_token ) ;
}
if ( resethunk )
{
2005-10-09 21:59:54 +00:00
header_crc = crc ;
2004-08-23 01:38:21 +00:00
if ( externs - > loadcompleate )
externs - > loadcompleate ( entsize ) ;
sv_num_edicts = numents ;
}
if ( resethunk )
{
return entsize ;
}
else
2005-03-28 00:11:59 +00:00
return max_fields_size ;
2004-08-23 01:38:21 +00:00
}
# define AddS(str) strcpy(s, str);s+=strlen(str);
char * SaveEnt ( progfuncs_t * progfuncs , char * buf , int * size , struct edict_s * ed )
{
fdef_t * d ;
int * v ;
2007-08-08 02:37:59 +00:00
unsigned int i ; unsigned int j ;
2004-08-23 01:38:21 +00:00
char * name ;
2005-10-09 21:59:54 +00:00
int type ;
2004-08-23 01:38:21 +00:00
char * s = buf ;
// if (ed->free)
// continue;
AddS ( " { \n " ) ;
for ( i = 0 ; i < numfields ; i + + )
{
2005-12-22 20:35:04 +00:00
int len ;
2004-08-23 01:38:21 +00:00
d = & field [ i ] ;
2005-05-15 18:49:04 +00:00
name = d - > name ;
2005-12-22 20:35:04 +00:00
len = strlen ( name ) ; // should we skip vars with no name?
if ( len > 2 & & name [ len - 2 ] = = ' _ ' )
2004-08-23 01:38:21 +00:00
continue ; // skip _x, _y, _z vars
2005-10-09 21:59:54 +00:00
2005-05-13 10:42:48 +00:00
v = ( int * ) ( ( edictrun_t * ) ed ) - > fields + d - > ofs ;
2004-08-23 01:38:21 +00:00
// if the value is still all 0, skip the field
type = d - > type & ~ DEF_SAVEGLOBAL ;
for ( j = 0 ; j < type_size [ type ] ; j + + )
if ( v [ j ] )
break ;
if ( j = = type_size [ type ] )
continue ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
//add it to the file
AddS ( qcva ( " \" %s \" " , name ) ) ;
AddS ( qcva ( " \" %s \" \n " , PR_UglyValueString ( progfuncs , d - > type , ( eval_t * ) v ) ) ) ;
}
AddS ( " } \n " ) ;
* size = s - buf ;
return buf ;
}
struct edict_s * RestoreEnt ( progfuncs_t * progfuncs , char * buf , int * size , struct edict_s * ed )
{
edictrun_t * ent ;
char * start = buf ;
buf = QCC_COM_Parse ( buf ) ; //read the key
2004-11-23 00:29:10 +00:00
if ( ! buf | | ! * qcc_token )
return NULL ;
2004-08-23 01:38:21 +00:00
if ( strcmp ( qcc_token , " { " ) )
Sys_Error ( " Restore Ent with no opening brace " ) ;
if ( ! ed )
ent = ( edictrun_t * ) ED_Alloc ( progfuncs ) ;
else
ent = ( edictrun_t * ) ed ;
ent - > isfree = false ;
buf = ED_ParseEdict ( progfuncs , buf , ent ) ;
* size = buf - start ;
return ( struct edict_s * ) ent ;
}
# define Host_Error Sys_Error
//return true if pr_progs needs recompiling (source files have changed)
pbool PR_TestRecompile ( progfuncs_t * progfuncs )
{
int newsize ;
int num , found = 0 , lost = 0 , changed = 0 ;
includeddatafile_t * s ;
if ( ! pr_progs - > ofsfiles )
return false ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
num = * ( int * ) ( ( char * ) pr_progs + pr_progs - > ofsfiles ) ;
2005-10-09 21:59:54 +00:00
s = ( includeddatafile_t * ) ( ( char * ) pr_progs + pr_progs - > ofsfiles + 4 ) ;
2004-08-23 01:38:21 +00:00
while ( num > 0 )
{
newsize = externs - > FileSize ( s - > filename ) ;
if ( newsize = = - 1 ) //ignore now missing files. - the referencer must have changed...
lost + + ;
else if ( s - > size ! = newsize ) //file
changed + + ;
else
found + + ;
s + + ;
num - - ;
}
if ( lost > found + changed )
return false ;
if ( changed )
return true ;
return false ;
}
2005-07-16 00:53:08 +00:00
/*
2005-05-17 02:36:54 +00:00
# ifdef _DEBUG
//this is for debugging.
//I'm using this to detect incorrect string types while converting 32bit string pointers with bias to bound indexes.
void PR_TestForWierdness ( progfuncs_t * progfuncs )
{
unsigned int i ;
int e ;
edictrun_t * ed ;
for ( i = 0 ; i < pr_progs - > numglobaldefs ; i + + )
{
if ( ( pr_globaldefs16 [ i ] . type & ~ ( DEF_SHARED | DEF_SAVEGLOBAL ) ) = = ev_string )
{
if ( G_INT ( pr_globaldefs16 [ i ] . ofs ) < 0 | | G_INT ( pr_globaldefs16 [ i ] . ofs ) > = addressableused )
printf ( " String type irregularity on \" %s \" \" %s \" \n " , pr_globaldefs16 [ i ] . s_name + progfuncs - > stringtable , G_INT ( pr_globaldefs16 [ i ] . ofs ) + progfuncs - > stringtable ) ;
}
}
for ( i = 0 ; i < numfields ; i + + )
{
if ( ( field [ i ] . type & ~ ( DEF_SHARED | DEF_SAVEGLOBAL ) ) = = ev_string )
{
for ( e = 0 ; e < sv_num_edicts ; e + + )
{
2005-05-19 02:53:03 +00:00
ed = ( edictrun_t * ) EDICT_NUM ( progfuncs , e ) ;
2005-05-17 02:36:54 +00:00
if ( ed - > isfree )
continue ;
if ( ( ( int * ) ed - > fields ) [ field [ i ] . ofs ] < 0 | | ( ( int * ) ed - > fields ) [ field [ i ] . ofs ] > = addressableused )
printf ( " String type irregularity \" %s \" \" %s \" \n " , field [ i ] . name , ( ( int * ) ed - > fields ) [ field [ i ] . ofs ] + progfuncs - > stringtable ) ;
}
}
}
}
# endif
2005-07-16 00:53:08 +00:00
*/
2004-08-23 01:38:21 +00:00
char * decode ( int complen , int len , int method , char * info , char * buffer ) ;
/*
= = = = = = = = = = = = = = =
PR_LoadProgs
= = = = = = = = = = = = = = =
*/
int PR_ReallyLoadProgs ( progfuncs_t * progfuncs , char * filename , int headercrc , progstate_t * progstate , pbool complain )
{
unsigned int i , type ;
2005-05-15 18:49:04 +00:00
// extensionbuiltin_t *eb;
2004-08-23 01:38:21 +00:00
// float fl;
int len ;
// int num;
// dfunction_t *f, *f2;
2005-10-09 21:59:54 +00:00
ddef16_t * d16 ;
2004-08-23 01:38:21 +00:00
ddef32_t * d32 ;
int * d2 ;
eval_t * eval ;
char * s ;
int progstype ;
int trysleft = 2 ;
// bool qfhack = false;
pbool isfriked = false ; //all imediate values were stripped, which causes problems with strings.
pbool hexencalling = false ; //hexen style calling convention. The opcodes themselves are used as part of passing the arguments;
ddef16_t * gd16 , * fld16 ;
float * glob ;
dfunction_t * fnc ;
dstatement16_t * st16 ;
int hmark = 0xffffffff ;
2006-09-18 22:56:14 +00:00
int reorg = prinst - > reorganisefields | | numfields ;
2004-08-23 01:38:21 +00:00
2005-05-15 18:49:04 +00:00
int stringadjust ;
2005-10-09 21:59:54 +00:00
current_progstate = progstate ;
2004-08-23 01:38:21 +00:00
strcpy ( current_progstate - > filename , filename ) ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
// flush the non-C variable lookup cache
// for (i=0 ; i<GEFV_CACHESIZE ; i++)
// gefvCache[i].field[0] = 0;
memset ( & spawnflagscache , 0 , sizeof ( evalc_t ) ) ;
if ( externs - > autocompile = = PR_COMPILEALWAYS ) //always compile before loading
{
printf ( " Forcing compile of progs %s \n " , filename ) ;
if ( ! CompileFile ( progfuncs , filename ) )
return false ;
}
// CRC_Init (&pr_crc);
retry :
if ( ( len = externs - > FileSize ( filename ) ) < = 0 )
{
if ( externs - > autocompile = = PR_COMPILENEXIST | | externs - > autocompile = = PR_COMPILECHANGED ) //compile if file is not found (if 2, we have already tried, so don't bother)
{
if ( hmark = = 0xffffffff ) //first try
{
printf ( " couldn't open progs %s. Attempting to compile. \n " , filename ) ;
CompileFile ( progfuncs , filename ) ;
}
if ( ( len = externs - > FileSize ( filename ) ) < 0 )
{
printf ( " Couldn't find or compile file %s \n " , filename ) ;
return false ;
}
}
else if ( externs - > autocompile = = PR_COMPILEIGNORE )
return false ;
else
{
printf ( " Couldn't find file %s \n " , filename ) ;
return false ;
}
}
hmark = PRHunkMark ( progfuncs ) ;
pr_progs = PRHunkAlloc ( progfuncs , len + 1 ) ;
if ( ! externs - > ReadFile ( filename , pr_progs , len + 1 ) )
{
if ( ! complain )
return false ;
printf ( " Failed to open %s " , filename ) ;
return false ;
}
// for (i=0 ; i<len ; i++)
// CRC_ProcessByte (&pr_crc, ((byte *)pr_progs)[i]);
// byte swap the header
# ifndef NOENDIAN
2007-07-20 17:02:38 +00:00
for ( i = 0 ; i < standard_dprograms_t_size / sizeof ( int ) ; i + + )
2005-05-22 13:42:10 +00:00
( ( int * ) pr_progs ) [ i ] = PRLittleLong ( ( ( int * ) pr_progs ) [ i ] ) ;
2004-08-23 01:38:21 +00:00
# endif
if ( pr_progs - > version = = PROG_VERSION )
{
// printf("Opening standard progs file \"%s\"\n", filename);
current_progstate - > intsize = 16 ;
}
2005-02-28 07:16:19 +00:00
else if ( pr_progs - > version = = PROG_EXTENDEDVERSION )
2004-08-23 01:38:21 +00:00
{
2007-07-20 17:02:38 +00:00
# ifndef NOENDIAN
for ( i = standard_dprograms_t_size / sizeof ( int ) ; i < sizeof ( dprograms_t ) / sizeof ( int ) ; i + + )
( ( int * ) pr_progs ) [ i ] = PRLittleLong ( ( ( int * ) pr_progs ) [ i ] ) ;
# endif
2004-08-23 01:38:21 +00:00
if ( pr_progs - > secondaryversion = = PROG_SECONDARYVERSION16 )
{
// printf("Opening 16bit fte progs file \"%s\"\n", filename);
current_progstate - > intsize = 16 ;
}
else if ( pr_progs - > secondaryversion = = PROG_SECONDARYVERSION32 )
{
// printf("Opening 32bit fte progs file \"%s\"\n", filename);
current_progstate - > intsize = 32 ;
}
else
{
// printf("Opening KK7 progs file \"%s\"\n", filename);
current_progstate - > intsize = 24 ; //KK progs. Yuck. Disabling saving would be a VERY good idea.
pr_progs - > version = PROG_VERSION ; //not fte.
}
/* else
{
printf ( " Progs extensions are not compatable \n Try recompiling with the FTE compiler \n " ) ;
HunkFree ( hmark ) ;
pr_progs = NULL ;
return false ;
}
*/ }
else
{
printf ( " %s has wrong version number (%i should be %i) \n " , filename , pr_progs - > version , PROG_VERSION ) ;
PRHunkFree ( progfuncs , hmark ) ;
pr_progs = NULL ;
return false ;
}
//progs contains enough info for use to recompile it.
2005-03-10 03:55:18 +00:00
if ( trysleft & & ( externs - > autocompile = = PR_COMPILECHANGED | | externs - > autocompile = = PR_COMPILEEXISTANDCHANGED ) & & pr_progs - > version = = PROG_EXTENDEDVERSION )
2004-08-23 01:38:21 +00:00
{
if ( PR_TestRecompile ( progfuncs ) )
{
printf ( " Source file has changed \n Recompiling. \n " ) ;
if ( CompileFile ( progfuncs , filename ) )
{
PRHunkFree ( progfuncs , hmark ) ;
pr_progs = NULL ;
trysleft - - ;
goto retry ;
}
}
}
if ( ! trysleft ) //the progs exists, let's just be happy about it.
printf ( " Progs is out of date and uncompilable \n " ) ;
if ( headercrc ! = - 1 & & headercrc ! = 0 )
if ( pr_progs - > crc ! = headercrc & & pr_progs - > crc ! = 0 ) //This shouldn't affect us. However, it does adjust expectations and usage of builtins.
{
// printf ("%s system vars have been modified, progdefs.h is out of date\n", filename);
PRHunkFree ( progfuncs , hmark ) ;
pr_progs = NULL ;
return false ;
}
fnc = pr_functions = ( dfunction_t * ) ( ( qbyte * ) pr_progs + pr_progs - > ofs_functions ) ;
pr_strings = ( ( char * ) pr_progs + pr_progs - > ofs_strings ) ;
2004-09-20 23:25:38 +00:00
current_progstate - > globaldefs = * ( void * * ) & gd16 = ( void * ) ( ( qbyte * ) pr_progs + pr_progs - > ofs_globaldefs ) ;
current_progstate - > fielddefs = * ( void * * ) & fld16 = ( void * ) ( ( qbyte * ) pr_progs + pr_progs - > ofs_fielddefs ) ;
current_progstate - > statements = ( void * ) ( ( qbyte * ) pr_progs + pr_progs - > ofs_statements ) ;
2004-08-23 01:38:21 +00:00
glob = pr_globals = ( void * ) ( ( qbyte * ) pr_progs + pr_progs - > ofs_globals ) ;
pr_linenums = NULL ;
pr_types = NULL ;
2005-02-28 07:16:19 +00:00
if ( pr_progs - > version = = PROG_EXTENDEDVERSION )
2004-08-23 01:38:21 +00:00
{
if ( pr_progs - > ofslinenums )
pr_linenums = ( int * ) ( ( qbyte * ) pr_progs + pr_progs - > ofslinenums ) ;
if ( pr_progs - > ofs_types )
pr_types = ( typeinfo_t * ) ( ( qbyte * ) pr_progs + pr_progs - > ofs_types ) ;
//start decompressing stuff...
if ( pr_progs - > blockscompressed & 1 ) //statements
{
switch ( current_progstate - > intsize )
{
case 16 :
len = sizeof ( dstatement16_t ) * pr_progs - > numstatements ;
break ;
case 32 :
len = sizeof ( dstatement32_t ) * pr_progs - > numstatements ;
break ;
default :
Sys_Error ( " Bad intsize " ) ;
}
s = PRHunkAlloc ( progfuncs , len ) ;
2005-05-22 13:42:10 +00:00
QC_decode ( progfuncs , PRLittleLong ( * ( int * ) pr_statements16 ) , len , 2 , ( char * ) ( ( ( int * ) pr_statements16 ) + 1 ) , s ) ;
2004-08-23 01:38:21 +00:00
2004-09-20 23:25:38 +00:00
current_progstate - > statements = ( dstatement16_t * ) s ;
2004-08-23 01:38:21 +00:00
}
if ( pr_progs - > blockscompressed & 2 ) //global defs
{
switch ( current_progstate - > intsize )
{
case 16 :
len = sizeof ( ddef16_t ) * pr_progs - > numglobaldefs ;
break ;
case 32 :
len = sizeof ( ddef32_t ) * pr_progs - > numglobaldefs ;
break ;
default :
Sys_Error ( " Bad intsize " ) ;
2005-10-09 21:59:54 +00:00
}
2004-08-23 01:38:21 +00:00
s = PRHunkAlloc ( progfuncs , len ) ;
2005-05-22 13:42:10 +00:00
QC_decode ( progfuncs , PRLittleLong ( * ( int * ) pr_globaldefs16 ) , len , 2 , ( char * ) ( ( ( int * ) pr_globaldefs16 ) + 1 ) , s ) ;
2004-08-23 01:38:21 +00:00
2004-09-20 23:25:38 +00:00
gd16 = * ( ddef16_t * * ) & current_progstate - > globaldefs = ( ddef16_t * ) s ;
2004-08-23 01:38:21 +00:00
}
if ( pr_progs - > blockscompressed & 4 ) //fields
{
switch ( current_progstate - > intsize )
{
case 16 :
len = sizeof ( ddef16_t ) * pr_progs - > numglobaldefs ;
break ;
case 32 :
len = sizeof ( ddef32_t ) * pr_progs - > numglobaldefs ;
break ;
default :
Sys_Error ( " Bad intsize " ) ;
2005-10-09 21:59:54 +00:00
}
2004-08-23 01:38:21 +00:00
s = PRHunkAlloc ( progfuncs , len ) ;
2005-05-22 13:42:10 +00:00
QC_decode ( progfuncs , PRLittleLong ( * ( int * ) pr_fielddefs16 ) , len , 2 , ( char * ) ( ( ( int * ) pr_fielddefs16 ) + 1 ) , s ) ;
2004-08-23 01:38:21 +00:00
2004-09-20 23:25:38 +00:00
* ( ddef16_t * * ) & current_progstate - > fielddefs = ( ddef16_t * ) s ;
2004-08-23 01:38:21 +00:00
}
if ( pr_progs - > blockscompressed & 8 ) //functions
{
len = sizeof ( dfunction_t ) * pr_progs - > numfunctions ;
s = PRHunkAlloc ( progfuncs , len ) ;
2005-05-22 13:42:10 +00:00
QC_decode ( progfuncs , PRLittleLong ( * ( int * ) pr_functions ) , len , 2 , ( char * ) ( ( ( int * ) pr_functions ) + 1 ) , s ) ;
2004-08-23 01:38:21 +00:00
fnc = pr_functions = ( dfunction_t * ) s ;
}
if ( pr_progs - > blockscompressed & 16 ) //string table
{
len = sizeof ( char ) * pr_progs - > numstrings ;
2005-05-15 18:49:04 +00:00
s = PRHunkAlloc ( progfuncs , len ) ;
2005-05-22 13:42:10 +00:00
QC_decode ( progfuncs , PRLittleLong ( * ( int * ) pr_strings ) , len , 2 , ( char * ) ( ( ( int * ) pr_strings ) + 1 ) , s ) ;
2004-08-23 01:38:21 +00:00
pr_strings = ( char * ) s ;
}
if ( pr_progs - > blockscompressed & 32 ) //globals
{
len = sizeof ( float ) * pr_progs - > numglobals ;
2005-05-15 18:49:04 +00:00
s = PRHunkAlloc ( progfuncs , len ) ;
2005-05-22 13:42:10 +00:00
QC_decode ( progfuncs , PRLittleLong ( * ( int * ) pr_globals ) , len , 2 , ( char * ) ( ( ( int * ) pr_globals ) + 1 ) , s ) ;
2004-08-23 01:38:21 +00:00
2005-05-15 18:49:04 +00:00
glob = pr_globals = ( float * ) s ;
2004-08-23 01:38:21 +00:00
}
2005-03-28 00:11:59 +00:00
if ( pr_linenums & & pr_progs - > blockscompressed & 64 ) //line numbers
2004-08-23 01:38:21 +00:00
{
len = sizeof ( int ) * pr_progs - > numstatements ;
s = PRHunkAlloc ( progfuncs , len ) ;
2005-05-22 13:42:10 +00:00
QC_decode ( progfuncs , PRLittleLong ( * ( int * ) pr_linenums ) , len , 2 , ( char * ) ( ( ( int * ) pr_linenums ) + 1 ) , s ) ;
2004-08-23 01:38:21 +00:00
2005-10-09 21:59:54 +00:00
pr_linenums = ( int * ) s ;
2004-08-23 01:38:21 +00:00
}
if ( pr_types & & pr_progs - > blockscompressed & 128 ) //types
2005-10-09 21:59:54 +00:00
{
2004-08-23 01:38:21 +00:00
len = sizeof ( typeinfo_t ) * pr_progs - > numtypes ;
s = PRHunkAlloc ( progfuncs , len ) ;
2005-05-22 13:42:10 +00:00
QC_decode ( progfuncs , PRLittleLong ( * ( int * ) pr_types ) , len , 2 , ( char * ) ( ( ( int * ) pr_types ) + 1 ) , s ) ;
2004-08-23 01:38:21 +00:00
pr_types = ( typeinfo_t * ) s ;
}
}
2005-05-15 18:49:04 +00:00
len = sizeof ( char ) * pr_progs - > numstrings ;
s = PRAddressableAlloc ( progfuncs , len ) ;
memcpy ( s , pr_strings , len ) ;
pr_strings = ( char * ) s ;
len = sizeof ( float ) * pr_progs - > numglobals ;
s = PRAddressableAlloc ( progfuncs , len ) ;
memcpy ( s , pr_globals , len ) ;
glob = pr_globals = ( float * ) s ;
if ( progfuncs - > stringtable )
stringadjust = pr_strings - progfuncs - > stringtable ;
else
stringadjust = 0 ;
2004-11-13 17:32:03 +00:00
if ( ! pr_linenums )
{
unsigned int lnotype = * ( unsigned int * ) " LNOF " ;
unsigned int version = 1 ;
int ohm ;
unsigned int * file ;
char lnoname [ 128 ] ;
ohm = PRHunkMark ( progfuncs ) ;
2004-11-23 00:29:10 +00:00
strcpy ( lnoname , filename ) ;
StripExtension ( lnoname ) ;
2004-11-13 17:32:03 +00:00
strcat ( lnoname , " .lno " ) ;
if ( ( len = externs - > FileSize ( lnoname ) ) > 0 )
{
file = PRHunkAlloc ( progfuncs , len + 1 ) ;
if ( externs - > ReadFile ( lnoname , file , len + 1 ) )
{
2004-12-10 05:11:46 +00:00
if ( file [ 0 ] ! = lnotype
| | file [ 1 ] ! = version
| | file [ 2 ] ! = pr_progs - > numglobaldefs
| | file [ 3 ] ! = pr_progs - > numglobals
| | file [ 4 ] ! = pr_progs - > numfielddefs
| | file [ 5 ] ! = pr_progs - > numstatements
)
{
PRHunkFree ( progfuncs , ohm ) ; //whoops: old progs or incompatable
}
else
pr_linenums = file + 6 ;
2004-11-13 17:32:03 +00:00
}
}
}
2004-08-23 01:38:21 +00:00
pr_functions = fnc ;
// pr_strings = ((char *)pr_progs + pr_progs->ofs_strings);
2004-09-20 23:25:38 +00:00
gd16 = * ( ddef16_t * * ) & current_progstate - > globaldefs = ( ddef16_t * ) ( ( qbyte * ) pr_progs + pr_progs - > ofs_globaldefs ) ;
2004-08-23 01:38:21 +00:00
fld16 = ( ddef16_t * ) ( ( qbyte * ) pr_progs + pr_progs - > ofs_fielddefs ) ;
// pr_statements16 = (dstatement16_t *)((qbyte *)pr_progs + pr_progs->ofs_statements);
pr_globals = glob ;
st16 = pr_statements16 ;
# undef pr_globals
# undef pr_globaldefs16
# undef pr_functions
# undef pr_statements16
# undef pr_fielddefs16
current_progstate - > edict_size = pr_progs - > entityfields * 4 + externs - > edictsize ;
// byte swap the lumps
for ( i = 0 ; i < pr_progs - > numfunctions ; i + + )
{
# ifndef NOENDIAN
2005-05-22 13:42:10 +00:00
fnc [ i ] . first_statement = PRLittleLong ( fnc [ i ] . first_statement ) ;
fnc [ i ] . parm_start = PRLittleLong ( fnc [ i ] . parm_start ) ;
fnc [ i ] . s_name = ( string_t ) PRLittleLong ( ( long ) fnc [ i ] . s_name ) ;
fnc [ i ] . s_file = ( string_t ) PRLittleLong ( ( long ) fnc [ i ] . s_file ) ;
fnc [ i ] . numparms = PRLittleLong ( fnc [ i ] . numparms ) ;
fnc [ i ] . locals = PRLittleLong ( fnc [ i ] . locals ) ;
2004-08-23 01:38:21 +00:00
# endif
2005-05-15 18:49:04 +00:00
/* if (!strncmp(fnc[i].s_name+pr_strings, "ext_", 4))
2004-08-23 01:38:21 +00:00
{
for ( eb = extensionbuiltin ; eb ; eb = eb - > prev )
{
if ( * eb - > name = = ' _ ' )
{
2005-05-15 18:49:04 +00:00
if ( ! strncmp ( fnc [ i ] . s_name + pr_strings + 4 , eb - > name + 1 , strlen ( eb - > name + 1 ) ) )
2004-08-23 01:38:21 +00:00
{
fnc [ i ] . first_statement = - 0x7fffffff ;
2004-09-20 23:25:38 +00:00
* ( void * * ) & fnc [ i ] . profile = ( void * ) eb - > func ;
2004-08-23 01:38:21 +00:00
break ;
}
}
else if ( ! strcmp ( fnc [ i ] . s_name + 4 , eb - > name ) )
{
fnc [ i ] . first_statement = - 0x7fffffff ;
2004-09-20 23:25:38 +00:00
* ( void * * ) & fnc [ i ] . profile = ( void * ) eb - > func ;
2004-08-23 01:38:21 +00:00
break ;
}
}
}
2005-05-15 18:49:04 +00:00
*/
fnc [ i ] . s_name + = stringadjust ;
fnc [ i ] . s_file + = stringadjust ;
2004-08-23 01:38:21 +00:00
}
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
//actual global values
# ifndef NOENDIAN
for ( i = 0 ; i < pr_progs - > numglobals ; i + + )
2005-05-22 13:42:10 +00:00
( ( int * ) glob ) [ i ] = PRLittleLong ( ( ( int * ) glob ) [ i ] ) ;
2005-10-09 21:59:54 +00:00
# endif
2004-08-23 01:38:21 +00:00
2005-03-28 00:11:59 +00:00
if ( pr_types )
2005-10-09 21:59:54 +00:00
{
2005-03-28 00:11:59 +00:00
for ( i = 0 ; i < pr_progs - > numtypes ; i + + )
2005-10-09 21:59:54 +00:00
{
2004-08-23 01:38:21 +00:00
# ifndef NOENDIAN
2005-05-22 13:42:10 +00:00
pr_types [ i ] . type = PRLittleLong ( current_progstate - > types [ i ] . type ) ;
pr_types [ i ] . next = PRLittleLong ( current_progstate - > types [ i ] . next ) ;
pr_types [ i ] . aux_type = PRLittleLong ( current_progstate - > types [ i ] . aux_type ) ;
pr_types [ i ] . num_parms = PRLittleLong ( current_progstate - > types [ i ] . num_parms ) ;
pr_types [ i ] . ofs = PRLittleLong ( current_progstate - > types [ i ] . ofs ) ;
pr_types [ i ] . size = PRLittleLong ( current_progstate - > types [ i ] . size ) ;
2006-03-04 20:43:48 +00:00
pr_types [ i ] . name = ( char * ) PRLittleLong ( ( long ) current_progstate - > types [ i ] . name ) ;
2004-08-23 01:38:21 +00:00
# endif
2005-05-15 18:49:04 +00:00
pr_types [ i ] . name + = stringadjust ;
2004-08-23 01:38:21 +00:00
}
}
if ( reorg )
reorg = ( headercrc ! = - 1 ) ;
2005-09-14 04:36:07 +00:00
QC_FlushProgsOffsets ( progfuncs ) ;
2004-08-23 01:38:21 +00:00
switch ( current_progstate - > intsize )
{
case 24 :
case 16 :
2005-03-28 00:11:59 +00:00
//byteswap the globals and fix name offsets
2004-08-23 01:38:21 +00:00
for ( i = 0 ; i < pr_progs - > numglobaldefs ; i + + )
{
# ifndef NOENDIAN
2005-05-22 13:42:10 +00:00
gd16 [ i ] . type = PRLittleShort ( gd16 [ i ] . type ) ;
gd16 [ i ] . ofs = PRLittleShort ( gd16 [ i ] . ofs ) ;
gd16 [ i ] . s_name = ( string_t ) PRLittleLong ( ( long ) gd16 [ i ] . s_name ) ;
2004-08-23 01:38:21 +00:00
# endif
2005-05-15 18:49:04 +00:00
gd16 [ i ] . s_name + = stringadjust ;
2004-08-23 01:38:21 +00:00
}
2005-03-28 00:11:59 +00:00
//byteswap fields and fix name offets. Also register the fields (which will result in some offset adjustments in the globals segment).
2004-08-23 01:38:21 +00:00
for ( i = 0 ; i < pr_progs - > numfielddefs ; i + + )
{
# ifndef NOENDIAN
2005-05-22 13:42:10 +00:00
fld16 [ i ] . type = PRLittleShort ( fld16 [ i ] . type ) ;
fld16 [ i ] . ofs = PRLittleShort ( fld16 [ i ] . ofs ) ;
fld16 [ i ] . s_name = ( string_t ) PRLittleLong ( ( long ) fld16 [ i ] . s_name ) ;
2004-08-23 01:38:21 +00:00
# endif
if ( reorg )
{
if ( pr_types )
type = pr_types [ fld16 [ i ] . type & ~ ( DEF_SHARED | DEF_SAVEGLOBAL ) ] . type ;
else
type = fld16 [ i ] . type & ~ ( DEF_SHARED | DEF_SAVEGLOBAL ) ;
2005-09-14 04:36:07 +00:00
if ( progfuncs - > fieldadjust & & ! pr_typecurrent ) //we need to make sure all fields appear in thier original place.
2005-05-15 18:49:04 +00:00
QC_RegisterFieldVar ( progfuncs , type , fld16 [ i ] . s_name + pr_strings , 4 * ( fld16 [ i ] . ofs + progfuncs - > fieldadjust ) , - 1 ) ;
2004-08-23 01:38:21 +00:00
else if ( type = = ev_vector ) //emit vector vars early, so thier fields cannot be alocated before the vector itself. (useful against scramblers)
{
2005-10-10 22:29:05 +00:00
QC_RegisterFieldVar ( progfuncs , type , fld16 [ i ] . s_name + pr_strings , - 1 , fld16 [ i ] . ofs ) ;
2004-08-23 01:38:21 +00:00
}
}
2006-09-18 22:56:14 +00:00
else
{
fdef_t * nf ;
if ( numfields + 1 > maxfields )
{
i = maxfields ;
maxfields + = 32 ;
nf = memalloc ( sizeof ( fdef_t ) * maxfields ) ;
memcpy ( nf , field , sizeof ( fdef_t ) * i ) ;
memfree ( field ) ;
field = nf ;
}
nf = & field [ numfields ] ;
nf - > name = fld16 [ i ] . s_name + pr_strings ;
nf - > type = fld16 [ i ] . type ;
nf - > progsofs = fld16 [ i ] . ofs ;
nf - > ofs = fld16 [ i ] . ofs ;
if ( fields_size < ( nf - > ofs + type_size [ nf - > type ] ) * 4 )
fields_size = ( nf - > ofs + type_size [ nf - > type ] ) * 4 ;
numfields + + ;
}
2005-05-15 18:49:04 +00:00
fld16 [ i ] . s_name + = stringadjust ;
2004-08-23 01:38:21 +00:00
}
2005-10-10 22:29:05 +00:00
if ( reorg & & ! ( progfuncs - > fieldadjust & & ! pr_typecurrent ) )
for ( i = 0 ; i < pr_progs - > numfielddefs ; i + + )
{
if ( pr_types )
type = pr_types [ fld16 [ i ] . type & ~ ( DEF_SHARED | DEF_SAVEGLOBAL ) ] . type ;
else
type = fld16 [ i ] . type & ~ ( DEF_SHARED | DEF_SAVEGLOBAL ) ;
if ( type ! = ev_vector )
2005-10-21 15:09:46 +00:00
QC_RegisterFieldVar ( progfuncs , type , fld16 [ i ] . s_name + pr_strings - stringadjust , - 1 , fld16 [ i ] . ofs ) ;
2005-10-10 22:29:05 +00:00
}
2004-08-23 01:38:21 +00:00
break ;
case 32 :
for ( i = 0 ; i < pr_progs - > numglobaldefs ; i + + )
{
# ifndef NOENDIAN
2005-05-22 13:42:10 +00:00
pr_globaldefs32 [ i ] . type = PRLittleLong ( pr_globaldefs32 [ i ] . type ) ;
pr_globaldefs32 [ i ] . ofs = PRLittleLong ( pr_globaldefs32 [ i ] . ofs ) ;
pr_globaldefs32 [ i ] . s_name = ( string_t ) PRLittleLong ( ( long ) pr_globaldefs32 [ i ] . s_name ) ;
2004-08-23 01:38:21 +00:00
# endif
2005-05-15 18:49:04 +00:00
pr_globaldefs32 [ i ] . s_name + = stringadjust ;
2004-08-23 01:38:21 +00:00
}
for ( i = 0 ; i < pr_progs - > numfielddefs ; i + + )
{
# ifndef NOENDIAN
2005-05-22 13:42:10 +00:00
pr_fielddefs32 [ i ] . type = PRLittleLong ( pr_fielddefs32 [ i ] . type ) ;
pr_fielddefs32 [ i ] . ofs = PRLittleLong ( pr_fielddefs32 [ i ] . ofs ) ;
pr_fielddefs32 [ i ] . s_name = ( string_t ) PRLittleLong ( ( long ) pr_fielddefs32 [ i ] . s_name ) ;
2004-08-23 01:38:21 +00:00
# endif
if ( reorg )
{
if ( pr_types )
type = pr_types [ pr_fielddefs32 [ i ] . type & ~ ( DEF_SHARED | DEF_SAVEGLOBAL ) ] . type ;
else
type = pr_fielddefs32 [ i ] . type & ~ ( DEF_SHARED | DEF_SAVEGLOBAL ) ;
2005-09-14 04:36:07 +00:00
if ( progfuncs - > fieldadjust & & ! pr_typecurrent ) //we need to make sure all fields appear in thier original place.
QC_RegisterFieldVar ( progfuncs , type , fld16 [ i ] . s_name + pr_strings , 4 * ( fld16 [ i ] . ofs + progfuncs - > fieldadjust ) , - 1 ) ;
else if ( type = = ev_vector )
2005-10-10 22:29:05 +00:00
QC_RegisterFieldVar ( progfuncs , type , pr_fielddefs32 [ i ] . s_name + pr_strings , - 1 , pr_fielddefs32 [ i ] . ofs ) ;
2004-08-23 01:38:21 +00:00
}
2005-05-15 18:49:04 +00:00
pr_fielddefs32 [ i ] . s_name + = stringadjust ;
2005-10-09 21:59:54 +00:00
}
2005-10-10 22:29:05 +00:00
if ( reorg & & ! ( progfuncs - > fieldadjust & & ! pr_typecurrent ) )
for ( i = 0 ; i < pr_progs - > numfielddefs ; i + + )
{
if ( pr_types )
type = pr_types [ pr_fielddefs32 [ i ] . type & ~ ( DEF_SHARED | DEF_SAVEGLOBAL ) ] . type ;
else
type = pr_fielddefs32 [ i ] . type & ~ ( DEF_SHARED | DEF_SAVEGLOBAL ) ;
if ( type ! = ev_vector )
2005-10-21 15:09:46 +00:00
QC_RegisterFieldVar ( progfuncs , type , pr_fielddefs32 [ i ] . s_name + pr_strings - stringadjust , - 1 , pr_fielddefs32 [ i ] . ofs ) ;
2005-10-10 22:29:05 +00:00
}
2004-08-23 01:38:21 +00:00
break ;
default :
Sys_Error ( " Bad int size " ) ;
2004-11-13 17:32:03 +00:00
}
2004-08-23 01:38:21 +00:00
2005-10-09 21:59:54 +00:00
//ifstring fixes arn't performed anymore.
2005-03-28 00:11:59 +00:00
//the following switch just fixes endian and hexen2 calling conventions (by using different opcodes).
2004-08-23 01:38:21 +00:00
switch ( current_progstate - > intsize )
{
case 16 :
for ( i = 0 ; i < pr_progs - > numstatements ; i + + )
{
# ifndef NOENDIAN
2005-05-22 13:42:10 +00:00
st16 [ i ] . op = PRLittleShort ( st16 [ i ] . op ) ;
st16 [ i ] . a = PRLittleShort ( st16 [ i ] . a ) ;
st16 [ i ] . b = PRLittleShort ( st16 [ i ] . b ) ;
st16 [ i ] . c = PRLittleShort ( st16 [ i ] . c ) ;
2004-08-23 01:38:21 +00:00
# endif
2004-11-23 00:29:10 +00:00
if ( st16 [ i ] . op > = OP_CALL1 & & st16 [ i ] . op < = OP_CALL8 )
2004-08-23 01:38:21 +00:00
{
if ( st16 [ i ] . b )
hexencalling = true ;
}
}
if ( hexencalling )
{
for ( i = 0 ; i < pr_progs - > numstatements ; i + + )
{
if ( st16 [ i ] . op > = OP_CALL1 & & st16 [ i ] . op < = OP_CALL8 )
st16 [ i ] . op + = OP_CALL1H - OP_CALL1 ;
}
}
break ;
2005-10-09 21:59:54 +00:00
2004-08-23 01:38:21 +00:00
case 24 : //24 sucks. Guess why.
for ( i = 0 ; i < pr_progs - > numstatements ; i + + )
{
# ifndef NOENDIAN
2005-05-22 13:42:10 +00:00
pr_statements32 [ i ] . op = PRLittleLong ( pr_statements32 [ i ] . op ) ;
pr_statements32 [ i ] . a = PRLittleLong ( pr_statements32 [ i ] . a ) ;
pr_statements32 [ i ] . b = PRLittleLong ( pr_statements32 [ i ] . b ) ;
pr_statements32 [ i ] . c = PRLittleLong ( pr_statements32 [ i ] . c ) ;
2004-08-23 01:38:21 +00:00
# endif
2005-03-28 00:11:59 +00:00
if ( pr_statements32 [ i ] . op > = OP_CALL1 & & pr_statements32 [ i ] . op < = OP_CALL8 )
2004-08-23 01:38:21 +00:00
{
if ( pr_statements32 [ i ] . b )
hexencalling = true ;
}
}
if ( hexencalling )
{
for ( i = 0 ; i < pr_progs - > numstatements ; i + + )
{
if ( pr_statements32 [ i ] . op > = OP_CALL1 & & pr_statements32 [ i ] . op < = OP_CALL8 )
pr_statements32 [ i ] . op + = OP_CALL1H - OP_CALL1 ;
}
}
break ;
case 32 :
for ( i = 0 ; i < pr_progs - > numstatements ; i + + )
{
2005-03-28 00:11:59 +00:00
# ifndef NOENDIAN
2005-05-22 13:42:10 +00:00
pr_statements32 [ i ] . op = PRLittleLong ( pr_statements32 [ i ] . op ) ;
pr_statements32 [ i ] . a = PRLittleLong ( pr_statements32 [ i ] . a ) ;
pr_statements32 [ i ] . b = PRLittleLong ( pr_statements32 [ i ] . b ) ;
pr_statements32 [ i ] . c = PRLittleLong ( pr_statements32 [ i ] . c ) ;
2005-03-28 00:11:59 +00:00
# endif
if ( pr_statements32 [ i ] . op > = OP_CALL1 & & pr_statements32 [ i ] . op < = OP_CALL8 )
{
if ( pr_statements32 [ i ] . b )
hexencalling = true ;
2004-08-23 01:38:21 +00:00
2005-03-28 00:11:59 +00:00
}
}
if ( hexencalling )
{
for ( i = 0 ; i < pr_progs - > numstatements ; i + + )
{
if ( pr_statements32 [ i ] . op > = OP_CALL1 & & pr_statements32 [ i ] . op < = OP_CALL8 )
pr_statements32 [ i ] . op + = OP_CALL1H - OP_CALL1 ;
}
2004-08-23 01:38:21 +00:00
}
break ;
}
if ( headercrc = = - 1 )
{
isfriked = true ;
if ( current_progstate - > intsize ! = 16 )
Sys_Error ( " Decompiling a bigprogs " ) ;
return true ;
}
progstype = current_progstate - pr_progstate ;
// QC_StartShares(progfuncs);
isfriked = true ;
2004-10-03 10:14:26 +00:00
if ( ! pr_typecurrent ) //progs 0 always acts as string stripped.
isfriked = - 1 ; //partly to avoid some bad progs.
2004-08-23 01:38:21 +00:00
// len = 0;
switch ( current_progstate - > intsize )
{
case 24 :
case 16 :
for ( i = 0 ; i < pr_progs - > numglobaldefs ; i + + )
{
if ( pr_types )
type = pr_types [ gd16 [ i ] . type & ~ ( DEF_SHARED | DEF_SAVEGLOBAL ) ] . type ;
else
type = gd16 [ i ] . type & ~ ( DEF_SHARED | DEF_SAVEGLOBAL ) ;
if ( gd16 [ i ] . type & DEF_SHARED )
{
gd16 [ i ] . type & = ~ DEF_SHARED ;
if ( pr_types )
QC_AddSharedVar ( progfuncs , gd16 [ i ] . ofs , pr_types [ gd16 [ i ] . type & ~ ( DEF_SHARED | DEF_SAVEGLOBAL ) ] . size ) ;
else
QC_AddSharedVar ( progfuncs , gd16 [ i ] . ofs , type_size [ type ] ) ;
}
switch ( type )
{
case ev_field :
2006-09-18 22:56:14 +00:00
if ( reorg )
QC_AddSharedFieldVar ( progfuncs , i , pr_strings - stringadjust ) ;
2004-08-23 01:38:21 +00:00
break ;
case ev_string :
if ( ( ( unsigned int * ) glob ) [ gd16 [ i ] . ofs ] > = progstate - > progs - > numstrings )
printf ( " Insane value \n " ) ;
else if ( isfriked ! = - 1 )
{
if ( pr_strings [ ( ( int * ) glob ) [ gd16 [ i ] . ofs ] ] ) //quakec uses string tables. 0 must remain null, or 'if (s)' can break.
{
2005-05-15 18:49:04 +00:00
( ( int * ) glob ) [ gd16 [ i ] . ofs ] + = stringadjust ;
2004-08-23 01:38:21 +00:00
isfriked = false ;
}
else
2005-05-15 18:49:04 +00:00
( ( int * ) glob ) [ gd16 [ i ] . ofs ] = 0 ;
2004-08-23 01:38:21 +00:00
}
break ;
case ev_function :
if ( ( ( int * ) glob ) [ gd16 [ i ] . ofs ] ) //don't change null funcs
{
// if (fnc[((int *)glob)[gd16[i].ofs]].first_statement>=0) //this is a hack. Make all builtins switch to the main progs first. Allows builtin funcs to cache vars from just the main progs.
( ( int * ) glob ) [ gd16 [ i ] . ofs ] | = progstype < < 24 ;
}
break ;
}
}
break ;
case 32 :
for ( i = 0 ; i < pr_progs - > numglobaldefs ; i + + )
{
if ( pr_types )
type = pr_types [ pr_globaldefs32 [ i ] . type & ~ ( DEF_SHARED | DEF_SAVEGLOBAL ) ] . type ;
else
type = pr_globaldefs32 [ i ] . type & ~ ( DEF_SHARED | DEF_SAVEGLOBAL ) ;
if ( pr_globaldefs32 [ i ] . type & DEF_SHARED )
{
pr_globaldefs32 [ i ] . type & = ~ DEF_SHARED ;
if ( pr_types )
QC_AddSharedVar ( progfuncs , pr_globaldefs32 [ i ] . ofs , pr_types [ pr_globaldefs32 [ i ] . type & ~ ( DEF_SHARED | DEF_SAVEGLOBAL ) ] . size ) ;
else
QC_AddSharedVar ( progfuncs , pr_globaldefs32 [ i ] . ofs , type_size [ type ] ) ;
}
switch ( type )
{
case ev_field :
2005-05-15 18:49:04 +00:00
QC_AddSharedFieldVar ( progfuncs , i , pr_strings - stringadjust ) ;
2004-08-23 01:38:21 +00:00
break ;
case ev_string :
if ( pr_strings [ ( ( int * ) glob ) [ pr_globaldefs32 [ i ] . ofs ] ] ) //quakec uses string tables. 0 must remain null, or 'if (s)' can break.
{
2005-05-15 18:49:04 +00:00
( ( int * ) glob ) [ pr_globaldefs32 [ i ] . ofs ] + = stringadjust ;
2004-08-23 01:38:21 +00:00
isfriked = false ;
}
break ;
case ev_function :
if ( ( ( int * ) glob ) [ pr_globaldefs32 [ i ] . ofs ] ) //don't change null funcs
( ( int * ) glob ) [ pr_globaldefs32 [ i ] . ofs ] | = progstype < < 24 ;
break ;
}
}
2005-02-28 07:16:19 +00:00
if ( pr_progs - > version = = PROG_EXTENDEDVERSION & & pr_progs - > numbodylessfuncs )
2004-08-23 01:38:21 +00:00
{
s = & ( ( char * ) pr_progs ) [ pr_progs - > ofsbodylessfuncs ] ;
for ( i = 0 ; i < pr_progs - > numbodylessfuncs ; i + + )
{
d32 = ED_FindGlobal32 ( progfuncs , s ) ;
d2 = ED_FindGlobalOfsFromProgs ( progfuncs , s , 0 , ev_function ) ;
if ( ! d2 )
Sys_Error ( " Runtime-linked function %s was not found in existing progs " , s ) ;
if ( ! d32 )
Sys_Error ( " Couldn't find def for \" %s \" " , s ) ;
( ( int * ) glob ) [ d32 - > ofs ] = ( * ( func_t * ) & pr_progstate [ 0 ] . globals [ * d2 ] ) ;
s + = strlen ( s ) + 1 ;
}
}
break ;
default :
Sys_Error ( " Bad int size " ) ;
}
if ( ( isfriked & & pr_typecurrent ) ) //friked progs only allow one file.
{
printf ( " You are trying to load a string-stripped progs as an addon. \n This behaviour is not supported. Try removing some optimizations. " ) ;
PRHunkFree ( progfuncs , hmark ) ;
pr_progs = NULL ;
return false ;
}
2005-05-15 18:49:04 +00:00
pr_strings + = stringadjust ;
2004-08-23 01:38:21 +00:00
if ( ! progfuncs - > stringtable )
2005-05-15 18:49:04 +00:00
progfuncs - > stringtable = pr_strings ;
2007-03-11 16:51:45 +00:00
if ( progfuncs - > stringtablesize + progfuncs - > stringtable < pr_strings + pr_progs - > numstrings )
progfuncs - > stringtablesize = ( pr_strings + pr_progs - > numstrings ) - progfuncs - > stringtable ;
2005-10-09 21:59:54 +00:00
eval = PR_FindGlobal ( progfuncs , " thisprogs " , progstype ) ;
2005-05-15 18:49:04 +00:00
if ( eval )
eval - > prog = progstype ;
2005-08-12 01:17:07 +00:00
switch ( current_progstate - > intsize )
{
case 16 :
if ( pr_progs - > version = = PROG_EXTENDEDVERSION & & pr_progs - > numbodylessfuncs )
{
s = & ( ( char * ) pr_progs ) [ pr_progs - > ofsbodylessfuncs ] ;
for ( i = 0 ; i < pr_progs - > numbodylessfuncs ; i + + )
{
d16 = ED_FindGlobal16 ( progfuncs , s ) ;
if ( ! d16 )
Sys_Error ( " Progs requires \" %s \" the external function \" %s \" , but the definition was stripped " , filename , s ) ;
( ( int * ) glob ) [ d16 - > ofs ] = PR_FindFunc ( progfuncs , s , PR_ANY ) ;
if ( ! ( ( int * ) glob ) [ d16 - > ofs ] )
Sys_Error ( " Runtime-linked function %s was not found in primary progs (loading %s) " , s , filename ) ;
/*
d2 = ED_FindGlobalOfsFromProgs ( progfuncs , s , 0 , ev_function ) ;
if ( ! d2 )
Sys_Error ( " Runtime-linked function %s was not found in primary progs (loading %s) " , s , filename ) ;
( ( int * ) glob ) [ d16 - > ofs ] = ( * ( func_t * ) & pr_progstate [ 0 ] . globals [ * d2 ] ) ;
*/
s + = strlen ( s ) + 1 ;
}
}
break ;
case 24 :
break ; //cannot happen anyway.
case 32 :
if ( pr_progs - > version = = PROG_EXTENDEDVERSION & & pr_progs - > numbodylessfuncs )
{
s = & ( ( char * ) pr_progs ) [ pr_progs - > ofsbodylessfuncs ] ;
for ( i = 0 ; i < pr_progs - > numbodylessfuncs ; i + + )
{
d32 = ED_FindGlobal32 ( progfuncs , s ) ;
d2 = ED_FindGlobalOfsFromProgs ( progfuncs , s , 0 , ev_function ) ;
if ( ! d2 )
Sys_Error ( " Runtime-linked function %s was not found in existing progs " , s ) ;
if ( ! d32 )
Sys_Error ( " Couldn't find def for \" %s \" " , s ) ;
( ( int * ) glob ) [ d32 - > ofs ] = ( * ( func_t * ) & pr_progstate [ 0 ] . globals [ * d2 ] ) ;
s + = strlen ( s ) + 1 ;
}
}
break ;
}
2005-12-06 02:18:41 +00:00
eval = PR_FindGlobal ( progfuncs , " __ext__fasttrackarrays " , PR_CURRENT ) ;
if ( eval ) //we support these opcodes
eval - > _float = true ;
2004-08-23 01:38:21 +00:00
return true ;
}
2005-10-07 16:27:20 +00:00
struct edict_s * EDICT_NUM ( progfuncs_t * progfuncs , unsigned int n )
2004-08-23 01:38:21 +00:00
{
2005-10-07 16:27:20 +00:00
if ( n > = maxedicts )
2004-08-23 01:38:21 +00:00
Sys_Error ( " QCLIB: EDICT_NUM: bad number %i " , n ) ;
2004-09-20 23:25:38 +00:00
2004-08-23 01:38:21 +00:00
return prinst - > edicttable [ n ] ;
}
2005-10-07 16:27:20 +00:00
unsigned int NUM_FOR_EDICT ( progfuncs_t * progfuncs , struct edict_s * e )
2004-08-23 01:38:21 +00:00
{
edictrun_t * er = ( edictrun_t * ) e ;
2005-10-07 16:27:20 +00:00
if ( er - > entnum > = maxedicts )
2004-08-23 01:38:21 +00:00
Sys_Error ( " QCLIB: NUM_FOR_EDICT: bad pointer (%i) " , e ) ;
return er - > entnum ;
}