]> git.pond.sub.org Git - empserver/blob - src/lib/common/ef_verify.c
nsc: New enum ca_dump member CA_DUMP_ONLY
[empserver] / src / lib / common / ef_verify.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  ef_verify.c: Verify game configuration
28  *
29  *  Known contributors to this file:
30  *     Ron Koenderink, 2005
31  *     Markus Armbruster, 2006-2016
32  */
33
34 #include <config.h>
35
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include "empobj.h"
40 #include "file.h"
41 #include "misc.h"
42 #include "nsc.h"
43 #include "product.h"
44
45 static void verify_fail(int, int, struct castr *, int, char *, ...)
46     ATTRIBUTE((format (printf, 5, 6)));
47
48 static void
49 verify_fail(int type, int row, struct castr *ca, int idx, char *fmt, ...)
50 {
51     int base = empfile[type].base < 0 ? type : empfile[type].base;
52     va_list ap;
53
54     fprintf(stderr, "%s %s uid %d",
55             EF_IS_GAME_STATE(base) ? "File" : "Config",
56             ef_nameof(type), row);
57     if (ca) {
58         fprintf(stderr, " field %s", ca->ca_name);
59         if (CA_IS_ARRAY(ca))
60             fprintf(stderr, "(%d)", idx);
61     }
62     fprintf(stderr, ": ");
63     va_start(ap, fmt);
64     vfprintf(stderr, fmt, ap);
65     va_end(ap);
66     putc('\n', stderr);
67 }
68
69 static int
70 verify_ca(int type)
71 {
72     struct castr *ca = ef_cadef(type);
73     int i;
74
75     for (i = 0; ca[i].ca_name; i++) {
76         /*
77          * Virtual selectors can't be used in xundump, since we lack a
78          * setter to go with ca_get().  Exception: if EFF_MEM is not
79          * set, xundump doesn't touch the table.
80          */
81         if (CANT_HAPPEN((ef_flags(type) & EFF_MEM)
82                         && ca[i].ca_get
83                         && ca[i].ca_dump <= CA_DUMP_CONST))
84             ca[i].ca_dump = CA_DUMP_ONLY;
85     }
86     return 0;
87 }
88
89 static int
90 verify_tabref(int type, int row, struct castr *ca, int idx, long val)
91 {
92     int tabno = ca->ca_table;
93     struct castr *ca_sym = ef_cadef(tabno);
94     int i;
95
96     if (ca->ca_flags & NSC_BITS) {
97         /* symbol set */
98         if (CANT_HAPPEN(ca_sym != symbol_ca))
99             return -1;
100         for (i = 0; i < (int)sizeof(long) * 8; i++) {
101             if (val & (1UL << i)) {
102                 if (!symbol_by_value(1L << i, ef_ptr(tabno, 0))) {
103                     verify_fail(type, row, ca, idx,
104                                 "bit %d is not in symbol table %s",
105                                 i, ef_nameof(tabno));
106                     return -1;
107                 }
108             }
109         }
110     } else if (ca_sym == symbol_ca) {
111         /* symbol */
112         if (!symbol_by_value(val, ef_ptr(tabno, 0))) {
113             verify_fail(type, row, ca, idx,
114                         "value %ld is not in symbol table %s",
115                         val, ef_nameof(tabno));
116             return -1;
117         }
118     } else {
119         /* table index */
120         if (val >= ef_nelem(tabno) || val < -1) {
121             verify_fail(type, row, ca, idx,
122                         "value %ld indexes table %s out of bounds 0..%d",
123                         val, ef_nameof(tabno), ef_nelem(tabno));
124             return -1;
125         }
126         /* laziness: assumes TABNO is EFF_MEM */
127         if (val >= 0 && !empobj_in_use(tabno, ef_ptr(tabno, val))) {
128             verify_fail(type, row, ca, idx,
129                         "value %ld refers to missing element of table %s",
130                         val, ef_nameof(tabno));
131             return -1;
132         }
133     }
134     return 0;
135 }
136
137 static int
138 verify_row(int type, int row)
139 {
140     struct castr *ca = ef_cadef(type);
141     struct empobj *row_ref;
142     int i, j, n;
143     struct valstr val;
144     int ret_val = 0;
145     int flags = ef_flags(type);
146
147     if (flags & EFF_MEM)
148         row_ref = ef_ptr(type, row);
149     else {
150         row_ref = malloc(empfile[type].size);
151         ef_read(type, row, row_ref);
152     }
153
154     if ((flags & EFF_TYPED) && !EF_IS_VIEW(type)) {
155         if (row_ref->ef_type != type || row_ref->uid != row) {
156             verify_fail(type, row, NULL, 0, "header corrupt");
157             ret_val = -1;
158         }
159     }
160
161     if (!empobj_in_use(type, row_ref))
162         goto out;
163
164     for (i = 0; ca[i].ca_name; ++i) {
165         if (ca[i].ca_get)
166             continue;           /* virtual */
167         n = CA_ARRAY_LEN(&ca[i]);
168         j = 0;
169         do {
170             if (ca[i].ca_table == EF_BAD)
171                 continue;
172             nstr_mksymval(&val, &ca[i], j);
173             nstr_eval(&val, 0, row_ref, NSC_NOTYPE);
174             if (CANT_HAPPEN(val.val_type != NSC_LONG)) {
175                 ret_val = -1;
176                 continue;
177             }
178             if (ca[i].ca_table == type && i == 0) {
179                 /* uid */
180                 if (val.val_as.lng != row) {
181                     verify_fail(type, row, &ca[i], j,
182                                 "value is %ld instead of %d",
183                                 val.val_as.lng, row);
184                     ret_val = -1;
185                 }
186             } else {
187                 if (verify_tabref(type, row, &ca[i], j, val.val_as.lng) < 0)
188                     ret_val = -1;
189             }
190         } while (++j < n);
191     }
192
193 out:
194     if (!(flags & EFF_MEM))
195         free(row_ref);
196     return ret_val;
197 }
198
199 static int
200 verify_table(int type)
201 {
202     int retval = 0;
203     int i;
204
205     if (!ef_cadef(type))
206         return 0;
207     verify_ca(type);
208     for (i = 0; i < ef_nelem(type); i++)
209         retval |= verify_row(type, i);
210     return retval;
211 }
212
213 static int
214 verify_sectors(int may_put)
215 {
216     int i;
217     struct sctstr *sp;
218     coord x, y;
219
220     /* laziness: assumes sector file is EFF_MEM */
221     for (i = 0; (sp = getsectid(i)); i++) {
222         sctoff2xy(&x, &y, sp->sct_uid);
223         if (sp->sct_x != x || sp->sct_y != y) {
224             sp->sct_x = x;
225             sp->sct_y = y;
226             if (may_put)
227                 putsect(sp);
228             verify_fail(EF_SECTOR, i, NULL, 0, "bogus coordinates (fixed)");
229         }
230     }
231     return 0;
232 }
233
234 static int
235 verify_planes(int may_put)
236 {
237     int retval = 0;
238     int i;
239     struct plnstr *pp;
240
241     /* laziness: assumes plane file is EFF_MEM */
242     for (i = 0; (pp = getplanep(i)); i++) {
243         if (pp->pln_own) {
244             if (pp->pln_flags & PLN_LAUNCHED
245                 && (plchr[pp->pln_type].pl_flags & (P_M | P_O)) != P_O) {
246                 pp->pln_flags &= ~PLN_LAUNCHED;
247                 /* FIXME missile should be destroyed instead */
248                 if (may_put)
249                     putplane(i, pp);
250                 verify_fail(EF_PLANE, i, NULL, 0, "stuck in the air (fixed)");
251             }
252             if (pp->pln_ship >= 0 && pp->pln_land >= 0) {
253                 verify_fail(EF_PLANE, i, NULL, 0, "on two carriers");
254                 retval = -1;
255             }
256         } else {
257             if (pp->pln_ship >= 0 || pp->pln_land >= 0) {
258                 pp->pln_ship = pp->pln_land = -1;
259                 if (may_put)
260                     putplane(i, pp);
261                 verify_fail(EF_PLANE, i, NULL, 0,
262                             "ghost stuck on carrier (fixed)");
263             }
264         }
265     }
266     return retval;
267 }
268
269 static int
270 verify_lands(int may_put)
271 {
272     int retval = 0;
273     int i;
274     struct lndstr *lp;
275
276     /* laziness: assumes land file is EFF_MEM */
277     for (i = 0; (lp = getlandp(i)); i++) {
278         if (lp->lnd_own) {
279             if (lp->lnd_ship >= 0 && lp->lnd_land >= 0) {
280                 verify_fail(EF_LAND, i, NULL, 0, "on two carriers");
281                 retval = -1;
282             }
283         } else {
284             if (lp->lnd_ship >= 0 || lp->lnd_land >= 0) {
285                 lp->lnd_ship = lp->lnd_land = -1;
286                 if (may_put)
287                     putland(i, lp);
288                 verify_fail(EF_LAND, i, NULL, 0,
289                             "ghost stuck on carrier (fixed)");
290             }
291         }
292     }
293     return retval;
294 }
295
296 static int
297 verify_nukes(int may_put)
298 {
299     int retval = 0;
300     int i;
301     struct nukstr *np;
302
303     /* laziness: assumes nuke file is EFF_MEM */
304     for (i = 0; (np = getnukep(i)); i++) {
305         if (!np->nuk_own) {
306             if (np->nuk_plane >= 0) {
307                 np->nuk_plane = -1;
308                 if (may_put)
309                     putnuke(i, np);
310                 verify_fail(EF_NUKE, i, NULL, 0,
311                             "ghost stuck on carrier (fixed)");
312             }
313         }
314     }
315     return retval;
316 }
317
318 static int
319 verify_ship_chr(void)
320 {
321     int retval = 0;
322     int i;
323
324     for (i = 0; mchr[i].m_name; i++) {
325         if (!mchr[i].m_name[0])
326             continue;
327         if ((mchr[i].m_flags & M_DCH) && !mchr[i].m_glim) {
328             verify_fail(EF_SHIP_CHR, i, NULL, 0,
329                         "flag %s requires non-zero glim",
330                         symbol_by_value(M_DCH, ship_chr_flags));
331             retval = -1;
332         }
333         if (mchr[i].m_nplanes && !(mchr[i].m_flags & (M_MSL | M_FLY))) {
334             verify_fail(EF_SHIP_CHR, i, NULL, 0,
335                         "non-zero nplanes needs flag %s or %s",
336                         symbol_by_value(M_FLY, ship_chr_flags),
337                         symbol_by_value(M_MSL, ship_chr_flags));
338             retval = -1;
339         }
340     }
341     return retval;
342 }
343
344 static int
345 verify_plane_chr(void)
346 {
347     int retval = 0;
348     int i;
349
350     for (i = 0; plchr[i].pl_name; i++) {
351         if (!plchr[i].pl_name[0])
352             continue;
353         if ((plchr[i].pl_flags & (P_M | P_V)) == P_M) {
354             verify_fail(EF_PLANE_CHR, i, NULL, 0,
355                         "flag %s requires flag %s",
356                         symbol_by_value(P_M, plane_chr_flags),
357                         symbol_by_value(P_V, plane_chr_flags));
358             retval = -1;
359         }
360     }
361     return retval;
362 }
363
364 static int
365 verify_products(void)
366 {
367     int retval = 0;
368     int i;
369
370     /* product makes either level or item, not both */
371     for (i = 0; pchr[i].p_sname; i++) {
372         if (!pchr[i].p_sname[0])
373             continue;
374         if ((pchr[i].p_type >= 0) == (pchr[i].p_level >= 0)) {
375             verify_fail(EF_PRODUCT, i, NULL, 0,
376                         "level must be none or type -1");
377             retval = -1;
378         }
379     }
380     return retval;
381 }
382
383 /*
384  * Verify game configuration is sane.
385  * Return 0 on success, -1 on failure.
386  */
387 int
388 ef_verify_config(void)
389 {
390     int retval = 0;
391     int i;
392
393     for (i = 0; i < EF_MAX; i++) {
394         if (!EF_IS_GAME_STATE(i) && !EF_IS_VIEW(i))
395             retval |= verify_table(i);
396     }
397
398     /* Special checks */
399     retval |= verify_ship_chr();
400     retval |= verify_plane_chr();
401     retval |= verify_products();
402     return retval;
403 }
404
405 /*
406  * Verify game state is sane.
407  * Correct minor problems, but write corrections to backing files only
408  * if @may_put is non-zero.
409  * Return -1 if uncorrected problems remain, else 0.
410  */
411 int
412 ef_verify_state(int may_put)
413 {
414     int retval = 0;
415     int i;
416
417     for (i = 0; i < EF_MAX; i++) {
418         if (EF_IS_GAME_STATE(i) || EF_IS_VIEW(i))
419             retval |= verify_table(i);
420     }
421
422     /* Special checks */
423     retval |= verify_sectors(may_put);
424     retval |= verify_planes(may_put);
425     retval |= verify_lands(may_put);
426     retval |= verify_nukes(may_put);
427     return retval;
428 }