]> git.pond.sub.org Git - empserver/blob - src/lib/commands/xdump.c
Fix the previous revision.
[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-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         return ((struct natstr *)p)->nat_stat != 0
208             && (gp->own == player->cnum || player->god);
209     case EF_NEWS:
210         return ((struct nwsstr *)p)->nws_vrb != 0
211             && (!opt_HIDDEN || player->god); /* FIXME */
212     case EF_TREATY:
213         return tp->trt_status != TS_FREE
214             && (tp->trt_cna == player->cnum || tp->trt_cnb == player->cnum
215                 || player->god);
216     case EF_LOAN:
217         if (lp->l_status == LS_FREE)
218             return 0;
219         if (lp->l_status == LS_SIGNED)
220             return 1;
221         return lp->l_loner == player->cnum || lp->l_lonee == player->cnum
222             || player->god;
223     case EF_TRADE:
224     case EF_COMM:
225         return gp->own != 0;
226     case EF_SHIP_CHR:
227         tlev = ((struct mchrstr *)p)->m_tech;
228         goto tech;
229     case EF_PLANE_CHR:
230         tlev = ((struct plchrstr *)p)->pl_tech;
231         goto tech;
232     case EF_LAND_CHR:
233         tlev = ((struct lchrstr *)p)->l_tech;
234     tech:
235         natp = getnatp(player->cnum);
236         return player->god || tlev <= (int)(1.25 * natp->nat_level[NAT_TLEV]);
237     case EF_NUKE_CHR:
238         tlev = ((struct nchrstr *)p)->n_tech;
239         natp = getnatp(player->cnum);
240         if (opt_DRNUKE)
241             if (tlev > (int)((int)(1.25 * natp->nat_level[NAT_RLEV])
242                              / drnuke_const))
243                 return 0;
244         return tlev <= (int)(1.25 * natp->nat_level[NAT_TLEV]);
245     case EF_NEWS_CHR:
246         return ((struct rptstr *)p)->r_newspage != 0;
247     case EF_TABLE:
248         return ((struct empfile *)p)->cadef != NULL;
249     default:
250         return 1;
251     }
252 }
253
254 /*
255  * Dump items of type TYPE selected by ARG.
256  * Return RET_OK on success, RET_SYN on error.
257  */
258 static int
259 xditem(int type, char *arg)
260 {
261     int check_owner = !player->god && (ef_flags(type) & EFF_OWNER) != 0;
262     struct castr *ca;
263     struct nstr_item ni;
264     int n;
265     char buf[2048];             /* FIXME buffer size? */
266
267     ca = ef_cadef(type);
268     if (!ca)
269         return RET_SYN;
270
271     if (!snxtitem(&ni, type, arg))
272         return RET_SYN;
273
274     xdhdr(ef_nameof(type), 0);
275
276     n = 0;
277     while (nxtitem(&ni, buf)) {
278         if (!xdvisible(type, buf))
279             continue;
280         ++n;
281         xdflds(ca, buf);
282         pr("\n");
283     }
284
285     xdftr(n);
286
287     return RET_OK;
288 }
289
290 /*
291  * Dump meta-data for items of type TYPE.
292  * Return RET_OK.
293  */
294 static int
295 xdmeta(int type)
296 {
297     struct castr *ca = ef_cadef(type);
298     int i;
299     int n = 0;
300
301     if (!ca)
302         return RET_SYN;
303
304     xdhdr(ef_nameof(type), 1);
305
306     for (i = 0; ca[i].ca_name; i++) {
307         if (ca[i].ca_flags & NSC_DEITY && !player->god)
308             continue;
309         if (ca[i].ca_flags & NSC_EXTRA)
310             continue;
311         xdflds(mdchr_ca, &ca[i]);
312         n++;
313         pr("\n");
314     }
315
316     xdftr(n);
317
318     return RET_OK;
319 }
320
321 /*
322  * Dump configkeys[], return RET_OK.
323  * If META, dump meta-data rather than data.
324  */
325 static int
326 xdver(int meta)
327 {
328     struct keymatch *kp;
329     char *sep;
330     int n;
331     struct castr ca;
332     struct valstr val;
333
334     xdhdr("version", meta);
335
336     if (meta) {
337         n = 0;
338         for (kp = configkeys; kp->km_key; ++kp) {
339             if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
340                 ca.ca_type = kp->km_type;
341                 ca.ca_flags = 0;
342                 ca.ca_len = 0;
343                 ca.ca_off = 0;
344                 ca.ca_name = kp->km_key;
345                 ca.ca_table = EF_BAD;
346                 xdflds(mdchr_ca, &ca);
347                 pr("\n");
348                 n++;
349             }
350         }
351         xdftr(n);
352         return RET_OK;
353     }
354
355     sep = "";
356     for (kp = configkeys; kp->km_key; ++kp) {
357         if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
358             xdeval(&val, kp->km_type, kp->km_data, 0, 0);
359             sep = xdprval(&val, sep);
360         }
361     }
362     pr("\n");
363
364     xdftr(1);
365
366     return RET_OK;
367 }
368
369 /* Experimental extended dump command */
370 int
371 xdump(void)
372 {
373     char *p;
374     char buf[1024];
375     int type;
376     int meta = 0;
377
378     if (!opt_GUINEA_PIGS) {
379         pr("You are not a guinea pig!\n");
380         return RET_FAIL;
381     }
382
383     p = getstarg(player->argp[1], "What? ", buf);
384     if (p && strcmp(p, "meta") == 0) {
385         meta = 1;
386         p = getstarg(player->argp[2], "What? ", buf);
387     }
388     if (!p)
389         return RET_SYN;
390
391     type = isdigit(p[0]) ? atoi(p) : ef_byname(p);
392     if (type >= 0 && type < EF_MAX) {
393         if (meta)
394             return xdmeta(type);
395         else
396             return xditem(type, player->argp[2]);
397     } else if (!strncmp(p, "ver", strlen(p))) {
398         return xdver(meta);
399     }
400
401     return RET_SYN;
402 }