]>
Commit | Line | Data |
---|---|---|
1 | #include <fstream> | |
2 | #include <string.h> | |
3 | #include <stdlib.h> | |
4 | #include <stdio.h> | |
5 | #include "extern.h" | |
6 | ||
7 | using std::ifstream; | |
8 | using std::cerr; | |
9 | using std::endl; | |
10 | ||
11 | int load_config_file(char *config); | |
12 | void unload_config_file(); | |
13 | ||
14 | /* Random Configuration Stuff Goes Here until I code it to load from a .conf file :)*/ | |
15 | ||
16 | char *s_GameServ; // GameServ's nickname | |
17 | char *gshost; // GameServ's Hostname | |
18 | char *gsident; // GameServ's ident/username | |
19 | char *servername; // GameServ's Server | |
20 | char *c_Forest; // Forest channel | |
21 | char *c_ForestTopic; // Forest Channel Topic | |
22 | char *adminpass; // Administrator password | |
23 | char *welcomemsg; // Welcome Message | |
24 | int welcomedelay; // Welcome Message Delay | |
25 | int updateperiod; // Seconds until another player database update | |
26 | int forestfights; // Forest fights per day | |
27 | int maxafightdistance; // Max levels above a player they can fight player->player | |
28 | int maxbfightdistance; // Max levels below a player they can fight player->player | |
29 | ||
30 | // Remote server stuff. This is used for the outgoing connection gameserv needs to make | |
31 | // to a real ircd. | |
32 | char *remoteserver; // Server to connect to | |
33 | char *remoteport; // Port to connect to on remoteserver | |
34 | char *remotepass; // Password for the server link | |
35 | ||
36 | char *playerdata; // File to store player data in | |
37 | char *monsterdata; // File to load monster data from | |
38 | char *pidfile; // Process ID file | |
39 | ||
40 | #if defined(P10) | |
41 | char *gsnum = "[]AAA"; // GameServ Numeric | |
42 | #endif | |
43 | ||
44 | void unload_config_file() | |
45 | { | |
46 | if (s_GameServ) | |
47 | delete [] s_GameServ; | |
48 | if (gshost) | |
49 | delete [] gshost; | |
50 | if (gsident) | |
51 | delete [] gsident; | |
52 | if (servername) | |
53 | delete [] servername; | |
54 | if (c_Forest) | |
55 | delete [] c_Forest; | |
56 | if (c_ForestTopic) | |
57 | delete [] c_ForestTopic; | |
58 | if (remoteserver) | |
59 | delete [] remoteserver; | |
60 | if (remoteport) | |
61 | delete [] remoteport; | |
62 | if (remotepass) | |
63 | delete [] remotepass; | |
64 | if (playerdata) | |
65 | delete [] playerdata; | |
66 | if (monsterdata) | |
67 | delete [] monsterdata; | |
68 | if (adminpass) | |
69 | delete [] adminpass; | |
70 | if (welcomemsg) | |
71 | delete [] welcomemsg; | |
72 | if (pidfile) | |
73 | delete [] pidfile; | |
74 | } | |
75 | ||
76 | int load_config_file(char *config) | |
77 | { | |
78 | char *buf, *directive, *value; | |
79 | ||
80 | #define numdirectives 19 | |
81 | ||
82 | unload_config_file(); | |
83 | ||
84 | struct DIRECTIVE { | |
85 | bool done; | |
86 | char *desc; | |
87 | }; | |
88 | ||
89 | DIRECTIVE directives[numdirectives]; | |
90 | ||
91 | directives[0].desc = "s_GameServ - GameServ Nickname"; | |
92 | directives[1].desc = "GSHOST - GameServ Hostname"; | |
93 | directives[2].desc = "GSIDENT - GameServ Ident"; | |
94 | directives[3].desc = "SERVERNAME - Pseudo Server's Name"; | |
95 | directives[4].desc = "C_FOREST - Forest Channel"; | |
96 | directives[5].desc = "C_FORESTTOPIC - Topic for the Forest Channel"; | |
97 | directives[6].desc = "REMOTESERVER - Server for gameserv to connect to (ip or hostname)"; | |
98 | directives[7].desc = "REMOTEPORT - Port on the remote server to connect to"; | |
99 | directives[8].desc = "REMOTEPASS - Password on the remote server"; | |
100 | directives[9].desc = "PLAYERDATA - File to store the player saves in"; | |
101 | directives[10].desc = "MONSTERDATA - File to load the monsters from"; | |
102 | directives[11].desc = "ADMINPASS - Password to identify as an admin with"; | |
103 | directives[12].desc = "WELCOMEDELAY - Delay (in seconds) to wait before welcoming new users to the network"; | |
104 | directives[13].desc = "FORESTFIGHTS - Number of forest fights players get every day"; | |
105 | directives[14].desc = "UPDATEPERIOD - Number of seconds between every player data save"; | |
106 | directives[15].desc = "WELCOMEMSG - Message to send to new users on the network"; | |
107 | directives[16].desc = "PIDFILE - Filename to store the gameserv process ID in"; | |
108 | directives[17].desc = "MAXAFIGHTDISTANCE - The maximum number of levels above you "\ | |
109 | "that you can fight player->player"; | |
110 | directives[18].desc = "MAXBFIGHTDISTANCE - The maximum number of levels below you "\ | |
111 | "that you can fight player->player"; | |
112 | ||
113 | for (int count = 0; count < numdirectives; count++) | |
114 | { | |
115 | directives[count].done = false; | |
116 | } | |
117 | ||
118 | ifstream infile; | |
119 | infile.open(config); | |
120 | if (infile.fail()) | |
121 | { | |
122 | log("Error opening %s", config); | |
123 | cerr << "Error opening " << config << endl; | |
124 | return 0; | |
125 | } | |
126 | ||
127 | buf = new char[1024]; | |
128 | ||
129 | while (infile.getline(buf, 1024, '\n')) | |
130 | { | |
131 | #ifdef DEBUGMODE | |
132 | log("Config file entry buf: %s", buf); | |
133 | #endif | |
134 | ||
135 | if (buf[0] == '#' || buf[0] == ' ' || buf[0] == '\0' || buf[0] == '\n' || buf[0] == '\r') | |
136 | continue; | |
137 | ||
138 | directive = strtok(buf, " "); | |
139 | ||
140 | if (stricmp(directive, "DIE") == 0) | |
141 | { | |
142 | value = strtok(NULL, ""); | |
143 | log("You should read the entire %s file!", config); | |
144 | cerr << "You should read the entire " << config << " file!" | |
145 | << endl; | |
146 | delete []buf; | |
147 | exit(0); | |
148 | } | |
149 | if (stricmp(directive, "S_GAMESERV") == 0) | |
150 | { | |
151 | value = strtok(NULL, " "); | |
152 | s_GameServ = new char[strlen(value) + 1]; | |
153 | strcpy(s_GameServ, value); | |
154 | directives[0].done = true; | |
155 | } | |
156 | else if (stricmp(directive, "GSHOST") == 0) | |
157 | { | |
158 | value = strtok(NULL, " "); | |
159 | gshost = new char[strlen(value) + 1]; | |
160 | strcpy(gshost, value); | |
161 | directives[1].done = true; | |
162 | } | |
163 | else if (stricmp(directive, "GSIDENT") == 0) | |
164 | { | |
165 | value = strtok(NULL, " "); | |
166 | gsident = new char[strlen(value) + 1]; | |
167 | strcpy(gsident, value); | |
168 | directives[2].done = true; | |
169 | } | |
170 | else if (stricmp(directive, "SERVERNAME") == 0) | |
171 | { | |
172 | value = strtok(NULL, " "); | |
173 | servername = new char[strlen(value) + 1]; | |
174 | strcpy(servername, value); | |
175 | directives[3].done = true; | |
176 | } | |
177 | else if (stricmp(directive, "C_FOREST") == 0) | |
178 | { | |
179 | value = strtok(NULL, " "); | |
180 | c_Forest = new char[strlen(value) + 1]; | |
181 | strcpy(c_Forest, value); | |
182 | directives[4].done = true; | |
183 | } | |
184 | else if (stricmp(directive, "C_FORESTTOPIC") == 0) | |
185 | { | |
186 | value = strtok(NULL, ""); | |
187 | c_ForestTopic = new char[strlen(value) + 1]; | |
188 | strcpy(c_ForestTopic, value); | |
189 | directives[5].done = true; | |
190 | } | |
191 | else if (stricmp(directive, "REMOTESERVER") == 0) | |
192 | { | |
193 | value = strtok(NULL, " "); | |
194 | remoteserver = new char[strlen(value) + 1]; | |
195 | strcpy(remoteserver, value); | |
196 | directives[6].done = true; | |
197 | } | |
198 | else if (stricmp(directive, "REMOTEPORT") == 0) | |
199 | { | |
200 | value = strtok(NULL, " "); | |
201 | remoteport = new char[strlen(value) + 1]; | |
202 | strcpy(remoteport, value); | |
203 | directives[7].done = true; | |
204 | } | |
205 | else if (stricmp(directive, "REMOTEPASS") == 0) | |
206 | { | |
207 | value = strtok(NULL, ""); | |
208 | remotepass = new char[strlen(value) + 1]; | |
209 | strcpy(remotepass, value); | |
210 | directives[8].done = true; | |
211 | } | |
212 | else if (stricmp(directive, "PLAYERDATA") == 0) | |
213 | { | |
214 | value = strtok(NULL, ""); | |
215 | playerdata = new char[strlen(value) + 1]; | |
216 | strcpy(playerdata, value); | |
217 | directives[9].done = true; | |
218 | } | |
219 | else if (stricmp(directive, "MONSTERDATA") == 0) | |
220 | { | |
221 | value = strtok(NULL, ""); | |
222 | monsterdata = new char[strlen(value) + 1]; | |
223 | strcpy(monsterdata, value); | |
224 | directives[10].done = true; | |
225 | } | |
226 | else if (stricmp(directive, "ADMINPASS") == 0) | |
227 | { | |
228 | value = strtok(NULL, ""); | |
229 | adminpass = new char[strlen(value) + 1]; | |
230 | strcpy(adminpass, value); | |
231 | directives[11].done = true; | |
232 | } | |
233 | else if (stricmp(directive, "WELCOMEDELAY") == 0) | |
234 | { | |
235 | value = strtok(NULL, " "); | |
236 | welcomedelay = stringtoint(value); | |
237 | directives[12].done = true; | |
238 | } | |
239 | else if (stricmp(directive, "FORESTFIGHTS") == 0) | |
240 | { | |
241 | value = strtok(NULL, " "); | |
242 | forestfights = stringtoint(value); | |
243 | directives[13].done = true; | |
244 | } | |
245 | else if (stricmp(directive, "UPDATEPERIOD") == 0) | |
246 | { | |
247 | value = strtok(NULL, " "); | |
248 | updateperiod = stringtoint(value); | |
249 | directives[14].done = true; | |
250 | } | |
251 | else if (stricmp(directive, "WELCOMEMSG") == 0) | |
252 | { | |
253 | value = strtok(NULL, ""); | |
254 | welcomemsg = new char[strlen(value) + 1]; | |
255 | strcpy(welcomemsg, value); | |
256 | directives[15].done = true; | |
257 | } | |
258 | else if (stricmp(directive, "PIDFILE") == 0) | |
259 | { | |
260 | value = strtok(NULL, " "); | |
261 | pidfile = new char[strlen(value) + 1]; | |
262 | strcpy(pidfile, value); | |
263 | directives[16].done = true; | |
264 | } | |
265 | else if (stricmp(directive, "MAXAFIGHTDISTANCE") == 0) | |
266 | { | |
267 | value = strtok(NULL, " "); | |
268 | maxafightdistance = stringtoint(value); | |
269 | directives[17].done = true;; | |
270 | } | |
271 | else if (stricmp(directive, "MAXBFIGHTDISTANCE") == 0) | |
272 | { | |
273 | value = strtok(NULL, " "); | |
274 | maxbfightdistance = stringtoint(value); | |
275 | directives[18].done = true; | |
276 | } | |
277 | else | |
278 | { | |
279 | #ifdef DEBUGMODE | |
280 | log("Unknown Directive. Buffer: %s", buf); | |
281 | cerr << "Unknown " << config << " directive. Buffer: " | |
282 | << buf << endl; | |
283 | #endif | |
284 | continue; | |
285 | } | |
286 | } | |
287 | delete [] buf; | |
288 | infile.close(); | |
289 | ||
290 | int nonemissing = 1; | |
291 | for (int count2 = 0; count2 < numdirectives; count2++) | |
292 | { | |
293 | if (!directives[count2].done) | |
294 | { | |
295 | cerr << "Missing config directive: " << directives[count2].desc << endl; | |
296 | nonemissing = 0; | |
297 | } | |
298 | } | |
299 | ||
300 | return nonemissing; | |
301 | } |