]> git.pond.sub.org Git - empserver/blob - src/lib/subs/fileinit.c
Update copyright notice
[empserver] / src / lib / subs / fileinit.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2012, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  fileinit.c: Initialize Empire tables for full server operations.
28  *
29  *  Known contributors to this file:
30  *     Ron Koenderink, 2005
31  *     Markus Armbruster, 2005-2011
32  */
33
34 #include <config.h>
35
36 #include "file.h"
37 #include "nsc.h"
38 #include "prototypes.h"
39 #include "unit.h"
40
41 struct fileinit {
42     int ef_type;
43     void (*postread)(int, void *);
44     void (*prewrite)(int, void *, void *);
45     void (*onresize)(int);
46 };
47
48 static struct fileinit fileinit[] = {
49     {EF_SECTOR, sct_postread, sct_prewrite, NULL},
50     {EF_SHIP, shp_postread, shp_prewrite, unit_onresize},
51     {EF_PLANE, pln_postread, pln_prewrite, unit_onresize},
52     {EF_LAND, lnd_postread, lnd_prewrite, unit_onresize},
53     {EF_NUKE, nuk_postread, nuk_prewrite, unit_onresize}
54 };
55
56 static void ef_open_srv(void);
57 static void ef_close_srv(void);
58
59 /*
60  * Initialize empfile for full server operations.
61  */
62 void
63 ef_init_srv(int force_bad_state)
64 {
65     unsigned i;
66
67     for (i = 0; i < sizeof(fileinit) / sizeof(fileinit[0]); i++) {
68         empfile[fileinit[i].ef_type].postread = fileinit[i].postread;
69         empfile[fileinit[i].ef_type].prewrite = fileinit[i].prewrite;
70         empfile[fileinit[i].ef_type].onresize = fileinit[i].onresize;
71     }
72
73     nsc_init();
74     if (ef_verify_config() < 0)
75         exit(EXIT_FAILURE);
76     ef_open_srv();
77     if (ef_verify_state(1) < 0 && !force_bad_state) {
78         fprintf(stderr, "You can try -F to force running anyway,"
79                 " but that's risky; see the manual page\n");
80         exit(EXIT_FAILURE);
81     }
82     global_init();
83     unit_cargo_init();
84 }
85
86 void
87 ef_fin_srv(void)
88 {
89     ef_close_srv();
90 }
91
92 static void
93 ef_open_srv(void)
94 {
95     int failed = 0;
96
97     failed |= !ef_open(EF_NATION, EFF_MEM);
98     failed |= !ef_open(EF_SECTOR, EFF_MEM);
99     failed |= !ef_open(EF_SHIP, EFF_MEM);
100     failed |= !ef_open(EF_PLANE, EFF_MEM);
101     failed |= !ef_open(EF_LAND, EFF_MEM);
102     failed |= !ef_open(EF_GAME, EFF_MEM);
103     failed |= !ef_open(EF_NEWS, 0);
104     failed |= !ef_open(EF_LOAN, 0);
105     failed |= !ef_open(EF_TREATY, 0);
106     failed |= !ef_open(EF_NUKE, EFF_MEM);
107     failed |= !ef_open(EF_POWER, 0);
108     failed |= !ef_open(EF_TRADE, 0);
109     failed |= !ef_open(EF_MAP, EFF_MEM);
110     failed |= !ef_open(EF_BMAP, EFF_MEM);
111     failed |= !ef_open(EF_COMM, 0);
112     failed |= !ef_open(EF_LOST, 0);
113     failed |= !ef_open(EF_REALM, EFF_MEM);
114     if (!failed)
115         failed |= ef_open_view(EF_COUNTRY);
116     if (failed) {
117         logerror("Missing files, giving up");
118         exit(EXIT_FAILURE);
119     }
120 }
121
122 static void
123 ef_close_srv(void)
124 {
125     ef_close(EF_COUNTRY);
126     ef_close(EF_NATION);
127     ef_close(EF_SECTOR);
128     ef_close(EF_SHIP);
129     ef_close(EF_PLANE);
130     ef_close(EF_LAND);
131     ef_close(EF_GAME);
132     ef_close(EF_NEWS);
133     ef_close(EF_LOAN);
134     ef_close(EF_TREATY);
135     ef_close(EF_NUKE);
136     ef_close(EF_POWER);
137     ef_close(EF_TRADE);
138     ef_close(EF_MAP);
139     ef_close(EF_COMM);
140     ef_close(EF_BMAP);
141     ef_close(EF_LOST);
142     ef_close(EF_REALM);
143 }