]> git.pond.sub.org Git - empserver/blob - src/lib/common/ef_verify.c
init ef_verify: Don't monkey-patch capability miss, require it
[empserver] / src / lib / common / ef_verify.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2015, 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-2015
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 must be NSC_EXTRA, because xundump can't
78          * cope with them without setter methods.  Exception: if
79          * EFF_MEM is not set, xundump doesn't touch the table.
80          */
81         if (CANT_HAPPEN((ef_flags(type) & EFF_MEM)
82                         && ca[i].ca_get && !(ca[i].ca_flags & NSC_EXTRA)))
83             ca[i].ca_flags |= NSC_EXTRA;
84     }
85     return 0;
86 }
87
88 static int
89 verify_tabref(int type, int row, struct castr *ca, int idx, long val)
90 {
91     int tabno = ca->ca_table;
92     struct castr *ca_sym = ef_cadef(tabno);
93     int i;
94
95     if (ca->ca_flags & NSC_BITS) {
96         /* symbol set */
97         if (CANT_HAPPEN(ca_sym != symbol_ca))
98             return -1;
99         for (i = 0; i < (int)sizeof(long) * 8; i++) {
100             if (val & (1L << i)) {
101                 if (!symbol_by_value(1L << i, ef_ptr(tabno, 0))) {
102                     verify_fail(type, row, ca, idx,
103                                 "bit %d is not in symbol table %s",
104                                 i, ef_nameof(tabno));
105                     return -1;
106                 }
107             }
108         }
109     } else if (ca_sym == symbol_ca) {
110         /* symbol */
111         if (!symbol_by_value(val, ef_ptr(tabno, 0))) {
112             verify_fail(type, row, ca, idx,
113                         "value %ld is not in symbol table %s",
114                         val, ef_nameof(tabno));
115             return -1;
116         }
117     } else {
118         /* table index */
119         if (val >= ef_nelem(tabno) || val < -1) {
120             verify_fail(type, row, ca, idx,
121                         "value %ld indexes table %s out of bounds 0..%d",
122                         val, ef_nameof(tabno), ef_nelem(tabno));
123             return -1;
124         }
125         /* laziness: assumes TABNO is EFF_MEM */
126         if (val >= 0 && !empobj_in_use(tabno, ef_ptr(tabno, val))) {
127             verify_fail(type, row, ca, idx,
128                         "value %ld refers to missing element of table %s",
129                         val, ef_nameof(tabno));
130             return -1;
131         }
132     }
133     return 0;
134 }
135
136 static int
137 verify_row(int type, int row)
138 {
139     struct castr *ca = ef_cadef(type);
140     struct empobj *row_ref;
141     int i, j, n;
142     struct valstr val;
143     int ret_val = 0;
144     int flags = ef_flags(type);
145
146     if (flags & EFF_MEM)
147         row_ref = ef_ptr(type, row);
148     else {
149         row_ref = malloc(empfile[type].size);
150         ef_read(type, row, row_ref);
151     }
152
153     if ((flags & EFF_TYPED) && !EF_IS_VIEW(type)) {
154         if (row_ref->ef_type != type || row_ref->uid != row) {
155             verify_fail(type, row, NULL, 0, "header corrupt");
156             ret_val = -1;
157         }
158     }
159
160     if (!empobj_in_use(type, row_ref))
161         goto out;
162
163     for (i = 0; ca[i].ca_name; ++i) {
164         if (ca[i].ca_get)
165             continue;           /* virtual */
166         n = CA_ARRAY_LEN(&ca[i]);
167         j = 0;
168         do {
169             if (ca[i].ca_table == EF_BAD)
170                 continue;
171             nstr_mksymval(&val, &ca[i], j);
172             nstr_eval(&val, 0, row_ref, NSC_NOTYPE);
173             if (CANT_HAPPEN(val.val_type != NSC_LONG)) {
174                 ret_val = -1;
175                 continue;
176             }
177             if (ca[i].ca_table == type && i == 0) {
178                 /* uid */
179                 if (val.val_as.lng != row) {
180                     verify_fail(type, row, &ca[i], j,
181                                 "value is %ld instead of %d",
182                                 val.val_as.lng, row);
183                     ret_val = -1;
184                 }
185             } else {
186                 if (verify_tabref(type, row, &ca[i], j, val.val_as.lng) < 0)
187                     ret_val = -1;
188             }
189         } while (++j < n);
190     }
191
192 out:
193     if (!(flags & EFF_MEM))
194         free(row_ref);
195     return ret_val;
196 }
197
198 static int
199 verify_table(int type)
200 {
201     int retval = 0;
202     int i;
203
204     if (!ef_cadef(type))
205         return 0;
206     verify_ca(type);
207     for (i = 0; i < ef_nelem(type); i++)
208         retval |= verify_row(type, i);
209     return retval;
210 }
211
212 static int
213 verify_sectors(int may_put)
214 {
215     int i;
216     struct sctstr *sp;
217     coord x, y;
218
219     /* laziness: assumes sector file is EFF_MEM */
220     for (i = 0; (sp = getsectid(i)); i++) {
221         sctoff2xy(&x, &y, sp->sct_uid);
222         if (sp->sct_x != x || sp->sct_y != y) {
223             sp->sct_x = x;
224             sp->sct_y = y;
225             if (may_put)
226                 putsect(sp);
227             verify_fail(EF_SECTOR, i, NULL, 0, "bogus coordinates (fixed)");
228         }
229     }
230     return 0;
231 }
232
233 static int
234 verify_planes(int may_put)
235 {
236     int retval = 0;
237     int i;
238     struct plnstr *pp;
239
240     /* laziness: assumes plane file is EFF_MEM */
241     for (i = 0; (pp = getplanep(i)); i++) {
242         if (pp->pln_own) {
243             if (pp->pln_flags & PLN_LAUNCHED
244                 && (plchr[pp->pln_type].pl_flags & (P_M | P_O)) != P_O) {
245                 pp->pln_flags &= ~PLN_LAUNCHED;
246                 /* FIXME missile should be destroyed instead */
247                 if (may_put)
248                     putplane(i, pp);
249                 verify_fail(EF_PLANE, i, NULL, 0, "stuck in the air (fixed)");
250             }
251             if (pp->pln_ship >= 0 && pp->pln_land >= 0) {
252                 verify_fail(EF_PLANE, i, NULL, 0, "on two carriers");
253                 retval = -1;
254             }
255         } else {
256             if (pp->pln_ship >= 0 || pp->pln_land >= 0) {
257                 pp->pln_ship = pp->pln_land = -1;
258                 if (may_put)
259                     putplane(i, pp);
260                 verify_fail(EF_PLANE, i, NULL, 0,
261                             "ghost stuck on carrier (fixed)");
262             }
263         }
264     }
265     return retval;
266 }
267
268 static int
269 verify_lands(int may_put)
270 {
271     int retval = 0;
272     int i;
273     struct lndstr *lp;
274
275     /* laziness: assumes land file is EFF_MEM */
276     for (i = 0; (lp = getlandp(i)); i++) {
277         if (lp->lnd_own) {
278             if (lp->lnd_ship >= 0 && lp->lnd_land >= 0) {
279                 verify_fail(EF_LAND, i, NULL, 0, "on two carriers");
280                 retval = -1;
281             }
282         } else {
283             if (lp->lnd_ship >= 0 || lp->lnd_land >= 0) {
284                 lp->lnd_ship = lp->lnd_land = -1;
285                 if (may_put)
286                     putland(i, lp);
287                 verify_fail(EF_LAND, i, NULL, 0,
288                             "ghost stuck on carrier (fixed)");
289             }
290         }
291     }
292     return retval;
293 }
294
295 static int
296 verify_nukes(int may_put)
297 {
298     int retval = 0;
299     int i;
300     struct nukstr *np;
301
302     /* laziness: assumes nuke file is EFF_MEM */
303     for (i = 0; (np = getnukep(i)); i++) {
304         if (!np->nuk_own) {
305             if (np->nuk_plane >= 0) {
306                 np->nuk_plane = -1;
307                 if (may_put)
308                     putnuke(i, np);
309                 verify_fail(EF_NUKE, i, NULL, 0,
310                             "ghost stuck on carrier (fixed)");
311             }
312         }
313     }
314     return retval;
315 }
316
317 static int
318 verify_ship_chr(void)
319 {
320     int retval = 0;
321     int i;
322
323     for (i = 0; mchr[i].m_name; i++) {
324         if (!mchr[i].m_name[0])
325             continue;
326         if ((mchr[i].m_flags & M_DCH) && !mchr[i].m_glim) {
327             verify_fail(EF_SHIP_CHR, i, NULL, 0,
328                         "flag %s requires non-zero glim",
329                         symbol_by_value(M_DCH, ship_chr_flags));
330             retval = -1;
331         }
332         if (mchr[i].m_nplanes && !(mchr[i].m_flags & (M_MSL | M_FLY))) {
333             verify_fail(EF_SHIP_CHR, i, NULL, 0,
334                         "non-zero nplanes needs flag %s or %s",
335                         symbol_by_value(M_FLY, ship_chr_flags),
336                         symbol_by_value(M_MSL, ship_chr_flags));
337             retval = -1;
338         }
339     }
340     return retval;
341 }
342
343 static int
344 verify_products(void)
345 {
346     int retval = 0;
347     int i;
348
349     /* product makes either level or item, not both */
350     for (i = 0; pchr[i].p_sname; i++) {
351         if (!pchr[i].p_sname[0])
352             continue;
353         if ((pchr[i].p_type >= 0) == (pchr[i].p_level >= 0)) {
354             fprintf(stderr,
355                 "Config %s uid %d field level doesn't match field type\n",
356                 ef_nameof(EF_PRODUCT), i);
357             retval = -1;
358         }
359     }
360     return retval;
361 }
362
363 /*
364  * Verify game configuration is sane.
365  * Return 0 on success, -1 on failure.
366  */
367 int
368 ef_verify_config(void)
369 {
370     int retval = 0;
371     int i;
372
373     for (i = 0; i < EF_MAX; i++) {
374         if (!EF_IS_GAME_STATE(i) && !EF_IS_VIEW(i))
375             retval |= verify_table(i);
376     }
377
378     /* Special checks */
379     retval |= verify_ship_chr();
380     retval |= verify_products();
381     return retval;
382 }
383
384 /*
385  * Verify game state is sane.
386  * Correct minor problems, but write corrections to backing files only
387  * if MAY_PUT is non-zero.
388  * Return -1 if uncorrected problems remain, else 0.
389  */
390 int
391 ef_verify_state(int may_put)
392 {
393     int retval = 0;
394     int i;
395
396     for (i = 0; i < EF_MAX; i++) {
397         if (EF_IS_GAME_STATE(i) || EF_IS_VIEW(i))
398             retval |= verify_table(i);
399     }
400
401     /* Special checks */
402     retval |= verify_sectors(may_put);
403     retval |= verify_planes(may_put);
404     retval |= verify_lands(may_put);
405     retval |= verify_nukes(may_put);
406     return retval;
407 }