]> git.pond.sub.org Git - empserver/blob - src/lib/commands/xdump.c
(xdhdr,xdhdr1,xditem,xdmeta,xdopt,xdver,xdfldnam):
[empserver] / src / lib / commands / xdump.c
1 /*
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
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  xdump.c: Experimental extended dump
29  * 
30  *  Known contributors to this file:
31  *     Markus Armbruster, 2004
32  */
33
34 #include <stddef.h>
35 #include "misc.h"
36 #include "file.h"
37 #include "match.h"
38 #include "news.h"
39 #include "nsc.h"
40 #include "optlist.h"
41 #include "commands.h"
42
43 /*
44  * Dump everything under the sun
45  *
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[]
55  * Less important:
56  * - News item characteristics: rpt[]
57  * - News page headings: page_headings[] (TODO)
58  * - Commands: player_coms[] (TODO)
59  * - Options: Options[]
60  * - Configuration: configkeys[]
61  *
62  * Dynamic game data:
63  * - Sectors: EF_SECTOR, sect_ca[] (already have dump)
64  * - Land units: EF_LAND, land_ca[] (already have ldump)
65  * - Lost: EF_LOST, lost_ca[] (already have lost)
66  * - Nukes: EF_NUKE, nuke_ca[] (already have ndump)
67  * - Planes: EF_PLANE, plane_ca[] (already have pdump)
68  * - Ships: EF_SHIP, ship_ca[] (already have sdump)
69  * - News: EF_NEWS, news_ca[]
70  * - Treaties: EF_TREATY, treaty_ca[]
71  * - Power: EF_POWER
72  * - Nations: EF_NATION, nat_ca[]
73  * - Loans: EF_LOAN, loan_ca[]
74  * - Map: EF_MAP (TODO)
75  * - Bmap: EF_BMAP (TODO)
76  * - Market: EF_COMM, commodity_ca[]
77  */
78
79 /* FIXME document dump format */
80 /* FIXME don't dump stuff that's useless due to options */
81
82 /*
83  * Evaluate a attribute of an object into VAL, return VAL.
84  * TYPE is the attribute's type.
85  * PTR points to the context object.
86  * The attribute is stored there at offset OFF + IDX * S, where S is
87  * its size.
88  */
89 static struct valstr *
90 xdeval(struct valstr *val, nsc_type type, void *ptr, ptrdiff_t off, int idx)
91 {
92     val->val_type = type;
93     val->val_cat = NSC_OFF;
94     val->val_as.sym.off = off;
95     val->val_as.sym.idx = idx;
96     nstr_exec_val(val, player->cnum, ptr, NSC_NOTYPE);
97     return val;                 /* FIXME nstr_exec_val() should return VAL */
98 }
99
100 /* Dump VAL prefixed with SEP, return " ".  */
101 static char *
102 xdprval(struct valstr *val, char *sep)
103 {
104     unsigned char *s, *e, *l;
105
106     switch (val->val_type) {
107     case NSC_TYPEID:
108     case NSC_LONG:
109         pr("%s%ld", sep, val->val_as.lng);
110         break;
111     case NSC_DOUBLE:
112         pr("%s%#g", sep, val->val_as.dbl);
113         break;
114     case NSC_STRING:
115         s = (unsigned char *)val->val_as.str.base;
116         if (s) {
117             pr("%s\"", sep);
118             l = s + val->val_as.str.maxsz;
119             /* FIXME maxsz == INT_MAX ! */
120             for (;;) {
121                 for (e=s; e<l && *e != '"' && *e != '\\' && isgraph(*e); ++e) ;
122                 pr("%.*s", (int)(e-s), s);
123                 if (e < l && *e)
124                     pr("\\%03o", *e++);
125                 else
126                     break;
127                 s = e;
128             }
129             pr("\"");
130         } else
131             pr("%snil", sep);
132         break;
133     default:
134         CANT_HAPPEN("Bad VAL type");
135         pr("0");
136     }
137     return " ";
138 }
139
140 /*
141  * Dump field values of a context object.
142  * CA[] describes fields.
143  * PTR points to context object.
144  */
145 static void
146 xdflds(struct castr ca[], void *ptr)
147 {
148     int i, j, n;
149     struct valstr val;
150     char *sep = "";
151
152     for (i = 0; ca[i].ca_name; ++i) {
153         if (ca[i].ca_flags & NSC_DEITY && !player->god)
154             continue;
155         if (ca[i].ca_flags & NSC_EXTRA)
156             continue;
157         n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
158         j = 0;
159         do {
160             xdeval(&val, ca[i].ca_type, ptr, ca[i].ca_off, j);
161             sep = xdprval(&val, sep);
162         } while (++j < n);
163     }
164 }
165
166 /* Dump first line of header for dump NAME.  */
167 static void
168 xdhdr(char *name, int meta)
169 {
170     pr("XDUMP %s%s %ld\n", meta ? "meta " : "", name, (long)time(NULL));
171 }
172
173 /* Dump footer for a dump that dumped N objects.  */
174 static void
175 xdftr(int n)
176 {
177     pr("/%d\n", n);
178 }
179
180 /*
181  * Dump items of type TYPE selected by ARG.
182  * Return RET_OK on success, RET_SYN on error.
183  */
184 static int
185 xditem(int type, char *arg)
186 {
187     int check_owner = !player->god && (ef_flags(type) & EFF_OWNER) != 0;
188     struct castr *ca;
189     struct nstr_item ni;
190     int n;
191     char buf[2048];             /* FIXME buffer size? */
192
193     ca = ef_cadef(type);
194     if (!ca)
195         return RET_SYN;
196
197     if (!snxtitem(&ni, type, arg))
198         return RET_SYN;
199
200     xdhdr(ef_nameof(type), 0);
201
202     n = 0;
203     while (nxtitem(&ni, buf)) {
204         if (check_owner && ((struct genitem *)buf)->own != player->cnum)
205             continue;
206         ++n;
207         xdflds(ca, buf);
208         pr("\n");
209     }
210
211     xdftr(n);
212
213     return RET_OK;
214 }
215
216 /*
217  * Dump meta-data for items of type TYPE.
218  * Return RET_OK.
219  */
220 static int
221 xdmeta(int type)
222 {
223     struct castr *ca = ef_cadef(type);
224     int i;
225     int n = 0;
226
227     if (!ca)
228         return RET_SYN;
229
230     xdhdr(ef_nameof(type), 1);
231
232     for (i = 0; ca[i].ca_name; i++) {
233         if (ca[i].ca_flags & NSC_DEITY && !player->god)
234             continue;
235         if (ca[i].ca_flags & NSC_EXTRA)
236             continue;
237         xdflds(mdchr_ca, &ca[i]);
238         n++;
239         pr("\n");
240     }
241
242     xdftr(n);
243
244     return RET_OK;
245 }
246
247 /* Dump Options[], return RET_OK.  */
248 static int
249 xdopt(int meta)
250 {
251     int i;
252     char *sep;
253     struct castr ca;
254
255     if (meta) {
256         xdhdr("options", 1);
257         for (i = 0; Options[i].opt_key; ++i) {
258             ca.ca_type = NSC_INT;
259             ca.ca_flags = 0;
260             ca.ca_len = 0;
261             ca.ca_off = 0;
262             ca.ca_name = Options[i].opt_key;
263             ca.ca_table = EF_BAD;
264             xdflds(mdchr_ca, &ca);
265             pr("\n");
266         }
267         xdftr(i);
268         return RET_OK;
269     }
270
271     xdhdr("options", 0);
272
273     sep = "";
274     for (i = 0; Options[i].opt_key; ++i) {
275         pr("%s%d", sep, *Options[i].opt_valuep);
276         sep = " ";
277     }
278     pr("\n");
279
280     xdftr(1);
281
282     return RET_OK;
283 }
284
285 static int
286 xdver(int meta)
287 {
288     struct keymatch *kp;
289     char *sep;
290     int n;
291     struct castr ca;
292     struct valstr val;
293
294     if (meta) {
295         xdhdr("version", 1);
296         n = 0;
297         for (kp = configkeys; kp->km_key; ++kp) {
298             if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
299                 ca.ca_type = kp->km_type;
300                 ca.ca_flags = 0;
301                 ca.ca_len = 0;
302                 ca.ca_off = 0;
303                 ca.ca_name = kp->km_key;
304                 ca.ca_table = EF_BAD;
305                 xdflds(mdchr_ca, &ca);
306                 pr("\n");
307                 n++;
308             }
309         }
310         xdftr(n);
311         return RET_OK;
312     }
313
314     xdhdr("version", 0);
315
316     sep = "";
317     for (kp = configkeys; kp->km_key; ++kp) {
318         if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
319             xdeval(&val, kp->km_type, kp->km_data, 0, 0);
320             sep = xdprval(&val, sep);
321         }
322     }
323     pr("\n");
324
325     xdftr(1);
326
327     return RET_OK;
328 }
329
330 /* Experimental extended dump command */
331 int
332 xdump(void)
333 {
334     char *p;
335     char buf[1024];
336     int type;
337     int meta = 0;
338
339     if (!opt_GUINEA_PIGS) {
340         pr("You are not a guinea pig!\n");
341         return RET_FAIL;
342     }
343
344     p = getstarg(player->argp[1], "What? ", buf);
345     if (p && strcmp(p, "meta") == 0) {
346         meta = 1;
347         p = getstarg(player->argp[2], "What? ", buf);
348     }
349     if (!p)
350         return RET_SYN;
351
352     type = ef_byname(p);
353     if (type >= 0) {
354         if (meta)
355             return xdmeta(type);
356         else
357             return xditem(type, player->argp[2]);
358     } else if (!strncmp(p, "opt", strlen(p))) {
359         return xdopt(meta);
360     } else if (!strncmp(p, "ver", strlen(p))) {
361         return xdver(meta);
362     }
363
364     return RET_SYN;
365 }