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