Skip to content

Commit

Permalink
ensure the userdata is initialized before trying to use the preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
Pentarctagon committed Jun 20, 2024
1 parent cb71759 commit 1a1f362
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
9 changes: 5 additions & 4 deletions src/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/game_launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
}
}
Expand Down
5 changes: 0 additions & 5 deletions src/preferences/preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_()
Expand Down Expand Up @@ -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);
}
Expand Down
15 changes: 11 additions & 4 deletions src/preferences/preferences.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class prefs
// 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);
assert(called_before_init && "Attempt to use preferences before userdata initialization");

static prefs prefs_manager;
return prefs_manager;
Expand All @@ -220,8 +220,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);

Expand Down Expand Up @@ -742,6 +740,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
Expand All @@ -750,8 +756,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_;
Expand Down
4 changes: 4 additions & 0 deletions src/wesnoth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 << "'";
Expand Down

0 comments on commit 1a1f362

Please sign in to comment.