Clean up initialization of empfile[]

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.
This commit is contained in:
Markus Armbruster 2008-02-06 20:14:39 +01:00
parent 3290e87576
commit 55ff4f8e3a
6 changed files with 69 additions and 48 deletions

View 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

View 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);
}
}
}

View 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();
}

View 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)

View 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)

View 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);