use the script api for parsing ent data and savegames

This is an imperfect revision of history.
This commit is contained in:
Bill Currie 2004-11-12 02:39:00 +00:00 committed by Jeff Teunissen
parent 53f4d13f43
commit 0e324d1851
3 changed files with 150 additions and 121 deletions

View file

@ -1203,6 +1203,7 @@ struct progs_s {
int pr_edict_size; ///< in bytes
int pr_edictareasize; ///< for bounds checking, starts at 0
func_t edict_parse;
int pr_argc;

View file

@ -45,12 +45,12 @@ static __attribute__ ((unused)) const char rcsid[] =
#include "QF/cvar.h"
#include "QF/dstring.h"
#include "QF/hash.h"
#include "QF/idparse.h"
#include "QF/progs.h"
#include "QF/qdefs.h"
#include "QF/qfplist.h"
#include "QF/qendian.h"
#include "QF/quakefs.h"
#include "QF/script.h"
#include "QF/sys.h"
#include "QF/zone.h"
#include "QF/va.h"
@ -218,7 +218,7 @@ ED_NewString (progs_t *pr, const char *string)
Can parse either fields or globals
returns false if error
*/
static qboolean
qboolean
ED_ParseEpair (progs_t *pr, pr_type_t *base, ddef_t *key, const char *s)
{
int i;
@ -288,8 +288,8 @@ ED_ParseEpair (progs_t *pr, pr_type_t *base, ddef_t *key, const char *s)
ent should be a properly initialized empty edict.
Used for initial level load and for savegames.
*/
const char *
ED_ParseEdict (progs_t *pr, const char *data, edict_t *ent)
void
ED_ParseEdict (progs_t *pr, script_t *script, edict_t *ent)
{
ddef_t *key;
qboolean anglehack;
@ -302,17 +302,17 @@ ED_ParseEdict (progs_t *pr, const char *data, edict_t *ent)
if (ent != *(pr)->edicts) // hack
memset (&ent->v, 0, pr->progs->entityfields * 4);
while (1) { // go through all the dictionary pairs
// parse key
data = COM_Parse (data);
if (com_token[0] == '}')
break;
if (!data)
// go through all the dictionary pairs
while (1) {
if (!Script_GetToken (script, 1))
PR_Error (pr, "ED_ParseEntity: EOF without closing brace");
// parse key
if (script->token->str[0] == '}')
break;
token = com_token;
token = script->token->str;
// anglehack is to allow QuakeEd to write single scalar angles
// and allow them to be turned into vectors. (FIXME...)
// and allow them to be turned into vectors.
if (!strcmp (token, "angle")) {
token = "angles";
anglehack = true;
@ -332,11 +332,12 @@ ED_ParseEdict (progs_t *pr, const char *data, edict_t *ent)
}
// parse value
data = COM_Parse (data);
if (!data)
//FIXME shouldn't cross line
if (!Script_GetToken (script, 1))
PR_Error (pr, "ED_ParseEntity: EOF without closing brace");
token = script->token->str;
if (com_token[0] == '}')
if (token[0] == '}')
PR_Error (pr, "ED_ParseEntity: closing brace without data");
init = true;
@ -349,7 +350,7 @@ ED_ParseEdict (progs_t *pr, const char *data, edict_t *ent)
key = PR_FindField (pr, keyname->str);
if (!key) {
if (!pr->parse_field
|| !pr->parse_field (pr, keyname->str, com_token)) {
|| !pr->parse_field (pr, keyname->str, token)) {
Sys_Printf ("'%s' is not a field\n", keyname->str);
continue;
}
@ -370,31 +371,32 @@ ED_ParseEdict (progs_t *pr, const char *data, edict_t *ent)
ent->free = true;
dstring_delete (keyname);
return data;
}
void
ED_ParseGlobals (progs_t *pr, script_t *script)
{
dstring_t *keyname = dstring_new ();
ddef_t *key;
dstring_t *keyname = dstring_new ();
ddef_t *key;
const char *token;
while (1) {
// parse key
data = COM_Parse (data);
if (com_token[0] == '}')
break;
if (!data)
if (!Script_GetToken (script, 1))
PR_Error (pr, "ED_ParseEntity: EOF without closing brace");
token = script->token->str;
if (token[0] == '}')
break;
dstring_copystr (keyname, com_token);
dstring_copystr (keyname, script->token->str);
// parse value
data = COM_Parse (data);
if (!data)
//FIXME shouldn't cross line
if (!Script_GetToken (script, 1))
PR_Error (pr, "ED_ParseEntity: EOF without closing brace");
token = script->token->str;
if (com_token[0] == '}')
if (token[0] == '}')
PR_Error (pr, "ED_ParseEntity: closing brace without data");
key = PR_FindGlobal (pr, keyname->str);
@ -403,7 +405,7 @@ ED_ParseGlobals (progs_t *pr, script_t *script)
continue;
}
if (!ED_ParseEpair (pr, pr->pr_globals, key, com_token))
if (!ED_ParseEpair (pr, pr->pr_globals, key, token))
PR_Error (pr, "ED_ParseGlobals: parse error");
}
dstring_delete (keyname);
@ -423,7 +425,7 @@ ED_ParseGlobals (progs_t *pr, script_t *script)
to call ED_CallSpawnFunctions () to let the objects initialize themselves.
*/
static void
ED_ParseOld (progs_t *pr, const char *data)
ED_ParseOld (progs_t *pr, script_t *script)
{
edict_t *ent = NULL;
int inhibit = 0;
@ -431,19 +433,17 @@ ED_ParseOld (progs_t *pr, const char *data)
pr_type_t *classname;
ddef_t *def;
while (1) { // parse ents
while (Script_GetToken (script, 1)) { // parse ents
// parse the opening brace
data = COM_Parse (data);
if (!data)
break;
if (com_token[0] != '{')
PR_Error (pr, "ED_LoadFromFile: found %s when expecting {", com_token);
if (script->token->str[0] != '{')
PR_Error (pr, "ED_LoadFromFile: found %s when expecting {",
script->token->str);
if (!ent)
ent = EDICT_NUM (pr, 0);
else
ent = ED_Alloc (pr);
data = ED_ParseEdict (pr, data, ent);
ED_ParseEdict (pr, script, ent);
// remove things from different skill levels or deathmatch
if (pr->prune_edict && pr->prune_edict (pr, ent)) {
@ -483,12 +483,35 @@ ED_ParseOld (progs_t *pr, const char *data)
void
ED_LoadFromFile (progs_t *pr, const char *data)
{
if (*data == '(') {
// new style (plist) entity data
plitem_t *plist = PL_GetPropertyList (data);
plist = plist;
} else {
// oldstyle entity data
ED_ParseOld (pr, data);
script_t *script;
if (pr->edict_parse) {
PR_PushFrame (pr);
P_INT (pr, 0) = PR_SetTempString (pr, data);
PR_ExecuteProgram (pr, pr->edict_parse);
PR_PopFrame (pr);
return;
}
script = Script_New ();
Script_Start (script, "ent data", data);
if (Script_GetToken (script, 1)) {
if (*script->token->str == '(') {
// new style (plist) entity data
plitem_t *plist = PL_GetPropertyList (data);
plist = plist;
} else {
// oldstyle entity data
Script_UngetToken (script);
ED_ParseOld (pr, script);
}
}
Script_Delete (script);
}
void
ED_EntityParseFunction (progs_t *pr)
{
pr->edict_parse = P_FUNCTION (pr, 0);
}

View file

@ -39,16 +39,18 @@ static __attribute__ ((unused)) const char rcsid[] =
#endif
#include "QF/cbuf.h"
#include "QF/idparse.h"
#include "QF/cmd.h"
#include "QF/cvar.h"
#include "QF/va.h"
#include "QF/screen.h"
#include "QF/msg.h"
#include "QF/model.h"
#include "QF/console.h"
#include "QF/cvar.h"
#include "QF/dstring.h"
#include "QF/idparse.h"
#include "QF/keys.h"
#include "QF/model.h"
#include "QF/msg.h"
#include "QF/screen.h"
#include "QF/script.h"
#include "QF/sys.h"
#include "QF/va.h"
#include "client.h"
#include "compat.h"
@ -410,22 +412,26 @@ Host_Connect_f (void)
Writes a SAVEGAME_COMMENT_LENGTH character comment describing the current
*/
static void
Host_SavegameComment (char *text)
Host_SavegameComment (QFile *file)
{
int i;
char kills[20];
unsigned i;
dstring_t *comment = dstring_newstr ();
for (i = 0; i < SAVEGAME_COMMENT_LENGTH; i++)
text[i] = ' ';
memcpy (text, cl.levelname, strlen (cl.levelname));
snprintf (kills, sizeof (kills), "kills:%3i/%3i", cl.stats[STAT_MONSTERS],
cl.stats[STAT_TOTALMONSTERS]);
memcpy (text + 22, kills, strlen (kills));
dsprintf (comment, "%-21s kills:%3i/%3i", cl.levelname,
cl.stats[STAT_MONSTERS], cl.stats[STAT_TOTALMONSTERS]);
if ((i = comment->size - 1) < SAVEGAME_COMMENT_LENGTH) {
comment->size = SAVEGAME_COMMENT_LENGTH + 1;
dstring_adjust (comment);
while (i < comment->size - 1)
comment->str[i++] = ' ';
}
comment->str[SAVEGAME_COMMENT_LENGTH] = '\n';
comment->str[SAVEGAME_COMMENT_LENGTH + 1] = '\0';
// convert space to _ to make stdio happy
for (i = 0; i < SAVEGAME_COMMENT_LENGTH; i++)
if (text[i] == ' ')
text[i] = '_';
text[SAVEGAME_COMMENT_LENGTH] = '\0';
if (comment->str[i] == ' ')
comment->str[i] = '_';
Qwrite (file, comment->str, SAVEGAME_COMMENT_LENGTH + 1);
}
static void
@ -434,7 +440,6 @@ Host_Savegame_f (void)
char name[256];
QFile *f;
int i;
char comment[SAVEGAME_COMMENT_LENGTH + 1];
if (cmd_source != src_command)
return;
@ -484,8 +489,7 @@ Host_Savegame_f (void)
}
Qprintf (f, "%i\n", SAVEGAME_VERSION);
Host_SavegameComment (comment);
Qprintf (f, "%s\n", comment);
Host_SavegameComment (f);
for (i = 0; i < NUM_SPAWN_PARMS; i++)
Qprintf (f, "%f\n", svs.clients->spawn_parms[i]);
Qprintf (f, "%d\n", current_skill);
@ -513,77 +517,88 @@ Host_Savegame_f (void)
static void
Host_Loadgame_f (void)
{
char name[MAX_OSPATH];
dstring_t *name = 0;
QFile *f;
char mapname[MAX_QPATH];
char *mapname = 0;
script_t *script = 0;
char *str = 0;
float time, tfloat;
char str[32768];
const char *start;
unsigned int i;
int r;
edict_t *ent;
int entnum;
int version;
float spawn_parms[NUM_SPAWN_PARMS];
char buf[100];
if (cmd_source != src_command)
return;
goto end;
if (Cmd_Argc () != 2) {
Con_Printf ("load <savename> : load a game\n");
return;
goto end;
}
cls.demonum = -1; // stop demo loop in case this fails
snprintf (name, sizeof (name), "%s/%s",
qfs_gamedir->dir.def, Cmd_Argv (1));
QFS_DefaultExtension (name, ".sav");
name = dstring_newstr ();
dsprintf (name, "%s/%s", qfs_gamedir->dir.def, Cmd_Argv (1));
name->size += 4;
dstring_adjust (name);
QFS_DefaultExtension (name->str, ".sav");
// we can't call SCR_BeginLoadingPlaque, because too much stack space has
// been used. The menu calls it before stuffing loadgame command
// SCR_BeginLoadingPlaque ();
Con_Printf ("Loading game from %s...\n", name);
f = QFS_Open (name, "rz");
Con_Printf ("Loading game from %s...\n", name->str);
f = QFS_Open (name->str, "rz");
if (!f) {
Con_Printf ("ERROR: couldn't open.\n");
return;
goto end;
}
str = malloc (Qfilesize (f) + 1);
i = Qread (f, str, Qfilesize (f));
str[i] = 0;
Qclose (f);
Qgets (f, buf, sizeof (buf));
sscanf (buf, "%i\n", &version);
script = Script_New ();
Script_Start (script, name->str, str);
Script_GetToken (script, 1);
sscanf (script->token->str, "%i", &version);
if (version != SAVEGAME_VERSION) {
Qclose (f);
Con_Printf ("Savegame is version %i, not %i\n", version,
SAVEGAME_VERSION);
return;
goto end;
}
Qgets (f, buf, sizeof (buf));
sscanf (buf, "%s\n", str);
// savegame comment (ignored)
Script_GetToken (script, 1);
for (i = 0; i < NUM_SPAWN_PARMS; i++) {
Qgets (f, buf, sizeof (buf));
sscanf (buf, "%f\n", &spawn_parms[i]);
Script_GetToken (script, 1);
sscanf (script->token->str, "%f", &spawn_parms[i]);
}
// this silliness is so we can load 1.06 save files, which have float skill
// values
Qgets (f, buf, sizeof (buf));
sscanf (buf, "%f\n", &tfloat);
Script_GetToken (script, 1);
sscanf (script->token->str, "%f", &tfloat);
current_skill = (int) (tfloat + 0.1);
Cvar_SetValue (skill, (float) current_skill);
Qgets (f, buf, sizeof (buf));
sscanf (buf, "%s\n", mapname);
Qgets (f, buf, sizeof (buf));
sscanf (buf, "%f\n", &time);
Script_GetToken (script, 1);
mapname = strdup (script->token->str);
Script_GetToken (script, 1);
sscanf (script->token->str, "%f", &time);
CL_Disconnect_f ();
SV_SpawnServer (mapname);
if (!sv.active) {
Con_Printf ("Couldn't load map\n");
return;
Con_Printf ("Couldn't load map %s\n", mapname);
goto end;
}
sv.paused = true; // pause until all clients connect
sv.loadgame = true;
@ -591,44 +606,27 @@ Host_Loadgame_f (void)
// load the light styles
for (i = 0; i < MAX_LIGHTSTYLES; i++) {
char *s;
Qgets (f, buf, sizeof (buf));
sscanf (buf, "%s\n", str);
s = Hunk_Alloc (strlen (str) + 1);
strcpy (s, str);
Script_GetToken (script, 1);
s = Hunk_Alloc (strlen (script->token->str) + 1);
strcpy (s, script->token->str);
sv.lightstyles[i] = s;
}
// load the edicts out of the savegame file
entnum = -1; // -1 is the globals
while (!Qeof (f)) {
for (i = 0; i < sizeof (str) - 1; i++) {
r = Qgetc (f);
if (r == EOF || !r)
break;
str[i] = r;
if (r == '}') {
i++;
break;
}
}
if (i == sizeof (str) - 1)
Sys_Error ("Loadgame buffer overflow");
str[i] = 0;
start = str;
start = COM_Parse (str);
if (!com_token[0])
break; // end of file
if (strcmp (com_token, "{"))
while (Script_GetToken (script, 1)) {
if (strcmp (script->token->str, "{"))
Sys_Error ("First token isn't a brace");
if (entnum == -1) { // parse the global vars
ED_ParseGlobals (&sv_pr_state, start);
ED_ParseGlobals (&sv_pr_state, script);
} else { // parse an edict
ent = EDICT_NUM (&sv_pr_state, entnum);
memset (&ent->v, 0, sv_pr_state.progs->entityfields * 4);
ent->free = false;
ED_ParseEdict (&sv_pr_state, start, ent);
ED_ParseEdict (&sv_pr_state, script, ent);
// link it into the bsp tree
if (!ent->free)
@ -641,8 +639,6 @@ Host_Loadgame_f (void)
sv.num_edicts = entnum;
sv.time = time;
Qclose (f);
for (i = 0; i < NUM_SPAWN_PARMS; i++)
svs.clients->spawn_parms[i] = spawn_parms[i];
@ -650,6 +646,15 @@ Host_Loadgame_f (void)
CL_EstablishConnection ("local");
Host_Reconnect_f ();
}
end:
if (mapname)
free (mapname);
if (script)
Script_Delete (script);
if (str)
free (str);
if (name)
dstring_delete (name);
}
//============================================================================