]> git.pond.sub.org Git - empserver/blobdiff - src/lib/common/ef_verify.c
Update copyright notice
[empserver] / src / lib / common / ef_verify.c
index 83f66251ad2c1d6e7a9245661db687d8d9b7a478..7660507480da44d9bf910f32b04d2c177e84731e 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
  *  ---
  *
  *  ef_verify.c: Verify game configuration
- * 
+ *
  *  Known contributors to this file:
  *     Ron Koenderink, 2005
+ *     Markus Armbruster, 2006-2008
  */
 
 #include <config.h>
 
+#include <stdarg.h>
 #include <stdio.h>
-#include <time.h>
-
-#include "prototypes.h"
+#include <stdlib.h>
 #include "file.h"
+#include "misc.h"
 #include "nsc.h"
+#include "plane.h"
+#include "product.h"
+
+static void verify_fail(int, int, struct castr *, int, char *, ...)
+    ATTRIBUTE((format (printf, 5, 6)));
+
+static void
+verify_fail(int type, int row, struct castr *ca, int idx, char *fmt, ...)
+{
+    int i;
+    va_list ap;
+
+    /* Find base table of view, if any */
+    for (i = 0; empfile[i].cache == empfile[type].cache; i++) ;
+
+    fprintf(stderr, "%s %s uid %d",
+           EF_IS_GAME_STATE(i) ? "File" : "Config",
+           ef_nameof(type), row);
+    if (ca) {
+       fprintf(stderr, " field %s", ca->ca_name);
+       if (ca->ca_type != NSC_STRINGY && ca->ca_len != 0)
+           fprintf(stderr, "(%d)", idx);
+    }
+    fprintf(stderr, ": ");
+    va_start(ap, fmt);
+    vfprintf(stderr, fmt, ap);
+    va_end(ap);
+    putc('\n', stderr);
+}
+
+static int
+verify_ca(int type)
+{
+    struct castr *ca = ef_cadef(type);
+    int i;
+
+    for (i = 0; ca[i].ca_name; i++) {
+       /*
+        * Virtual selectors must be NSC_EXTRA, because xundump can't
+        * cope with them without setter methods.  Exception: if
+        * EFF_MEM is not set, xundump doesn't touch the table.
+        */
+       if (CANT_HAPPEN((ef_flags(type) & EFF_MEM)
+                       && ca[i].ca_get && !(ca[i].ca_flags & NSC_EXTRA)))
+           ca[i].ca_flags |= NSC_EXTRA;
+    }
+    return 0;
+}
 
 static int
 verify_row(int type, int row)
 {
     struct castr *ca = ef_cadef(type);
-    void *row_ref;
+    struct emptypedstr *row_ref;
     int i, j, k, n;
     struct castr *ca_sym;
     struct valstr val;
-    int ret_val = 0; 
-    int in_mem = (ef_flags(type) & EFF_MEM) != 0; 
-    if (in_mem)
-       row_ref = ef_ptr(type, row); 
+    int ret_val = 0;
+    int flags = ef_flags(type);
+
+    if (flags & EFF_MEM)
+       row_ref = ef_ptr(type, row);
     else {
-       row_ref = malloc(empfile[type].size); 
+       row_ref = malloc(empfile[type].size);
        ef_read(type, row, row_ref);
     }
 
+    if ((flags & EFF_TYPED) && !EF_IS_VIEW(type)) {
+       if (row_ref->ef_type != type || row_ref->uid != row) {
+           verify_fail(type, row, NULL, 0, "header corrupt");
+           ret_val = -1;
+       }
+    }
+
     for (i = 0; ca[i].ca_name; ++i) {
        if (ca[i].ca_flags & NSC_EXTRA)
            continue;
        n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
        j = 0;
        do {
-           if (ca[i].ca_table == EF_BAD || ca[i].ca_table == type)
+           if (ca[i].ca_table == EF_BAD)
                continue;
-           val.val_type = ca[i].ca_type;
-           val.val_cat = NSC_OFF;
-           val.val_as.sym.off = ca[i].ca_off;
-           val.val_as.sym.idx = j;
+           nstr_mksymval(&val, &ca[i], j);
            nstr_exec_val(&val, 0, row_ref, NSC_NOTYPE);
-           if (val.val_type != NSC_LONG && val.val_type != NSC_TYPEID)
+           if (val.val_type != NSC_LONG)
                continue;
            ca_sym = ef_cadef(ca[i].ca_table);
            if (ca[i].ca_flags & NSC_BITS) {
@@ -82,60 +135,81 @@ verify_row(int type, int row)
                }
                for (k = 0; k < (int)sizeof(long) * 8; k++) {
                    if (val.val_as.lng & (1L << k))
-                       if (!symbol_by_value(1L << k, ef_ptr(ca[i].ca_table, 0))) {
-                           fprintf(stderr,
-                                   "bit %d not found in symbol table %s "
-                                   "when verify table %s row %d field %s\n",
-                                   k, ef_nameof(ca[i].ca_table),
-                                   ef_nameof(type), row + 1, ca[i].ca_name);
+                       if (!symbol_by_value(1L << k,
+                                            ef_ptr(ca[i].ca_table, 0))) {
+                           verify_fail(type, row, &ca[i], j,
+                                       "bit %d is not in symbol table %s",
+                                       k, ef_nameof(ca[i].ca_table));
                            ret_val = -1;
                        }
                }
            } else if (ca[i].ca_table == type && i == 0) {
                /* uid */
+               /* Some files contain zeroed records, cope */
+               /* TODO tighten this check */
+               if (val.val_as.lng == 0)
+                   continue;
                if (val.val_as.lng != row) {
-                   fprintf(stderr,
-                           "table %s row %d field %s is %ld instead of %d\n",
-                           ef_nameof(type), row + 1, ca[i].ca_name,
-                           val.val_as.lng, row);
+                   verify_fail(type, row, &ca[i], j,
+                               "value is %ld instead of %d",
+                               val.val_as.lng, row);
                    ret_val = -1;
                }
 
            } else if (ca_sym == symbol_ca) {
                /* symbol */
-               if (val.val_as.lng > -1) {
-                   if (!symbol_by_value(val.val_as.lng,
-                                        ef_ptr(ca[i].ca_table,0))) {
-                       fprintf(stderr, "value %ld not found in "
-                               "symbol table %s when verify table %s "
-                               "row %d field %s\n",
-                               val.val_as.lng,
-                               ef_nameof(ca[i].ca_table),
-                               ef_nameof(type), row + 1,
-                               ca[i].ca_name);
-                       ret_val = -1;
-                   }
+               if (!symbol_by_value(val.val_as.lng,
+                                    ef_ptr(ca[i].ca_table, 0))) {
+                   verify_fail(type, row, &ca[i], j,
+                               "value %ld is not in symbol table %s",
+                               val.val_as.lng, ef_nameof(ca[i].ca_table));
+                   ret_val = -1;
                }
            } else {
                /* table index */
-               if (val.val_as.lng >= ef_nelem(ca[i].ca_table) ||
-                   val.val_as.lng < -2) {
-                   fprintf(stderr, "Table index %ld to table %s "
-                           "out of range, nelements %d for table %s "
-                           "row %d field %s\n",
-                           val.val_as.lng, ef_nameof(ca[i].ca_table),
-                           ef_nelem(ca[i].ca_table), ef_nameof(type), 
-                           row + 1, ca[i].ca_name);
+               if (val.val_as.lng >= ef_nelem(ca[i].ca_table)
+                   || val.val_as.lng < -1) {
+                   verify_fail(type, row, &ca[i], j,
+                       "value %ld indexes table %s out of bounds 0..%d",
+                       val.val_as.lng, ef_nameof(ca[i].ca_table),
+                       ef_nelem(ca[i].ca_table));
                    ret_val = -1;
                }
            }
        } while (++j < n);
-    } 
-    if (!in_mem)
+    }
+    if (!(flags & EFF_MEM))
         free(row_ref);
     return ret_val;
 }
 
+static void
+pln_zap_transient_flags(void)
+{
+    int i;
+    struct plnstr *pp;
+    int dirty = 0;
+
+    /* laziness: assumes plane file is EFF_MEM */
+    for (i = 0; (pp = getplanep(i)) != NULL; i++) {
+       if (!pp->pln_own)
+           continue;
+       if (pp->pln_flags & PLN_LAUNCHED
+           && (plchr[pp->pln_type].pl_flags & (P_M | P_O)) != P_O) {
+           pp->pln_flags &= ~PLN_LAUNCHED;
+           /* FIXME missile should be destroyed instead */
+           dirty = 1;
+           verify_fail(EF_PLANE, i, NULL, 0, "stuck in the air (fixed)");
+           /*
+            * Can't putplane() here, because pln_prewrite() crashes
+            * without a valid player.
+            */
+       }
+    }
+    if (dirty)
+       ef_flush(EF_PLANE);     /* pretty wasteful */
+}
+
 int
 ef_verify()
 {
@@ -146,9 +220,22 @@ ef_verify()
     for (ep = empfile; ep->name; ep++) {
        if (!ef_cadef(ep->uid))
            continue;
+       verify_ca(ep->uid);
        for (i = 0; i < ef_nelem(ep->uid); i++) {
            retval += verify_row(ep->uid, i);
        }
     }
+
+    /* Special checks */
+    for (i = 0; pchr[i].p_sname; i++) {
+       if ((pchr[i].p_type >= 0) == (pchr[i].p_level >= 0)) {
+           fprintf(stderr,
+               "Config %s uid %d field level doesn't match field type\n",
+               ef_nameof(EF_PRODUCT), i);
+           retval = -1;
+       }
+    }
+
+    pln_zap_transient_flags();
     return retval;
 }