1 /*
2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2021, 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-2020
32 */
33
34 #include <config.h>
35
36 #include "nsc.h"
37 #include "prototypes.h"
38 #include "unit.h"
39
40 struct fileinit {
41 int ef_type;
42 void (*postread)(int, void *);
43 void (*prewrite)(int, void *, void *);
44 void (*onresize)(int);
45 };
46
47 static struct fileinit fileinit[] = {
48 {EF_SECTOR, sct_postread, sct_prewrite, NULL},
49 {EF_SHIP, shp_postread, shp_prewrite, unit_onresize},
50 {EF_PLANE, pln_postread, pln_prewrite, unit_onresize},
51 {EF_LAND, lnd_postread, lnd_prewrite, unit_onresize},
52 {EF_NUKE, nuk_postread, nuk_prewrite, unit_onresize}
53 };
54
55 static void ef_open_srv(void);
56 static void ef_close_srv(void);
57
58 /*
59 * Initialize empfile for full server operations.
60 */
61 void
ef_init_srv(int force_bad_state)62 ef_init_srv(int force_bad_state)
63 {
64 unsigned i;
65
66 for (i = 0; i < ARRAY_SIZE(fileinit); i++) {
67 empfile[fileinit[i].ef_type].postread = fileinit[i].postread;
68 empfile[fileinit[i].ef_type].prewrite = fileinit[i].prewrite;
69 empfile[fileinit[i].ef_type].onresize = fileinit[i].onresize;
70 }
71
72 nsc_init();
73 if (ef_verify_config() < 0)
74 exit(EXIT_FAILURE);
75 ef_open_srv();
76 if (ef_verify_state(1) < 0 && !force_bad_state) {
77 fprintf(stderr, "You can try -F to force running anyway,"
78 " but that's risky; see the manual page\n");
79 exit(EXIT_FAILURE);
80 }
81 global_init();
82 unit_cargo_init();
83 }
84
85 void
ef_fin_srv(void)86 ef_fin_srv(void)
87 {
88 ef_close_srv();
89 }
90
91 static void
ef_open_srv(void)92 ef_open_srv(void)
93 {
94 int failed = 0;
95
96 failed |= !ef_open(EF_NATION, EFF_MEM);
97 failed |= !ef_open(EF_SECTOR, EFF_MEM);
98 failed |= !ef_open(EF_SHIP, EFF_MEM);
99 failed |= !ef_open(EF_PLANE, EFF_MEM);
100 failed |= !ef_open(EF_LAND, EFF_MEM);
101 failed |= !ef_open(EF_GAME, EFF_MEM);
102 failed |= !ef_open(EF_NEWS, 0);
103 failed |= !ef_open(EF_LOAN, 0);
104 failed |= !ef_open(EF_NUKE, EFF_MEM);
105 failed |= !ef_open(EF_POWER, 0);
106 failed |= !ef_open(EF_TRADE, 0);
107 failed |= !ef_open(EF_MAP, EFF_MEM);
108 failed |= !ef_open(EF_BMAP, EFF_MEM);
109 failed |= !ef_open(EF_COMM, 0);
110 failed |= !ef_open(EF_LOST, 0);
111 failed |= !ef_open(EF_REALM, EFF_MEM);
112 failed |= !ef_open(EF_RELAT, EFF_MEM);
113 failed |= !ef_open(EF_CONTACT, EFF_MEM);
114 failed |= !ef_open(EF_REJECT, EFF_MEM);
115 if (!failed)
116 failed |= ef_open_view(EF_COUNTRY);
117 if (failed) {
118 logerror("Missing files, giving up");
119 exit(EXIT_FAILURE);
120 }
121 }
122
123 static void
ef_close_srv(void)124 ef_close_srv(void)
125 {
126 ef_close(EF_COUNTRY);
127 ef_close(EF_NATION);
128 ef_close(EF_SECTOR);
129 ef_close(EF_SHIP);
130 ef_close(EF_PLANE);
131 ef_close(EF_LAND);
132 ef_close(EF_GAME);
133 ef_close(EF_NEWS);
134 ef_close(EF_LOAN);
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 ef_close(EF_RELAT);
144 ef_close(EF_CONTACT);
145 ef_close(EF_REJECT);
146 }
147