2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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
47 * Dump everything under the sun
49 * Static game data (configuration):
50 * - Item characteristics: ichr[]
51 * - Land unit characteristics: lchr[]
52 * - Nuke characteristics: nchr[]
53 * - Plane characteristics: plchr[]
54 * - Product characteristics: pchr[]
55 * - Sector designation characteristics: dchr[]
56 * - Sector infrastructure characteristics: intrchr[]
57 * - Ship characteristics: mchr[]
58 * - News item characteristics: rpt[]
59 * - News page headings: page_headings[]
60 * - Commands: player_coms[] (TODO)
61 * - Configuration: configkeys[]
64 * - Sectors: EF_SECTOR (superseding dump)
65 * - Land units: EF_LAND (superseding ldump)
66 * - Lost: EF_LOST (superseding lost)
67 * - Nukes: EF_NUKE (superseding ndump)
68 * - Planes: EF_PLANE (superseding pdump)
69 * - Ships: EF_SHIP (superseding sdump)
71 * - Treaties: EF_TREATY
72 * - Power: EF_POWER (TODO)
73 * - Nations: EF_NATION
75 * - Map: EF_MAP (TODO)
76 * - Bmap: EF_BMAP (TODO)
80 /* FIXME document dump format */
81 /* TODO don't dump stuff that's useless due to options */
84 * Evaluate a attribute of an object into VAL, return VAL.
85 * TYPE is the attribute's type.
86 * PTR points to the context object.
87 * The attribute is stored there at offset OFF + IDX * S, where S is
89 * LEN is the #array elements if it is an array, else zero.
91 static struct valstr *
92 xdeval(struct valstr *val,
93 nsc_type type, void *ptr, ptrdiff_t off, int idx, int len)
96 val->val_cat = NSC_OFF;
97 val->val_as.sym.off = off;
98 val->val_as.sym.len = len;
99 val->val_as.sym.idx = idx;
100 nstr_exec_val(val, player->cnum, ptr, NSC_NOTYPE);
101 return val; /* FIXME nstr_exec_val() should return VAL */
104 /* Dump VAL prefixed with SEP, return " ". */
106 xdprval(struct valstr *val, char *sep)
108 unsigned char *s, *e, *l;
110 switch (val->val_type) {
113 pr("%s%ld", sep, val->val_as.lng);
116 pr("%s%#g", sep, val->val_as.dbl);
119 s = (unsigned char *)val->val_as.str.base;
122 l = s + val->val_as.str.maxsz;
123 /* FIXME maxsz == INT_MAX ! */
125 for (e=s; e<l && *e != '"' && *e != '\\' && isgraph(*e); ++e) ;
126 pr("%.*s", (int)(e-s), s);
138 CANT_HAPPEN("Bad VAL type");
145 * Dump field values of a context object.
146 * CA[] describes fields.
147 * PTR points to context object.
150 xdflds(struct castr ca[], void *ptr)
156 for (i = 0; ca[i].ca_name; ++i) {
157 if (ca[i].ca_flags & NSC_DEITY && !player->god)
159 if (ca[i].ca_flags & NSC_EXTRA)
161 n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
164 xdeval(&val, ca[i].ca_type, ptr, ca[i].ca_off, j, ca[i].ca_len);
165 sep = xdprval(&val, sep);
171 * Dump header for dump NAME.
172 * If META, it's for the meta-data dump rather than the data dump.
175 xdhdr(char *name, int meta)
177 pr("XDUMP %s%s %ld\n", meta ? "meta " : "", name, (long)time(NULL));
180 /* Dump footer for a dump that dumped N objects. */
188 * Is object P of type TYPE visible to the player?
189 * TODO: Fold this into interators.
192 xdvisible(int type, void *p)
194 struct genitem *gp = p;
195 struct trtstr *tp = p;
196 struct lonstr *lp = p;
202 return gp->own == player->cnum || player->god;
208 return gp->own != 0 && (gp->own == player->cnum || player->god);
210 return gp->own == player->cnum;
212 return ((struct natstr *)p)->nat_stat != STAT_UNUSED;
214 return ((struct nwsstr *)p)->nws_vrb != 0
215 && (!opt_HIDDEN || player->god); /* FIXME */
217 return tp->trt_status != TS_FREE
218 && (tp->trt_cna == player->cnum || tp->trt_cnb == player->cnum
221 if (lp->l_status == LS_FREE)
223 if (lp->l_status == LS_SIGNED)
225 return lp->l_loner == player->cnum || lp->l_lonee == player->cnum
231 tlev = ((struct mchrstr *)p)->m_tech;
234 tlev = ((struct plchrstr *)p)->pl_tech;
237 tlev = ((struct lchrstr *)p)->l_tech;
239 natp = getnatp(player->cnum);
240 return player->god || tlev <= (int)(1.25 * natp->nat_level[NAT_TLEV]);
242 tlev = ((struct nchrstr *)p)->n_tech;
243 if (drnuke_const > MIN_DRNUKE_CONST) {
244 natp = getnatp(player->cnum);
245 if (tlev > (int)((int)(1.25 * natp->nat_level[NAT_RLEV])
251 return ((struct rptstr *)p)->r_newspage != 0;
253 return ((struct empfile *)p)->cadef != NULL;
260 * Dump items of type TYPE selected by ARG.
261 * Return RET_OK on success, RET_SYN on error.
264 xditem(int type, char *arg)
266 int check_owner = !player->god && (ef_flags(type) & EFF_OWNER) != 0;
270 char buf[2048]; /* FIXME buffer size? */
276 if (!snxtitem(&ni, type, arg))
279 xdhdr(ef_nameof(type), 0);
282 while (nxtitem(&ni, buf)) {
283 if (!xdvisible(type, buf))
296 * Dump meta-data for items of type TYPE.
302 struct castr *ca = ef_cadef(type);
309 xdhdr(ef_nameof(type), 1);
311 for (i = 0; ca[i].ca_name; i++) {
312 if (ca[i].ca_flags & NSC_DEITY && !player->god)
314 if (ca[i].ca_flags & NSC_EXTRA)
316 xdflds(mdchr_ca, &ca[i]);
327 * Dump configkeys[], return RET_OK.
328 * If META, dump meta-data rather than data.
333 static struct castr vers_ca = {
334 NSC_STRINGY, 0, sizeof(PACKAGE_STRING), 0, "version", EF_BAD
342 xdhdr("version", meta);
346 xdflds(mdchr_ca, &vers_ca);
349 for (kp = configkeys; kp->km_key; ++kp) {
350 if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
351 ca.ca_type = kp->km_type;
355 ca.ca_name = kp->km_key;
356 ca.ca_table = EF_BAD;
357 xdflds(mdchr_ca, &ca);
366 xdeval(&val, vers_ca.ca_type, version, vers_ca.ca_off, 0, vers_ca.ca_len);
367 sep = xdprval(&val, "");
368 for (kp = configkeys; kp->km_key; ++kp) {
369 if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
370 xdeval(&val, kp->km_type, kp->km_data, 0, 0, 0);
371 sep = xdprval(&val, sep);
381 /* Experimental extended dump command */
390 p = getstarg(player->argp[1], "What? ", buf);
391 if (p && strcmp(p, "meta") == 0) {
393 p = getstarg(player->argp[2], "What? ", buf);
398 type = isdigit(p[0]) ? atoi(p) : ef_byname(p);
399 if (type >= 0 && type < EF_MAX) {
403 return xditem(type, player->argp[2]);
404 } else if (!strncmp(p, "ver", strlen(p))) {