2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * xdump.c: Experimental extended dump
30 * Known contributors to this file:
31 * Markus Armbruster, 2004
44 * Dump everything under the sun
46 * Static game data (configuration):
47 * - Item characteristics: ichr[]
48 * - Land unit characteristics: lchr[]
49 * - Nuke characteristics: nchr[]
50 * - Plane characteristics: plchr[]
51 * - Product characteristics: pchr[]
52 * - Sector designation characteristics: dchr[]
53 * - Sector infrastructure characteristics: intrchr[]
54 * - Ship characteristics: mchr[]
56 * - News item characteristics: rpt[] (TODO)
57 * - News page headings: page_headings[] (TODO)
58 * - Treaty clause characteristics: tchr[]
59 * - Commands: player_coms[] (TODO)
60 * - Options: Options[]
61 * - Configuration: configkeys[]
64 * - Sectors: EF_SECTOR, sect_ca[] (already have dump)
65 * - Land units: EF_LAND, land_ca[] (already have ldump)
66 * - Lost: EF_LOST, lost_ca[] (already have lost)
67 * - Nukes: EF_NUKE, nuke_ca[] (already have ndump)
68 * - Planes: EF_PLANE, plane_ca[] (already have pdump)
69 * - Ships: EF_SHIP, ship_ca[] (already have sdump)
70 * - News: EF_NEWS, news_ca[]
71 * - Treaties: EF_TREATY, treaty_ca[]
73 * - Nations: EF_NATION, nat_ca[]
74 * - Loans: EF_LOAN, loan_ca[]
75 * - Map: EF_MAP (TODO)
76 * - Bmap: EF_BMAP (TODO)
77 * - Market: EF_COMM, commodity_ca[]
80 /* FIXME document dump format */
81 /* FIXME don't dump stuff that's useless due to options */
83 /* Selector descriptors for characteristics tables */
85 /* Characteristics table meta data */
87 char *name; /* name for lookup */
88 struct castr *ca; /* selector descriptors */
89 void *chr; /* characteristics table */
90 size_t size; /* size of characteristics table element */
93 /* Table of characteristics tables */
94 static struct camap chr_camap[] = {
95 {"sect chr", dchr_ca, dchr, sizeof(dchr[0])},
96 {"ship chr", mchr_ca, mchr, sizeof(mchr[0])},
97 {"plane chr", plchr_ca, plchr, sizeof(plchr[0])},
98 {"land chr", lchr_ca, lchr, sizeof(lchr[0])},
99 {"nuke chr", nchr_ca, nchr, sizeof(nchr[0])},
101 /* FIXME rpt[] lacks sentinel, xdchr() doesn't terminate */
102 {"news chr", rpt_ca, rpt, sizeof(rpt[0])},
104 {"treaty chr", tchr_ca, tchr, sizeof(tchr[0])},
105 {"item", ichr_ca, ichr, sizeof(ichr[0])},
106 {"infrastructure", intrchr_ca, intrchr, sizeof(intrchr[0])},
107 {"product", pchr_ca, pchr, sizeof(pchr[0])},
108 {NULL, NULL, NULL, 0}
112 * Search chr_camap[] for element named NAME, return its index.
113 * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
117 chridx_by_name(char *name)
119 return stmtch(name, chr_camap, offsetof(struct camap, name),
120 sizeof(chr_camap[0]));
124 * Evaluate a attribute of an object into VAL, return VAL.
125 * TYPE is the attribute's type.
126 * PTR points to the context object.
127 * The attribute is stored there at offset OFF + IDX * S, where S is
130 static struct valstr *
131 xdeval(struct valstr *val, nsc_type type, void *ptr, ptrdiff_t off, int idx)
133 val->val_type = type;
134 val->val_cat = NSC_OFF;
135 val->val_as.sym.off = off;
136 val->val_as.sym.idx = idx;
137 nstr_exec_val(val, player->cnum, ptr, NSC_NOTYPE);
138 return val; /* FIXME nstr_exec_val() should return VAL */
141 /* Dump VAL prefixed with SEP, return " ". */
143 xdprval(struct valstr *val, char *sep)
145 unsigned char *s, *e, *l;
147 switch (val->val_type) {
150 pr("%s%ld", sep, val->val_as.lng);
153 pr("%s%#g", sep, val->val_as.dbl);
156 s = (unsigned char *)val->val_as.str.base;
159 l = s + val->val_as.str.maxsz;
161 for (e=s; e<l && *e != '"' && *e != '\\' && isgraph(*e); ++e) ;
162 pr("%.*s", (int)(e-s), s);
174 CANT_HAPPEN("Bad VAL type");
181 * Dump field values of a context object.
182 * CA[] describes fields.
183 * PTR points to context object.
186 xdflds(struct castr ca[], void *ptr)
192 for (i = 0; ca[i].ca_name; ++i) {
193 if (ca[i].ca_flags & NSC_DEITY && !player->god)
195 if (ca[i].ca_flags & NSC_EXTRA)
197 n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
200 xdeval(&val, ca[i].ca_type, ptr, ca[i].ca_off, j);
201 sep = xdprval(&val, sep);
206 /* Dump field names; CA[] describes fields. */
208 xdfldnam(struct castr ca[])
213 for (i = 0; ca[i].ca_name; ++i) {
214 if (ca[i].ca_flags & NSC_DEITY && !player->god)
216 if (ca[i].ca_flags & NSC_EXTRA)
218 pr("%s%s", sep, ca[i].ca_name);
219 if (ca[i].ca_len && ca[i].ca_type != NSC_STRINGY)
220 pr(" %d", ca[i].ca_len);
225 /* Dump first line of header for dump NAME. */
229 pr("XDUMP %s %ld\n", name, (long)time(NULL));
232 /* Dump header for dump NAME with fields described by CA[]. */
234 xdhdr(char *name, struct castr ca[])
241 /* Dump footer for a dump that dumped N objects. */
245 pr("dumped %d\n", n);
249 * Dump items of type TYPE selected by ARG.
250 * Return RET_OK on success, RET_SYN on error.
253 xditem(int type, char *arg)
255 int check_owner = !player->god && (ef_flags(type) & EFF_OWNER) != 0;
259 char buf[2048]; /* FIXME buffer size? */
265 if (!snxtitem(&ni, type, arg))
268 xdhdr(ef_nameof(type), ca);
271 while (nxtitem(&ni, buf)) {
272 if (check_owner && ((struct genitem *)buf)->own != player->cnum)
285 * Dump characteristics described by chr_camap[IDX].
286 * Return RET_OK on success, RET_SYN if IDX < 0.
298 cm = &chr_camap[chridx];
300 xdhdr(cm->name, cm->ca);
303 for (p = cm->chr; ; p += cm->size) {
304 val.val_type = cm->ca[0].ca_type;
305 val.val_cat = NSC_OFF;
306 val.val_as.sym.off = cm->ca[0].ca_off;
307 val.val_as.sym.idx = 0;
308 nstr_exec_val(&val, player->cnum, p, NSC_STRING);
309 if (!val.val_as.str.base || !*val.val_as.str.base)
321 /* Dump Options[], return RET_OK. */
331 for (i = 0; Options[i].opt_key; ++i) {
332 pr("%s%s", sep, Options[i].opt_key);
338 for (i = 0; Options[i].opt_key; ++i) {
339 pr("%s%d", sep, *Options[i].opt_valuep);
357 for (kp = configkeys; kp->km_key; ++kp) {
358 if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
359 pr("%s%s", sep, kp->km_key);
366 for (kp = configkeys; kp->km_key; ++kp) {
367 if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
368 xdeval(&val, kp->km_type, kp->km_data, 0, 0);
369 sep = xdprval(&val, sep);
377 /* Experimental extended dump command */
385 if (!opt_GUINEA_PIGS) {
386 pr("You are not a guinea pig!\n");
390 p = getstarg(player->argp[1], "What? ", buf);
396 return xditem(type, player->argp[2]);
397 } else if (!strncmp(p, "chr", strlen(p)) && player->argp[2]) {
398 return xdchr(chridx_by_name(player->argp[2]));
399 } else if (!strncmp(p, "opt", strlen(p))) {
401 } else if (!strncmp(p, "ver", strlen(p))) {