From ed3946e295f0e74880d85ffb6f6a933ca87e460d Mon Sep 17 00:00:00 2001 From: Patric Stout Date: Thu, 10 Jun 2021 23:13:34 +0200 Subject: Add: '-X' option to ignore global folders in the search path (#9341) This is extreme useful for automated testing. Without this, OpenTTD will always look in your personal-dir (like ~/.local/share/openttd or %USER%\Documents\OpenTTD). For most users this is exactly what we want, that there is a shared place for all their files. However, for automated testing this is rather annoying, as your local development files influence the automated test. As such, '-X' counters this, and only gives the local folders. This is especially useful in combination with '-x' and '-c'. --- src/fileio.cpp | 29 +++++++++++++++++++++++++---- src/fileio_func.h | 2 +- src/openttd.cpp | 10 +++++++--- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/fileio.cpp b/src/fileio.cpp index bd05a4e7d..c958684dd 100644 --- a/src/fileio.cpp +++ b/src/fileio.cpp @@ -79,10 +79,22 @@ static bool IsValidSearchPath(Searchpath sp) return sp < _searchpaths.size() && !_searchpaths[sp].empty(); } -static void FillValidSearchPaths() +static void FillValidSearchPaths(bool only_local_path) { _valid_searchpaths.clear(); for (Searchpath sp = SP_FIRST_DIR; sp < NUM_SEARCHPATHS; sp++) { + if (only_local_path) { + switch (sp) { + case SP_WORKING_DIR: // Can be influence by "-c" option. + case SP_BINARY_DIR: // Most likely contains all the language files. + case SP_AUTODOWNLOAD_DIR: // Otherwise we cannot download in-game content. + break; + + default: + continue; + } + } + if (IsValidSearchPath(sp)) _valid_searchpaths.emplace_back(sp); } } @@ -952,11 +964,12 @@ std::string _personal_dir; * fill all other paths (save dir, autosave dir etc) and * make the save and scenario directories. * @param exe the path from the current path to the executable + * @param only_local_path Whether we shouldn't fill searchpaths with global folders. */ -void DeterminePaths(const char *exe) +void DeterminePaths(const char *exe, bool only_local_path) { DetermineBasePaths(exe); - FillValidSearchPaths(); + FillValidSearchPaths(only_local_path); #ifdef USE_XDG std::string config_home; @@ -1023,6 +1036,13 @@ void DeterminePaths(const char *exe) /* We are using the XDG configuration home for the config file, * then store the rest in the XDG data home folder. */ _personal_dir = _searchpaths[SP_PERSONAL_DIR_XDG]; + if (only_local_path) { + /* In case of XDG and we only want local paths and we detected that + * the user either manually indicated the XDG path or didn't use + * "-c" option, we change the working-dir to the XDG personal-dir, + * as this is most likely what the user is expecting. */ + _searchpaths[SP_WORKING_DIR] = _searchpaths[SP_PERSONAL_DIR_XDG]; + } } else #endif { @@ -1047,8 +1067,9 @@ void DeterminePaths(const char *exe) /* If we have network we make a directory for the autodownloading of content */ _searchpaths[SP_AUTODOWNLOAD_DIR] = _personal_dir + "content_download" PATHSEP; + DEBUG(misc, 4, "%s added as search path", _searchpaths[SP_AUTODOWNLOAD_DIR].c_str()); FioCreateDirectory(_searchpaths[SP_AUTODOWNLOAD_DIR]); - FillValidSearchPaths(); + FillValidSearchPaths(only_local_path); /* Create the directory for each of the types of content */ const Subdirectory dirs[] = { SCENARIO_DIR, HEIGHTMAP_DIR, BASESET_DIR, NEWGRF_DIR, AI_DIR, AI_LIBRARY_DIR, GAME_DIR, GAME_LIBRARY_DIR }; diff --git a/src/fileio_func.h b/src/fileio_func.h index baa8cb1bb..3556849a0 100644 --- a/src/fileio_func.h +++ b/src/fileio_func.h @@ -27,7 +27,7 @@ const char *FiosGetScreenshotDir(); void SanitizeFilename(char *filename); void AppendPathSeparator(std::string &buf); -void DeterminePaths(const char *exe); +void DeterminePaths(const char *exe, bool only_local_path); std::unique_ptr ReadFileToMem(const std::string &filename, size_t &lenp, size_t maxsize); bool FileExists(const std::string &filename); bool ExtractTar(const std::string &tar_filename, Subdirectory subdir); diff --git a/src/openttd.cpp b/src/openttd.cpp index f39b87c87..fcd8fcd34 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -197,6 +197,7 @@ static void ShowHelp() " -M music_set = Force the music set (see below)\n" " -c config_file = Use 'config_file' instead of 'openttd.cfg'\n" " -x = Never save configuration changes to disk\n" + " -X = Don't use global folders to search for files\n" " -q savegame = Write some information about the savegame and exit\n" "\n", lastof(buf) @@ -506,6 +507,7 @@ static const OptionData _options[] = { GETOPT_SHORT_VALUE('G'), GETOPT_SHORT_VALUE('c'), GETOPT_SHORT_NOVAL('x'), + GETOPT_SHORT_NOVAL('X'), GETOPT_SHORT_VALUE('q'), GETOPT_SHORT_NOVAL('h'), GETOPT_END() @@ -530,6 +532,7 @@ int openttd_main(int argc, char *argv[]) std::unique_ptr scanner(new AfterNewGRFScan()); bool dedicated = false; char *debuglog_conn = nullptr; + bool only_local_path = false; extern bool _dedicated_forks; _dedicated_forks = false; @@ -608,7 +611,7 @@ int openttd_main(int argc, char *argv[]) } break; case 'q': { - DeterminePaths(argv[0]); + DeterminePaths(argv[0], only_local_path); if (StrEmpty(mgo.opt)) { ret = 1; return ret; @@ -637,6 +640,7 @@ int openttd_main(int argc, char *argv[]) case 'G': scanner->generation_seed = strtoul(mgo.opt, nullptr, 10); break; case 'c': _config_file = mgo.opt; break; case 'x': scanner->save_config = false; break; + case 'X': only_local_path = true; break; case 'h': i = -2; // Force printing of help. break; @@ -650,7 +654,7 @@ int openttd_main(int argc, char *argv[]) * * The next two functions are needed to list the graphics sets. We can't do them earlier * because then we cannot show it on the debug console as that hasn't been configured yet. */ - DeterminePaths(argv[0]); + DeterminePaths(argv[0], only_local_path); TarScanner::DoScan(TarScanner::BASESET); BaseGraphics::FindSets(); BaseSounds::FindSets(); @@ -659,7 +663,7 @@ int openttd_main(int argc, char *argv[]) return ret; } - DeterminePaths(argv[0]); + DeterminePaths(argv[0], only_local_path); TarScanner::DoScan(TarScanner::BASESET); if (dedicated) DEBUG(net, 3, "Starting dedicated server, version %s", _openttd_revision); -- cgit v1.2.3-70-g09d2