Skip to content

Commit

Permalink
Config: Correctly handle relative paths with portable
Browse files Browse the repository at this point in the history
It is desired that FEX_APP_CONFIG and FEX_APP_CONFIG_LOCATION support
relative paths when portable is used. Support this.
  • Loading branch information
Sonicadvance1 committed Feb 12, 2025
1 parent 73e7240 commit e3ee579
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
6 changes: 6 additions & 0 deletions FEXCore/Scripts/config_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ def print_man_environment_tail():
"Allows the user to override where FEX looks for configuration files",
"By default FEX will look in {$HOME, $XDG_CONFIG_HOME}/.fex-emu/",
"This will override the full path",
"If FEX_PORTABLE is declared then relative paths are also supported",
"For FEXInterpreter: Relative to the FEXInterpreter binary",
"For WINE: Relative to %LOCALAPPDATA%"
],
"''", True)

Expand All @@ -204,6 +207,9 @@ def print_man_environment_tail():
"This will override this file location",
"One must be careful with this option as it will override any applications that load with execve as well"
"If you need to support applications that execve then use FEX_APP_CONFIG_LOCATION instead"
"If FEX_PORTABLE is declared then relative paths are also supported",
"For FEXInterpreter: Relative to the FEXInterpreter binary",
"For WINE: Relative to %LOCALAPPDATA%"
],
"''", True)

Expand Down
24 changes: 19 additions & 5 deletions Source/Common/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class MainLoader final : public OptionMapper {
public:
explicit MainLoader(FEXCore::Config::LayerType Type);
explicit MainLoader(fextl::string ConfigFile);
explicit MainLoader(FEXCore::Config::LayerType Type, const char* ConfigFile);
explicit MainLoader(FEXCore::Config::LayerType Type, std::string_view ConfigFile);

void Load() override;

Expand Down Expand Up @@ -168,7 +168,7 @@ MainLoader::MainLoader(fextl::string ConfigFile)
, Config {std::move(ConfigFile)} {}


MainLoader::MainLoader(FEXCore::Config::LayerType Type, const char* ConfigFile)
MainLoader::MainLoader(FEXCore::Config::LayerType Type, std::string_view ConfigFile)
: OptionMapper(Type)
, Config {ConfigFile} {}

Expand Down Expand Up @@ -259,7 +259,7 @@ fextl::unique_ptr<FEXCore::Config::Layer> CreateMainLayer(const fextl::string* F
}
}

fextl::unique_ptr<FEXCore::Config::Layer> CreateUserOverrideLayer(const char* AppConfig) {
fextl::unique_ptr<FEXCore::Config::Layer> CreateUserOverrideLayer(std::string_view AppConfig) {
return fextl::make_unique<MainLoader>(FEXCore::Config::LayerType::LAYER_USER_OVERRIDE, AppConfig);
}

Expand Down Expand Up @@ -418,8 +418,15 @@ void LoadConfig(fextl::unique_ptr<FEX::ArgLoader::ArgLoader> ArgsLoader, fextl::
}

const char* AppConfig = getenv("FEX_APP_CONFIG");
if (AppConfig && FHU::Filesystem::Exists(AppConfig)) {
FEXCore::Config::AddLayer(CreateUserOverrideLayer(AppConfig));
if (AppConfig) {
fextl::string AppConfigStr = AppConfig;
if (IsPortable && FHU::Filesystem::IsRelative(AppConfig)) {
AppConfigStr = PortableInfo.InterpreterPath + AppConfigStr;
}

if (FHU::Filesystem::Exists(AppConfigStr)) {
FEXCore::Config::AddLayer(CreateUserOverrideLayer(AppConfigStr));
}
}

FEXCore::Config::AddLayer(CreateEnvironmentLayer(envp));
Expand Down Expand Up @@ -503,6 +510,13 @@ fextl::string GetConfigDirectory(bool Global, const PortableInformation& Portabl
const char* ConfigOverride = getenv("FEX_APP_CONFIG_LOCATION");
if (PortableInfo.IsPortable && (Global || !ConfigOverride)) {
return fextl::fmt::format("{}/fex-emu/", PortableInfo.InterpreterPath);
} else if (PortableInfo.IsPortable && ConfigOverride && !Global) {
fextl::string AppConfigStr = ConfigOverride;
if (PortableInfo.IsPortable && FHU::Filesystem::IsRelative(AppConfigStr)) {
AppConfigStr = PortableInfo.InterpreterPath + AppConfigStr;
}

return AppConfigStr;
}

fextl::string ConfigDir;
Expand Down
2 changes: 1 addition & 1 deletion Source/Common/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fextl::unique_ptr<FEXCore::Config::Layer> CreateGlobalMainLayer();
* @return unique_ptr for that layer
*/
fextl::unique_ptr<FEXCore::Config::Layer> CreateMainLayer(const fextl::string* File = nullptr);
fextl::unique_ptr<FEXCore::Config::Layer> CreateUserOverrideLayer(const char* AppConfig);
fextl::unique_ptr<FEXCore::Config::Layer> CreateUserOverrideLayer(std::string_view AppConfig);

/**
* @brief Create an application configuration loader
Expand Down

0 comments on commit e3ee579

Please sign in to comment.