]> git.pond.sub.org Git - empserver/commitdiff
Clean up initialization of empfile[]
authorMarkus Armbruster <armbru@pond.sub.org>
Wed, 6 Feb 2008 19:14:39 +0000 (20:14 +0100)
committerMarkus Armbruster <armbru@pond.sub.org>
Sun, 10 Feb 2008 10:40:57 +0000 (11:40 +0100)
Split ef_init() into two functions: empfile_init() for initialization,
and empfile_fixup() to fix it up for configuration.  Put them next to
empfile[].  Move the call to empfile_init() from behind emp_config()
to before it.

include/file.h
src/lib/common/file.c
src/lib/global/file.c
src/lib/subs/fileinit.c
src/server/main.c
src/util/files.c

index c8f82c332073fab2646515dc822de8d30b6ceaca..6748ba78a682a7b05333fc04560a1bcd519ebf87 100644 (file)
@@ -46,16 +46,19 @@ struct empfile {
     int size;                  /* size of a table entry */
     int flags;                 /* only EFF_IMMUTABLE immutable, see below
                                   for use of remaining bits */
+
     /* Members whose values are fixed when the cache is mapped */
     char *cache;               /* pointer to cache */
     int csize;                 /* cache size, in entries */
-    /* and flags bit EFF_MEM */
+    /* flags bit EFF_MEM also fixed then */
+
     /* Members whose values may vary throughout operation */
     int baseid;                        /* id of first entry in cache */
     int cids;                  /* # entries in cache */
     int fids;                  /* # entries in table */
     int fd;                    /* file descriptor, -1 if not open */
-    /* and flags bit EFF_RDONLY */
+    /* flags bits EFF_RDONLY, EFF_CUSTOM also vary */
+
     /* User callbacks */
     void (*init)(int, void *); /* called after entry creation, unless null */
     int (*postread)(int, void *); /* called after read, unless null */
@@ -131,7 +134,7 @@ enum {
     EF_INFRASTRUCTURE,
     EF_UPDATES,                        /* not actually static */
     EF_TABLE,
-    EF_META,
+    EF_META,                   /* not really configuration */
     /* Symbol tables */
     EF_AGREEMENT_STATUS,
     EF_LAND_CHR_FLAGS,
@@ -179,10 +182,11 @@ extern int ef_nelem(int);
 extern int ef_flags(int);
 extern int ef_byname(char *);
 extern int ef_byname_from(char *, int *);
-extern void ef_init(void);
 extern int ef_verify(void);
 extern int ef_elt_byname(int, char *);
 
 extern struct empfile empfile[EF_MAX + 1];
+extern void empfile_init(void);
+extern void empfile_fixup(void);
 
 #endif
index 72f213b253cebe1fe2e57b8aba465e751ba159d1..ea29bb2a82aff22cccaf2669eb0ddc47b533ec97 100644 (file)
@@ -44,7 +44,6 @@
 #include "match.h"
 #include "misc.h"
 #include "nsc.h"
-#include "optlist.h"
 #include "prototypes.h"
 
 static int fillcache(struct empfile *, int);
@@ -524,37 +523,3 @@ ef_ensure_space(int type, int id, int count)
     }
     return 1;
 }
-
-static void
-ef_fix_size(struct empfile *ep, int n)
-{
-    ep->cids = ep->fids = n;
-    ep->csize = n + 1;
-}
-
-/*
- * Initialize Empire tables.
- * Must be called once, before using anything else from this module.
- */
-void
-ef_init(void)
-{
-    struct castr *ca;
-    struct empfile *ep;
-    struct symbol *lup;
-    int i;
-
-    empfile[EF_MAP].size = empfile[EF_BMAP].size = WORLD_SZ();
-
-    ca = (struct castr *)empfile[EF_META].cache;
-    for (i = 0; ca[i].ca_name; i++) ;
-    ef_fix_size(&empfile[EF_META], i);
-
-    for (ep = empfile; ep->uid >= 0; ep++) {
-       if (ep->cadef == symbol_ca) {
-           lup = (struct symbol *)ep->cache;
-           for (i = 0; lup[i].name; i++) ;
-           ef_fix_size(ep, i);
-       }
-    }
-}
index 1104a528b7001393dbf658f4aecf7bef3070dda8..51e28dc1170f41308a0db2c92052e993af0ff200 100644 (file)
@@ -44,6 +44,7 @@
 #include "news.h"
 #include "nsc.h"
 #include "nuke.h"
+#include "optlist.h"
 #include "plane.h"
 #include "power.h"
 #include "product.h"
@@ -52,6 +53,7 @@
 #include "server.h"
 #include "trade.h"
 #include "treaty.h"
+#include "xy.h"
 
 /* Number of elements in ARRAY.  */
 #define SZ(array) (sizeof(array) / sizeof((array)[0]))
