]> git.pond.sub.org Git - empserver/blob - src/lib/commands/xdump.c
WIP empdump, can't shrink tables
[empserver] / src / lib / commands / xdump.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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: Extended dump
29  * 
30  *  Known contributors to this file:
31  *     Markus Armbruster, 2004-2008
32  */
33
34 #include <config.h>
35
36 #include <ctype.h>
37 #include "commands.h"
38 #include "empobj.h"
39 #include "optlist.h"
40 #include "version.h"
41 #include "xdump.h"
42
43 /*
44  * Is object P of type TYPE visible to the player?
45  * TODO: Fold this into interators.
46  */
47 static int
48 xdvisible(int type, void *p)
49 {
50     struct empobj *gp = p;
51     struct trtstr *tp = p;
52     struct lonstr *lp = p;
53     struct natstr *natp;
54     int tlev;
55
56     switch (type) {
57     case EF_SECTOR:
58     case EF_REALM:
59         return gp->own == player->cnum || player->god;
60     case EF_SHIP:
61     case EF_PLANE:
62     case EF_LAND:
63     case EF_NUKE:
64     case EF_LOST:
65         return gp->own != 0 && (gp->own == player->cnum || player->god);
66     case EF_NATION:
67         return ((struct natstr *)p)->nat_stat != STAT_UNUSED;
68     case EF_COUNTRY:
69         return gp->own == player->cnum;
70     case EF_NEWS:
71         return ((struct nwsstr *)p)->nws_vrb != 0
72             && (!opt_HIDDEN || player->god); /* FIXME */
73     case EF_TREATY:
74         return tp->trt_status != TS_FREE
75             && (tp->trt_cna == player->cnum || tp->trt_cnb == player->cnum
76                 || player->god);
77     case EF_LOAN:
78         if (lp->l_status == LS_FREE)
79             return 0;
80         if (lp->l_status == LS_SIGNED)
81             return 1;
82         return lp->l_loner == player->cnum || lp->l_lonee == player->cnum
83             || player->god;
84     case EF_TRADE:
85     case EF_COMM:
86         return gp->own != 0;
87     case EF_SHIP_CHR:
88         tlev = ((struct mchrstr *)p)->m_tech;
89         goto tech;
90     case EF_PLANE_CHR:
91         tlev = ((struct plchrstr *)p)->pl_tech;
92         goto tech;
93     case EF_LAND_CHR:
94         tlev = ((struct lchrstr *)p)->l_tech;
95     tech:
96         natp = getnatp(player->cnum);
97         return player->god || tlev <= (int)(1.25 * natp->nat_level[NAT_TLEV]);
98     case EF_NUKE_CHR:
99         tlev = ((struct nchrstr *)p)->n_tech;
100         if (drnuke_const > MIN_DRNUKE_CONST) {
101             natp = getnatp(player->cnum);
102             if (tlev > (int)((int)(1.25 * natp->nat_level[NAT_RLEV])
103                              / drnuke_const))
104                 return player->god;
105         }
106         goto tech;
107     case EF_NEWS_CHR:
108         return ((struct rptstr *)p)->r_newspage != 0;
109     case EF_TABLE:
110         return ((struct empfile *)p)->cadef != NULL;
111     default:
112         return 1;
113     }
114 }
115
116 /*
117  * Dump items of type TYPE selected by ARG to XD.
118  * Return RET_OK on success, RET_SYN on error.
119  */
120 static int
121 xditem(struct xdstr *xd, int type, char *arg)
122 {
123     struct castr *ca;
124     struct nstr_item ni;
125     int n;
126     char buf[2048];             /* FIXME buffer size? */
127
128     ca = ef_cadef(type);
129     if (!ca)
130         return RET_SYN;
131
132     if (!snxtitem(&ni, type, arg))
133         return RET_SYN;
134
135     xdhdr(xd, ef_nameof(type), 0);
136
137     n = 0;
138     while (nxtitem(&ni, buf)) {
139         if (!xdvisible(type, buf))
140             continue;
141         ++n;
142         xdflds(xd, ca, buf);
143         xd->pr("\n");
144     }
145
146     xdftr(xd, n);
147
148     return RET_OK;
149 }
150
151 /*
152  * Dump configkeys[] to XD.
153  * If META, dump meta-data rather than data.
154  * Return RET_OK.
155  */
156 static int
157 xdver(struct xdstr *xd, int meta)
158 {
159     static struct castr vers_ca = {
160         NSC_STRINGY, 0, sizeof(PACKAGE_STRING), 0, "version", EF_BAD
161     };
162     struct keymatch *kp;
163     char *sep;
164     int n;
165     struct castr ca;
166     struct valstr val;
167
168     xdhdr(xd, "version", meta);
169
170     if (meta) {
171         n = 0;
172         xdflds(xd, mdchr_ca, &vers_ca);
173         pr("\n");
174         n++;
175         for (kp = configkeys; kp->km_key; ++kp) {
176             if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
177                 ca.ca_type = kp->km_type;
178                 ca.ca_flags = 0;
179                 ca.ca_len = 0;
180                 ca.ca_off = 0;
181                 ca.ca_name = kp->km_key;
182                 ca.ca_table = EF_BAD;
183                 xdflds(xd, mdchr_ca, &ca);
184                 pr("\n");
185                 n++;
186             }
187         }
188         xdftr(xd, n);
189         return RET_OK;
190     }
191
192     xdeval(&val, xd,
193            vers_ca.ca_type, version, vers_ca.ca_off, 0, vers_ca.ca_len);
194     sep = xdprval(xd, &val, "");
195     for (kp = configkeys; kp->km_key; ++kp) {
196         if (kp->km_type != NSC_NOTYPE && !(kp->km_flags & KM_INTERNAL)) {
197             xdeval(&val, xd, kp->km_type, kp->km_data, 0, 0, 0);
198             sep = xdprval(xd, &val, sep);
199         }
200     }
201     pr("\n");
202
203     xdftr(xd, 1);
204
205     return RET_OK;
206 }
207
208 int
209 xdump(void)
210 {
211     char *p;
212     char buf[1024];
213     struct xdstr xd;
214     struct natstr *natp;
215     int type;
216     int meta = 0;
217
218     p = getstarg(player->argp[1], "Table name, or meta? ", buf);
219     if (p && strcmp(p, "meta") == 0) {
220         meta = 1;
221         p = getstarg(player->argp[2], "Table name? ", buf);
222     }
223     if (!p || !*p)
224         return RET_SYN;
225
226     xdinit(&xd, player->cnum, 0, pr);
227     natp = getnatp(player->cnum);
228     type = isdigit(p[0]) ? atoi(p) : ef_byname(p);
229     if (type >= 0 && type < EF_MAX) {
230         if (meta)
231             return xdmeta(&xd, type);
232         else if ((EF_IS_GAME_STATE(type) || EF_IS_VIEW(type))
233                  && !(natp->nat_stat == STAT_ACTIVE || player->god)) {
234             pr("Access to table %s denied\n", ef_nameof(type));
235             return RET_FAIL;
236         } else
237             return xditem(&xd, type, player->argp[2]);
238     } else if (!strncmp(p, "ver", strlen(p))) {
239         return xdver(&xd, meta);
240     }
241
242     return RET_SYN;
243 }