Beispiel #1
0
//note: we probably suffer from progs with renamed system globals.
int QC_RegisterFieldVar(progfuncs_t *progfuncs, unsigned int type, char *name, int engineofs, int progsofs)
{
//	progstate_t *p;
//	int pnum;
	unsigned int i;
	int namelen;
	int ofs;

	int fnum;

	if (!name)	//engine can use this to offset all progs fields
	{			//which fixes constant field offsets (some ktpro arrays)
		progfuncs->fieldadjust = fields_size/4;
		return 0;
	}


	prinst->reorganisefields = true;

	//look for an existing match
	for (i = 0; i < numfields; i++)
	{		
		if (!strcmp(name, field[i].name))
		{
			if (field[i].type != type)
			{
				printf("Field type mismatch on \"%s\"\n", name);
				continue;
			}
			if (!progfuncs->fieldadjust && engineofs>=0)
				if ((unsigned)engineofs/4 != field[i].ofs)
					Sys_Error("Field %s at wrong offset", name);

			if (field[i].progsofs == -1)
				field[i].progsofs = progsofs;
//			printf("Dupfield %s %i -> %i\n", name, field[i].progsofs,field[i].ofs);
			return field[i].ofs-progfuncs->fieldadjust;	//got a match
		}
	}

	if (numfields+1>maxfields)
	{
		fdef_t *nf;
		i = maxfields;
		maxfields += 32;
		nf = memalloc(sizeof(fdef_t) * maxfields);
		memcpy(nf, field, sizeof(fdef_t) * i);
		memfree(field);
		field = nf;
	}

	//try to add a new one
	fnum = numfields;
	numfields++;
	field[fnum].name = name;	
	if (type == ev_vector)
	{
		char *n;		
		namelen = strlen(name)+5;	

		n=PRHunkAlloc(progfuncs, namelen);
		sprintf(n, "%s_x", name);
		ofs = QC_RegisterFieldVar(progfuncs, ev_float, n, engineofs, progsofs);
		field[fnum].ofs = ofs+progfuncs->fieldadjust;

		n=PRHunkAlloc(progfuncs, namelen);
		sprintf(n, "%s_y", name);
		QC_RegisterFieldVar(progfuncs, ev_float, n, (engineofs==-1)?-1:(engineofs+4), (progsofs==-1)?-1:progsofs+1);

		n=PRHunkAlloc(progfuncs, namelen);
		sprintf(n, "%s_z", name);
		QC_RegisterFieldVar(progfuncs, ev_float, n, (engineofs==-1)?-1:(engineofs+8), (progsofs==-1)?-1:progsofs+2);
	}
	else if (engineofs >= 0)
	{	//the engine is setting up a list of required field indexes.

		//paranoid checking of the offset.
	/*	for (i = 0; i < numfields-1; i++)
		{
			if (field[i].ofs == ((unsigned)engineofs)/4)
			{
				if (type == ev_float && field[i].type == ev_vector)	//check names
				{
					if (strncmp(field[i].name, name, strlen(field[i].name)))
						Sys_Error("Duplicated offset");
				}
				else
					Sys_Error("Duplicated offset");
			}
		}*/
		if (engineofs&3)
			Sys_Error("field %s is %i&3", name, engineofs);
		field[fnum].ofs = ofs = engineofs/4;
	}
	else
	{	//we just found a new fieldname inside a progs
		field[fnum].ofs = ofs = fields_size/4;	//add on the end

		//if the progs field offset matches annother offset in the same progs, make it match up with the earlier one.
		if (progsofs>=0)
		{
			for (i = 0; i < numfields-1; i++)
			{
				if (field[i].progsofs == (unsigned)progsofs)
				{
//					printf("found union field %s %i -> %i\n", field[i].name, field[i].progsofs, field[i].ofs);
					field[fnum].ofs = ofs = field[i].ofs;
					break;
				}
			}
		}
	}
//	if (type != ev_vector)
		if (fields_size < (ofs+type_size[type])*4)
			fields_size = (ofs+type_size[type])*4;

	if (max_fields_size && fields_size > max_fields_size)
		Sys_Error("Allocated too many additional fields after ents were inited.");
	field[fnum].type = type;

	field[fnum].progsofs = progsofs;

//	printf("Field %s %i -> %i\n", name, field[fnum].progsofs,field[fnum].ofs);
	
	//we've finished setting the structure	
	return ofs - progfuncs->fieldadjust;
}
Beispiel #2
0
//called if a global is defined as a field
void QC_AddSharedFieldVar(progfuncs_t *progfuncs, int num, char *stringtable)
{
//	progstate_t *p;
//	int pnum;
	unsigned int i, o;

	char *s;

	//look for an existing match not needed, cos we look a little later too.
	/*
	for (i = 0; i < numfields; i++)
	{		
		if (!strcmp(pr_globaldefs[num].s_name, field[i].s_name))
		{
			//really we should look for a field def

			*(int *)&pr_globals[pr_globaldefs[num].ofs] = field[i].ofs;	//got a match

			return;
		}
	}
	*/
	
	switch(current_progstate->intsize)
	{
	case 24:
	case 16:
		for (i=1 ; i<pr_progs->numfielddefs; i++)
		{
			if (!strcmp(pr_fielddefs16[i].s_name+stringtable, pr_globaldefs16[num].s_name+stringtable))
			{
//				int old = *(int *)&pr_globals[pr_globaldefs16[num].ofs];
				*(int *)&pr_globals[pr_globaldefs16[num].ofs] = QC_RegisterFieldVar(progfuncs, pr_fielddefs16[i].type, pr_globaldefs16[num].s_name+stringtable, -1, *(int *)&pr_globals[pr_globaldefs16[num].ofs]);

//				printf("Field %s %i -> %i\n", pr_globaldefs16[num].s_name+stringtable, old, *(int *)&pr_globals[pr_globaldefs16[num].ofs]);
				return;
			}
		}

		s = pr_globaldefs16[num].s_name+stringtable;

		for (i = 0; i < numfields; i++)
		{
			o = field[i].progsofs;
			if (o == *(unsigned int *)&pr_globals[pr_globaldefs16[num].ofs])
			{
//				int old = *(int *)&pr_globals[pr_globaldefs16[num].ofs];
				*(int *)&pr_globals[pr_globaldefs16[num].ofs] = field[i].ofs-progfuncs->fieldadjust;
//				printf("Field %s %i -> %i\n", pr_globaldefs16[num].s_name+stringtable, old, *(int *)&pr_globals[pr_globaldefs16[num].ofs]);
				return;
			}
		}

		//oh well, must be a parameter.
//		if (*(int *)&pr_globals[pr_globaldefs16[num].ofs])
//			Sys_Error("QCLIB: Global field var with no matching field \"%s\", from offset %i", pr_globaldefs16[num].s_name+stringtable, *(int *)&pr_globals[pr_globaldefs16[num].ofs]);
		return;
	case 32:
		for (i=1 ; i<pr_progs->numfielddefs; i++)
		{
			if (!strcmp(pr_fielddefs32[i].s_name+stringtable, pr_globaldefs32[num].s_name+stringtable))
			{
				*(int *)&pr_globals[pr_globaldefs32[num].ofs] = QC_RegisterFieldVar(progfuncs, pr_fielddefs32[i].type, pr_globaldefs32[num].s_name+stringtable, -1, *(int *)&pr_globals[pr_globaldefs32[num].ofs]);
				return;
			}
		}

		s = pr_globaldefs32[num].s_name+stringtable;

		for (i = 0; i < numfields; i++)
		{
			o = field[i].progsofs;
			if (o == *(unsigned int *)&pr_globals[pr_globaldefs32[num].ofs])
			{
				*(int *)&pr_globals[pr_globaldefs32[num].ofs] = field[i].ofs-progfuncs->fieldadjust;
				return;
			}
		}

		//oh well, must be a parameter.
		if (*(int *)&pr_globals[pr_globaldefs32[num].ofs])
			Sys_Error("QCLIB: Global field var with no matching field \"%s\", from offset %i", pr_globaldefs32[num].s_name+stringtable, *(int *)&pr_globals[pr_globaldefs32[num].ofs]);
		return;
	default:
		Sys_Error("Bad bits");
		break;
	}
	Sys_Error("Should be unreachable");	
}
Beispiel #3
0
//note: we probably suffer from progs with renamed system globals.
int QC_RegisterFieldVar(progfuncs_t *progfuncs, unsigned int type, char *name, signed long engineofs, signed long progsofs)
{
//	progstate_t *p;
//	int pnum;
	unsigned int i;
	int namelen;
	int ofs;

	int fnum;

	if (!name)	//engine can use this to offset all progs fields
	{			//which fixes constant field offsets (some ktpro arrays)
		progfuncs->fieldadjust = fields_size/4;
		return 0;
	}


	prinst->reorganisefields = true;

	//look for an existing match
	for (i = 0; i < numfields; i++)
	{		
		if (!strcmp(name, field[i].name))
		{
			if (field[i].type != type)
			{
				/*Hexen2/DP compat hack: if the new type is a float and the original type is a vector, make the new def alias to the engine's _x field
				this 'works around' the unused .vector color field used for rtlight colours vs the .float color used for particle colours (the float initialisers in map files will expand into the x slot safely).
				qc/hc can work around this by just using .vector color/color_x instead, which is the same as this hack, but would resolve defs to allow rtlight colours.
				*/
				if (field[i].type != ev_vector || type != ev_float)
				{
					printf("Field type mismatch on \"%s\". %i != %i\n", name, field[i].type, type);
					continue;
				}
			}
			if (!progfuncs->fieldadjust && engineofs>=0)
				if ((unsigned)engineofs/4 != field[i].ofs)
					Sys_Error("Field %s at wrong offset", name);

			if (field[i].progsofs == -1)
				field[i].progsofs = progsofs;
//			printf("Dupfield %s %i -> %i\n", name, field[i].progsofs,field[i].ofs);
			return field[i].ofs-progfuncs->fieldadjust;	//got a match
		}
	}

	if (numfields+1>maxfields)
	{
		fdef_t *nf;
		i = maxfields;
		maxfields += 32;
		nf = memalloc(sizeof(fdef_t) * maxfields);
		memcpy(nf, field, sizeof(fdef_t) * i);
		memfree(field);
		field = nf;
	}

	//try to add a new one
	fnum = numfields;
	numfields++;
	field[fnum].name = name;	
	if (type == ev_vector)
	{
		char *n;		
		namelen = strlen(name)+5;	

		n=PRHunkAlloc(progfuncs, namelen);
		sprintf(n, "%s_x", name);
		ofs = QC_RegisterFieldVar(progfuncs, ev_float, n, engineofs, progsofs);
		field[fnum].ofs = ofs+progfuncs->fieldadjust;

		n=PRHunkAlloc(progfuncs, namelen);
		sprintf(n, "%s_y", name);
		QC_RegisterFieldVar(progfuncs, ev_float, n, (engineofs==-1)?-1:(engineofs+4), (progsofs==-1)?-1:progsofs+1);

		n=PRHunkAlloc(progfuncs, namelen);
		sprintf(n, "%s_z", name);
		QC_RegisterFieldVar(progfuncs, ev_float, n, (engineofs==-1)?-1:(engineofs+8), (progsofs==-1)?-1:progsofs+2);
	}
	else if (engineofs >= 0)
	{	//the engine is setting up a list of required field indexes.

		//paranoid checking of the offset.
	/*	for (i = 0; i < numfields-1; i++)
		{
			if (field[i].ofs == ((unsigned)engineofs)/4)
			{
				if (type == ev_float && field[i].type == ev_vector)	//check names
				{
					if (strncmp(field[i].name, name, strlen(field[i].name)))
						Sys_Error("Duplicated offset");
				}
				else
					Sys_Error("Duplicated offset");
			}
		}*/
		if (engineofs&3)
			Sys_Error("field %s is %i&3", name, (int)engineofs);
		field[fnum].ofs = ofs = engineofs/4;
	}
	else
	{	//we just found a new fieldname inside a progs
		field[fnum].ofs = ofs = fields_size/4;	//add on the end

		//if the progs field offset matches annother offset in the same progs, make it match up with the earlier one.
		if (progsofs>=0)
		{
			for (i = 0; i < numfields-1; i++)
			{
				if (field[i].progsofs == (unsigned)progsofs)
				{
//					printf("found union field %s %i -> %i\n", field[i].name, field[i].progsofs, field[i].ofs);
					field[fnum].ofs = ofs = field[i].ofs;
					break;
				}
			}
		}
	}
//	if (type != ev_vector)
		if (fields_size < (ofs+type_size[type])*4)
			fields_size = (ofs+type_size[type])*4;

	if (max_fields_size && fields_size > max_fields_size)
		Sys_Error("Allocated too many additional fields after ents were inited.");
	field[fnum].type = type;

	field[fnum].progsofs = progsofs;

//	printf("Field %s %i -> %i\n", name, field[fnum].progsofs,field[fnum].ofs);
	
	//we've finished setting the structure	
	return ofs - progfuncs->fieldadjust;
}