2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * xdump.c: Experimental extended dump
30 * Known contributors to this file:
31 * Markus Armbruster, 2004-2006
35 * Dump everything under the sun
37 * Static game data (configuration):
38 * - Item characteristics: ichr[]
39 * - Land unit characteristics: lchr[]
40 * - Nuke characteristics: nchr[]
41 * - Plane characteristics: plchr[]
42 * - Product characteristics: pchr[]
43 * - Sector designation characteristics: dchr[]
44 * - Sector infrastructure characteristics: intrchr[]
45 * - Ship characteristics: mchr[]
46 * - News item characteristics: rpt[]
47 * - News page headings: page_headings[]
48 * - Commands: player_coms[] (TODO)
49 * - Configuration: configkeys[]
52 * - Sectors: EF_SECTOR (superseding dump)
53 * - Land units: EF_LAND (superseding ldump)
54 * - Lost: EF_LOST (superseding lost)
55 * - Nukes: EF_NUKE (superseding ndump)
56 * - Planes: EF_PLANE (superseding pdump)
57 * - Ships: EF_SHIP (superseding sdump)
59 * - Treaties: EF_TREATY
60 * - Power: EF_POWER (TODO)
61 * - Nations: EF_NATION
63 * - Map: EF_MAP (TODO)
64 * - Bmap: EF_BMAP (TODO)
69 * See doc/xdump for motivation, syntax, semantics, and rationale.
70 * Make sure to keep it up-to-date!
73 /* TODO don't dump stuff that's useless due to options */
88 * Evaluate a attribute of an object into VAL, return VAL.
89 * TYPE is the attribute's type.
90 * PTR points to the context object.
91 * The attribute is stored there at offset OFF + IDX * S, where S is
93 * LEN is the #array elements if it is an array, else zero.
95 static struct valstr *
96 xdeval(struct valstr *val,
97 nsc_type type, void *ptr, ptrdiff_t off, int idx, int len)
100 val->val_cat = NSC_OFF;
101 val->val_as.sym.off = off;
102 val->val_as.sym.len = len;
103 val->val_as.sym.idx = idx;
104 nstr_exec_val(val, player->cnum, ptr, NSC_NOTYPE);
105 return val; /* FIXME nstr_exec_val() should return VAL */
108 /* Dump VAL prefixed with SEP, return " ". */
110 xdprval(struct valstr *val, char *sep)
112 unsigned char *s, *e, *l;
114 switch (val->val_type) {
116 pr("%s%ld", sep, val->val_as.lng);
119 pr("%s%#g", sep, val->val_as.dbl);
122 s = (unsigned char *)val->val_as.str.base;
125 l = s + val->val_as.str.maxsz;
126 /* FIXME maxsz == INT_MAX ! */
129 e < l && *e != '"' && *e != '\\' && isgraph(*e);
132 pr("%.*s", (int)(e-s), s);
151 * Dump field values of a context object.
152 * CA[] describes fields.
153 * PTR points to context object.
156 xdflds(struct castr ca[], void *ptr)
162 for (i = 0; ca[i].ca_name; ++i) {
163 if (ca[i].ca_flags & NSC_DEITY && !player->god)
165 if (ca[i].ca_flags & NSC_EXTRA)
167 n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
170 xdeval(&val, ca[i].ca_type, ptr, ca[i].ca_off, j, ca[i].ca_len);
171 sep = xdprval(&val, sep);
177 * Dump header for dump NAME.
178 * If META, it's for the meta-data dump rather than the data dump.
181 xdhdr(char *name, int meta)
183 pr("XDUMP %s%s %ld\n", meta ? "meta " : "", name, (long)time(NULL));
186 /* Dump footer for a dump that dumped N objects. */
194 * Is object P of type TYPE visible to the player?
195 * TODO: Fold this into interators.
198 xdvisible(int type, void *p)
200 struct empobj *gp = p;
201 struct trtstr *tp = p;
202 struct lonstr *lp = p;
209 return gp->own == player->cnum || player->god;
215 return gp->own != 0 && (gp->own == player->cnum || player->god);
217 return ((struct natstr *)p)->nat_stat != STAT_UNUSED;
219 return gp->own == player->cnum;
221 return ((struct nwsstr *)p)->nws_vrb != 0
222 && (!opt_HIDDEN || player->god); /* FIXME */
224 return tp->trt_status != TS_FREE
225 && (tp->trt_cna == player->cnum || tp->trt_cnb == player->cnum
228 if (lp->l_status == LS_FREE)
230 if (lp->l_status == LS_SIGNED)
232 return lp->l_loner == player->cnum || lp->l_lonee == player->cnum
238 tlev = ((struct mchrstr *)p)->m_tech;
241 tlev = ((struct plchrstr *)p)->pl_tech;
244 tlev = ((struct lchrstr *)p)->l_tech;
246 natp = getnatp(player->cnum);
247 return player->god || tlev <= (int)(1.25 * natp->nat_level[NAT_TLEV]);
249 tlev = ((struct nchrstr *)p)->n_tech;
250 if (drnuke_const > MIN_DRNUKE_CONST) {
251 natp = getnatp(player->cnum);
252 if (tlev > (int)((int)(1.25 * natp->nat_level[NAT_RLEV])
258 return ((struct rptstr *)p)->r_newspage != 0;
260 return ((struct empfile *)p)->cadef != NULL;
267 * Dump items of type TYPE selected by ARG.
268 * Return RET_OK on success, RET_SYN on error.
271 xditem(int type, char *arg)
276 char buf[2048]; /* FIXME buffer size? */
282 if (!snxtitem(&ni, type, arg))
285 xdhdr(ef_nameof(type), 0);
288 while (nxtitem(&ni, buf)) {
289 if (!xdvisible(type, buf))
302 * Dump meta-data for items of type TYPE.
308 struct castr *ca = ef_cadef(type);
315 xdhdr(ef_nameof(type), 1);
317 for (i = 0; ca[i].ca_name; i++) {
318 if (ca[i].ca_flags & NSC_DEITY && !player->god)
320 if (ca[i].ca_flags & NSC_EXTRA)
322 xdflds(mdchr_ca, &ca[i]);
333 * Dump configkeys[], return RET_OK.
334 * If META, dump meta-data rather than data.
339 static struct castr vers_ca = {
340 NSC_STRINGY, 0, sizeof(PACKAGE_STRING), 0, "version", EF_BAD
348 xdhdr("version", meta);
352 xdflds(mdchr_ca, &vers_ca);
355 for (kp = configkeys; kp->km_key; ++kp) {
356 if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
357 ca.ca_type = kp->km_type;
361 ca.ca_name = kp->km_key;
362 ca.ca_table = EF_BAD;
363 xdflds(mdchr_ca, &ca);
372 xdeval(&val, vers_ca.ca_type, version, vers_ca.ca_off, 0, vers_ca.ca_len);
373 sep = xdprval(&val, "");
374 for (kp = configkeys; kp->km_key; ++kp) {
375 if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
376 xdeval(&val, kp->km_type, kp->km_data, 0, 0, 0);
377 sep = xdprval(&val, sep);
387 /* Experimental extended dump command */
396 p = getstarg(player->argp[1], "Table name, or meta? ", buf);
397 if (p && strcmp(p, "meta") == 0) {
399 p = getstarg(player->argp[2], "Table name? ", buf);
404 type = isdigit(p[0]) ? atoi(p) : ef_byname(p);
405 if (type >= 0 && type < EF_MAX) {
408 else if ((EF_IS_GAME_STATE(type) || EF_IS_VIEW(type))
409 && (player->ncomstat & NORM) != NORM) {
410 pr("Access to table %s denied\n", ef_nameof(type));
413 return xditem(type, player->argp[2]);
414 } else if (!strncmp(p, "ver", strlen(p))) {