Changeset - r25625:c14e95bbb24a
[Not reviewed]
master
0 3 0
Patric Stout - 3 years ago 2021-06-10 21:13:34
truebrain@openttd.org
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'.
3 files changed with 33 insertions and 8 deletions:
0 comments (0 inline, 0 general)
src/fileio.cpp
Show inline comments
 
@@ -76,16 +76,28 @@ static TarLinkList _tar_linklist[NUM_SUB
 
 */
 
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);
 
	}
 
}
 

	
 
/**
 
 * Check whether the given file exists
 
@@ -949,17 +961,18 @@ std::string _personal_dir;
 

	
 
/**
 
 * Acquire the base paths (personal dir and game data 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;
 
	const std::string homedir = GetHomeDir();
 
	const char *xdg_config_home = getenv("XDG_CONFIG_HOME");
 
	if (xdg_config_home != nullptr) {
 
@@ -1020,12 +1033,19 @@ void DeterminePaths(const char *exe)
 

	
 
#ifdef USE_XDG
 
	if (config_dir == config_home) {
 
		/* 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
 
	{
 
		_personal_dir = config_dir;
 
	}
 

	
 
@@ -1044,14 +1064,15 @@ void DeterminePaths(const char *exe)
 
	for (uint i = 0; i < lengthof(default_subdirs); i++) {
 
		FioCreateDirectory(_personal_dir + _subdirs[default_subdirs[i]]);
 
	}
 

	
 
	/* 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 };
 
	for (uint i = 0; i < lengthof(dirs); i++) {
 
		FioCreateDirectory(FioGetDirectory(SP_AUTODOWNLOAD_DIR, dirs[i]));
 
	}
src/fileio_func.h
Show inline comments
 
@@ -24,13 +24,13 @@ std::string FioFindDirectory(Subdirector
 
void FioCreateDirectory(const std::string &name);
 

	
 
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<char[]> 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);
 

	
 
extern std::string _personal_dir; ///< custom directory for personal settings, saves, newgrf, etc.
 
extern std::vector<Searchpath> _valid_searchpaths;
src/openttd.cpp
Show inline comments
 
@@ -194,12 +194,13 @@ static void ShowHelp()
 
#endif
 
		"  -I graphics_set     = Force the graphics set (see below)\n"
 
		"  -S sounds_set       = Force the sounds set (see below)\n"
 
		"  -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)
 
	);
 

	
 
	/* List the graphics packs */
 
@@ -503,12 +504,13 @@ static const OptionData _options[] = {
 
	GETOPT_SHORT_OPTVAL('d'),
 
	 GETOPT_SHORT_NOVAL('e'),
 
	GETOPT_SHORT_OPTVAL('g'),
 
	 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()
 
};
 

	
 
/**
 
@@ -527,12 +529,13 @@ int openttd_main(int argc, char *argv[])
 
	std::string sounds_set;
 
	std::string music_set;
 
	Dimension resolution = {0, 0};
 
	std::unique_ptr<AfterNewGRFScan> scanner(new AfterNewGRFScan());
 
	bool dedicated = false;
 
	char *debuglog_conn = nullptr;
 
	bool only_local_path = false;
 

	
 
	extern bool _dedicated_forks;
 
	_dedicated_forks = false;
 

	
 
	_game_mode = GM_MENU;
 
	_switch_mode = SM_MENU;
 
@@ -605,13 +608,13 @@ int openttd_main(int argc, char *argv[])
 
			/* Give a random map if no seed has been given */
 
			if (scanner->generation_seed == GENERATE_NEW_SEED) {
 
				scanner->generation_seed = InteractiveRandom();
 
			}
 
			break;
 
		case 'q': {
 
			DeterminePaths(argv[0]);
 
			DeterminePaths(argv[0], only_local_path);
 
			if (StrEmpty(mgo.opt)) {
 
				ret = 1;
 
				return ret;
 
			}
 

	
 
			char title[80];
 
@@ -634,12 +637,13 @@ int openttd_main(int argc, char *argv[])
 
			WriteSavegameInfo(title);
 
			return ret;
 
		}
 
		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;
 
		}
 
		if (i == -2) break;
 
	}
 
@@ -647,22 +651,22 @@ int openttd_main(int argc, char *argv[])
 
	if (i == -2 || mgo.numleft > 0) {
 
		/* Either the user typed '-h', they made an error, or they added unrecognized command line arguments.
 
		 * In all cases, print the help, and exit.
 
		 *
 
		 * 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();
 
		BaseMusic::FindSets();
 
		ShowHelp();
 
		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);
 
	if (_dedicated_forks && !dedicated) _dedicated_forks = false;
 

	
 
#if defined(UNIX)
0 comments (0 inline, 0 general)