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
46 * Dump everything under the sun
48 * Static game data (configuration):
49 * - Item characteristics: ichr[]
50 * - Land unit characteristics: lchr[]
51 * - Nuke characteristics: nchr[]
52 * - Plane characteristics: plchr[]
53 * - Product characteristics: pchr[]
54 * - Sector designation characteristics: dchr[]
55 * - Sector infrastructure characteristics: intrchr[]
56 * - Ship characteristics: mchr[]
58 * - News item characteristics: rpt[]
59 * - News page headings: page_headings[] (TODO)
60 * - Commands: player_coms[] (TODO)
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 */
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
90 static struct valstr *
91 xdeval(struct valstr *val, nsc_type type, void *ptr, ptrdiff_t off, int idx)
94 val->val_cat = NSC_OFF;
95 val->val_as.sym.off = off;
96 val->val_as.sym.idx = idx;
97 nstr_exec_val(val, player->cnum, ptr, NSC_NOTYPE);
98 return val; /* FIXME nstr_exec_val() should return VAL */
101 /* Dump VAL prefixed with SEP, return " ". */
103 xdprval(struct valstr *val, char *sep)
105 unsigned char *s, *e, *l;
107 switch (val->val_type) {
110 pr("%s%ld", sep, val->val_as.lng);
113 pr("%s%#g", sep, val->val_as.dbl);
116 s = (unsigned char *)val->val_as.str.base;
119 l = s + val->val_as.str.maxsz;
120 /* FIXME maxsz == INT_MAX ! */
122 for (e=s; e<l && *e != '"' && *e != '\\' && isgraph(*e); ++e) ;
123 pr("%.*s", (int)(e-s), s);
135 CANT_HAPPEN("Bad VAL type");
142 * Dump field values of a context object.
143 * CA[] describes fields.
144 * PTR points to context object.
147 xdflds(struct castr ca[], void *ptr)
153 for (i = 0; ca[i].ca_name; ++i) {
154 if (ca[i].ca_flags & NSC_DEITY && !player->god)
156 if (ca[i].ca_flags & NSC_EXTRA)
158 n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
161 xdeval(&val, ca[i].ca_type, ptr, ca[i].ca_off, j);
162 sep = xdprval(&val, sep);
168 * Dump header for dump NAME.
169 * If META, it's for the meta-data dump rather than the data dump.
172 xdhdr(char *name, int meta)
174 pr("XDUMP %s%s %ld\n", meta ? "meta " : "", name, (long)time(NULL));
177 /* Dump footer for a dump that dumped N objects. */
185 * Dump items of type TYPE selected by ARG.
186 * Return RET_OK on success, RET_SYN on error.
189 xditem(int type, char *arg)
191 int check_owner = !player->god && (ef_flags(type) & EFF_OWNER) != 0;
195 char buf[2048]; /* FIXME buffer size? */
201 if (!snxtitem(&ni, type, arg))
204 xdhdr(ef_nameof(type), 0);
207 while (nxtitem(&ni, buf)) {
208 if (check_owner && ((struct genitem *)buf)->own != player->cnum)
221 * Dump meta-data for items of type TYPE.
227 struct castr *ca = ef_cadef(type);
234 xdhdr(ef_nameof(type), 1);
236 for (i = 0; ca[i].ca_name; i++) {
237 if (ca[i].ca_flags & NSC_DEITY && !player->god)
239 if (ca[i].ca_flags & NSC_EXTRA)
241 xdflds(mdchr_ca, &ca[i]);
252 * Dump configkeys[], return RET_OK.
253 * If META, dump meta-data rather than data.
264 xdhdr("version", meta);
268 for (kp = configkeys; kp->km_key; ++kp) {
269 if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
270 ca.ca_type = kp->km_type;
274 ca.ca_name = kp->km_key;
275 ca.ca_table = EF_BAD;
276 xdflds(mdchr_ca, &ca);
286 for (kp = configkeys; kp->km_key; ++kp) {
287 if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
288 xdeval(&val, kp->km_type, kp->km_data, 0, 0);
289 sep = xdprval(&val, sep);
299 /* Experimental extended dump command */
308 if (!opt_GUINEA_PIGS) {
309 pr("You are not a guinea pig!\n");
313 p = getstarg(player->argp[1], "What? ", buf);
314 if (p && strcmp(p, "meta") == 0) {
316 p = getstarg(player->argp[2], "What? ", buf);
321 type = isdigit(p[0]) ? atoi(p) : ef_byname(p);
322 if (type >= 0 && type < EF_MAX) {
326 return xditem(type, player->argp[2]);
327 } else if (!strncmp(p, "ver", strlen(p))) {