| 1 | /* |
| 2 | * Copyright (C) 2000,2001 Onlyer ([email protected]) |
| 3 | * Copyright (C) 2005 Olaf Freyer ([email protected]) |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | #include "common/setup_before.h" |
| 20 | #include "setup.h" |
| 21 | #include "prefs.h" |
| 22 | |
| 23 | #include <cstdio> |
| 24 | |
| 25 | #include "common/conf.h" |
| 26 | #include "common/eventlog.h" |
| 27 | #include "common/setup_after.h" |
| 28 | |
| 29 | namespace pvpgn |
| 30 | { |
| 31 | |
| 32 | namespace d2dbs |
| 33 | { |
| 34 | |
| 35 | static struct |
| 36 | { |
| 37 | char const * logfile; |
| 38 | char const * logfile_gs; |
| 39 | char const * servaddrs; |
| 40 | char const * charsave_dir; |
| 41 | char const * charinfo_dir; |
| 42 | char const * charsave_bak_dir; |
| 43 | char const * charinfo_bak_dir; |
| 44 | char const * ladder_dir; |
| 45 | char const * d2gs_list; |
| 46 | unsigned int laddersave_interval; |
| 47 | unsigned int ladderinit_time; |
| 48 | char const * loglevels; |
| 49 | char const * pidfile; |
| 50 | unsigned int shutdown_delay; |
| 51 | unsigned int shutdown_decr; |
| 52 | unsigned int idletime; |
| 53 | unsigned int keepalive_interval; |
| 54 | unsigned int timeout_checkinterval; |
| 55 | unsigned int XML_output_ladder; |
| 56 | unsigned int ladderupdate_threshold; |
| 57 | unsigned int ladder_chars_only; |
| 58 | unsigned int difficulty_hack; |
| 59 | |
| 60 | } prefs_conf; |
| 61 | |
| 62 | static int conf_set_logfile(const char* valstr); |
| 63 | static int conf_setdef_logfile(void); |
| 64 | |
| 65 | static int conf_set_logfile_gs(const char* valstr); |
| 66 | static int conf_setdef_logfile_gs(void); |
| 67 | |
| 68 | static int conf_set_servaddrs(const char* valstr); |
| 69 | static int conf_setdef_servaddrs(void); |
| 70 | |
| 71 | static int conf_set_charsave_dir(const char* valstr); |
| 72 | static int conf_setdef_charsave_dir(void); |
| 73 | |
| 74 | static int conf_set_charinfo_dir(const char* valstr); |
| 75 | static int conf_setdef_charinfo_dir(void); |
| 76 | |
| 77 | static int conf_set_charsave_bak_dir(const char* valstr); |
| 78 | static int conf_setdef_charsave_bak_dir(void); |
| 79 | |
| 80 | static int conf_set_charinfo_bak_dir(const char* valstr); |
| 81 | static int conf_setdef_charinfo_bak_dir(void); |
| 82 | |
| 83 | static int conf_set_ladder_dir(const char* valstr); |
| 84 | static int conf_setdef_ladder_dir(void); |
| 85 | |
| 86 | static int conf_set_d2gs_list(const char* valstr); |
| 87 | static int conf_setdef_d2gs_list(void); |
| 88 | |
| 89 | static int conf_set_laddersave_interval(const char* valstr); |
| 90 | static int conf_setdef_laddersave_interval(void); |
| 91 | |
| 92 | static int conf_set_ladderinit_time(const char* valstr); |
| 93 | static int conf_setdef_ladderinit_time(void); |
| 94 | |
| 95 | static int conf_set_loglevels(const char* valstr); |
| 96 | static int conf_setdef_loglevels(void); |
| 97 | |
| 98 | static int conf_set_pidfile(const char* valstr); |
| 99 | static int conf_setdef_pidfile(void); |
| 100 | |
| 101 | static int conf_set_shutdown_delay(const char* valstr); |
| 102 | static int conf_setdef_shutdown_delay(void); |
| 103 | |
| 104 | static int conf_set_shutdown_decr(const char* valstr); |
| 105 | static int conf_setdef_shutdown_decr(void); |
| 106 | |
| 107 | static int conf_set_idletime(const char* valstr); |
| 108 | static int conf_setdef_idletime(void); |
| 109 | |
| 110 | static int conf_set_keepalive_interval(const char* valstr); |
| 111 | static int conf_setdef_keepalive_interval(void); |
| 112 | |
| 113 | static int conf_set_timeout_checkinterval(const char* valstr); |
| 114 | static int conf_setdef_timeout_checkinterval(void); |
| 115 | |
| 116 | static int conf_set_XML_output_ladder(const char* valstr); |
| 117 | static int conf_setdef_XML_output_ladder(void); |
| 118 | |
| 119 | static int conf_set_ladderupdate_threshold(const char* valstr); |
| 120 | static int conf_setdef_ladderupdate_threshold(void); |
| 121 | |
| 122 | static int conf_set_ladder_chars_only(const char* valstr); |
| 123 | static int conf_setdef_ladder_chars_only(void); |
| 124 | |
| 125 | static int conf_set_difficulty_hack(const char* valstr); |
| 126 | static int conf_setdef_difficulty_hack(void); |
| 127 | |
| 128 | |
| 129 | static t_conf_entry prefs_conf_table[] = { |
| 130 | { "logfile", conf_set_logfile, NULL, conf_setdef_logfile }, |
| 131 | { "logfile-gs", conf_set_logfile_gs, NULL, conf_setdef_logfile_gs }, |
| 132 | { "servaddrs", conf_set_servaddrs, NULL, conf_setdef_servaddrs }, |
| 133 | { "charsavedir", conf_set_charsave_dir, NULL, conf_setdef_charsave_dir }, |
| 134 | { "charinfodir", conf_set_charinfo_dir, NULL, conf_setdef_charinfo_dir }, |
| 135 | { "bak_charsavedir", conf_set_charsave_bak_dir, NULL, conf_setdef_charsave_bak_dir }, |
| 136 | { "bak_charinfodir", conf_set_charinfo_bak_dir, NULL, conf_setdef_charinfo_bak_dir }, |
| 137 | { "ladderdir", conf_set_ladder_dir, NULL, conf_setdef_ladder_dir }, |
| 138 | { "gameservlist", conf_set_d2gs_list, NULL, conf_setdef_d2gs_list }, |
| 139 | { "laddersave_interval", conf_set_laddersave_interval, NULL, conf_setdef_laddersave_interval }, |
| 140 | { "ladderinit_time", conf_set_ladderinit_time, NULL, conf_setdef_ladderinit_time }, |
| 141 | { "loglevels", conf_set_loglevels, NULL, conf_setdef_loglevels }, |
| 142 | { "pidfile", conf_set_pidfile, NULL, conf_setdef_pidfile }, |
| 143 | { "shutdown_delay", conf_set_shutdown_delay, NULL, conf_setdef_shutdown_delay }, |
| 144 | { "shutdown_decr", conf_set_shutdown_decr, NULL, conf_setdef_shutdown_decr }, |
| 145 | { "idletime", conf_set_idletime, NULL, conf_setdef_idletime }, |
| 146 | { "keepalive_interval", conf_set_keepalive_interval, NULL, conf_setdef_keepalive_interval }, |
| 147 | { "timeout_checkinterval", conf_set_timeout_checkinterval, NULL, conf_setdef_timeout_checkinterval }, |
| 148 | { "XML_ladder_output", conf_set_XML_output_ladder, NULL, conf_setdef_XML_output_ladder }, |
| 149 | { "ladderupdate_threshold", conf_set_ladderupdate_threshold, NULL, conf_setdef_ladderupdate_threshold }, |
| 150 | { "ladder_chars_only", conf_set_ladder_chars_only, NULL, conf_setdef_ladder_chars_only }, |
| 151 | { "difficulty_hack", conf_set_difficulty_hack, NULL, conf_setdef_difficulty_hack }, |
| 152 | { NULL, NULL, NULL, NULL } |
| 153 | }; |
| 154 | |
| 155 | extern int d2dbs_prefs_load(char const * filename) |
| 156 | { |
| 157 | std::FILE *fd; |
| 158 | |
| 159 | if (!filename) { |
| 160 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL filename"); |
| 161 | return -1; |
| 162 | } |
| 163 | |
| 164 | fd = std::fopen(filename, "rt"); |
| 165 | if (!fd) { |
| 166 | eventlog(eventlog_level_error, __FUNCTION__, "could not open file '{}'", filename); |
| 167 | return -1; |
| 168 | } |
| 169 | |
| 170 | if (conf_load_file(fd, prefs_conf_table)) { |
| 171 | eventlog(eventlog_level_error, __FUNCTION__, "error loading config file '{}'", filename); |
| 172 | std::fclose(fd); |
| 173 | return -1; |
| 174 | } |
| 175 | |
| 176 | std::fclose(fd); |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | extern int d2dbs_prefs_reload(char const * filename) |
| 182 | { |
| 183 | d2dbs_prefs_unload(); |
| 184 | if (d2dbs_prefs_load(filename) < 0) return -1; |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | extern int d2dbs_prefs_unload(void) |
| 189 | { |
| 190 | conf_unload(prefs_conf_table); |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | extern char const * d2dbs_prefs_get_servaddrs(void) |
| 195 | { |
| 196 | return prefs_conf.servaddrs; |
| 197 | } |
| 198 | |
| 199 | static int conf_set_servaddrs(const char* valstr) |
| 200 | { |
| 201 | return conf_set_str(&prefs_conf.servaddrs, valstr, NULL); |
| 202 | } |
| 203 | |
| 204 | static int conf_setdef_servaddrs(void) |
| 205 | { |
| 206 | return conf_set_str(&prefs_conf.servaddrs, NULL, D2DBS_SERVER_ADDRS); |
| 207 | } |
| 208 | |
| 209 | |
| 210 | extern char const * d2dbs_prefs_get_logfile(void) |
| 211 | { |
| 212 | return prefs_conf.logfile; |
| 213 | } |
| 214 | |
| 215 | static int conf_set_logfile(const char * valstr) |
| 216 | { |
| 217 | return conf_set_str(&prefs_conf.logfile, valstr, NULL); |
| 218 | } |
| 219 | |
| 220 | static int conf_setdef_logfile(void) |
| 221 | { |
| 222 | return conf_set_str(&prefs_conf.logfile, NULL, DEFAULT_LOG_FILE); |
| 223 | } |
| 224 | |
| 225 | |
| 226 | extern char const * prefs_get_logfile_gs(void) |
| 227 | { |
| 228 | return prefs_conf.logfile_gs; |
| 229 | } |
| 230 | |
| 231 | static int conf_set_logfile_gs(const char * valstr) |
| 232 | { |
| 233 | return conf_set_str(&prefs_conf.logfile_gs, valstr, NULL); |
| 234 | } |
| 235 | |
| 236 | static int conf_setdef_logfile_gs(void) |
| 237 | { |
| 238 | return conf_set_str(&prefs_conf.logfile_gs, NULL, DEFAULT_LOG_FILE); |
| 239 | } |
| 240 | |
| 241 | |
| 242 | extern char const * d2dbs_prefs_get_charsave_dir(void) |
| 243 | { |
| 244 | return prefs_conf.charsave_dir; |
| 245 | } |
| 246 | |
| 247 | static int conf_set_charsave_dir(const char * valstr) |
| 248 | { |
| 249 | return conf_set_str(&prefs_conf.charsave_dir, valstr, NULL); |
| 250 | } |
| 251 | |
| 252 | static int conf_setdef_charsave_dir(void) |
| 253 | { |
| 254 | return conf_set_str(&prefs_conf.charsave_dir, NULL, D2DBS_CHARSAVE_DIR); |
| 255 | } |
| 256 | |
| 257 | |
| 258 | extern char const * d2dbs_prefs_get_charinfo_dir(void) |
| 259 | { |
| 260 | return prefs_conf.charinfo_dir; |
| 261 | } |
| 262 | |
| 263 | static int conf_set_charinfo_dir(const char * valstr) |
| 264 | { |
| 265 | return conf_set_str(&prefs_conf.charinfo_dir, valstr, NULL); |
| 266 | } |
| 267 | |
| 268 | static int conf_setdef_charinfo_dir(void) |
| 269 | { |
| 270 | return conf_set_str(&prefs_conf.charinfo_dir, NULL, D2DBS_CHARINFO_DIR); |
| 271 | } |
| 272 | |
| 273 | |
| 274 | extern char const * prefs_get_charsave_bak_dir(void) |
| 275 | { |
| 276 | return prefs_conf.charsave_bak_dir; |
| 277 | } |
| 278 | |
| 279 | static int conf_set_charsave_bak_dir(const char * valstr) |
| 280 | { |
| 281 | return conf_set_str(&prefs_conf.charsave_bak_dir, valstr, NULL); |
| 282 | } |
| 283 | |
| 284 | static int conf_setdef_charsave_bak_dir(void) |
| 285 | { |
| 286 | return conf_set_str(&prefs_conf.charsave_bak_dir, NULL, D2DBS_CHARSAVEBAK_DIR); |
| 287 | } |
| 288 | |
| 289 | |
| 290 | extern char const * prefs_get_charinfo_bak_dir(void) |
| 291 | { |
| 292 | return prefs_conf.charinfo_bak_dir; |
| 293 | } |
| 294 | |
| 295 | static int conf_set_charinfo_bak_dir(const char * valstr) |
| 296 | { |
| 297 | return conf_set_str(&prefs_conf.charinfo_bak_dir, valstr, NULL); |
| 298 | } |
| 299 | |
| 300 | static int conf_setdef_charinfo_bak_dir(void) |
| 301 | { |
| 302 | return conf_set_str(&prefs_conf.charinfo_bak_dir, NULL, D2DBS_CHARINFOBAK_DIR); |
| 303 | } |
| 304 | |
| 305 | |
| 306 | extern char const * d2dbs_prefs_get_ladder_dir(void) |
| 307 | { |
| 308 | return prefs_conf.ladder_dir; |
| 309 | } |
| 310 | |
| 311 | static int conf_set_ladder_dir(const char * valstr) |
| 312 | { |
| 313 | return conf_set_str(&prefs_conf.ladder_dir, valstr, NULL); |
| 314 | } |
| 315 | |
| 316 | static int conf_setdef_ladder_dir(void) |
| 317 | { |
| 318 | return conf_set_str(&prefs_conf.ladder_dir, NULL, D2DBS_LADDER_DIR); |
| 319 | } |
| 320 | |
| 321 | |
| 322 | extern char const * d2dbs_prefs_get_d2gs_list(void) |
| 323 | { |
| 324 | return prefs_conf.d2gs_list; |
| 325 | } |
| 326 | |
| 327 | static int conf_set_d2gs_list(const char * valstr) |
| 328 | { |
| 329 | return conf_set_str(&prefs_conf.d2gs_list, valstr, NULL); |
| 330 | } |
| 331 | |
| 332 | static int conf_setdef_d2gs_list(void) |
| 333 | { |
| 334 | return conf_set_str(&prefs_conf.d2gs_list, NULL, D2GS_SERVER_LIST); |
| 335 | } |
| 336 | |
| 337 | |
| 338 | extern unsigned int prefs_get_laddersave_interval(void) |
| 339 | { |
| 340 | return prefs_conf.laddersave_interval; |
| 341 | } |
| 342 | |
| 343 | static int conf_set_laddersave_interval(char const * valstr) |
| 344 | { |
| 345 | return conf_set_int(&prefs_conf.laddersave_interval, valstr, 0); |
| 346 | } |
| 347 | |
| 348 | static int conf_setdef_laddersave_interval(void) |
| 349 | { |
| 350 | return conf_set_int(&prefs_conf.laddersave_interval, NULL, 3600); |
| 351 | } |
| 352 | |
| 353 | extern unsigned int prefs_get_ladderinit_time(void) |
| 354 | { |
| 355 | return prefs_conf.ladderinit_time; |
| 356 | } |
| 357 | |
| 358 | static int conf_set_ladderinit_time(char const * valstr) |
| 359 | { |
| 360 | return conf_set_int(&prefs_conf.ladderinit_time, valstr, 0); |
| 361 | } |
| 362 | |
| 363 | static int conf_setdef_ladderinit_time(void) |
| 364 | { |
| 365 | return conf_set_int(&prefs_conf.ladderinit_time, NULL, 0); |
| 366 | } |
| 367 | |
| 368 | |
| 369 | extern char const * d2dbs_prefs_get_loglevels(void) |
| 370 | { |
| 371 | return prefs_conf.loglevels; |
| 372 | } |
| 373 | |
| 374 | static int conf_set_loglevels(const char* valstr) |
| 375 | { |
| 376 | return conf_set_str(&prefs_conf.loglevels, valstr, NULL); |
| 377 | } |
| 378 | |
| 379 | static int conf_setdef_loglevels(void) |
| 380 | { |
| 381 | return conf_set_str(&prefs_conf.loglevels, NULL, DEFAULT_LOG_LEVELS); |
| 382 | } |
| 383 | |
| 384 | |
| 385 | extern char const * d2dbs_prefs_get_pidfile(void) |
| 386 | { |
| 387 | return prefs_conf.pidfile; |
| 388 | } |
| 389 | |
| 390 | static int conf_set_pidfile(const char* valstr) |
| 391 | { |
| 392 | return conf_set_str(&prefs_conf.pidfile, valstr, NULL); |
| 393 | } |
| 394 | |
| 395 | static int conf_setdef_pidfile(void) |
| 396 | { |
| 397 | return conf_set_str(&prefs_conf.pidfile, NULL, ""); |
| 398 | } |
| 399 | |
| 400 | |
| 401 | extern unsigned int d2dbs_prefs_get_shutdown_delay(void) |
| 402 | { |
| 403 | return prefs_conf.shutdown_delay; |
| 404 | } |
| 405 | |
| 406 | static int conf_set_shutdown_delay(const char * valstr) |
| 407 | { |
| 408 | return conf_set_int(&prefs_conf.shutdown_delay, valstr, 0); |
| 409 | } |
| 410 | |
| 411 | static int conf_setdef_shutdown_delay(void) |
| 412 | { |
| 413 | return conf_set_int(&prefs_conf.shutdown_delay, NULL, DEFAULT_SHUTDOWN_DELAY); |
| 414 | } |
| 415 | |
| 416 | |
| 417 | extern unsigned int d2dbs_prefs_get_shutdown_decr(void) |
| 418 | { |
| 419 | return prefs_conf.shutdown_decr; |
| 420 | } |
| 421 | |
| 422 | static int conf_set_shutdown_decr(const char * valstr) |
| 423 | { |
| 424 | return conf_set_int(&prefs_conf.shutdown_decr, valstr, 0); |
| 425 | } |
| 426 | |
| 427 | static int conf_setdef_shutdown_decr(void) |
| 428 | { |
| 429 | return conf_set_int(&prefs_conf.shutdown_decr, NULL, DEFAULT_SHUTDOWN_DECR); |
| 430 | } |
| 431 | |
| 432 | |
| 433 | extern unsigned int d2dbs_prefs_get_idletime(void) |
| 434 | { |
| 435 | return prefs_conf.idletime; |
| 436 | } |
| 437 | |
| 438 | static int conf_set_idletime(const char * valstr) |
| 439 | { |
| 440 | return conf_set_int(&prefs_conf.idletime, valstr, 0); |
| 441 | } |
| 442 | |
| 443 | static int conf_setdef_idletime(void) |
| 444 | { |
| 445 | return conf_set_int(&prefs_conf.idletime, NULL, DEFAULT_IDLETIME); |
| 446 | } |
| 447 | |
| 448 | |
| 449 | extern unsigned int prefs_get_keepalive_interval(void) |
| 450 | { |
| 451 | return prefs_conf.keepalive_interval; |
| 452 | } |
| 453 | |
| 454 | static int conf_set_keepalive_interval(const char * valstr) |
| 455 | { |
| 456 | return conf_set_int(&prefs_conf.keepalive_interval, valstr, 0); |
| 457 | } |
| 458 | |
| 459 | static int conf_setdef_keepalive_interval(void) |
| 460 | { |
| 461 | return conf_set_int(&prefs_conf.keepalive_interval, NULL, DEFAULT_KEEPALIVE_INTERVAL); |
| 462 | } |
| 463 | |
| 464 | |
| 465 | extern unsigned int d2dbs_prefs_get_timeout_checkinterval(void) |
| 466 | { |
| 467 | return prefs_conf.timeout_checkinterval; |
| 468 | } |
| 469 | |
| 470 | static int conf_set_timeout_checkinterval(const char * valstr) |
| 471 | { |
| 472 | return conf_set_int(&prefs_conf.timeout_checkinterval, valstr, 0); |
| 473 | } |
| 474 | |
| 475 | static int conf_setdef_timeout_checkinterval(void) |
| 476 | { |
| 477 | return conf_set_int(&prefs_conf.timeout_checkinterval, NULL, DEFAULT_TIMEOUT_CHECKINTERVAL); |
| 478 | } |
| 479 | |
| 480 | |
| 481 | extern unsigned int d2dbs_prefs_get_XML_output_ladder(void) |
| 482 | { |
| 483 | return prefs_conf.XML_output_ladder; |
| 484 | } |
| 485 | |
| 486 | static int conf_set_XML_output_ladder(const char * valstr) |
| 487 | { |
| 488 | return conf_set_bool(&prefs_conf.XML_output_ladder, valstr, 0); |
| 489 | } |
| 490 | |
| 491 | static int conf_setdef_XML_output_ladder(void) |
| 492 | { |
| 493 | return conf_set_bool(&prefs_conf.XML_output_ladder, NULL, 0); |
| 494 | } |
| 495 | |
| 496 | extern unsigned int prefs_get_ladderupdate_threshold(void) |
| 497 | { |
| 498 | return prefs_conf.ladderupdate_threshold; |
| 499 | } |
| 500 | |
| 501 | static int conf_set_ladderupdate_threshold(const char * valstr) |
| 502 | { |
| 503 | return conf_set_int(&prefs_conf.ladderupdate_threshold, valstr, 0); |
| 504 | } |
| 505 | |
| 506 | static int conf_setdef_ladderupdate_threshold(void) |
| 507 | { |
| 508 | return conf_set_int(&prefs_conf.ladderupdate_threshold, NULL, DEFAULT_LADDERUPDATE_THRESHOLD); |
| 509 | } |
| 510 | |
| 511 | |
| 512 | extern unsigned int prefs_get_ladder_chars_only(void) |
| 513 | { |
| 514 | return prefs_conf.ladder_chars_only; |
| 515 | } |
| 516 | |
| 517 | static int conf_set_ladder_chars_only(const char * valstr) |
| 518 | { |
| 519 | return conf_set_bool(&prefs_conf.ladder_chars_only, valstr, 0); |
| 520 | } |
| 521 | |
| 522 | static int conf_setdef_ladder_chars_only(void) |
| 523 | { |
| 524 | return conf_set_bool(&prefs_conf.ladder_chars_only, NULL, 0); |
| 525 | } |
| 526 | |
| 527 | |
| 528 | extern unsigned int prefs_get_difficulty_hack(void) |
| 529 | { |
| 530 | return prefs_conf.difficulty_hack; |
| 531 | } |
| 532 | |
| 533 | static int conf_set_difficulty_hack(const char * valstr) |
| 534 | { |
| 535 | return conf_set_int(&prefs_conf.difficulty_hack, valstr, 0); |
| 536 | } |
| 537 | |
| 538 | static int conf_setdef_difficulty_hack(void) |
| 539 | { |
| 540 | return conf_set_int(&prefs_conf.difficulty_hack, NULL, 0); |
| 541 | } |
| 542 | |
| 543 | } |
| 544 | |
| 545 | } |