2002-01-28 17:15:36 +00:00
|
|
|
/*
|
|
|
|
pr_edict.c
|
|
|
|
|
|
|
|
entity dictionary
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2003-01-15 15:31:36 +00:00
|
|
|
|
2002-01-28 17:15:36 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "QF/crc.h"
|
|
|
|
#include "QF/cvar.h"
|
2003-05-08 23:24:02 +00:00
|
|
|
#include "QF/dstring.h"
|
2002-01-28 17:15:36 +00:00
|
|
|
#include "QF/hash.h"
|
2012-05-06 12:35:42 +00:00
|
|
|
#include "QF/mathlib.h"
|
2002-01-28 17:15:36 +00:00
|
|
|
#include "QF/progs.h"
|
|
|
|
#include "QF/qendian.h"
|
2002-08-27 07:16:28 +00:00
|
|
|
#include "QF/quakefs.h"
|
2002-01-28 17:15:36 +00:00
|
|
|
#include "QF/sys.h"
|
|
|
|
#include "QF/zone.h"
|
|
|
|
|
2022-02-05 05:07:45 +00:00
|
|
|
#include "QF/progs/pr_type.h"
|
|
|
|
|
2002-01-28 17:15:36 +00:00
|
|
|
#include "compat.h"
|
|
|
|
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
VISIBLE int pr_boundscheck;
|
|
|
|
static cvar_t pr_boundscheck_cvar = {
|
|
|
|
.name = "pr_boundscheck",
|
|
|
|
.description =
|
|
|
|
"Server progs bounds checking",
|
|
|
|
.default_value = "0",
|
|
|
|
.flags = CVAR_NONE,
|
|
|
|
.value = { .type = &cexpr_int, .value = &pr_boundscheck },
|
|
|
|
};
|
|
|
|
int pr_deadbeef_ents;
|
|
|
|
static cvar_t pr_deadbeef_ents_cvar = {
|
|
|
|
.name = "pr_deadbeef_ents",
|
|
|
|
.description =
|
|
|
|
"set to clear unallocated memory to 0xdeadbeef",
|
|
|
|
.default_value = "0",
|
|
|
|
.flags = CVAR_NONE,
|
|
|
|
.value = { .type = &cexpr_int, .value = &pr_deadbeef_ents },
|
|
|
|
};
|
|
|
|
int pr_deadbeef_locals;
|
|
|
|
static cvar_t pr_deadbeef_locals_cvar = {
|
|
|
|
.name = "pr_deadbeef_locals",
|
|
|
|
.description =
|
|
|
|
"set to clear uninitialized local vars to 0xdeadbeef",
|
|
|
|
.default_value = "0",
|
|
|
|
.flags = CVAR_NONE,
|
|
|
|
.value = { .type = &cexpr_int, .value = &pr_deadbeef_locals },
|
|
|
|
};
|
|
|
|
int pr_faultchecks;
|
|
|
|
static cvar_t pr_faultchecks_cvar = {
|
|
|
|
.name = "pr_faultchecks",
|
|
|
|
.description =
|
|
|
|
"capture and handle division by 0 in progs",
|
|
|
|
.default_value = "0",
|
|
|
|
.flags = CVAR_NONE,
|
|
|
|
.value = { .type = &cexpr_int, .value = &pr_faultchecks },
|
|
|
|
};
|
2002-11-13 19:26:44 +00:00
|
|
|
|
2002-01-28 17:15:36 +00:00
|
|
|
static const char *
|
2012-07-18 13:34:37 +00:00
|
|
|
function_get_key (const void *f, void *_pr)
|
2002-01-28 17:15:36 +00:00
|
|
|
{
|
|
|
|
progs_t *pr = (progs_t*)_pr;
|
|
|
|
dfunction_t *func = (dfunction_t*)f;
|
2021-12-31 06:02:31 +00:00
|
|
|
return PR_GetString (pr, func->name);
|
2002-01-28 17:15:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
2012-07-18 13:34:37 +00:00
|
|
|
var_get_key (const void *d, void *_pr)
|
2002-01-28 17:15:36 +00:00
|
|
|
{
|
|
|
|
progs_t *pr = (progs_t*)_pr;
|
2020-02-22 13:33:44 +00:00
|
|
|
pr_def_t *def = (pr_def_t*)d;
|
|
|
|
return PR_GetString (pr, def->name);
|
2002-01-28 17:15:36 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 05:07:45 +00:00
|
|
|
static const char *
|
|
|
|
type_get_key (const void *t, void *_pr)
|
|
|
|
{
|
|
|
|
progs_t *pr = (progs_t *) _pr;
|
|
|
|
__auto_type type = (qfot_type_t *) t;
|
|
|
|
return PR_GetString (pr, type->encoding);
|
|
|
|
}
|
|
|
|
|
2003-03-12 22:31:44 +00:00
|
|
|
static void
|
|
|
|
file_error (progs_t *pr, const char *path)
|
|
|
|
{
|
|
|
|
Sys_Printf ("failed to load %s\n", path);
|
|
|
|
}
|
|
|
|
|
2002-06-11 17:24:37 +00:00
|
|
|
static void *
|
2020-02-21 12:17:28 +00:00
|
|
|
load_file (progs_t *pr, const char *path, off_t *size)
|
2002-06-11 17:24:37 +00:00
|
|
|
{
|
2020-02-21 12:17:28 +00:00
|
|
|
void *data = QFS_LoadHunkFile (QFS_FOpenFile (path));
|
|
|
|
*size = qfs_filesize;
|
|
|
|
return data;
|
2002-06-11 17:24:37 +00:00
|
|
|
}
|
|
|
|
|
2002-01-29 20:53:44 +00:00
|
|
|
static void *
|
|
|
|
allocate_progs_mem (progs_t *pr, int size)
|
|
|
|
{
|
2021-07-28 06:01:45 +00:00
|
|
|
return Hunk_AllocName (0, size, pr->progs_name);
|
2002-01-29 20:53:44 +00:00
|
|
|
}
|
|
|
|
|
2002-01-30 06:21:20 +00:00
|
|
|
static void
|
|
|
|
free_progs_mem (progs_t *pr, void *mem)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-10-11 04:24:03 +00:00
|
|
|
static int
|
2022-01-26 07:55:14 +00:00
|
|
|
align_size (int size, int align)
|
2018-10-11 04:24:03 +00:00
|
|
|
{
|
2022-01-26 07:55:14 +00:00
|
|
|
size += align - 1;
|
|
|
|
size &= ~(align - 1);
|
2018-10-11 04:24:03 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2018-10-11 01:06:48 +00:00
|
|
|
PR_LoadProgsFile (progs_t *pr, QFile *file, int size)
|
2002-01-28 17:15:36 +00:00
|
|
|
{
|
2007-04-06 00:47:41 +00:00
|
|
|
size_t i;
|
2004-01-03 08:43:57 +00:00
|
|
|
int mem_size;
|
2010-11-20 00:02:56 +00:00
|
|
|
int offset_tweak;
|
2002-01-29 20:53:44 +00:00
|
|
|
dprograms_t progs;
|
2010-11-20 00:02:56 +00:00
|
|
|
byte *base;
|
2021-03-29 08:31:17 +00:00
|
|
|
byte *heap;
|
2020-02-22 13:44:08 +00:00
|
|
|
ddef_t *global_ddefs;
|
|
|
|
ddef_t *field_ddefs;
|
2022-01-26 07:55:14 +00:00
|
|
|
// absolute minimum alignment is 4 bytes
|
2022-03-30 17:51:05 +00:00
|
|
|
int edict_alignment = __alignof__(pr_lvec4_t);
|
2002-01-28 17:15:36 +00:00
|
|
|
|
2010-01-13 06:27:19 +00:00
|
|
|
if (!pr->file_error)
|
|
|
|
pr->file_error = file_error;
|
|
|
|
if (!pr->load_file)
|
|
|
|
pr->load_file = load_file;
|
|
|
|
if (!pr->allocate_progs_mem)
|
|
|
|
pr->allocate_progs_mem = allocate_progs_mem;
|
|
|
|
if (!pr->free_progs_mem)
|
|
|
|
pr->free_progs_mem = free_progs_mem;
|
|
|
|
|
|
|
|
PR_Resources_Clear (pr);
|
|
|
|
if (pr->progs)
|
|
|
|
pr->free_progs_mem (pr, pr->progs);
|
2002-01-29 20:53:44 +00:00
|
|
|
pr->progs = 0;
|
2010-01-13 06:27:19 +00:00
|
|
|
|
2002-01-29 20:53:44 +00:00
|
|
|
if (Qread (file, &progs, sizeof (progs)) != sizeof (progs))
|
2003-01-29 20:32:44 +00:00
|
|
|
PR_Error (pr, "error reading header");
|
2002-01-28 17:15:36 +00:00
|
|
|
|
|
|
|
// store prog crc
|
2002-01-29 20:53:44 +00:00
|
|
|
pr->crc = CRC_Block ((byte*)&progs, sizeof (progs));
|
2002-01-28 17:15:36 +00:00
|
|
|
|
2010-01-13 06:36:54 +00:00
|
|
|
pr->denorm_found = 0;
|
|
|
|
|
2002-01-28 17:15:36 +00:00
|
|
|
// byte swap the header
|
2002-01-29 20:53:44 +00:00
|
|
|
for (i = 0; i < sizeof (progs) / 4; i++)
|
|
|
|
((int *) &progs)[i] = LittleLong (((int *) &progs)[i]);
|
2002-01-28 17:15:36 +00:00
|
|
|
|
2002-01-29 20:53:44 +00:00
|
|
|
if (progs.version != PROG_VERSION
|
2022-01-03 04:56:43 +00:00
|
|
|
&& progs.version != PROG_V6P_VERSION
|
2002-01-29 20:53:44 +00:00
|
|
|
&& progs.version != PROG_ID_VERSION) {
|
|
|
|
if (progs.version < 0x00fff000) {
|
2007-04-06 00:47:41 +00:00
|
|
|
PR_Error (pr, "%s has unrecognised version number (%u)",
|
2002-01-29 20:53:44 +00:00
|
|
|
pr->progs_name, progs.version);
|
2002-01-28 17:15:36 +00:00
|
|
|
} else {
|
|
|
|
PR_Error (pr,
|
|
|
|
"%s has unrecognised version number (%02x.%03x.%03x)"
|
|
|
|
" [%02x.%03x.%03x expected]",
|
2002-01-29 20:53:44 +00:00
|
|
|
pr->progs_name,
|
|
|
|
progs.version >> 24,
|
|
|
|
(progs.version >> 12) & 0xfff,
|
|
|
|
progs.version & 0xfff,
|
2002-01-28 17:15:36 +00:00
|
|
|
PROG_VERSION >> 24,
|
|
|
|
(PROG_VERSION >> 12) & 0xfff,
|
|
|
|
PROG_VERSION & 0xfff);
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 07:55:14 +00:00
|
|
|
if (progs.version == PROG_VERSION) {
|
|
|
|
// ensure SIMD types can be aligned (qfcc will align them within
|
|
|
|
// the progs memory map, so the engine needs to ensure the progs memory
|
|
|
|
// is aligned)
|
2022-03-30 17:51:05 +00:00
|
|
|
edict_alignment = __alignof__(pr_lvec4_t);
|
2022-01-26 07:55:14 +00:00
|
|
|
}
|
2022-03-30 17:51:05 +00:00
|
|
|
// edict size is in words
|
|
|
|
edict_alignment /= 4;
|
2002-01-28 17:15:36 +00:00
|
|
|
|
2010-11-20 00:02:56 +00:00
|
|
|
// Some compilers (eg, FTE) put extra data between the header and the
|
|
|
|
// strings section. What's worse, they de-align the data.
|
2022-01-26 10:30:25 +00:00
|
|
|
offset_tweak = progs.strings.offset % sizeof (pr_int_t);
|
2010-11-20 00:02:56 +00:00
|
|
|
offset_tweak = (sizeof (pr_int_t) - offset_tweak) % sizeof (pr_int_t);
|
|
|
|
|
2002-01-29 20:53:44 +00:00
|
|
|
// size of progs themselves
|
2010-11-20 00:02:56 +00:00
|
|
|
pr->progs_size = size + offset_tweak;
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_dev, "Programs occupy %iK.\n", size / 1024);
|
2002-01-28 17:15:36 +00:00
|
|
|
|
2022-03-30 17:51:05 +00:00
|
|
|
pr->progs_size = align_size (pr->progs_size, 64);
|
|
|
|
pr->zone_size = align_size (pr->zone_size, 64);
|
|
|
|
pr->stack_size = align_size (pr->stack_size, 64);
|
2002-01-28 17:15:36 +00:00
|
|
|
|
2013-01-17 05:11:54 +00:00
|
|
|
// size of edict asked for by progs, but at least 1
|
|
|
|
pr->pr_edict_size = max (1, progs.entityfields);
|
2022-03-30 17:51:05 +00:00
|
|
|
pr->pr_edict_size = align_size (pr->pr_edict_size, edict_alignment);
|
2021-03-25 13:01:31 +00:00
|
|
|
pr->pr_edict_area_size = pr->max_edicts * pr->pr_edict_size;
|
2022-03-30 17:51:05 +00:00
|
|
|
pr->pr_edict_area_size = align_size (pr->pr_edict_area_size, 64);
|
2002-01-29 20:53:44 +00:00
|
|
|
|
2013-01-17 05:11:54 +00:00
|
|
|
mem_size = pr->pr_edict_area_size * sizeof (pr_type_t);
|
2021-03-25 13:01:31 +00:00
|
|
|
mem_size += pr->progs_size + pr->zone_size + pr->stack_size;
|
2022-02-05 09:37:23 +00:00
|
|
|
// space for return buffer
|
|
|
|
mem_size += PR_MAX_RETURN * sizeof (pr_type_t);
|
2002-01-29 20:53:44 +00:00
|
|
|
|
2018-10-11 04:24:03 +00:00
|
|
|
// +1 for a nul terminator
|
2004-01-03 08:43:57 +00:00
|
|
|
pr->progs = pr->allocate_progs_mem (pr, mem_size + 1);
|
2002-01-29 20:53:44 +00:00
|
|
|
if (!pr->progs)
|
|
|
|
return;
|
2018-10-11 04:24:03 +00:00
|
|
|
// Place a nul at the end of progs memory to ensure any unterminated
|
|
|
|
// strings within progs memory don't run off the end.
|
2004-01-03 08:43:57 +00:00
|
|
|
((byte *) pr->progs)[mem_size] = 0;
|
2002-01-29 20:53:44 +00:00
|
|
|
|
|
|
|
memcpy (pr->progs, &progs, sizeof (progs));
|
2010-11-20 00:02:56 +00:00
|
|
|
base = (byte *) (pr->progs + 1) + offset_tweak;
|
|
|
|
Qread (file, base, size - sizeof (progs));
|
|
|
|
CRC_ProcessBlock (base, &pr->crc, size - sizeof (progs));
|
2002-01-29 20:53:44 +00:00
|
|
|
|
2013-01-17 05:11:54 +00:00
|
|
|
pr->pr_edict_area = (pr_type_t *)((byte *) pr->progs + pr->progs_size);
|
2018-10-11 04:24:03 +00:00
|
|
|
|
2021-03-29 08:31:17 +00:00
|
|
|
base -= sizeof (progs); // offsets are from file start
|
|
|
|
heap = (byte *) &pr->pr_edict_area[pr->pr_edict_area_size];
|
|
|
|
|
2020-02-24 15:25:24 +00:00
|
|
|
pr->zone = 0;
|
2018-10-11 01:22:10 +00:00
|
|
|
if (pr->zone_size) {
|
|
|
|
//FIXME zone_size needs to be at least as big as memzone_t, but
|
|
|
|
//memzone_t is opaque so its size is unknown
|
2021-03-29 08:31:17 +00:00
|
|
|
pr->zone = (memzone_t *) heap;
|
2018-10-11 01:22:10 +00:00
|
|
|
}
|
2002-01-28 17:15:36 +00:00
|
|
|
|
2022-01-26 10:30:25 +00:00
|
|
|
pr->pr_functions = (dfunction_t *) (base + pr->progs->functions.offset);
|
|
|
|
pr->pr_strings = (char *) base + pr->progs->strings.offset;
|
2021-03-29 08:31:17 +00:00
|
|
|
pr->pr_stringsize = (heap - base) + pr->zone_size;
|
2022-01-26 10:30:25 +00:00
|
|
|
global_ddefs = (ddef_t *) (base + pr->progs->globaldefs.offset);
|
|
|
|
field_ddefs = (ddef_t *) (base + pr->progs->fielddefs.offset);
|
|
|
|
pr->pr_statements = (dstatement_t *) (base + pr->progs->statements.offset);
|
2010-11-20 00:02:56 +00:00
|
|
|
|
2022-01-26 10:30:25 +00:00
|
|
|
pr->pr_globals = (pr_type_t *) (base + pr->progs->globals.offset);
|
2021-03-25 13:01:31 +00:00
|
|
|
pr->stack = (pr_type_t *) ((byte *) pr->zone + pr->zone_size);
|
2018-10-11 04:24:03 +00:00
|
|
|
pr->stack_bottom = pr->stack - pr->pr_globals;
|
|
|
|
pr->globals_size = (pr_type_t *) ((byte *) pr->stack + pr->stack_size)
|
2002-01-29 20:53:44 +00:00
|
|
|
- pr->pr_globals;
|
2022-01-21 11:33:15 +00:00
|
|
|
if (pr->globals.stack && pr->stack_bottom) {
|
|
|
|
*pr->globals.stack = pr->globals_size;
|
|
|
|
}
|
2022-02-05 09:37:23 +00:00
|
|
|
pr->pr_return_buffer = pr->pr_globals + pr->globals_size;
|
2018-10-11 04:24:03 +00:00
|
|
|
|
2018-10-11 01:22:10 +00:00
|
|
|
if (pr->zone) {
|
2011-03-08 13:44:56 +00:00
|
|
|
PR_Zone_Init (pr);
|
2018-10-11 01:22:10 +00:00
|
|
|
}
|
2002-01-28 17:15:36 +00:00
|
|
|
|
2021-12-26 11:33:37 +00:00
|
|
|
Hash_FlushTable (pr->function_hash);
|
|
|
|
Hash_FlushTable (pr->global_hash);
|
|
|
|
Hash_FlushTable (pr->field_hash);
|
2022-02-05 05:07:45 +00:00
|
|
|
Hash_FlushTable (pr->type_hash);
|
2002-01-28 17:15:36 +00:00
|
|
|
|
|
|
|
// byte swap the lumps
|
2022-01-26 10:30:25 +00:00
|
|
|
for (i = 0; i < pr->progs->statements.count; i++) {
|
2002-01-28 17:15:36 +00:00
|
|
|
pr->pr_statements[i].op = LittleShort (pr->pr_statements[i].op);
|
|
|
|
pr->pr_statements[i].a = LittleShort (pr->pr_statements[i].a);
|
|
|
|
pr->pr_statements[i].b = LittleShort (pr->pr_statements[i].b);
|
|
|
|
pr->pr_statements[i].c = LittleShort (pr->pr_statements[i].c);
|
|
|
|
}
|
|
|
|
|
2022-01-26 10:30:25 +00:00
|
|
|
for (i = 0; i < pr->progs->functions.count; i++) {
|
2002-01-28 17:15:36 +00:00
|
|
|
pr->pr_functions[i].first_statement =
|
|
|
|
LittleLong (pr->pr_functions[i].first_statement);
|
2022-01-27 01:55:06 +00:00
|
|
|
pr->pr_functions[i].params_start =
|
|
|
|
LittleLong (pr->pr_functions[i].params_start);
|
2021-12-31 06:02:31 +00:00
|
|
|
pr->pr_functions[i].name = LittleLong (pr->pr_functions[i].name);
|
|
|
|
pr->pr_functions[i].file = LittleLong (pr->pr_functions[i].file);
|
2022-01-27 01:55:06 +00:00
|
|
|
pr->pr_functions[i].numparams =
|
|
|
|
LittleLong (pr->pr_functions[i].numparams);
|
2002-01-28 17:15:36 +00:00
|
|
|
pr->pr_functions[i].locals = LittleLong (pr->pr_functions[i].locals);
|
2021-12-31 06:02:31 +00:00
|
|
|
if (pr->pr_functions[i].name)
|
2012-07-20 07:17:26 +00:00
|
|
|
Hash_Add (pr->function_hash, &pr->pr_functions[i]);
|
2002-01-28 17:15:36 +00:00
|
|
|
}
|
|
|
|
|
2020-02-22 13:33:44 +00:00
|
|
|
if (pr->pr_globaldefs) {
|
|
|
|
free (pr->pr_globaldefs);
|
|
|
|
}
|
2022-01-26 10:30:25 +00:00
|
|
|
pr->pr_globaldefs = calloc (pr->progs->globaldefs.count, sizeof (pr_def_t));
|
2020-02-22 13:33:44 +00:00
|
|
|
|
2022-01-26 10:30:25 +00:00
|
|
|
for (i = 0; i < pr->progs->globaldefs.count; i++) {
|
2020-02-23 14:29:58 +00:00
|
|
|
pr_ushort_t safe_type = global_ddefs[i].type & ~DEF_SAVEGLOBAL;
|
2020-02-22 13:44:08 +00:00
|
|
|
global_ddefs[i].type = LittleShort (global_ddefs[i].type);
|
|
|
|
global_ddefs[i].ofs = LittleShort (global_ddefs[i].ofs);
|
2021-12-31 06:02:31 +00:00
|
|
|
global_ddefs[i].name = LittleLong (global_ddefs[i].name);
|
2020-02-22 13:33:44 +00:00
|
|
|
|
2020-02-22 13:44:08 +00:00
|
|
|
pr->pr_globaldefs[i].type = global_ddefs[i].type;
|
2020-02-23 14:29:58 +00:00
|
|
|
pr->pr_globaldefs[i].size = pr_type_size[safe_type];
|
2020-02-22 13:44:08 +00:00
|
|
|
pr->pr_globaldefs[i].ofs = global_ddefs[i].ofs;
|
2021-12-31 06:02:31 +00:00
|
|
|
pr->pr_globaldefs[i].name = global_ddefs[i].name;
|
2002-01-28 17:15:36 +00:00
|
|
|
Hash_Add (pr->global_hash, &pr->pr_globaldefs[i]);
|
|
|
|
}
|
|
|
|
|
2020-02-22 13:33:44 +00:00
|
|
|
if (pr->pr_fielddefs) {
|
|
|
|
free (pr->pr_fielddefs);
|
|
|
|
}
|
2022-01-26 10:30:25 +00:00
|
|
|
pr->pr_fielddefs = calloc (pr->progs->fielddefs.count, sizeof (pr_def_t));
|
|
|
|
for (i = 0; i < pr->progs->fielddefs.count; i++) {
|
2020-02-22 13:44:08 +00:00
|
|
|
field_ddefs[i].type = LittleShort (field_ddefs[i].type);
|
|
|
|
if (field_ddefs[i].type & DEF_SAVEGLOBAL)
|
|
|
|
PR_Error (pr, "PR_LoadProgs: DEF_SAVEGLOBAL on field def %zd", i);
|
|
|
|
field_ddefs[i].ofs = LittleShort (field_ddefs[i].ofs);
|
2021-12-31 06:02:31 +00:00
|
|
|
field_ddefs[i].name = LittleLong (field_ddefs[i].name);
|
2020-02-22 13:44:08 +00:00
|
|
|
|
|
|
|
pr->pr_fielddefs[i].type = field_ddefs[i].type;
|
|
|
|
pr->pr_fielddefs[i].ofs = field_ddefs[i].ofs;
|
2021-12-31 06:02:31 +00:00
|
|
|
pr->pr_fielddefs[i].name = field_ddefs[i].name;
|
2002-01-28 17:15:36 +00:00
|
|
|
Hash_Add (pr->field_hash, &pr->pr_fielddefs[i]);
|
|
|
|
}
|
|
|
|
|
2022-01-26 10:30:25 +00:00
|
|
|
for (i = 0; i < pr->progs->globals.count; i++)
|
2002-01-28 17:15:36 +00:00
|
|
|
((int *) pr->pr_globals)[i] = LittleLong (((int *) pr->pr_globals)[i]);
|
2020-02-22 13:33:44 +00:00
|
|
|
|
2022-02-05 05:07:45 +00:00
|
|
|
pr_def_t *types_def = PR_FindGlobal (pr, ".type_encodings");
|
|
|
|
if (types_def) {
|
|
|
|
__auto_type encodings = &G_STRUCT (pr, qfot_type_encodings_t,
|
|
|
|
types_def->ofs);
|
|
|
|
pr->type_encodings = encodings->types;
|
|
|
|
qfot_type_t *type;
|
|
|
|
for (pr_ptr_t type_ptr = 4; type_ptr < encodings->size;
|
|
|
|
type_ptr += type->size) {
|
|
|
|
type = &G_STRUCT (pr, qfot_type_t, pr->type_encodings + type_ptr);
|
|
|
|
Hash_Add (pr->type_hash, type);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pr->type_encodings = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pr_def_t *xdefs_def = PR_FindGlobal (pr, ".xdefs");
|
2020-02-22 13:33:44 +00:00
|
|
|
if (xdefs_def) {
|
|
|
|
pr_xdefs_t *xdefs = &G_STRUCT (pr, pr_xdefs_t, xdefs_def->ofs);
|
|
|
|
xdef_t *xdef = &G_STRUCT (pr, xdef_t, xdefs->xdefs);
|
|
|
|
pr_def_t *def;
|
2022-01-26 10:30:25 +00:00
|
|
|
for (def = pr->pr_globaldefs, i = 0; i < pr->progs->globaldefs.count;
|
2020-02-22 13:33:44 +00:00
|
|
|
i++, xdef++, def++) {
|
|
|
|
def->ofs = xdef->ofs;
|
|
|
|
def->type_encoding = xdef->type;
|
|
|
|
}
|
2022-01-26 10:30:25 +00:00
|
|
|
for (def = pr->pr_fielddefs, i = 0; i < pr->progs->fielddefs.count;
|
2020-02-22 13:33:44 +00:00
|
|
|
i++, xdef++, def++) {
|
|
|
|
def->ofs = xdef->ofs;
|
|
|
|
def->type_encoding = xdef->type;
|
|
|
|
}
|
2021-04-25 03:20:32 +00:00
|
|
|
} else {
|
|
|
|
pr_def_t *def;
|
2022-01-26 10:30:25 +00:00
|
|
|
for (def = pr->pr_globaldefs, i = 0; i < pr->progs->globaldefs.count;
|
2021-04-25 03:20:32 +00:00
|
|
|
i++, def++) {
|
2021-06-13 13:13:47 +00:00
|
|
|
def->size = pr_type_size[def->type & ~DEF_SAVEGLOBAL];
|
2021-04-25 03:20:32 +00:00
|
|
|
}
|
2022-01-26 10:30:25 +00:00
|
|
|
for (def = pr->pr_fielddefs, i = 0; i < pr->progs->fielddefs.count;
|
2021-04-25 03:20:32 +00:00
|
|
|
i++, def++) {
|
2021-06-13 13:13:47 +00:00
|
|
|
def->size = pr_type_size[def->type & ~DEF_SAVEGLOBAL];
|
2021-04-25 03:20:32 +00:00
|
|
|
}
|
2020-02-22 13:33:44 +00:00
|
|
|
}
|
2020-02-25 06:19:21 +00:00
|
|
|
pr->pr_trace = 0;
|
|
|
|
pr->pr_trace_depth = 0;
|
|
|
|
pr->pr_xfunction = 0;
|
|
|
|
pr->pr_xstatement = 0;
|
|
|
|
pr->pr_depth = 0;
|
|
|
|
pr->localstack_used = 0;
|
2020-02-25 08:28:32 +00:00
|
|
|
pr->pr_argc = 0;
|
2002-01-28 17:15:36 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2003-11-20 07:46:56 +00:00
|
|
|
PR_AddLoadFunc (progs_t *pr, int (*func)(progs_t *))
|
|
|
|
{
|
|
|
|
if (pr->num_load_funcs == pr->max_load_funcs) {
|
|
|
|
int n;
|
|
|
|
pr->max_load_funcs += 8;
|
|
|
|
n = pr->max_load_funcs;
|
|
|
|
pr->load_funcs = realloc (pr->load_funcs,
|
|
|
|
n * sizeof (pr_load_func_t *));
|
|
|
|
SYS_CHECKMEM (pr->load_funcs);
|
|
|
|
}
|
|
|
|
pr->load_funcs[pr->num_load_funcs++] = func;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2004-01-16 08:02:31 +00:00
|
|
|
PR_AddLoadFinishFunc (progs_t *pr, int (*func)(progs_t *))
|
|
|
|
{
|
|
|
|
if (pr->num_load_finish_funcs == pr->max_load_finish_funcs) {
|
|
|
|
int n;
|
|
|
|
pr->max_load_finish_funcs += 8;
|
|
|
|
n = pr->max_load_finish_funcs;
|
|
|
|
pr->load_finish_funcs = realloc (pr->load_finish_funcs,
|
|
|
|
n * sizeof (pr_load_func_t *));
|
|
|
|
SYS_CHECKMEM (pr->load_finish_funcs);
|
|
|
|
}
|
|
|
|
pr->load_finish_funcs[pr->num_load_finish_funcs++] = func;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
pr_run_ctors (progs_t *pr)
|
|
|
|
{
|
2020-02-25 12:23:13 +00:00
|
|
|
pr_uint_t fnum;
|
2004-01-16 08:02:31 +00:00
|
|
|
dfunction_t *func;
|
|
|
|
|
2022-01-26 10:30:25 +00:00
|
|
|
for (fnum = 0; fnum < pr->progs->functions.count; fnum++) {
|
2004-01-16 08:02:31 +00:00
|
|
|
func = pr->pr_functions + fnum;
|
2021-12-31 06:02:31 +00:00
|
|
|
if (strequal (PR_GetString (pr, func->name), ".ctor"))
|
2004-01-16 08:02:31 +00:00
|
|
|
PR_ExecuteProgram (pr, fnum);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-01-21 08:09:47 +00:00
|
|
|
static int (*load_funcs_1[])(progs_t *) = {
|
|
|
|
PR_LoadStrings,
|
2012-07-13 01:22:25 +00:00
|
|
|
PR_RelocateBuiltins,
|
2004-01-21 08:09:47 +00:00
|
|
|
PR_LoadDebug,
|
|
|
|
0,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int (*load_funcs_2[])(progs_t *) = {
|
|
|
|
PR_ResolveGlobals,
|
|
|
|
PR_Check_Opcodes,
|
|
|
|
0,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
run_load_funcs (progs_t *pr, int (**load_funcs)(progs_t *))
|
|
|
|
{
|
|
|
|
int (**lf)(progs_t *);
|
|
|
|
|
|
|
|
for (lf = load_funcs; *lf; lf++)
|
|
|
|
if (!(*lf)(pr))
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE int
|
2003-11-20 07:46:56 +00:00
|
|
|
PR_RunLoadFuncs (progs_t *pr)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2004-01-21 08:09:47 +00:00
|
|
|
memset (&pr->globals, 0, sizeof (pr->globals));
|
|
|
|
pr->fields.nextthink = -1;
|
|
|
|
pr->fields.frame = -1;
|
|
|
|
pr->fields.think = -1;
|
|
|
|
pr->fields.this = -1;
|
|
|
|
|
2004-02-07 00:04:00 +00:00
|
|
|
if (!run_load_funcs(pr, load_funcs_1))
|
|
|
|
return 0;
|
2004-01-21 08:09:47 +00:00
|
|
|
if (pr->resolve)
|
|
|
|
if (!pr->resolve (pr))
|
2003-11-20 07:46:56 +00:00
|
|
|
return 0;
|
2004-02-07 00:04:00 +00:00
|
|
|
if (!run_load_funcs(pr, load_funcs_2))
|
|
|
|
return 0;
|
2003-11-20 07:46:56 +00:00
|
|
|
|
|
|
|
for (i = 0; i < pr->num_load_funcs; i++)
|
2004-01-16 08:02:31 +00:00
|
|
|
if (!pr->load_funcs[i] (pr))
|
2003-11-20 07:46:56 +00:00
|
|
|
return 0;
|
2004-01-16 08:02:31 +00:00
|
|
|
|
2020-04-02 08:28:37 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
VISIBLE int
|
|
|
|
PR_RunPostLoadFuncs (progs_t *pr)
|
|
|
|
{
|
2004-01-16 08:02:31 +00:00
|
|
|
if (!pr_run_ctors (pr))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
while (pr->num_load_finish_funcs)
|
|
|
|
if (!pr->load_finish_funcs[--pr->num_load_finish_funcs] (pr))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
2003-11-20 07:46:56 +00:00
|
|
|
}
|
|
|
|
|
2002-01-28 17:15:36 +00:00
|
|
|
/*
|
|
|
|
PR_LoadProgs
|
|
|
|
*/
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2018-10-11 01:06:48 +00:00
|
|
|
PR_LoadProgs (progs_t *pr, const char *progsname)
|
2002-01-28 17:15:36 +00:00
|
|
|
{
|
2002-08-27 07:16:28 +00:00
|
|
|
QFile *file;
|
2014-01-23 02:57:57 +00:00
|
|
|
file = QFS_FOpenFile (progsname);
|
2002-01-29 20:53:44 +00:00
|
|
|
|
|
|
|
pr->progs_name = progsname;
|
2002-03-20 22:57:26 +00:00
|
|
|
if (file) {
|
2018-10-11 01:06:48 +00:00
|
|
|
PR_LoadProgsFile (pr, file, qfs_filesize);
|
2002-03-20 22:57:26 +00:00
|
|
|
Qclose (file);
|
|
|
|
}
|
2002-01-28 17:15:36 +00:00
|
|
|
if (!pr->progs)
|
|
|
|
return;
|
|
|
|
|
2020-04-02 08:28:37 +00:00
|
|
|
if (!PR_RunLoadFuncs (pr)) {
|
2002-01-28 17:15:36 +00:00
|
|
|
PR_Error (pr, "unable to load %s", progsname);
|
2020-04-02 08:28:37 +00:00
|
|
|
}
|
|
|
|
if (!pr->debug_handler && !PR_RunPostLoadFuncs (pr)) {
|
2002-01-28 17:15:36 +00:00
|
|
|
PR_Error (pr, "unable to load %s", progsname);
|
2020-04-02 08:28:37 +00:00
|
|
|
}
|
2002-01-28 17:15:36 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2002-01-28 17:15:36 +00:00
|
|
|
PR_Init_Cvars (void)
|
|
|
|
{
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
Cvar_Register (&pr_boundscheck_cvar, 0, 0);
|
|
|
|
Cvar_Register (&pr_deadbeef_ents_cvar, 0, 0);
|
|
|
|
Cvar_Register (&pr_deadbeef_locals_cvar, 0, 0);
|
|
|
|
Cvar_Register (&pr_faultchecks_cvar, 0, 0);
|
2002-01-28 17:15:36 +00:00
|
|
|
PR_Debug_Init_Cvars ();
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2020-02-23 06:56:36 +00:00
|
|
|
PR_Init (progs_t *pr)
|
2002-01-28 17:15:36 +00:00
|
|
|
{
|
2020-02-23 06:56:36 +00:00
|
|
|
PR_Resources_Init (pr);
|
2020-02-24 15:21:56 +00:00
|
|
|
PR_Strings_Init (pr);
|
2020-02-23 06:56:36 +00:00
|
|
|
PR_Debug_Init (pr);
|
2021-12-26 11:33:37 +00:00
|
|
|
pr->function_hash = Hash_NewTable (1021, function_get_key, 0, pr,
|
2022-05-12 08:54:23 +00:00
|
|
|
pr->hashctx);
|
|
|
|
pr->global_hash = Hash_NewTable (1021, var_get_key, 0, pr, pr->hashctx);
|
|
|
|
pr->field_hash = Hash_NewTable (1021, var_get_key, 0, pr, pr->hashctx);
|
|
|
|
pr->type_hash = Hash_NewTable (1021, type_get_key, 0, pr, pr->hashctx);
|
2002-01-28 17:15:36 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 09:23:32 +00:00
|
|
|
VISIBLE void
|
|
|
|
PR_Shutdown (progs_t *pr)
|
|
|
|
{
|
|
|
|
PR_Resources_Shutdown (pr);
|
|
|
|
PR_Builtins_Shutdown (pr);
|
|
|
|
|
|
|
|
free (pr->load_funcs);
|
|
|
|
free (pr->load_finish_funcs);
|
|
|
|
pr->num_load_funcs = pr->max_load_funcs = 0;
|
|
|
|
pr->num_load_finish_funcs = pr->max_load_finish_funcs = 0;
|
|
|
|
|
|
|
|
Hash_DelTable (pr->function_hash);
|
|
|
|
Hash_DelTable (pr->global_hash);
|
|
|
|
Hash_DelTable (pr->field_hash);
|
|
|
|
Hash_DelTable (pr->type_hash);
|
|
|
|
pr->function_hash = 0;
|
|
|
|
pr->global_hash = 0;
|
|
|
|
pr->field_hash = 0;
|
|
|
|
pr->type_hash = 0;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2002-01-28 17:15:36 +00:00
|
|
|
PR_Error (progs_t *pr, const char *error, ...)
|
|
|
|
{
|
|
|
|
va_list argptr;
|
2020-03-26 02:44:02 +00:00
|
|
|
dstring_t *string = dstring_new ();//FIXME leaks when debugging
|
2002-01-28 17:15:36 +00:00
|
|
|
|
|
|
|
va_start (argptr, error);
|
2003-05-08 23:24:02 +00:00
|
|
|
dvsprintf (string, error, argptr);
|
2002-01-28 17:15:36 +00:00
|
|
|
va_end (argptr);
|
|
|
|
|
2020-03-24 06:35:42 +00:00
|
|
|
if (pr->debug_handler) {
|
2020-03-26 02:44:02 +00:00
|
|
|
pr->debug_handler (prd_error, string->str, pr->debug_data);
|
2020-03-24 06:35:42 +00:00
|
|
|
// not expected to return, but if so, behave as if there was no handler
|
|
|
|
}
|
2003-05-08 23:24:02 +00:00
|
|
|
Sys_Error ("%s: %s", pr->progs_name, string->str);
|
2002-01-28 17:15:36 +00:00
|
|
|
}
|