]> git.pond.sub.org Git - empserver/blob - src/util/empdump.c
Make server check game state file sizes on startup
[empserver] / src / util / empdump.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  *  empdump.c: Export/import Empire game state
29  * 
30  *  Known contributors to this file:
31  *     Markus Armbruster, 2008
32  */
33
34 #include <config.h>
35
36 #include <errno.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include "file.h"
42 #include "optlist.h"
43 #include "nat.h"
44 #include "prototypes.h"
45 #include "version.h"
46 #include "xdump.h"
47
48 static void exit_bad_arg(char *, ...) ATTRIBUTE((noreturn));
49 static void dump_table(int, int);
50 static void pln_fixup(void);
51 static void lnd_fixup(void);
52 static void nuk_fixup(void);
53
54 int
55 main(int argc, char *argv[])
56 {
57     char *config_file = NULL;
58     char *import = NULL;
59     int export = 0;
60     int private = 0;
61     int human = 1;
62     int opt, i, lineno, type;
63     FILE *impf = NULL;
64     int dirty[EF_MAX];
65
66     while ((opt = getopt(argc, argv, "e:i:mnxhv")) != EOF) {
67         switch (opt) {
68         case 'e':
69             config_file = optarg;
70             break;
71         case 'i':
72             import = optarg;
73             break;
74         case 'm':
75             human = 0;
76             break;
77         case 'n':
78             private = EFF_PRIVATE;
79             break;
80         case 'x':
81             export = 1;
82             break;
83         case 'h':
84             printf("Usage: %s [OPTION]...\n"
85                    "  -e CONFIG-FILE  configuration file\n"
86                    "                  (default %s)\n"
87                    "  -i DUMP-FILE    import from DUMP-FILE\n"
88                    "  -m              use machine-readable format\n"
89                    "  -n              dry run, don't update game state\n"
90                    "  -x              export to standard output\n"
91                    "  -h              display this help and exit\n"
92                    "  -v              display version information and exit\n",
93                    argv[0], dflt_econfig);
94             exit(0);
95         case 'v':
96             printf("%s\n\n%s", version, legal);
97             exit(0);
98         default:
99             exit_bad_arg(NULL);
100         }
101     }
102
103     if (argv[optind])
104         exit_bad_arg("%s: extra operand %s\n", argv[0], argv[optind]);
105
106     if (!import && !export)
107         exit_bad_arg("%s: nothing to do!\n", argv[0]);
108
109     if (import) {
110         impf = fopen(import, "r");
111         if (!impf) {
112             fprintf(stderr, "%s: Can't open %s for reading (%s)\n",
113                     argv[0], import, strerror(errno));
114             exit(1);
115         }
116     } else
117         private = EFF_PRIVATE;
118
119     /* read configuration & initialize */
120     empfile_init();
121     if (emp_config(config_file) < 0)
122         exit(1);
123     empfile_fixup();
124     nsc_init();
125     if (read_builtin_tables() < 0)
126         exit(1);
127     if (read_custom_tables() < 0)
128         exit(1);
129     if (chdir(gamedir)) {
130         fprintf(stderr, "%s: Can't chdir to %s (%s)\n",
131                 argv[0], gamedir, strerror(errno));
132         exit(1);
133     }
134     global_init();
135
136     for (i = 0; i < EF_MAX; i++) {
137         if (!EF_IS_GAME_STATE(i))
138             continue;
139         if (!ef_open(i, EFF_MEM | private, -1))
140             exit(1);
141     }
142
143     /* import from IMPORT */
144     memset(dirty, 0, sizeof(dirty));
145     if (import) {
146         lineno = 1;
147         while ((type = xundump(impf, import, &lineno, EF_BAD)) >= 0)
148             dirty[type] = 1;
149         if (type == EF_BAD)
150             exit(1);
151         pln_fixup();
152         lnd_fixup();
153         nuk_fixup();
154     }
155
156     if (ef_verify() < 0)
157         exit(1);
158
159     /* export to stdout */
160     if (export) {
161         for (i = 0; i < EF_MAX; i++) {
162             if (!EF_IS_GAME_STATE(i))
163                 continue;
164             dump_table(i, human);
165         }
166         if (fclose(stdout) != 0) {
167             fprintf(stderr, "%s: error writing export (%s)\n",
168                     argv[0], strerror(errno));
169             exit(1);
170         }
171     }
172
173     /* write out imported data */
174     for (i = 0; i < EF_MAX; i++) {
175         if (!EF_IS_GAME_STATE(i))
176             continue;
177         if (!private && dirty[i]) {
178             if (!ef_close(i))
179                 exit(1);
180         }
181     }
182
183     return 0;
184 }
185
186 static void
187 exit_bad_arg(char *complaint, ...)
188 {
189     va_list ap;
190
191     if (complaint) {
192         va_start(ap, complaint);
193         vfprintf(stderr, complaint, ap);
194         va_end(ap);
195     }
196     fprintf(stderr, "Try -h for help.\n");
197     exit(1);
198 }
199
200 static void
201 printf_wrapper(char *fmt, ...)
202 {
203     va_list ap;
204
205     va_start(ap, fmt);
206     vprintf(fmt, ap);
207     va_end(ap);
208 }
209
210 static void
211 dump_table(int type, int human)
212 {
213     struct xdstr xd;
214     struct castr *ca;
215     int i;
216     void *p;
217
218     ca = ef_cadef(type);
219     if (!ca)
220         return;
221
222     xdinit(&xd, NATID_BAD, human, printf_wrapper);
223     xdhdr(&xd, ef_nameof(type), 0);
224     xdcolhdr(&xd, ca);
225     for (i = 0; (p = ef_ptr(type, i)); i++) {
226         xdflds(&xd, ca, p);
227         printf("\n");
228     }
229     xdftr(&xd, i);
230 }
231
232
233 /* TODO remove need for this */
234
235 #include <math.h>
236 #include "ship.h"
237 #include "plane.h"
238 #include "land.h"
239 #include "nuke.h"
240
241 static int fit_plane_on_ship(struct plnstr *, struct shpstr *);
242 static int fit_plane_on_land(struct plnstr *, struct lndstr *);
243
244 static void
245 pln_fixup(void)
246 {
247     int i;
248     struct plnstr *pp;
249     struct shpstr *csp;
250     struct lndstr *clp;
251
252     for (i = 0; (pp = ef_ptr(EF_PLANE, i)); i++) {
253         if (!pp->pln_own)
254             continue;
255         csp = ef_ptr(EF_SHIP, pp->pln_ship);
256         clp = ef_ptr(EF_LAND, pp->pln_land);
257         if (csp)
258             fit_plane_on_ship(pp, csp);
259         else if (clp)
260             fit_plane_on_land(pp, clp);
261     }
262 }
263
264 static void
265 lnd_fixup(void)
266 {
267     int i;
268     struct lndstr *lp;
269     struct shpstr *csp;
270     struct lndstr *clp;
271
272     for (i = 0; (lp = ef_ptr(EF_LAND, i)); i++) {
273         if (!lp->lnd_own)
274             continue;
275         csp = ef_ptr(EF_SHIP, lp->lnd_ship);
276         clp = ef_ptr(EF_LAND, lp->lnd_land);
277         if (csp)
278             csp->shp_nland++;
279         else if (clp)
280             clp->lnd_nland++;
281     }
282 }
283
284 static void
285 nuk_fixup(void)
286 {
287     int i;
288     struct nukstr *np;
289     struct plnstr *cpp;
290
291     for (i = 0; (np = ef_ptr(EF_NUKE, i)); i++) {
292         if (!np->nuk_own)
293             continue;
294         cpp = ef_ptr(EF_PLANE, np->nuk_plane);
295         if (cpp)
296             cpp->pln_nuketype = np->nuk_type;
297     }
298 }
299
300 /* Temporarily copied from src/lib/subs/???sub.c */
301
302 /*
303  * Fit a plane of PP's type on ship SP.
304  * Adjust SP's plane counters.
305  * Updating the plane accordingly is the caller's job.
306  * Return whether it fits.
307  */
308 static int
309 fit_plane_on_ship(struct plnstr *pp, struct shpstr *sp)
310 {
311     struct plchrstr *pcp = plchr + pp->pln_type;
312     struct mchrstr *mcp = mchr + sp->shp_type;
313     int wanted;
314
315     if (pcp->pl_flags & P_K) {
316         /* chopper, try chopper slot first */
317         if (sp->shp_nchoppers < mcp->m_nchoppers)
318             return ++sp->shp_nchoppers;
319         /* else try plane slot */
320         wanted = M_FLY;
321     } else if (pcp->pl_flags & P_E) {
322         /* x-light, try x-light slot first */
323         if (sp->shp_nxlight < mcp->m_nxlight)
324             return ++sp->shp_nxlight;
325         /* else try plane slot */
326         wanted = M_MSL | M_FLY;
327     } else if (!(pcp->pl_flags & P_L)) {
328         /* not light, no go */
329         wanted = 0;
330     } else if (pcp->pl_flags & P_M) {
331         /* missile, use plane slot */
332         wanted = M_MSL | M_FLY;
333     } else {
334         /* fixed-wing plane, use plane slot */
335         wanted = M_FLY;
336     }
337
338     if ((mcp->m_flags & wanted) == 0)
339         return 0;               /* ship not capable */
340
341     if (sp->shp_nplane < mcp->m_nplanes)
342         return ++sp->shp_nplane;
343
344     return 0;
345 }
346
347 /*
348  * Fit a plane of PP's type on land unit LP.
349  * Adjust LP's plane counters.
350  * Updating the plane accordingly is the caller's job.
351  * Return whether it fits.
352  */
353 static int
354 fit_plane_on_land(struct plnstr *pp, struct lndstr *lp)
355 {
356     struct plchrstr *pcp = plchr + pp->pln_type;
357     struct lchrstr *lcp = lchr + lp->lnd_type;
358
359     if ((pcp->pl_flags & P_E) && lp->lnd_nxlight < lcp->l_nxlight)
360         return ++lp->lnd_nxlight;
361
362     return 0;
363 }