]> git.pond.sub.org Git - empserver/blob - src/lib/common/ef_verify.c
a288cb4fd2dff30248a3a25d6ca93e222ba80b6f
[empserver] / src / lib / common / ef_verify.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, 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-2011
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->ca_type != NSC_STRINGY && ca->ca_len != 0)
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         return ret_val;
162
163     for (i = 0; ca[i].ca_name; ++i) {
164         if (ca[i].ca_get)
165             continue;           /* virtual */
166         n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
167         j = 0;
168         do {
169             if (ca[i].ca_table == EF_BAD)
170                 continue;
171             nstr_mksymval(&val, &ca[i], j);
172             nstr_exec_val(&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     if (!(flags & EFF_MEM))
192         free(row_ref);
193     return ret_val;
194 }
195
196 static void
197 pln_zap_transient_flags(void)
198 {
199     int i;
200     struct plnstr *pp;
201     int dirty = 0;
202
203     /* laziness: assumes plane file is EFF_MEM */
204     for (i = 0; (pp = getplanep(i)) != NULL; i++) {
205         if (!pp->pln_own)
206             continue;
207         if (pp->pln_flags & PLN_LAUNCHED
208             && (plchr[pp->pln_type].pl_flags & (P_M | P_O)) != P_O) {
209             pp->pln_flags &= ~PLN_LAUNCHED;
210             /* FIXME missile should be destroyed instead */
211             dirty = 1;
212             verify_fail(EF_PLANE, i, NULL, 0, "stuck in the air (fixed)");
213             /*
214              * Can't putplane() here, because pln_prewrite() crashes
215              * without a valid player.
216              */
217         }
218     }
219     if (dirty)
220         ef_flush(EF_PLANE);     /* pretty wasteful */
221 }
222
223 int
224 ef_verify(void)
225 {
226     struct empfile *ep;
227     int retval = 0;
228     int i;
229
230     for (ep = empfile; ep->name; ep++) {
231         if (!ef_cadef(ep->uid))
232             continue;
233         verify_ca(ep->uid);
234         for (i = 0; i < ef_nelem(ep->uid); i++) {
235             retval += verify_row(ep->uid, i);
236         }
237     }
238
239     /* Special checks */
240     for (i = 0; pchr[i].p_sname; i++) {
241         if (!pchr[i].p_sname[0])
242             continue;
243         if ((pchr[i].p_type >= 0) == (pchr[i].p_level >= 0)) {
244             fprintf(stderr,
245                 "Config %s uid %d field level doesn't match field type\n",
246                 ef_nameof(EF_PRODUCT), i);
247             retval = -1;
248         }
249     }
250
251     pln_zap_transient_flags();
252     return retval;
253 }