VISIBLE void Cmd_StuffCmds (cbuf_t *cbuf) { int i, j; dstring_t *build; if (!*com_cmdline) return; build = dstring_newstr (); // pull out the commands for (i = 0; com_cmdline[i]; i++) { if (com_cmdline[i] == '+') { i++; for (j = i; (com_cmdline[j] && !((j == 0 || isspace((byte) com_cmdline[j - 1])) && ((com_cmdline[j] == '+') || (com_cmdline[j] == '-')))); j++) ; dstring_appendsubstr (build, com_cmdline + i, j - i); dstring_appendstr (build, "\n"); i = j - 1; } } if (build->str[0]) Cbuf_InsertText (cbuf, build->str); dstring_delete (build); }
const char * type_get_encoding (const type_t *type) { static dstring_t *encoding; if (!encoding) encoding = dstring_newstr(); else dstring_clearstr (encoding); encode_type (encoding, type); return save_string (encoding->str); }
static void Cmd_Alias_f (void) { cmdalias_t *alias; dstring_t *cmd; int i, c; const char *s; if (Cmd_Argc () == 1) { Sys_Printf ("Current alias commands:\n"); for (alias = cmd_alias; alias; alias = alias->next) Sys_Printf ("alias %s \"%s\"\n", alias->name, alias->value); return; } s = Cmd_Argv (1); // if the alias already exists, reuse it alias = (cmdalias_t *) Hash_Find (cmd_alias_hash, s); if (Cmd_Argc () == 2) { if (alias) Sys_Printf ("alias %s \"%s\"\n", alias->name, alias->value); return; } if (alias) free ((char *) alias->value); else if (!Cmd_Exists (s)) { cmdalias_t **a; alias = calloc (1, sizeof (cmdalias_t)); SYS_CHECKMEM (alias); alias->name = strdup (s); Hash_Add (cmd_alias_hash, alias); for (a = &cmd_alias; *a; a = &(*a)->next) if (strcmp ((*a)->name, alias->name) >= 0) break; alias->next = *a; *a = alias; Cmd_AddCommand (alias->name, Cmd_Runalias_f, "Alias command."); } else { Sys_Printf ("alias: a command with the name \"%s\" already exists.\n", s); return; } // copy the rest of the command line cmd = dstring_newstr (); c = Cmd_Argc (); for (i = 2; i < c; i++) { dstring_appendstr (cmd, Cmd_Argv (i)); if (i != c - 1) dstring_appendstr (cmd, " "); } alias->value = dstring_freeze (cmd); }
/* SV_Map_f handle a map <mapname> command from the console or progs. */ static void SV_Map_f (void) { const char *level; char *expanded; QFile *f; if (!curlevel) curlevel = dstring_newstr (); if (Cmd_Argc () > 2) { SV_Printf ("map <levelname> : continue game on a new level\n"); return; } if (Cmd_Argc () == 1) { SV_Printf ("map is %s \"%s\" (%s)\n", curlevel->str, PR_GetString (&sv_pr_state, SVstring (sv.edicts, message)), nice_time (sv.time)); return; } level = Cmd_Argv (1); // check to make sure the level exists expanded = nva ("maps/%s.bsp", level); f = QFS_FOpenFile (expanded); if (!f) { SV_Printf ("Can't find %s\n", expanded); free (expanded); return; } Qclose (f); free (expanded); if (sv.recording_demo) SV_Stop (0); SV_qtvChanging (); SV_BroadcastCommand ("changing\n"); SV_SendMessagesToAll (); dstring_copystr (curlevel, level); SV_SpawnServer (level); SV_qtvReconnect (); SV_BroadcastCommand ("reconnect\n"); }
static void parse_script (script_t *script) { int cmd; while (Script_GetToken (script, true)) { if (strcasecmp ("$LOAD", script->token->str) == 0) { Script_GetToken (script, false); load_image (script->token->str); continue; } if (strcasecmp ("$DEST", script->token->str) == 0) { Script_GetToken (script, false); dstring_copystr (&destfile, script->token->str); continue; } if (strcasecmp ("$SINGLEDEST", script->token->str) == 0) { Script_GetToken (script, false); dstring_copystr (&destfile, script->token->str); savesingle = true; continue; } if (!lumpname) lumpname = dstring_newstr (); dstring_copystr (lumpname, script->token->str); Script_GetToken (script, false); for (cmd = 0; commands[cmd].name; cmd++) { if (!strcasecmp (script->token->str, commands[cmd].name)) { commands[cmd].func (script); break; } } if (!commands[cmd].name) Sys_Error ("Unrecognized token '%s' at line %i", script->token->str, script->line); //grabbed++; if (savesingle) write_file (); else write_lump (TYP_LUMPY + cmd); } }
static void Host_Loadgame_f (void) { dstring_t *name = 0; QFile *f; char *mapname = 0; script_t *script = 0; plitem_t *game = 0; plitem_t *list; plitem_t *item; char *script_data = 0; int i; int entnum; int count; int version; float spawn_parms[NUM_SPAWN_PARMS]; if (cmd_source != src_command) goto end; if (Cmd_Argc () != 2) { Sys_Printf ("load <savename> : load a game\n"); goto end; } cls.demonum = -1; // stop demo loop in case this fails name = dstring_newstr (); dsprintf (name, "%s/%s", qfs_gamedir->dir.def, Cmd_Argv (1)); QFS_DefaultExtension (name, ".sav"); cl.loading = true; CL_UpdateScreen (cl.time); Sys_Printf ("Loading game from %s...\n", name->str); f = QFS_Open (name->str, "rz"); if (!f) { Sys_Printf ("ERROR: couldn't open.\n"); goto end; } script_data = malloc (Qfilesize (f) + 1); i = Qread (f, script_data, Qfilesize (f)); script_data[i] = 0; Qclose (f); script = Script_New (); script->single = ""; // disable {}()': lexing Script_Start (script, name->str, script_data); Script_GetToken (script, 1); if (strequal (script->token->str, PACKAGE_NAME)) { if (!Script_TokenAvailable (script, 1)) { Sys_Printf ("Unexpected EOF reading %s\n", name->str); goto end; } game = PL_GetPropertyList (script->p); } else { sscanf (script->token->str, "%i", &version); if (version != SAVEGAME_VERSION) { Sys_Printf ("Savegame is version %i, not %i\n", version, SAVEGAME_VERSION); goto end; } game = convert_to_game_dict (script); } item = PL_ObjectForKey (game, "spawn_parms"); for (i = 0; i < NUM_SPAWN_PARMS; i++) { if (i >= PL_A_NumObjects (item)) break; spawn_parms[i] = atof (PL_String (PL_ObjectAtIndex (item, i))); } current_skill = atoi (PL_String (PL_ObjectForKey (game, "current_skill"))); Cvar_SetValue (skill, current_skill); mapname = strdup (PL_String (PL_ObjectForKey (game, "name"))); CL_Disconnect_f (); SV_SpawnServer (mapname); if (!sv.active) { Sys_Printf ("Couldn't load map %s\n", mapname); goto end; } sv.paused = true; // pause until all clients connect sv.loadgame = true; list = PL_ObjectForKey (game, "lightstyles"); for (i = 0; i < MAX_LIGHTSTYLES; i++) { const char *style; char *str; if (i >= PL_A_NumObjects (list)) break; item = PL_ObjectAtIndex (list, i); style = PL_String (item); sv.lightstyles[i] = str = Hunk_Alloc (strlen (style) + 1); strcpy (str, style); } ED_InitGlobals (&sv_pr_state, PL_ObjectForKey (game, "globals")); list = PL_ObjectForKey (game, "entities"); entnum = 0; count = PL_A_NumObjects (list); if (count > sv.max_edicts) Host_Error ("too many entities in saved game. adjust max_edicts\n"); for (entnum = 0; entnum < count; entnum++) { plitem_t *entity = PL_ObjectAtIndex (list, entnum); edict_t *ent = EDICT_NUM (&sv_pr_state, entnum); memset (&ent->v, 0, sv_pr_state.progs->entityfields * 4); ent->free = false; ED_InitEntity (&sv_pr_state, entity, ent); // link it into the bsp tree if (!ent->free) SV_LinkEdict (ent, false); } sv.num_edicts = entnum; sv.time = atof (PL_String (PL_ObjectForKey (game, "time"))); for (i = 0; i < NUM_SPAWN_PARMS; i++) svs.clients->spawn_parms[i] = spawn_parms[i]; if (cls.state != ca_dedicated) { CL_EstablishConnection ("local"); Host_Reconnect_f (); } end: if (game) PL_Free (game); if (mapname) free (mapname); if (script) Script_Delete (script); if (script_data) free (script_data); if (name) dstring_delete (name); }
static void Host_Savegame_f (void) { dstring_t *name; const char *save_name; char *save_text; QFile *f; int i; char *bup1, *bup2 = 0; if (cmd_source != src_command) return; if (!sv.active) { Sys_Printf ("Not playing a local game.\n"); return; } if (cl.intermission) { Sys_Printf ("Can't save in intermission.\n"); return; } if (svs.maxclients != 1) { Sys_Printf ("Can't save multiplayer games.\n"); return; } if (Cmd_Argc () != 2) { Sys_Printf ("save <savename> : save a game\n"); return; } if (strstr (Cmd_Argv (1), "..")) { Sys_Printf ("Relative pathnames are not allowed.\n"); return; } for (i = 0; i < svs.maxclients; i++) { if (svs.clients[i].active && (SVfloat (svs.clients[i].edict, health) <= 0)) { Sys_Printf ("Can't savegame with a dead player\n"); return; } } save_name = Cmd_Argv (1); name = dstring_newstr (); if (strcmp (save_name, "quick") == 0) { bup2 = nva ("%s/%s%d.sav", qfs_gamedir->dir.def, save_name, MAX_QUICK); QFS_Remove (bup2); for (i = MAX_QUICK - 1; i > 0; i--) { bup1 = nva ("%s/%s%d.sav", qfs_gamedir->dir.def, save_name, i); QFS_Rename (bup1, bup2); free (bup2); bup2 = bup1; } } dsprintf (name, "%s/%s", qfs_gamedir->dir.def, save_name); QFS_DefaultExtension (name, ".sav"); if (bup2) { QFS_Rename (name->str, bup2); free (bup2); } Sys_Printf ("Saving game to %s...\n", name->str); f = QFS_WOpen (name->str, 0); dstring_delete (name); if (!f) { Sys_Printf ("ERROR: couldn't open.\n"); return; } save_text = PL_WritePropertyList (game_dict ()); Qprintf (f, "%s\n%s", PACKAGE_NAME, save_text); free (save_text); Qclose (f); Sys_Printf ("done.\n"); }