/* Copyright (C) 1996-2001 Id Software, Inc. Copyright (C) 2002-2009 John Fitzgibbons and others Copyright (C) 2010-2014 QuakeSpasm developers Copyright (C) 2019 Alison G. Watson 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 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "q_defs.h" ddef_t *pr_globaldefs; cvar_t nomonsters = {"nomonsters", "0", CVAR_NONE}; cvar_t gamecfg = {"gamecfg", "0", CVAR_NONE}; cvar_t scratch1 = {"scratch1", "0", CVAR_NONE}; cvar_t scratch2 = {"scratch2", "0", CVAR_NONE}; cvar_t scratch3 = {"scratch3", "0", CVAR_NONE}; cvar_t scratch4 = {"scratch4", "0", CVAR_NONE}; cvar_t savedgamecfg = {"savedgamecfg", "0", CVAR_ARCHIVE}; cvar_t saved1 = {"saved1", "0", CVAR_ARCHIVE}; cvar_t saved2 = {"saved2", "0", CVAR_ARCHIVE}; cvar_t saved3 = {"saved3", "0", CVAR_ARCHIVE}; cvar_t saved4 = {"saved4", "0", CVAR_ARCHIVE}; static void PR_LoadProgHeader(byte const *data) { progs.version = ReadLittleLong(&data); progs.crc = ReadLittleLong(&data); progs.ofs_statements = ReadLittleLong(&data); progs.numstatements = ReadLittleLong(&data); progs.ofs_globaldefs = ReadLittleLong(&data); progs.numglobaldefs = ReadLittleLong(&data); progs.ofs_fielddefs = ReadLittleLong(&data); progs.numfielddefs = ReadLittleLong(&data); progs.ofs_functions = ReadLittleLong(&data); progs.numfunctions = ReadLittleLong(&data); progs.ofs_strings = ReadLittleLong(&data); progs.numstrings = ReadLittleLong(&data); progs.ofs_globals = ReadLittleLong(&data); progs.numglobals = ReadLittleLong(&data); progs.entityfields = ReadLittleLong(&data); /* round off to next highest whole word address (esp for Alpha) this ensures * that pointers in the engine data area are always properly aligned */ pr_edict_size = progs.entityfields * 4 + sizeof(edict_t) - sizeof(entvars_t); pr_edict_size += sizeof(void *) - 1; pr_edict_size &= ~(sizeof(void *) - 1); if(progs.version != PROG_VERSION) Host_Error("PR_LoadProgHeader: has wrong version number (%" PRIi32 " should be %" PRIi32 ")", progs.version, PROG_VERSION); if(progs.crc != PROGHEADER_CRC) Host_Error("PR_LoadProgHeader: system vars have been modified, progdefs.h is out of date"); if(progs.ofs_strings + progs.numstrings >= com_filesize) Host_Error("PR_LoadProgHeader: strings go past end of file\n"); } static void PR_LoadFunctions(byte const *data) { int32_t i; pr_functions = Hunk_AllocName(sizeof(*pr_functions) * progs.numfunctions, "pr_functions"); for(i = 0; i < progs.numfunctions; i++) { pr_functions[i].first_statement = ReadLittleLong(&data); pr_functions[i].parm_start = ReadLittleLong(&data); pr_functions[i].locals = ReadLittleLong(&data); pr_functions[i].profile = 0; ReadSkip(&data, 4); pr_functions[i].s_name = ReadLittleLong(&data); pr_functions[i].s_file = ReadLittleLong(&data); pr_functions[i].numparms = ReadLittleLong(&data); ReadCopy(pr_functions[i].parm_size, &data, 8); } } static void PR_LoadStrings(byte const *data) { pr_strings = Hunk_Memdup(data, progs.numstrings, "pr_strings"); // initialize the strings pr_numknownstrings = 0; pr_maxknownstrings = 0; if(pr_knownstrings) Z_Free(pr_knownstrings); pr_knownstrings = NULL; PR_SetEngineString(""); } static void PR_LoadDef(ddef_t *def, byte const **data) { def->type = ReadLittleShort(data); def->ofs = ReadLittleShort(data); def->s_name = ReadLittleLong(data); } static void PR_LoadGlobalDefs(byte const *data) { int32_t i; pr_globaldefs = Hunk_AllocName(sizeof(*pr_globaldefs) * progs.numglobaldefs, "pr_globaldefs"); for(i = 0; i < progs.numglobaldefs; i++) { PR_LoadDef(&pr_globaldefs[i], &data); } } static void PR_LoadFieldDefs(byte const *data) { int32_t i; pr_fielddefs = Hunk_AllocName(sizeof(*pr_fielddefs) * progs.numfielddefs, "pr_fielddefs"); pr_alpha_supported = false; //johnfitz for(i = 0; i < progs.numfielddefs; i++) { PR_LoadDef(&pr_fielddefs[i], &data); if(pr_fielddefs[i].type & DEF_SAVEGLOBAL) Host_Error("PR_LoadProgs: field defs cannot have DEF_SAVEGLOBAL"); //johnfitz -- detect alpha support if(!strcmp(&pr_strings[pr_fielddefs[i].s_name], "alpha")) pr_alpha_supported = true; //johnfitz } } static void PR_LoadStatements(byte const *data) { int32_t i; pr_statements = Hunk_AllocName(sizeof(*pr_statements) * progs.numstatements, "pr_statements"); for(i = 0; i < progs.numstatements; i++) { pr_statements[i].op = ReadLittleShort(&data); pr_statements[i].a = ReadLittleShort(&data); pr_statements[i].b = ReadLittleShort(&data); pr_statements[i].c = ReadLittleShort(&data); } } static void PR_LoadGlobals(byte const *data) { int32_t i; // HACK pr_global_struct = Hunk_Memdup(data, progs.numglobals * 4, "pr_global_struct"); /* pr_global_struct = Hunk_AllocName(sizeof(*pr_global_struct), "pr_global_struct"); */ pr_globals = (float *)pr_global_struct; for(i = 0; i < progs.numglobals; i++) pr_globals[i] = LittleFloat(pr_globals[i]); } /* =============== PR_LoadProgs =============== */ void PR_LoadProgs(void) { byte *prog_data; int32_t i; ED_Load(); CRC_Init(&pr_crc); prog_data = COM_LoadTempFile("progs.dat", NULL); if(!prog_data) Host_Error("PR_LoadProgs: couldn't load progs.dat"); Con_DPrintf("Programs occupy %" PRIi32 "K\n", com_filesize / 1024); for(i = 0; i < com_filesize; i++) CRC_ProcessByte(&pr_crc, prog_data[i]); PR_LoadProgHeader(prog_data); PR_LoadStrings(&prog_data[progs.ofs_strings]); PR_LoadStatements(&prog_data[progs.ofs_statements]); PR_LoadFunctions(&prog_data[progs.ofs_functions]); PR_LoadGlobalDefs(&prog_data[progs.ofs_globaldefs]); PR_LoadFieldDefs(&prog_data[progs.ofs_fielddefs]); PR_LoadGlobals(&prog_data[progs.ofs_globals]); } /* =============== PR_Init =============== */ void PR_Init(void) { ED_Init(); Cmd_AddCommand("profile", PR_Profile_f); Cvar_RegisterVariable(&nomonsters); Cvar_RegisterVariable(&gamecfg); Cvar_RegisterVariable(&scratch1); Cvar_RegisterVariable(&scratch2); Cvar_RegisterVariable(&scratch3); Cvar_RegisterVariable(&scratch4); Cvar_RegisterVariable(&savedgamecfg); Cvar_RegisterVariable(&saved1); Cvar_RegisterVariable(&saved2); Cvar_RegisterVariable(&saved3); Cvar_RegisterVariable(&saved4); }