Simplify game_config::path determination

This commit is contained in:
Gunter Labes 2024-08-06 00:14:27 +02:00 committed by Steve Cotton
parent 8c581eee90
commit 150a738b39
2 changed files with 25 additions and 57 deletions

View file

@ -110,23 +110,6 @@ game_launcher::game_launcher(const commandline_options& cmdline_opts)
bool no_music = false;
bool no_sound = false;
// The path can be hardcoded and it might be a relative path.
if(!game_config::path.empty() &&
#ifdef _WIN32
// use c_str to ensure that index 1 points to valid element since c_str() returns null-terminated string
game_config::path.c_str()[1] != ':'
#else
game_config::path[0] != '/'
#endif
)
{
game_config::path = filesystem::get_cwd() + '/' + game_config::path;
// font_manager_.update_font_path()
// To update the font path, destroy and recreate the manager
font_manager_.~manager();
new (&font_manager_) font::manager();
}
if(cmdline_opts_.core_id) {
prefs::get().set_core(*cmdline_opts_.core_id);
}

View file

@ -344,6 +344,12 @@ static int process_command_args(commandline_options& cmdline_opts)
}
}
if(!cmdline_opts.nobanner) {
PLAIN_LOG << "Battle for Wesnoth v" << game_config::revision << " " << game_config::build_arch();
const std::time_t t = std::time(nullptr);
PLAIN_LOG << "Started on " << ctime(&t);
}
if(cmdline_opts.usercache_path) {
std::cout << filesystem::get_cache_dir();
return 0;
@ -355,54 +361,33 @@ static int process_command_args(commandline_options& cmdline_opts)
}
if(cmdline_opts.data_dir) {
const std::string datadir = *cmdline_opts.data_dir;
PLAIN_LOG << "Starting with directory: '" << datadir << "'";
#ifdef _WIN32
// use c_str to ensure that index 1 points to valid element since c_str() returns null-terminated string
if(datadir.c_str()[1] == ':') {
#else
if(datadir[0] == '/') {
#endif
game_config::path = datadir;
} else {
game_config::path = filesystem::get_cwd() + '/' + datadir;
}
PLAIN_LOG << "Now have with directory: '" << game_config::path << "'";
game_config::path = filesystem::normalize_path(game_config::path, true, true);
game_config::path = filesystem::normalize_path(*cmdline_opts.data_dir, true, true);
if(!cmdline_opts.nobanner) {
PLAIN_LOG << "Overriding data directory with '" << game_config::path << "'";
}
if(!filesystem::is_directory(game_config::path)) {
PLAIN_LOG << "Could not find directory '" << game_config::path << "'";
throw config::error("directory not found");
}
// don't update font as we already updating it in game ctor
// font_manager_.update_font_path();
}
if(!cmdline_opts.nobanner) {
PLAIN_LOG << "Battle for Wesnoth v" << game_config::revision << " " << game_config::build_arch();
const std::time_t t = std::time(nullptr);
PLAIN_LOG << "Started on " << ctime(&t);
}
if(std::string exe_dir = filesystem::get_exe_dir(); !exe_dir.empty()) {
if(std::string auto_dir = filesystem::autodetect_game_data_dir(std::move(exe_dir)); !auto_dir.empty()) {
if(!cmdline_opts.nobanner) {
PLAIN_LOG << "Automatically found a possible data directory at: " << auto_dir;
}
game_config::path = std::move(auto_dir);
} else if(game_config::path.empty()) {
if (!cmdline_opts.data_dir.has_value()) {
PLAIN_LOG << "Cannot find a data directory. Specify one with --data-dir";
} else {
// if a pre-defined path does not exist this will empty it
game_config::path = filesystem::normalize_path(game_config::path, true, true);
if(game_config::path.empty()) {
if(std::string exe_dir = filesystem::get_exe_dir(); !exe_dir.empty()) {
if(std::string auto_dir = filesystem::autodetect_game_data_dir(std::move(exe_dir)); !auto_dir.empty()) {
if(!cmdline_opts.nobanner) {
PLAIN_LOG << "Automatically found a possible data directory at: " << auto_dir;
}
game_config::path = filesystem::normalize_path(auto_dir, true, true);
}
} else {
PLAIN_LOG << "Cannot find game data directory. Specify one with --data-dir";
return 1;
}
}
}
if(!filesystem::is_directory(game_config::path)) {
PLAIN_LOG << "Could not find game data directory '" << game_config::path << "'";
return 1;
}
if(cmdline_opts.data_path) {
std::cout << game_config::path;
return 0;