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
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.
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.
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/>.
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.
27 * ef_verify.c: Verify game configuration
29 * Known contributors to this file:
30 * Ron Koenderink, 2005
31 * Markus Armbruster, 2006-2011
45 static void verify_fail(int, int, struct castr *, int, char *, ...)
46 ATTRIBUTE((format (printf, 5, 6)));
49 verify_fail(int type, int row, struct castr *ca, int idx, char *fmt, ...)
51 int base = empfile[type].base < 0 ? type : empfile[type].base;
54 fprintf(stderr, "%s %s uid %d",
55 EF_IS_GAME_STATE(base) ? "File" : "Config",
56 ef_nameof(type), row);
58 fprintf(stderr, " field %s", ca->ca_name);
59 if (ca->ca_type != NSC_STRINGY && ca->ca_len != 0)
60 fprintf(stderr, "(%d)", idx);
62 fprintf(stderr, ": ");
64 vfprintf(stderr, fmt, ap);
72 struct castr *ca = ef_cadef(type);
75 for (i = 0; ca[i].ca_name; i++) {
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.
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;
89 verify_tabref(int type, int row, struct castr *ca, int idx, long val)
91 int tabno = ca->ca_table;
92 struct castr *ca_sym = ef_cadef(tabno);
95 if (ca->ca_flags & NSC_BITS) {
97 if (CANT_HAPPEN(ca_sym != symbol_ca))
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));
109 } else if (ca_sym == symbol_ca) {
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));
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));
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));
137 verify_row(int type, int row)
139 struct castr *ca = ef_cadef(type);
140 struct empobj *row_ref;
144 int flags = ef_flags(type);
147 row_ref = ef_ptr(type, row);
149 row_ref = malloc(empfile[type].size);
150 ef_read(type, row, row_ref);
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");
160 if (!empobj_in_use(type, row_ref))
163 for (i = 0; ca[i].ca_name; ++i) {
165 continue; /* virtual */
166 n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
169 if (ca[i].ca_table == EF_BAD)
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)) {
177 if (ca[i].ca_table == type && i == 0) {
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);
186 if (verify_tabref(type, row, &ca[i], j, val.val_as.lng) < 0)
191 if (!(flags & EFF_MEM))
197 pln_zap_transient_flags(int may_put)
203 /* laziness: assumes plane file is EFF_MEM */
204 for (i = 0; (pp = getplanep(i)) != NULL; i++) {
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 */
212 verify_fail(EF_PLANE, i, NULL, 0, "stuck in the air (fixed)");
214 * Can't putplane() here, because pln_prewrite() crashes
215 * without a valid player.
219 if (dirty && may_put)
220 ef_flush(EF_PLANE); /* pretty wasteful */
224 * Verify game state and configuration are sane.
225 * Correct minor problems, but write corrections to backing files only
226 * if MAY_PUT is non-zero.
227 * Return -1 if uncorrected problems remain, else 0.
230 ef_verify(int may_put)
236 for (ep = empfile; ep->name; ep++) {
237 if (!ef_cadef(ep->uid))
240 for (i = 0; i < ef_nelem(ep->uid); i++) {
241 retval += verify_row(ep->uid, i);
246 for (i = 0; pchr[i].p_sname; i++) {
247 if (!pchr[i].p_sname[0])
249 if ((pchr[i].p_type >= 0) == (pchr[i].p_level >= 0)) {
251 "Config %s uid %d field level doesn't match field type\n",
252 ef_nameof(EF_PRODUCT), i);
257 pln_zap_transient_flags(may_put);