New way to read configuration tables:
(config_tables): New econfig key. (read_config_tables): New, implements config_tables. (main): Call it. Need to call ef_init() and chdir(configdir) first. (ef_init_srv): Don't call ef_init(), since main() did it already. Remove old way to do the same: (ef_load): Remove, along with calls. (empfile): Set configuration tables' member file back to NULL.
This commit is contained in:
parent
e4865d473e
commit
bd65a84eb5
8 changed files with 68 additions and 43 deletions
|
@ -67,6 +67,8 @@ EMPCFBOTH((fvname), (vname), int, NSC_INT, KM_OPTION, (descr))
|
|||
/* econfig key definitions: */
|
||||
|
||||
EMPCF_COMMENT("\n### Server configuration and information")
|
||||
EMPCFBOTH("config_tables", config_tables, char *, NSC_STRING, KM_INTERNAL,
|
||||
"Configuration table files, separated by space, relative to econfig's directory")
|
||||
EMPCFBOTH("data", gamedir, char *, NSC_STRING, KM_INTERNAL,
|
||||
"Directory where this game's data is stored")
|
||||
EMPCFBOTH("info", infodir, char *, NSC_STRING, KM_INTERNAL,
|
||||
|
|
|
@ -172,7 +172,6 @@ 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_load(void);
|
||||
extern int ef_verify(void);
|
||||
|
||||
extern struct empfile empfile[EF_MAX + 1];
|
||||
|
|
|
@ -283,8 +283,8 @@ extern int check_plane_ok(struct plnstr *);
|
|||
extern int check_comm_ok(struct comstr *);
|
||||
extern int check_loan_ok(struct lonstr *);
|
||||
extern int check_trade_ok(struct trdstr *);
|
||||
/* ef_load.c */
|
||||
/* in file.h */
|
||||
/* conftab.c */
|
||||
extern int read_config_tables(void);
|
||||
/* ef_verify.c */
|
||||
/* in file.h */
|
||||
/* fsize.c */
|
||||
|
|
|
@ -25,45 +25,65 @@
|
|||
*
|
||||
* ---
|
||||
*
|
||||
* ef_load.c: Load custom game configuration files
|
||||
* conftab.c: Load game configuration files
|
||||
*
|
||||
* Known contributors to this file:
|
||||
* Ron Koenderink, 2005
|
||||
* Markus Armbruster, 2006
|
||||
*
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "prototypes.h"
|
||||
#include "optlist.h"
|
||||
#include "file.h"
|
||||
|
||||
int
|
||||
ef_load()
|
||||
{
|
||||
struct empfile *ep;
|
||||
FILE *fp;
|
||||
int retval = 0;
|
||||
static int read_config_table_file(char *);
|
||||
|
||||
for (ep = empfile; ep->name; ep++) {
|
||||
if (!EF_IS_GAME_STATE(ep->uid) && ep->file) {
|
||||
if ((fp = fopen(ep->file, "r")) == NULL) {
|
||||
continue;
|
||||
}
|
||||
if (xundump(fp, ep->name, ep->uid) < 0)
|
||||
retval = -1;
|
||||
else {
|
||||
int ch = getc(fp);
|
||||
if (ch != EOF) {
|
||||
fprintf(stderr, "%s: Junk after the table\n",
|
||||
ep->file);
|
||||
retval = -1;
|
||||
}
|
||||
}
|
||||
fclose(fp);
|
||||
}
|
||||
/*
|
||||
* Read user configuration tables.
|
||||
* Return 0 on success, -1 on failure.
|
||||
*/
|
||||
int
|
||||
read_config_tables(void)
|
||||
{
|
||||
char *tmp = strdup(config_tables);
|
||||
char *fname;
|
||||
int res = 0;
|
||||
|
||||
for (fname = strtok(tmp, " \t"); fname; fname = strtok(NULL, " \t")) {
|
||||
if (read_config_table_file(fname) < 0)
|
||||
res = -1;
|
||||
}
|
||||
return retval;
|
||||
|
||||
free(tmp);
|
||||
return res;
|
||||
}
|
||||
|
||||
/*
|
||||
* Read configuaration table file FNAME.
|
||||
* Return 0 on success, -1 on error.
|
||||
*/
|
||||
static int
|
||||
read_config_table_file(char *fname)
|
||||
{
|
||||
int res, n;
|
||||
FILE *fp;
|
||||
|
||||
if (!(fp = fopen(fname, "r"))) {
|
||||
fprintf(stderr, "Can't open config table %s for reading (%s)\n",
|
||||
fname, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (n = 0; (res = xundump(fp, fname, EF_BAD)) >= 0; n++) ;
|
||||
if (res != EF_BAD && n == 0)
|
||||
fprintf(stderr, "Warning: configuration file %s is empty\n", fname);
|
||||
|
||||
fclose(fp);
|
||||
return -(res == EF_BAD);
|
||||
}
|
|
@ -145,18 +145,18 @@ struct empfile empfile[] = {
|
|||
UNMAPPED_CACHE(struct realmstr, EFF_OWNER)},
|
||||
|
||||
/* Static game data (configuration) */
|
||||
{EF_SECTOR_CHR, "sect-chr", "sect_def", dchr_ca, ARRAY_TABLE(dchr, EFF_CFG)},
|
||||
{EF_SHIP_CHR, "ship-chr", "ship_def", mchr_ca, ARRAY_CACHE(mchr, EFF_CFG)},
|
||||
{EF_PLANE_CHR, "plane-chr", "plane_def", plchr_ca, ARRAY_CACHE(plchr, EFF_CFG)},
|
||||
{EF_LAND_CHR, "land-chr", "land_def", lchr_ca, ARRAY_CACHE(lchr, EFF_CFG)},
|
||||
{EF_NUKE_CHR, "nuke-chr", "nuke_def", nchr_ca, ARRAY_CACHE(nchr, EFF_CFG)},
|
||||
{EF_SECTOR_CHR, "sect-chr", NULL, dchr_ca, ARRAY_TABLE(dchr, EFF_CFG)},
|
||||
{EF_SHIP_CHR, "ship-chr", NULL, mchr_ca, ARRAY_CACHE(mchr, EFF_CFG)},
|
||||
{EF_PLANE_CHR, "plane-chr", NULL, plchr_ca, ARRAY_CACHE(plchr, EFF_CFG)},
|
||||
{EF_LAND_CHR, "land-chr", NULL, lchr_ca, ARRAY_CACHE(lchr, EFF_CFG)},
|
||||
{EF_NUKE_CHR, "nuke-chr", NULL, nchr_ca, ARRAY_CACHE(nchr, EFF_CFG)},
|
||||
{EF_NEWS_CHR, "news-chr", NULL, rpt_ca, ARRAY_TABLE(rpt, EFF_CFG)},
|
||||
{EF_TREATY_FLAGS, "treaty-flags", NULL, symbol_ca,
|
||||
PTR_CACHE(treaty_flags, EFF_CFG)},
|
||||
{EF_ITEM, "item", "item_def", ichr_ca, ARRAY_TABLE(ichr, EFF_CFG)},
|
||||
{EF_INFRASTRUCTURE, "infrastructure", "infrastructure_def", intrchr_ca,
|
||||
{EF_ITEM, "item", NULL, ichr_ca, ARRAY_TABLE(ichr, EFF_CFG)},
|
||||
{EF_INFRASTRUCTURE, "infrastructure", NULL, intrchr_ca,
|
||||
ARRAY_TABLE(intrchr, EFF_CFG)},
|
||||
{EF_PRODUCT, "product", "product_def", pchr_ca, ARRAY_TABLE(pchr, EFF_CFG)},
|
||||
{EF_PRODUCT, "product", NULL, pchr_ca, ARRAY_TABLE(pchr, EFF_CFG)},
|
||||
{EF_TABLE, "table", NULL, empfile_ca, ARRAY_TABLE(empfile, EFF_CFG)},
|
||||
{EF_SHIP_CHR_FLAGS, "ship-chr-flags", NULL, symbol_ca,
|
||||
PTR_CACHE(ship_chr_flags, EFF_CFG)},
|
||||
|
|
|
@ -43,6 +43,9 @@ char dflt_econfig[] = "@econfig@";
|
|||
/* Where to find configuration, which is wherever econfig is looked for */
|
||||
char *configdir;
|
||||
|
||||
/* User configuration tables to load, relative to configdir */
|
||||
char *config_tables = "";
|
||||
|
||||
/* Where to find info pages */
|
||||
char *infodir = "@einfodir@";
|
||||
|
||||
|
|
|
@ -59,19 +59,13 @@ static void ef_fina_view(int);
|
|||
|
||||
/*
|
||||
* Initialize empfile for full server operations.
|
||||
* Like ef_init(), but additionally installs the server's callbacks.
|
||||
* This is separate from ef_init(), so that utility programs like
|
||||
* files can use empfile.
|
||||
* ef_init() must be called first.
|
||||
*/
|
||||
void
|
||||
ef_init_srv(void)
|
||||
{
|
||||
unsigned i;
|
||||
|
||||
ef_init();
|
||||
if (ef_load() < 0)
|
||||
exit(EXIT_FAILURE);
|
||||
|
||||
for (i = 0; i < sizeof(fileinit) / sizeof(fileinit[0]); i++) {
|
||||
empfile[fileinit[i].ef_type].init = fileinit[i].init;
|
||||
empfile[fileinit[i].ef_type].postread = fileinit[i].postread;
|
||||
|
|
|
@ -202,6 +202,13 @@ main(int argc, char **argv)
|
|||
|
||||
if (emp_config(config_file) < 0)
|
||||
exit(EXIT_FAILURE);
|
||||
ef_init();
|
||||
if (chdir(configdir)) {
|
||||
fprintf(stderr, "Can't chdir to %s (%s)\n", configdir, strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (read_config_tables() < 0)
|
||||
exit(EXIT_FAILURE);
|
||||
if (chdir(gamedir)) {
|
||||
fprintf(stderr, "Can't chdir to %s (%s)\n", gamedir, strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue