]> git.pond.sub.org Git - empserver/blob - src/lib/subs/fileinit.c
2b3f3b398da02596dde09ab762fcd9c65b605643
[empserver] / src / lib / subs / fileinit.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  fileinit.c: Initialize Empire tables for full server operations.
29  * 
30  *  Known contributors to this file:
31  *     Ron Koenderink, 2005
32  */
33
34 #include <config.h>
35
36 #include "file.h"
37 #include "prototypes.h"
38
39 struct fileinit {
40     int ef_type;
41     void (*init) (int, void *);
42     int (*postread) (int, void *);
43     int (*prewrite) (int, void *);
44 };
45
46 static struct fileinit fileinit[] = {
47     {EF_SECTOR, NULL, sct_postread, sct_prewrite},
48     {EF_SHIP, shp_init, shp_postread, shp_prewrite},
49     {EF_PLANE, pln_init, pln_postread, pln_prewrite},
50     {EF_LAND, lnd_init, lnd_postread, lnd_prewrite},
51     {EF_NUKE, nuk_init, nuk_postread, nuk_prewrite}
52 };
53
54 static void ef_open_srv(void);
55 static void ef_close_srv(void);
56 static int ef_init_view(int, int);
57 static void ef_fina_view(int);
58
59 /*
60  * Initialize empfile for full server operations.
61  */
62 void
63 ef_init_srv(void)
64 {
65     unsigned i, flags;
66
67     for (i = 0; i < sizeof(fileinit) / sizeof(fileinit[0]); i++) {
68         empfile[fileinit[i].ef_type].init = fileinit[i].init;
69         empfile[fileinit[i].ef_type].postread = fileinit[i].postread;
70         empfile[fileinit[i].ef_type].prewrite = fileinit[i].prewrite;
71     }
72
73     for (i = 0; cou_ca[i].ca_name; i++) {
74         nat_ca[i] = cou_ca[i];
75         flags = nat_ca[i].ca_flags;
76         if (flags & NSC_EXTRA)
77             flags &= ~NSC_EXTRA;
78         else if (i != 0)
79             flags |= NSC_DEITY;
80         nat_ca[i].ca_flags = flags;
81     }
82     nat_ca[i] = cou_ca[i];
83
84     ef_open_srv();
85     if (ef_verify() < 0)
86         exit(EXIT_FAILURE);
87     global_init();
88 }
89
90 void
91 ef_fin_srv(void)
92 {
93     ef_close_srv();
94 }
95
96 static void
97 ef_open_srv(void)
98 {
99     int failed = 0;
100
101     failed |= !ef_open(EF_NATION, EFF_MEM);
102     failed |= !ef_open(EF_SECTOR, EFF_MEM);
103     failed |= !ef_open(EF_SHIP, EFF_MEM);
104     failed |= !ef_open(EF_PLANE, EFF_MEM);
105     failed |= !ef_open(EF_LAND, EFF_MEM);
106     failed |= !ef_open(EF_GAME, EFF_MEM);
107     failed |= !ef_open(EF_NEWS, 0);
108     failed |= !ef_open(EF_LOAN, 0);
109     failed |= !ef_open(EF_TREATY, 0);
110     failed |= !ef_open(EF_NUKE, EFF_MEM);
111     failed |= !ef_open(EF_POWER, 0);
112     failed |= !ef_open(EF_TRADE, 0);
113     failed |= !ef_open(EF_MAP, EFF_MEM);
114     failed |= !ef_open(EF_BMAP, EFF_MEM);
115     failed |= !ef_open(EF_COMM, 0);
116     failed |= !ef_open(EF_LOST, 0);
117     failed |= !ef_open(EF_REALM, EFF_MEM);
118     if (!failed)
119         failed |= ef_init_view(EF_COUNTRY, EF_NATION);
120     if (failed) {
121         logerror("Missing files, giving up");
122         exit(EXIT_FAILURE);
123     }
124 }
125
126 static void
127 ef_close_srv(void)
128 {
129     ef_fina_view(EF_COUNTRY);
130     ef_close(EF_NATION);
131     ef_close(EF_SECTOR);
132     ef_close(EF_SHIP);
133     ef_close(EF_PLANE);
134     ef_close(EF_LAND);
135     ef_close(EF_GAME);
136     ef_close(EF_NEWS);
137     ef_close(EF_LOAN);
138     ef_close(EF_TREATY);
139     ef_close(EF_NUKE);
140     ef_close(EF_POWER);
141     ef_close(EF_TRADE);
142     ef_close(EF_MAP);
143     ef_close(EF_COMM);
144     ef_close(EF_BMAP);
145     ef_close(EF_LOST);
146     ef_close(EF_REALM);
147 }
148
149 static int
150 ef_init_view(int type, int base)
151 {
152     if (CANT_HAPPEN(!(empfile[base].flags & EFF_MEM)))
153         return -1;
154     empfile[type].cache = empfile[base].cache;
155     empfile[type].csize = empfile[base].csize;
156     empfile[type].flags |= EFF_MEM;
157     empfile[type].baseid = empfile[base].baseid;
158     empfile[type].cids = empfile[base].cids;
159     empfile[type].fids = empfile[base].fids;
160     return 0;
161 }
162
163 static void
164 ef_fina_view(int type)
165 {
166     empfile[type].cache = NULL;
167 }