]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/fileinit.c
License upgrade to GPL version 3 or later
[empserver] / src / lib / subs / fileinit.c
index 1477b04d4321c75533bc45f1f63d62f09d0a1c37..7d3183d64af76b88a028e2c4bbd5975fe230f309 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                Ken Stevens, Steve McClure, Markus Armbruster
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  Empire is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  *  ---
  *
- *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
- *  related information and legal notices. It is expected that any future
- *  projects/authors will amend these files as needed.
+ *  See files README, COPYING and CREDITS in the root of the source
+ *  tree for related information and legal notices.  It is expected
+ *  that future projects/authors will amend these files as needed.
  *
  *  ---
  *
- *  fileinit.c: Stuff that ef_init uses to initialize the ca pointers and
- *              the pre/post i/o calls.
- * 
+ *  fileinit.c: Initialize Empire tables for full server operations.
+ *
  *  Known contributors to this file:
- *  
+ *     Ron Koenderink, 2005
+ *     Markus Armbruster, 2005-2008
  */
 
-#include <fcntl.h>
-#include "misc.h"
-#include "xy.h"
-#include "nsc.h"
+#include <config.h>
+
 #include "file.h"
-#include "loan.h"
-#include "lost.h"
-#include "commodity.h"
-#include "prototypes.h"
+#include "nat.h"
 #include "optlist.h"
+#include "prototypes.h"
+#include "unit.h"
 
-extern struct castr sect_ca[];
-extern struct castr news_ca[];
-extern struct castr loan_ca[];
-extern struct castr ship_ca[];
-extern struct castr plane_ca[];
-extern struct castr treaty_ca[];
-extern struct castr nuke_ca[];
-extern struct castr trade_ca[];
-extern  struct castr commodity_ca[];
-extern  struct castr lost_ca[];
+struct fileinit {
+    int ef_type;
+    void (*oninit)(void *);
+    void (*postread)(int, void *);
+    void (*prewrite)(int, void *, void *);
+    int (*onresize)(int);
+};
 
-struct fileinit fileinit[EF_MAX] = {
-       { (ef_fileinit) sct_init,       sct_postread,   sct_prewrite,   sect_ca },
-       { shp_init,     shp_postread,   shp_prewrite,   ship_ca },
-       { pln_init,     pln_postread,   pln_prewrite,   plane_ca },
-       { lnd_init,     lnd_postread,   lnd_prewrite,   land_ca },
-       { nuk_init,     nuk_postread,   nuk_prewrite,   nuke_ca },
-       { 0,            0,              0,              news_ca },
-       { 0,            0,              0,              treaty_ca },
-       { 0,            0,              0,              trade_ca },
-       { 0,            0,              0,              0 }, /* power */
-       { 0,            0,              0,              0 }, /* nation */
-       { 0,            0,              0,              loan_ca },
-       { 0,            0,              0,              0 }, /* map */
-       { 0,            0,              0,              0 }, /* map */
-       { 0,            0,              0,              commodity_ca },
-       { 0,            0,              0,              lost_ca }
+static struct fileinit fileinit[] = {
+    {EF_SECTOR, NULL, sct_postread, sct_prewrite, NULL},
+    {EF_SHIP, NULL, shp_postread, shp_prewrite, unit_onresize},
+    {EF_PLANE, pln_oninit, pln_postread, pln_prewrite, unit_onresize},
+    {EF_LAND, lnd_oninit, lnd_postread, lnd_prewrite, unit_onresize},
+    {EF_NUKE, nuk_oninit, nuk_postread, nuk_prewrite, unit_onresize}
 };
 
+static void ef_open_srv(void);
+static void ef_close_srv(void);
+
+/*
+ * Initialize empfile for full server operations.
+ */
+void
+ef_init_srv(void)
+{
+    unsigned i;
+
+    for (i = 0; i < sizeof(fileinit) / sizeof(fileinit[0]); i++) {
+       empfile[fileinit[i].ef_type].oninit = fileinit[i].oninit;
+       empfile[fileinit[i].ef_type].postread = fileinit[i].postread;
+       empfile[fileinit[i].ef_type].prewrite = fileinit[i].prewrite;
+       empfile[fileinit[i].ef_type].onresize = fileinit[i].onresize;
+    }
+
+    nsc_init();
+    ef_open_srv();
+    if (ef_verify() < 0)
+       exit(EXIT_FAILURE);
+    global_init();
+    unit_cargo_init();
+}
+
 void
-ef_init(void)
+ef_fin_srv(void)
+{
+    ef_close_srv();
+}
+
+static void
+ef_open_srv(void)
 {
-       int     i;
-       struct  empfile *ef;
-       struct  fileinit *fi;
+    int failed = 0;
 
-       ef = empfile;
-       fi = fileinit;
-       for (i=0; i<EF_MAX; i++, ef++, fi++) {
-               ef->init = fi->init;
-               ef->postread = fi->postread;
-               ef->prewrite = fi->prewrite;
-               ef->cadef = fi->cadef;
-               /* We have to set the size for the map and bmap files at
-                  runtime. */
-               if (i == EF_MAP || i == EF_BMAP)
-                 ef->size = (WORLD_X * WORLD_Y) / 2;
-       }
+    failed |= !ef_open(EF_NATION, EFF_MEM, MAXNOC);
+    failed |= !ef_open(EF_SECTOR, EFF_MEM, WORLD_SZ());
+    failed |= !ef_open(EF_SHIP, EFF_MEM, -1);
+    failed |= !ef_open(EF_PLANE, EFF_MEM, -1);
+    failed |= !ef_open(EF_LAND, EFF_MEM, -1);
+    failed |= !ef_open(EF_GAME, EFF_MEM, 1);
+    failed |= !ef_open(EF_NEWS, 0, -1);
+    failed |= !ef_open(EF_LOAN, 0, -1);
+    failed |= !ef_open(EF_TREATY, 0, -1);
+    failed |= !ef_open(EF_NUKE, EFF_MEM, -1);
+    failed |= !ef_open(EF_POWER, 0, -1);
+    failed |= !ef_open(EF_TRADE, 0, -1);
+    failed |= !ef_open(EF_MAP, EFF_MEM, MAXNOC);
+    failed |= !ef_open(EF_BMAP, EFF_MEM, MAXNOC);
+    failed |= !ef_open(EF_COMM, 0, -1);
+    failed |= !ef_open(EF_LOST, 0, -1);
+    failed |= !ef_open(EF_REALM, EFF_MEM, MAXNOC * MAXNOR);
+    if (!failed)
+       failed |= ef_open_view(EF_COUNTRY, EF_NATION);
+    if (failed) {
+       logerror("Missing files, giving up");
+       exit(EXIT_FAILURE);
+    }
+}
+
+static void
+ef_close_srv(void)
+{
+    ef_close(EF_COUNTRY);
+    ef_close(EF_NATION);
+    ef_close(EF_SECTOR);
+    ef_close(EF_SHIP);
+    ef_close(EF_PLANE);
+    ef_close(EF_LAND);
+    ef_close(EF_GAME);
+    ef_close(EF_NEWS);
+    ef_close(EF_LOAN);
+    ef_close(EF_TREATY);
+    ef_close(EF_NUKE);
+    ef_close(EF_POWER);
+    ef_close(EF_TRADE);
+    ef_close(EF_MAP);
+    ef_close(EF_COMM);
+    ef_close(EF_BMAP);
+    ef_close(EF_LOST);
+    ef_close(EF_REALM);
 }