Skip to content

Commit 149a105

Browse files
committed
Fix loading OpenMW config from inside a Flatpak
HOST_XDG_CONFIG_HOME and HOST_XDG_DATA_HOME are only defined inside the Flatpak sandbox when XDG_CONFIG_HOME and XDG_DATA_HOME are defined (respectively) in the host environment, so if they're not defined then fall back to using $HOME/.config and $HOME/.local/share, as HOME is the same inside and outside the Flatpak.
1 parent 47055b6 commit 149a105

1 file changed

Lines changed: 39 additions & 12 deletions

File tree

src/openmw_config.rs

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,11 @@ fn default_user_config_dir(game_path: &Path) -> Result<PathBuf, Error> {
106106
// value.
107107
dirs::home_dir().map(|d| d.join(".var/app/org.openmw.OpenMW/config/openmw"))
108108
} else {
109-
// Checking $HOST_XDG_CONFIG_HOME first in case libloadorder is running
110-
// as part of a Flatpak app.
111-
std::env::var_os("HOST_XDG_CONFIG_HOME")
112-
.and_then(is_absolute_path)
113-
.or_else(dirs::config_dir)
114-
.map(|p| p.join("openmw"))
109+
xdg_home(
110+
"HOST_XDG_CONFIG_HOME",
111+
Path::new(".config"),
112+
dirs::config_dir,
113+
)
115114
}
116115
.ok_or_else(|| Error::NoUserConfigPath)
117116
}
@@ -122,16 +121,44 @@ fn default_user_data_dir(is_flatpak_install: bool) -> Result<PathBuf, Error> {
122121
if is_flatpak_install {
123122
dirs::home_dir().map(|d| d.join(".var/app/org.openmw.OpenMW/data/openmw"))
124123
} else {
125-
// Checking $HOST_XDG_DATA_HOME first in case libloadorder is
126-
// running as part of a Flatpak app.
127-
std::env::var_os("HOST_XDG_DATA_HOME")
128-
.and_then(is_absolute_path)
129-
.or_else(dirs::data_local_dir)
130-
.map(|p| p.join("openmw"))
124+
xdg_home(
125+
"HOST_XDG_DATA_HOME",
126+
Path::new(".local/share"),
127+
dirs::data_local_dir,
128+
)
131129
}
132130
.ok_or_else(|| Error::NoUserDataPath)
133131
}
134132

133+
/// Get the relevant XDG home directory, accounting for possibly running inside
134+
/// a Flatpak sandbox.
135+
///
136+
/// Check the given HOST_XDG_*_HOME env var first, which is set to the host's
137+
/// XDG_*_HOME value if that env var is set and this code is running inside a
138+
/// Flatpak sandbox.
139+
/// If running inside a Flatpak sandbox but XDG_*_HOME is not set on the host,
140+
/// use HOME and the given home path suffix to get the correct host path.
141+
/// Otherwise, just use the given getter.
142+
#[cfg(not(windows))]
143+
fn xdg_home(
144+
flatpak_host_env_var: &str,
145+
home_path_suffix: &Path,
146+
non_flatpak_getter: impl Fn() -> Option<PathBuf>,
147+
) -> Option<PathBuf> {
148+
std::env::var_os(flatpak_host_env_var)
149+
.and_then(is_absolute_path)
150+
.or_else(|| fallback_xdg_home(home_path_suffix))
151+
.or_else(non_flatpak_getter)
152+
.map(|p| p.join("openmw"))
153+
}
154+
155+
#[cfg(not(windows))]
156+
fn fallback_xdg_home(path_suffix: &Path) -> Option<PathBuf> {
157+
std::env::var_os("FLATPAK_ID")
158+
.and_then(|_| std::env::var_os("HOME"))
159+
.map(|s| Path::new(&s).join(path_suffix))
160+
}
161+
135162
#[expect(
136163
unsafe_code,
137164
reason = "There is currently no way to get this data safely"

0 commit comments

Comments
 (0)