From b467198f16c6bea39d30232e59833001ce512908 Mon Sep 17 00:00:00 2001 From: Alexander Batalov Date: Thu, 26 May 2022 00:16:20 +0300 Subject: [PATCH] Introduce sfall config --- CMakeLists.txt | 2 ++ src/game.cc | 7 +++++++ src/game_config.cc | 18 +--------------- src/main.cc | 15 +++++++++++++- src/sfall_config.cc | 50 +++++++++++++++++++++++++++++++++++++++++++++ src/sfall_config.h | 20 ++++++++++++++++++ 6 files changed, 94 insertions(+), 18 deletions(-) create mode 100644 src/sfall_config.cc create mode 100644 src/sfall_config.h diff --git a/CMakeLists.txt b/CMakeLists.txt index fc69879..e12f86f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -229,6 +229,8 @@ add_executable(fallout2-ce WIN32 target_sources(fallout2-ce PUBLIC "src/fps_limiter.cc" "src/fps_limiter.h" + "src/sfall_config.cc" + "src/sfall_config.h" ) target_compile_definitions(fallout2-ce PUBLIC diff --git a/src/game.cc b/src/game.cc index 80a4cd4..5bd4563 100644 --- a/src/game.cc +++ b/src/game.cc @@ -43,6 +43,7 @@ #include "queue.h" #include "random.h" #include "scripts.h" +#include "sfall_config.h" #include "skill.h" #include "skilldex.h" #include "stat.h" @@ -115,12 +116,17 @@ int gameInitWithOptions(const char* windowTitle, bool isMapper, int font, int a4 return -1; } + // Sfall config should be initialized before game config, since it can + // override it's file name. + sfallConfigInit(argc, argv); + gameConfigInit(isMapper, argc, argv); gIsMapper = isMapper; if (gameDbInit() == -1) { gameConfigExit(false); + sfallConfigExit(); return -1; } @@ -402,6 +408,7 @@ void gameExit() _windowClose(); dbExit(); gameConfigExit(true); + sfallConfigExit(); } // 0x442D44 diff --git a/src/game_config.cc b/src/game_config.cc index 349bb2d..be5041d 100644 --- a/src/game_config.cc +++ b/src/game_config.cc @@ -21,7 +21,7 @@ Config gGameConfig; // 0x58E978 char gGameConfigFilePath[FILENAME_MAX]; -// Inits main game config and optionally Sfall config. +// Inits main game config. // // [isMapper] is a flag indicating whether we're initing config for a main // game, or a mapper. This value is `false` for the game itself. @@ -136,22 +136,6 @@ bool gameConfigInit(bool isMapper, int argc, char** argv) // whatever was loaded from `fallout2.cfg`. configParseCommandLineArguments(&gGameConfig, argc, argv); - // Optional Sfall extension - Config sfallConfig; - if (configInit(&sfallConfig)) { - configRead(&sfallConfig, "ddraw.ini", false); - - configParseCommandLineArguments(&sfallConfig, argc, argv); - - char* startingMap; - if (configGetString(&sfallConfig, "misc", "StartingMap", &startingMap)) { - if (*startingMap != '\0') { - strncpy(_mainMap, startingMap, 16); - } - } - } - configFree(&sfallConfig); - gGameConfigInitialized = true; return true; diff --git a/src/main.cc b/src/main.cc index 6bbad81..8b0badf 100644 --- a/src/main.cc +++ b/src/main.cc @@ -22,6 +22,7 @@ #include "palette.h" #include "random.h" #include "scripts.h" +#include "sfall_config.h" #include "selfrun.h" #include "text_font.h" #include "version.h" @@ -155,7 +156,19 @@ int falloutMain(int argc, char** argv) if (characterSelectorOpen() == 2) { gameMoviePlay(MOVIE_ELDER, GAME_MOVIE_STOP_MUSIC); randomSeedPrerandom(-1); - _main_load_new(_mainMap); + + // SFALL: Override starting map. + char* mapName = NULL; + if (configGetString(&gSfallConfig, SFALL_CONFIG_MISC_KEY, SFALL_CONFIG_STARTING_MAP_KEY, &mapName)) { + if (*mapName == '\0') { + mapName = NULL; + } + } + + char* mapNameCopy = strdup(mapName != NULL ? mapName : _mainMap); + _main_load_new(mapNameCopy); + free(mapNameCopy); + mainLoop(fpsLimiter); paletteFadeTo(gPaletteWhite); objectHide(gDude, NULL); diff --git a/src/sfall_config.cc b/src/sfall_config.cc new file mode 100644 index 0000000..fac4faa --- /dev/null +++ b/src/sfall_config.cc @@ -0,0 +1,50 @@ +#include "sfall_config.h" + +#include +#include + +#define SFALL_CONFIG_FILE_NAME "ddraw.ini" + +bool gSfallConfigInitialized = false; +Config gSfallConfig; + +bool sfallConfigInit(int argc, char** argv) +{ + if (gSfallConfigInitialized) { + return false; + } + + if (!configInit(&gSfallConfig)) { + return false; + } + + // Initialize defaults. + configSetString(&gSfallConfig, SFALL_CONFIG_MISC_KEY, SFALL_CONFIG_STARTING_MAP_KEY, ""); + + char path[FILENAME_MAX]; + char* executable = argv[0]; + char* ch = strrchr(executable, '\\'); + if (ch != NULL) { + *ch = '\0'; + sprintf(path, "%s\\%s", executable, SFALL_CONFIG_FILE_NAME); + *ch = '\\'; + } else { + strcpy(path, SFALL_CONFIG_FILE_NAME); + } + + configRead(&gSfallConfig, path, false); + + configParseCommandLineArguments(&gSfallConfig, argc, argv); + + gSfallConfigInitialized = true; + + return true; +} + +void sfallConfigExit() +{ + if (gSfallConfigInitialized) { + configFree(&gSfallConfig); + gSfallConfigInitialized = false; + } +} diff --git a/src/sfall_config.h b/src/sfall_config.h new file mode 100644 index 0000000..fe0b6d7 --- /dev/null +++ b/src/sfall_config.h @@ -0,0 +1,20 @@ +#ifndef SFALL_CONFIG_H +#define SFALL_CONFIG_H + +#include "config.h" + +#include + +#define SFALL_CONFIG_FILE_NAME "ddraw.ini" + +#define SFALL_CONFIG_MISC_KEY "Misc" + +#define SFALL_CONFIG_STARTING_MAP_KEY "StartingMap" + +extern bool gSfallConfigInitialized; +extern Config gSfallConfig; + +bool sfallConfigInit(int argc, char** argv); +void sfallConfigExit(); + +#endif /* SFALL_CONFIG_H */