2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, 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: Extended dumps
30 * Known contributors to this file:
31 * Markus Armbruster, 2008
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 * - Update schedule: update_time[] (not really static)
50 * - Configuration: configkeys[]
53 * - Sectors: EF_SECTOR (superseding dump)
54 * - Land units: EF_LAND (superseding ldump)
55 * - Lost: EF_LOST (superseding lost)
56 * - Nukes: EF_NUKE (superseding ndump)
57 * - Planes: EF_PLANE (superseding pdump)
58 * - Ships: EF_SHIP (superseding sdump)
60 * - Treaties: EF_TREATY
61 * - Power: EF_POWER (TODO)
62 * - Nations: EF_NATION
64 * - Map: EF_MAP (TODO)
65 * - Bmap: EF_BMAP (TODO)
70 * See doc/xdump for motivation, syntax, semantics, and rationale.
71 * Make sure to keep it up-to-date!
74 /* TODO don't dump stuff that's useless due to options */
84 * Initialize XD to dump for country CNUM.
85 * Dump is to be delivered through callback PR.
89 xdinit(struct xdstr *xd, natid cnum, void (*pr)(char *fmt, ...))
92 xd->divine = getnatp(cnum)->nat_stat == STAT_GOD;
98 * Evaluate a attribute of an object into VAL, return VAL.
99 * XD is the xdump descriptor.
100 * TYPE is the attribute's type.
101 * PTR points to the context object.
102 * The attribute is stored there at offset OFF + IDX * S, where S is
104 * LEN is the #array elements if it is an array, else zero.
107 xdeval(struct valstr *val, struct xdstr *xd,
108 nsc_type type, void *ptr, ptrdiff_t off, int idx, int len)
110 val->val_type = type;
111 val->val_cat = NSC_OFF;
112 val->val_as.sym.off = off;
113 val->val_as.sym.len = len;
114 val->val_as.sym.idx = idx;
115 nstr_exec_val(val, xd->cnum, ptr, NSC_NOTYPE);
116 return val; /* FIXME nstr_exec_val() should return VAL */
119 /* Dump VAL prefixed with SEP, return " ". */
121 xdprval(struct xdstr *xd, struct valstr *val, char *sep)
123 unsigned char *s, *e, *l;
125 switch (val->val_type) {
127 xd->pr("%s%ld", sep, val->val_as.lng);
130 xd->pr("%s%#g", sep, val->val_as.dbl);
133 s = (unsigned char *)val->val_as.str.base;
136 l = s + val->val_as.str.maxsz;
137 /* FIXME maxsz == INT_MAX ! */
140 e < l && *e != '"' && *e != '\\' && isgraph(*e);
143 xd->pr("%.*s", (int)(e-s), s);
145 xd->pr("\\%03o", *e++);
152 xd->pr("%snil", sep);
162 * Dump field values of a context object to XD.
163 * CA[] describes fields.
164 * PTR points to context object.
167 xdflds(struct xdstr *xd, struct castr ca[], void *ptr)
173 for (i = 0; ca[i].ca_name; ++i) {
174 if (ca[i].ca_flags & NSC_DEITY && !xd->divine)
176 if (ca[i].ca_flags & NSC_EXTRA)
178 n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
182 ca[i].ca_type, ptr, ca[i].ca_off, j, ca[i].ca_len);
183 sep = xdprval(xd, &val, sep);
189 * Dump header for dump NAME.
190 * If META, it's for the meta-data dump rather than the data dump.
193 xdhdr(struct xdstr *xd, char *name, int meta)
195 xd->pr("XDUMP %s%s %ld\n", meta ? "meta " : "", name, (long)time(NULL));
198 /* Dump footer for a dump that dumped N objects. */
200 xdftr(struct xdstr *xd, int n)
206 * Dump meta-data for items of type TYPE to XD.
210 xdmeta(struct xdstr *xd, int type)
212 struct castr *ca = ef_cadef(type);
219 xdhdr(xd, ef_nameof(type), 1);
221 for (i = 0; ca[i].ca_name; i++) {
222 if (ca[i].ca_flags & NSC_DEITY && !xd->divine)
224 if (ca[i].ca_flags & NSC_EXTRA)
226 xdflds(xd, mdchr_ca, &ca[i]);