@@ -89,7 +91,7 @@ struct empfile empfile[] = {
     /*
      * How this initializer works:
      *
-     * Members uid, name, file, size, cadef, and the EFF_IMMUTABLE
+     * Members uid, name, file, cadef, size, and the EFF_IMMUTABLE
      * bits of flags get their final value.
      * If flags & EFF_STATIC, the cache is mapped here, and members
      * cache, csize get their final value.
@@ -100,7 +102,7 @@ struct empfile empfile[] = {
      * that can be changed by users.
      *
      * Whatever of the above can't be done here must be done in
-     * ef_init().
+     * empfile_init() or empfile_fixup().
      */
 
     /*
@@ -108,7 +110,12 @@ struct empfile empfile[] = {
      * values of member name: no whitespace there, please.
      */
 
-    /* Dynamic game data */
+    /*
+     * Dynamic game data
+     *
+     * All caches unmapped.  EF_MAP and EF_BMAP get a bogus size here.
+     * Fixed up by empfile_fixup().
+     */
     {EF_SECTOR, "sect", "sector", sect_ca,
      UNMAPPED_CACHE(struct sctstr, EFF_XY | EFF_OWNER)},
     {EF_SHIP, "ship", "ship", ship_ca,
@@ -144,7 +151,13 @@ struct empfile empfile[] = {
     {EF_GAME, "game", "game", game_ca,
      UNMAPPED_CACHE(struct gamestr, 0)},
 
-    /* Static game data (configuration) */
+    /*
+     * Static game data (configuration)
+     *
+     * These are all empty tables, except for EF_NEWS_CHR and EF_META.
+     * Use read_builtin_tables() to fill them.  EF_META gets bogus
+     * size, cids and fids here.  Fixed up by empfile_init().
+     */
     {EF_ITEM, "item", "item.config", ichr_ca,
      ARRAY_CACHE(ichr, EFF_CFG)},
     {EF_PRODUCT, "product", "product.config", pchr_ca,
@@ -170,7 +183,12 @@ struct empfile empfile[] = {
     {EF_META, "meta", NULL, mdchr_ca,
      PTR_CACHE(mdchr_ca, EFF_CFG)},
 
-    /* Symbol tables */
+    /*
+     * Symbol tables
+     *
+     * These get bogus size, cids and fids here.  Fixed up by
+     * empfile_init().
+     */
 #define SYMTAB(type, name, tab) \
        {(type), (name), NULL, symbol_ca, PTR_CACHE((tab), EFF_CFG)}
     SYMTAB(EF_AGREEMENT_STATUS, "agreement-status", agreement_statuses),
@@ -201,3 +219,37 @@ struct empfile empfile[] = {
     /* Sentinel */
     {EF_BAD, NULL, NULL, NULL, 0, 0, NULL, 0, 0, 0, 0, -1, NULL, NULL, NULL},
 };
+
+static void
+ef_fix_size(struct empfile *ep, int n)
+{
+    ep->cids = ep->fids = n;
+    ep->csize = n + 1;
+}
+
+void
+empfile_init(void)
+{
+    struct castr *ca;
+    struct empfile *ep;
+    struct symbol *lup;
+    int i;
+
+    ca = (struct castr *)empfile[EF_META].cache;
+    for (i = 0; ca[i].ca_name; i++) ;
+    ef_fix_size(&empfile[EF_META], i);
+
+    for (ep = empfile; ep->uid >= 0; ep++) {
+       if (ep->cadef == symbol_ca) {
+           lup = (struct symbol *)ep->cache;
+           for (i = 0; lup[i].name; i++) ;
+           ef_fix_size(ep, i);
+       }
+    }
+}
+
+void
+empfile_fixup(void)
+{
+    empfile[EF_MAP].size = empfile[EF_BMAP].size = WORLD_SZ();
+}
index 11dccda58c58c9d611c223da81b8ba1c6cb8b277..2b3f3b398da02596dde09ab762fcd9c65b605643 100644 (file)
@@ -58,7 +58,6 @@ static void ef_fina_view(int);
 
 /*
  * Initialize empfile for full server operations.
- * ef_init() must be called first.
  */
 void
 ef_init_srv(void)
index 3953f95c8bd870cfaec768cdb3a523a4f55c366c..77cf099944b073bce61a9f409801631a9efbb41f 100644 (file)
@@ -211,9 +211,10 @@ main(int argc, char **argv)
     }
 #endif /* _WIN32 */
 
+    empfile_init();
     if (emp_config(config_file) < 0)
        exit(EXIT_FAILURE);
-    ef_init();
+    empfile_fixup();
     if (read_builtin_tables() < 0)
        exit(EXIT_FAILURE);
     if (read_custom_tables() < 0)
index 335de8f3c0dba80e33d021d7191ec3140d1a86cf..2d8c9371268050494766d62fb04d380f009c1e78 100644 (file)
@@ -105,10 +105,10 @@ main(int argc, char *argv[])
        }
     }
 
+    empfile_init();
     if (emp_config(config_file) < 0)
        exit(1);
-
-    ef_init();
+    empfile_fixup();
 
     if (mkdir(gamedir, S_IRWXU | S_IRWXG) < 0 && errno != EEXIST) {
        perror(gamedir);