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