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
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.
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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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.
28 * ef_verify.c: Verify game configuration
30 * Known contributors to this file:
31 * Ron Koenderink, 2005
32 * Markus Armbruster, 2006-2008
46 static void verify_fail(int, int, struct castr *, int, char *, ...)
47 ATTRIBUTE((format (printf, 5, 6)));
50 verify_fail(int type, int row, struct castr *ca, int idx, char *fmt, ...)
55 /* Find base table of view, if any */
56 for (i = 0; empfile[i].cache == empfile[type].cache; i++) ;
58 fprintf(stderr, "%s %s uid %d",
59 EF_IS_GAME_STATE(i) ? "File" : "Config",
60 ef_nameof(type), row);
62 fprintf(stderr, " field %s", ca->ca_name);
63 if (ca->ca_type != NSC_STRINGY && ca->ca_len != 0)
64 fprintf(stderr, "(%d)", idx);
66 fprintf(stderr, ": ");
68 vfprintf(stderr, fmt, ap);
74 verify_row(int type, int row)
76 struct castr *ca = ef_cadef(type);
77 struct emptypedstr *row_ref;
82 int flags = ef_flags(type);
85 row_ref = ef_ptr(type, row);
87 row_ref = malloc(empfile[type].size);
88 ef_read(type, row, row_ref);
91 if ((flags & EFF_TYPED) && !EF_IS_VIEW(type)) {
92 if (row_ref->ef_type != type || row_ref->uid != row) {
93 verify_fail(type, row, NULL, 0, "header corrupt");
98 for (i = 0; ca[i].ca_name; ++i) {
99 if (ca[i].ca_flags & NSC_EXTRA)
101 n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
104 if (ca[i].ca_table == EF_BAD)
106 nstr_mksymval(&val, &ca[i], j);
107 nstr_exec_val(&val, 0, row_ref, NSC_NOTYPE);
108 if (val.val_type != NSC_LONG)
110 ca_sym = ef_cadef(ca[i].ca_table);
111 if (ca[i].ca_flags & NSC_BITS) {
113 if (CANT_HAPPEN(ca_sym != symbol_ca)) {
117 for (k = 0; k < (int)sizeof(long) * 8; k++) {
118 if (val.val_as.lng & (1L << k))
119 if (!symbol_by_value(1L << k,
120 ef_ptr(ca[i].ca_table, 0))) {
121 verify_fail(type, row, &ca[i], j,
122 "bit %d is not in symbol table %s",
123 k, ef_nameof(ca[i].ca_table));
127 } else if (ca[i].ca_table == type && i == 0) {
129 /* Some files contain zeroed records, cope */
130 /* TODO tighten this check */
131 if (val.val_as.lng == 0)
133 if (val.val_as.lng != row) {
134 verify_fail(type, row, &ca[i], j,
135 "value is %ld instead of %d",
136 val.val_as.lng, row);
140 } else if (ca_sym == symbol_ca) {
142 if (!symbol_by_value(val.val_as.lng,
143 ef_ptr(ca[i].ca_table, 0))) {
144 verify_fail(type, row, &ca[i], j,
145 "value %ld is not in symbol table %s",
146 val.val_as.lng, ef_nameof(ca[i].ca_table));
151 if (val.val_as.lng >= ef_nelem(ca[i].ca_table)
152 || val.val_as.lng < -1) {
153 verify_fail(type, row, &ca[i], j,
154 "value %ld indexes table %s out of bounds 0..%d",
155 val.val_as.lng, ef_nameof(ca[i].ca_table),
156 ef_nelem(ca[i].ca_table));
162 if (!(flags & EFF_MEM))
168 pln_zap_transient_flags(void)
174 /* laziness: assumes plane file is EFF_MEM */
175 for (i = 0; (pp = getplanep(i)) != NULL; i++) {
176 if (pp->pln_flags & PLN_LAUNCHED
177 && (plchr[pp->pln_type].pl_flags & (P_M | P_O)) != P_O) {
178 pp->pln_flags &= ~PLN_LAUNCHED;
179 /* FIXME missile should be destroyed instead */
181 verify_fail(EF_PLANE, i, NULL, 0, "stuck in the air (fixed)");
183 * Can't putplane() here, because pln_prewrite() crashes
184 * without a valid player.
189 ef_flush(EF_PLANE); /* pretty wasteful */
199 for (ep = empfile; ep->name; ep++) {
200 if (!ef_cadef(ep->uid))
202 for (i = 0; i < ef_nelem(ep->uid); i++) {
203 retval += verify_row(ep->uid, i);
208 for (i = 0; pchr[i].p_sname; i++) {
209 if ((pchr[i].p_type >= 0) == (pchr[i].p_level >= 0)) {
211 "Config %s uid %d field level doesn't match field type\n",
212 ef_nameof(EF_PRODUCT), i);
217 pln_zap_transient_flags();