src/lib/subs/fileinit.c

Go to the documentation of this file.
00001 /*
00002  *  Empire - A multi-player, client/server Internet based war game.
00003  *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
00004  *                           Ken Stevens, Steve McClure
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  *  ---
00021  *
00022  *  See files README, COPYING and CREDITS in the root of the source
00023  *  tree for related information and legal notices.  It is expected
00024  *  that future projects/authors will amend these files as needed.
00025  *
00026  *  ---
00027  *
00028  *  fileinit.c: Initialize Empire tables for full server operations.
00029  * 
00030  *  Known contributors to this file:
00031  *     Ron Koenderink, 2005
00032  */
00033 
00034 #include <config.h>
00035 
00036 #include "file.h"
00037 #include "prototypes.h"
00038 
00039 struct fileinit {
00040     int ef_type;
00041     void (*init) (int, void *);
00042     int (*postread) (int, void *);
00043     int (*prewrite) (int, void *);
00044 };
00045 
00046 static struct fileinit fileinit[] = {
00047     {EF_SECTOR, NULL, sct_postread, sct_prewrite},
00048     {EF_SHIP, shp_init, shp_postread, shp_prewrite},
00049     {EF_PLANE, pln_init, pln_postread, pln_prewrite},
00050     {EF_LAND, lnd_init, lnd_postread, lnd_prewrite},
00051     {EF_NUKE, nuk_init, nuk_postread, nuk_prewrite}
00052 };
00053 
00054 static void ef_open_srv(void);
00055 static void ef_close_srv(void);
00056 static int ef_init_view(int, int);
00057 static void ef_fina_view(int);
00058 
00059 /*
00060  * Initialize empfile for full server operations.
00061  * ef_init() must be called first.
00062  */
00063 void
00064 ef_init_srv(void)
00065 {
00066     unsigned i, flags;
00067 
00068     for (i = 0; i < sizeof(fileinit) / sizeof(fileinit[0]); i++) {
00069         empfile[fileinit[i].ef_type].init = fileinit[i].init;
00070         empfile[fileinit[i].ef_type].postread = fileinit[i].postread;
00071         empfile[fileinit[i].ef_type].prewrite = fileinit[i].prewrite;
00072     }
00073 
00074     for (i = 0; cou_ca[i].ca_name; i++) {
00075         nat_ca[i] = cou_ca[i];
00076         flags = nat_ca[i].ca_flags;
00077         if (flags & NSC_EXTRA)
00078             flags &= ~NSC_EXTRA;
00079         else if (i != 0)
00080             flags |= NSC_DEITY;
00081         nat_ca[i].ca_flags = flags;
00082     }
00083     nat_ca[i] = cou_ca[i];
00084 
00085     ef_open_srv();
00086     if (ef_verify() < 0)
00087         exit(EXIT_FAILURE);
00088     global_init();
00089 }
00090 
00091 void
00092 ef_fin_srv(void)
00093 {
00094     ef_close_srv();
00095 }
00096 
00097 static void
00098 ef_open_srv(void)
00099 {
00100     int failed = 0;
00101 
00102     failed |= !ef_open(EF_NATION, EFF_MEM);
00103     failed |= !ef_open(EF_SECTOR, EFF_MEM);
00104     failed |= !ef_open(EF_SHIP, EFF_MEM);
00105     failed |= !ef_open(EF_PLANE, EFF_MEM);
00106     failed |= !ef_open(EF_LAND, EFF_MEM);
00107     failed |= !ef_open(EF_GAME, EFF_MEM);
00108     failed |= !ef_open(EF_NEWS, 0);
00109     failed |= !ef_open(EF_LOAN, 0);
00110     failed |= !ef_open(EF_TREATY, 0);
00111     failed |= !ef_open(EF_NUKE, EFF_MEM);
00112     failed |= !ef_open(EF_POWER, 0);
00113     failed |= !ef_open(EF_TRADE, 0);
00114     failed |= !ef_open(EF_MAP, EFF_MEM);
00115     failed |= !ef_open(EF_BMAP, EFF_MEM);
00116     failed |= !ef_open(EF_COMM, 0);
00117     failed |= !ef_open(EF_LOST, 0);
00118     failed |= !ef_open(EF_REALM, EFF_MEM);
00119     if (!failed)
00120         failed |= ef_init_view(EF_COUNTRY, EF_NATION);
00121     if (failed) {
00122         logerror("Missing files, giving up");
00123         exit(EXIT_FAILURE);
00124     }
00125 }
00126 
00127 static void
00128 ef_close_srv(void)
00129 {
00130     ef_fina_view(EF_COUNTRY);
00131     ef_close(EF_NATION);
00132     ef_close(EF_SECTOR);
00133     ef_close(EF_SHIP);
00134     ef_close(EF_PLANE);
00135     ef_close(EF_LAND);
00136     ef_close(EF_GAME);
00137     ef_close(EF_NEWS);
00138     ef_close(EF_LOAN);
00139     ef_close(EF_TREATY);
00140     ef_close(EF_NUKE);
00141     ef_close(EF_POWER);
00142     ef_close(EF_TRADE);
00143     ef_close(EF_MAP);
00144     ef_close(EF_COMM);
00145     ef_close(EF_BMAP);
00146     ef_close(EF_LOST);
00147     ef_close(EF_REALM);
00148 }
00149 
00150 static int
00151 ef_init_view(int type, int base)
00152 {
00153     if (CANT_HAPPEN(!(empfile[base].flags & EFF_MEM)))
00154         return -1;
00155     empfile[type].cache = empfile[base].cache;
00156     empfile[type].csize = empfile[base].csize;
00157     empfile[type].flags |= EFF_MEM;
00158     empfile[type].baseid = empfile[base].baseid;
00159     empfile[type].cids = empfile[base].cids;
00160     empfile[type].fids = empfile[base].fids;
00161     return 0;
00162 }
00163 
00164 static void
00165 ef_fina_view(int type)
00166 {
00167     empfile[type].cache = NULL;
00168 }

Generated on Fri Mar 28 11:01:15 2008 for empserver by  doxygen 1.5.2