ensure the userdata is initialized before trying to use the preferences

This commit is contained in:
pentarctagon 2024-06-20 18:08:09 -05:00 committed by Pentarctagon
parent 7895b843fa
commit d911a86a8f
5 changed files with 27 additions and 19 deletions

View file

@ -694,7 +694,7 @@ static bfs::path windows_userdata(const std::string& newprefdir)
DBG_FS << "using default userdata folder name";
} else {
temp = "Wesnoth" + get_version_path_suffix();
ERR_FS << "relative path for userdata that doesn't start with '.' or '..' is not allowed, using default userdata folder name";
DBG_FS << "relative path for userdata that doesn't start with '.' or '..' is not allowed, using default userdata folder name";
}
PWSTR docs_path = nullptr;
@ -759,8 +759,9 @@ static bfs::path apple_userdata(const std::string& newprefdir)
dir = temp;
DBG_FS << "userdata using absolute path";
} else {
dir = "." / temp;
ERR_FS << "unable to determine location to use for userdata, defaulting to current working directory";
dir = ".";
dir / temp;
DBG_FS << "unable to determine location to use for userdata, defaulting to current working directory";
}
return dir;
@ -816,7 +817,7 @@ static bfs::path linux_userdata(const std::string& newprefdir)
// unable to determine another userdata directory, so just use the current working directory for the userdata
dir = ".";
ERR_FS << "unable to determine location to use for userdata, defaulting to current working directory";
DBG_FS << "unable to determine location to use for userdata, defaulting to current working directory";
dir /= temp;
return dir;

View file

@ -188,7 +188,7 @@ game_launcher::game_launcher(const commandline_options& cmdline_opts)
}
if(cmdline_opts_.nogui || cmdline_opts_.headless_unit_test) {
no_sound = true;
prefs::get().disable_preferences_save();
prefs::disable_preferences_save();
}
if(cmdline_opts_.new_widgets)
gui2::new_widgets = true;
@ -211,7 +211,7 @@ game_launcher::game_launcher(const commandline_options& cmdline_opts)
screenshot_map_ = *cmdline_opts_.screenshot_map_file;
screenshot_filename_ = *cmdline_opts_.screenshot_output_file;
no_sound = true;
prefs::get().disable_preferences_save();
prefs::disable_preferences_save();
}
if (cmdline_opts_.server){
jump_to_multiplayer_ = true;
@ -227,10 +227,10 @@ game_launcher::game_launcher(const commandline_options& cmdline_opts)
}
}
if(cmdline_opts_.username) {
prefs::get().disable_preferences_save();
prefs::disable_preferences_save();
prefs::get().set_login(*cmdline_opts_.username);
if(cmdline_opts_.password) {
prefs::get().disable_preferences_save();
prefs::disable_preferences_save();
prefs::get().set_password(*cmdline_opts.server, *cmdline_opts.username, *cmdline_opts_.password);
}
}

View file

@ -69,7 +69,6 @@ static lg::log_domain advanced_preferences("advanced_preferences");
prefs::prefs()
: preferences_()
, no_preferences_save_(false)
, fps_(false)
, completed_campaigns_()
, encountered_units_set_()
@ -441,10 +440,6 @@ config::attribute_value prefs::get_as_attribute(const std::string &key)
//
// accessors
//
void prefs::disable_preferences_save() {
no_preferences_save_ = true;
}
bool prefs::show_ally_orb() {
return preferences_[prefs_list::show_ally_orb].to_bool(game_config::show_ally_orb);
}

View file

@ -204,9 +204,10 @@ class prefs
{
// for wesnoth: checks that the userdata folders have been initialized/found since if it hasn't been then it's too soon to be getting any values from it
// for boost: the userdata folders don't get initialized and the preferences aren't used for anything, so skip the check here
// TODO: figure out how to make this not fail for all the other tests too
static bool called_before_init = !(filesystem::base_name(filesystem::get_exe_path()).find("boost") == std::string::npos && !filesystem::is_userdata_initialized());
assert(called_before_init);
// macos - called "unit_tests"
// others - called "boost_unit_tests"
static bool called_before_init = !(filesystem::base_name(filesystem::get_exe_path()).find("unit_tests") == std::string::npos && !filesystem::is_userdata_initialized());
assert(called_before_init && "Attempt to use preferences before userdata initialization");
static prefs prefs_manager;
return prefs_manager;
@ -220,8 +221,6 @@ class prefs
void reload_preferences();
std::set<std::string> all_attributes();
void disable_preferences_save();
std::string core_id();
void set_core_id(const std::string& root);
@ -742,6 +741,14 @@ class prefs
std::vector<preferences::option>& get_advanced_preferences() {return advanced_prefs_;}
static void disable_preferences_save() {
no_preferences_save_ = true;
}
static bool preferences_save() {
return no_preferences_save_;
}
private:
prefs();
// don't move, assign, or copy a singleton
@ -750,8 +757,9 @@ class prefs
prefs(const prefs&& p) = delete;
prefs& operator=(const prefs&& p) = delete;
inline static bool no_preferences_save_ = false;
config preferences_;
bool no_preferences_save_;
bool fps_;
std::map<std::string, std::set<std::string>> completed_campaigns_;
std::set<std::string> encountered_units_set_;

View file

@ -309,6 +309,10 @@ static int process_command_args(const commandline_options& cmdline_opts)
return 0;
}
// earliest possible point to ensure the userdata directory is known
// if you're hitting the assertion in the preferences about the userdata not being initialized, it means you're trying to use the preferences before wesnoth knows where the preferences are
filesystem::get_user_data_dir();
if(cmdline_opts.data_dir) {
const std::string datadir = *cmdline_opts.data_dir;
PLAIN_LOG << "Starting with directory: '" << datadir << "'";