diff --git a/External/FEXCore/Source/CMakeLists.txt b/External/FEXCore/Source/CMakeLists.txt index d534350c6..b767f8252 100644 --- a/External/FEXCore/Source/CMakeLists.txt +++ b/External/FEXCore/Source/CMakeLists.txt @@ -1,7 +1,6 @@ set (MAN_DIR ${CMAKE_INSTALL_PREFIX}/share/man CACHE PATH "MAN_DIR") set (FEXCORE_BASE_SRCS - Common/Paths.cpp Interface/Config/Config.cpp Utils/Allocator.cpp Utils/Allocator/64BitAllocator.cpp diff --git a/External/FEXCore/Source/Common/Paths.cpp b/External/FEXCore/Source/Common/Paths.cpp deleted file mode 100644 index 5c648dc29..000000000 --- a/External/FEXCore/Source/Common/Paths.cpp +++ /dev/null @@ -1,87 +0,0 @@ -#include "Common/Paths.h" -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -namespace FEXCore::Paths { - fextl::string CachePath{}; - fextl::string EntryCache{}; - - char const* FindUserHomeThroughUID() { - auto passwd = getpwuid(geteuid()); - if (passwd) { - return passwd->pw_dir; - } - return nullptr; - } - - const char *GetHomeDirectory() { - char const *HomeDir = getenv("HOME"); - - // Try to get home directory from uid - if (!HomeDir) { - HomeDir = FindUserHomeThroughUID(); - } - - // try the PWD - if (!HomeDir) { - HomeDir = getenv("PWD"); - } - - // Still doesn't exit? You get local - if (!HomeDir) { - HomeDir = "."; - } - - return HomeDir; - } - - void InitializePaths() { - char const *HomeDir = getenv("HOME"); - - if (!HomeDir) { - HomeDir = getenv("PWD"); - } - - if (!HomeDir) { - HomeDir = "."; - } - - char *XDGDataDir = getenv("XDG_DATA_DIR"); - if (XDGDataDir) { - CachePath = XDGDataDir; - } - else { - if (HomeDir) { - CachePath = HomeDir; - } - } - - CachePath += "/.fex-emu/"; - EntryCache = CachePath + "/EntryCache/"; - - // Ensure the folder structure is created for our Data - if (!FHU::Filesystem::Exists(EntryCache) && - !FHU::Filesystem::CreateDirectories(EntryCache)) { - LogMan::Msg::DFmt("Couldn't create EntryCache directory: '{}'", EntryCache); - } - } - - fextl::string GetCachePath() { - return CachePath; - } - - fextl::string GetEntryCachePath() { - return EntryCache; - } -} diff --git a/External/FEXCore/Source/Common/Paths.h b/External/FEXCore/Source/Common/Paths.h deleted file mode 100644 index 3cf13592a..000000000 --- a/External/FEXCore/Source/Common/Paths.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once -#include - -namespace FEXCore::Paths { - void InitializePaths(); - void ShutdownPaths(); - - const char *GetHomeDirectory(); - - fextl::string GetCachePath(); - fextl::string GetEntryCachePath(); -} diff --git a/External/FEXCore/Source/Interface/Config/Config.cpp b/External/FEXCore/Source/Interface/Config/Config.cpp index 4ca104ed4..37c7eac91 100644 --- a/External/FEXCore/Source/Interface/Config/Config.cpp +++ b/External/FEXCore/Source/Interface/Config/Config.cpp @@ -1,6 +1,5 @@ #include "Common/StringConv.h" #include "Common/StringUtils.h" -#include "Common/Paths.h" #include #include @@ -109,71 +108,41 @@ namespace JSON { } } - fextl::string GetDataDirectory() { - fextl::string DataDir{}; + enum Paths { + PATH_DATA_DIR = 0, + PATH_CONFIG_DIR_LOCAL, + PATH_CONFIG_DIR_GLOBAL, + PATH_CONFIG_FILE_LOCAL, + PATH_CONFIG_FILE_GLOBAL, + PATH_LAST, + }; + static std::array Paths; - char const *HomeDir = Paths::GetHomeDirectory(); - char const *DataXDG = getenv("XDG_DATA_HOME"); - char const *DataOverride = getenv("FEX_APP_DATA_LOCATION"); - if (DataOverride) { - // Data override will override the complete directory - DataDir = DataOverride; - } - else { - DataDir = DataXDG ?: HomeDir; - DataDir += "/.fex-emu/"; - } - return DataDir; + void SetDataDirectory(const std::string_view Path) { + Paths[PATH_DATA_DIR] = Path; } - fextl::string GetConfigDirectory(bool Global) { - fextl::string ConfigDir; - if (Global) { - ConfigDir = GLOBAL_DATA_DIRECTORY; - } - else { - char const *HomeDir = Paths::GetHomeDirectory(); - char const *ConfigXDG = getenv("XDG_CONFIG_HOME"); - char const *ConfigOverride = getenv("FEX_APP_CONFIG_LOCATION"); - if (ConfigOverride) { - // Config override completely overrides the config directory - ConfigDir = ConfigOverride; - } - else { - ConfigDir = ConfigXDG ? ConfigXDG : HomeDir; - ConfigDir += "/.fex-emu/"; - } - - // Ensure the folder structure is created for our configuration - if (!FHU::Filesystem::Exists(ConfigDir) && - !FHU::Filesystem::CreateDirectories(ConfigDir)) { - // Let's go local in this case - return "./"; - } - } - - return ConfigDir; + void SetConfigDirectory(const std::string_view Path, bool Global) { + Paths[PATH_CONFIG_DIR_LOCAL + Global] = Path; } - fextl::string GetConfigFileLocation(bool Global) { - fextl::string ConfigFile{}; - if (Global) { - ConfigFile = GetConfigDirectory(true) + "Config.json"; - } - else { - const char *AppConfig = getenv("FEX_APP_CONFIG"); - if (AppConfig) { - // App config environment variable overwrites only the config file - ConfigFile = AppConfig; - } - else { - ConfigFile = GetConfigDirectory(false) + "Config.json"; - } - } - return ConfigFile; + void SetConfigFileLocation(const std::string_view Path, bool Global) { + Paths[PATH_CONFIG_FILE_LOCAL + Global] = Path; } - fextl::string GetApplicationConfig(const fextl::string &Filename, bool Global) { + fextl::string const& GetDataDirectory() { + return Paths[PATH_DATA_DIR]; + } + + fextl::string const& GetConfigDirectory(bool Global) { + return Paths[PATH_CONFIG_DIR_LOCAL + Global]; + } + + fextl::string const& GetConfigFileLocation(bool Global) { + return Paths[PATH_CONFIG_FILE_LOCAL + Global]; + } + + fextl::string GetApplicationConfig(const std::string_view Program, bool Global) { fextl::string ConfigFile = GetConfigDirectory(Global); if (!Global && @@ -181,7 +150,7 @@ namespace JSON { !FHU::Filesystem::CreateDirectories(ConfigFile)) { LogMan::Msg::DFmt("Couldn't create config directory: '{}'", ConfigFile); // Let's go local in this case - return "./" + Filename + ".json"; + return fextl::fmt::format("./{}.json", Program); } ConfigFile += "AppConfig/"; @@ -191,11 +160,10 @@ namespace JSON { !FHU::Filesystem::Exists(ConfigFile) && !FHU::Filesystem::CreateDirectories(ConfigFile)) { // Let's go local in this case - return "./" + Filename + ".json"; + return fextl::fmt::format("./{}.json", Program); } - ConfigFile += Filename + ".json"; - return ConfigFile; + return fextl::fmt::format("{}{}.json", ConfigFile, Program); } void SetConfig(FEXCore::Context::Context *CTX, ConfigOption Option, uint64_t Config) { diff --git a/External/FEXCore/Source/Interface/Context/Context.cpp b/External/FEXCore/Source/Interface/Context/Context.cpp index 425bef118..4ef6c4140 100644 --- a/External/FEXCore/Source/Interface/Context/Context.cpp +++ b/External/FEXCore/Source/Interface/Context/Context.cpp @@ -1,4 +1,3 @@ -#include "Common/Paths.h" #include "Interface/Context/Context.h" #include "Interface/Core/Core.h" #include "Interface/Core/OpcodeDispatcher.h" @@ -19,7 +18,6 @@ namespace FEXCore::HLE { namespace FEXCore::Context { void InitializeStaticTables(OperatingMode Mode) { - FEXCore::Paths::InitializePaths(); X86Tables::InitializeInfoTables(Mode); IR::InstallOpcodeHandlers(Mode); } diff --git a/External/FEXCore/include/FEXCore/Common/Paths.h b/External/FEXCore/include/FEXCore/Common/Paths.h deleted file mode 100644 index 85ccbe89b..000000000 --- a/External/FEXCore/include/FEXCore/Common/Paths.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once -#include -#include - -namespace FEXCore::Paths { - FEX_DEFAULT_VISIBILITY const char *GetHomeDirectory(); - - FEX_DEFAULT_VISIBILITY fextl::string GetCachePath(); - - FEX_DEFAULT_VISIBILITY fextl::string GetEntryCachePath(); -} diff --git a/External/FEXCore/include/FEXCore/Config/Config.h b/External/FEXCore/include/FEXCore/Config/Config.h index da665ece5..d259786d8 100644 --- a/External/FEXCore/include/FEXCore/Config/Config.h +++ b/External/FEXCore/include/FEXCore/Config/Config.h @@ -102,10 +102,14 @@ namespace Type { #undef P } - FEX_DEFAULT_VISIBILITY fextl::string GetDataDirectory(); - FEX_DEFAULT_VISIBILITY fextl::string GetConfigDirectory(bool Global); - FEX_DEFAULT_VISIBILITY fextl::string GetConfigFileLocation(bool Global = false); - FEX_DEFAULT_VISIBILITY fextl::string GetApplicationConfig(const fextl::string &Filename, bool Global); + FEX_DEFAULT_VISIBILITY void SetDataDirectory(std::string_view Path); + FEX_DEFAULT_VISIBILITY void SetConfigDirectory(const std::string_view Path, bool Global); + FEX_DEFAULT_VISIBILITY void SetConfigFileLocation(std::string_view Path, bool Global); + + FEX_DEFAULT_VISIBILITY fextl::string const& GetDataDirectory(); + FEX_DEFAULT_VISIBILITY fextl::string const& GetConfigDirectory(bool Global); + FEX_DEFAULT_VISIBILITY fextl::string const& GetConfigFileLocation(bool Global = false); + FEX_DEFAULT_VISIBILITY fextl::string GetApplicationConfig(const std::string_view Program, bool Global); using LayerValue = fextl::list; using LayerOptions = fextl::unordered_map; diff --git a/Source/Common/Config.cpp b/Source/Common/Config.cpp index dfad91ff4..17f79a5eb 100644 --- a/Source/Common/Config.cpp +++ b/Source/Common/Config.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -117,6 +118,7 @@ namespace FEX::Config { char **const envp, bool ExecFDInterp, const std::string_view ProgramFDFromEnv) { + FEX::Config::InitializeConfigs(); FEXCore::Config::Initialize(); FEXCore::Config::AddLayer(FEXCore::Config::CreateGlobalMainLayer()); FEXCore::Config::AddLayer(FEXCore::Config::CreateMainLayer()); @@ -197,4 +199,105 @@ namespace FEX::Config { } return {}; } + + char const* FindUserHomeThroughUID() { + auto passwd = getpwuid(geteuid()); + if (passwd) { + return passwd->pw_dir; + } + return nullptr; + } + + const char *GetHomeDirectory() { + char const *HomeDir = getenv("HOME"); + + // Try to get home directory from uid + if (!HomeDir) { + HomeDir = FindUserHomeThroughUID(); + } + + // try the PWD + if (!HomeDir) { + HomeDir = getenv("PWD"); + } + + // Still doesn't exit? You get local + if (!HomeDir) { + HomeDir = "."; + } + + return HomeDir; + } + + fextl::string GetDataDirectory() { + fextl::string DataDir{}; + + char const *HomeDir = GetHomeDirectory(); + char const *DataXDG = getenv("XDG_DATA_HOME"); + char const *DataOverride = getenv("FEX_APP_DATA_LOCATION"); + if (DataOverride) { + // Data override will override the complete directory + DataDir = DataOverride; + } + else { + DataDir = DataXDG ?: HomeDir; + DataDir += "/.fex-emu/"; + } + return DataDir; + } + + fextl::string GetConfigDirectory(bool Global) { + fextl::string ConfigDir; + if (Global) { + ConfigDir = GLOBAL_DATA_DIRECTORY; + } + else { + char const *HomeDir = GetHomeDirectory(); + char const *ConfigXDG = getenv("XDG_CONFIG_HOME"); + char const *ConfigOverride = getenv("FEX_APP_CONFIG_LOCATION"); + if (ConfigOverride) { + // Config override completely overrides the config directory + ConfigDir = ConfigOverride; + } + else { + ConfigDir = ConfigXDG ? ConfigXDG : HomeDir; + ConfigDir += "/.fex-emu/"; + } + + // Ensure the folder structure is created for our configuration + if (!FHU::Filesystem::Exists(ConfigDir) && + !FHU::Filesystem::CreateDirectories(ConfigDir)) { + // Let's go local in this case + return "./"; + } + } + + return ConfigDir; + } + + fextl::string GetConfigFileLocation(bool Global) { + fextl::string ConfigFile{}; + if (Global) { + ConfigFile = GetConfigDirectory(true) + "Config.json"; + } + else { + const char *AppConfig = getenv("FEX_APP_CONFIG"); + if (AppConfig) { + // App config environment variable overwrites only the config file + ConfigFile = AppConfig; + } + else { + ConfigFile = GetConfigDirectory(false) + "Config.json"; + } + } + return ConfigFile; + } + + void InitializeConfigs() { + FEXCore::Config::SetDataDirectory(GetDataDirectory()); + FEXCore::Config::SetConfigDirectory(GetConfigDirectory(false), false); + FEXCore::Config::SetConfigDirectory(GetConfigDirectory(true), true); + FEXCore::Config::SetConfigFileLocation(GetConfigFileLocation(false), false); + FEXCore::Config::SetConfigFileLocation(GetConfigFileLocation(true), true); + } } diff --git a/Source/Common/Config.h b/Source/Common/Config.h index 8cbefcf4e..6cae21d6b 100644 --- a/Source/Common/Config.h +++ b/Source/Common/Config.h @@ -47,4 +47,12 @@ namespace FEX::Config { bool ExecFDInterp, const std::string_view ProgramFDFromEnv ); + + const char *GetHomeDirectory(); + + fextl::string GetDataDirectory(); + fextl::string GetConfigDirectory(bool Global); + fextl::string GetConfigFileLocation(bool Global); + + void InitializeConfigs(); } diff --git a/Source/Tools/FEXConfig/Main.cpp b/Source/Tools/FEXConfig/Main.cpp index ba9c70cd5..0bcdb0bff 100644 --- a/Source/Tools/FEXConfig/Main.cpp +++ b/Source/Tools/FEXConfig/Main.cpp @@ -972,6 +972,8 @@ int main(int argc, char **argv) { GlobalTime = std::chrono::high_resolution_clock::now(); + FEX::Config::InitializeConfigs(); + // Attempt to open the config passed in if (argc > 1) { if (OpenFile(argv[1], true)) { diff --git a/Source/Tools/FEXLoader/FEXLoader.cpp b/Source/Tools/FEXLoader/FEXLoader.cpp index e92ae7c53..d59d3f7ad 100644 --- a/Source/Tools/FEXLoader/FEXLoader.cpp +++ b/Source/Tools/FEXLoader/FEXLoader.cpp @@ -8,6 +8,7 @@ $end_info$ #include "AOT/AOTGenerator.h" #include "Common/ArgumentLoader.h" #include "Common/FEXServerClient.h" +#include "Common/Config.h" #include "ELFCodeLoader.h" #include "VDSO_Emulation.h" #include "LinuxSyscalls/LinuxAllocator.h" diff --git a/Source/Tools/FEXLoader/LinuxSyscalls/CMakeLists.txt b/Source/Tools/FEXLoader/LinuxSyscalls/CMakeLists.txt index a34b3600e..65cd7a106 100644 --- a/Source/Tools/FEXLoader/LinuxSyscalls/CMakeLists.txt +++ b/Source/Tools/FEXLoader/LinuxSyscalls/CMakeLists.txt @@ -83,6 +83,7 @@ PRIVATE target_link_libraries(LinuxEmulation PRIVATE + Common FEXCore FEX_Utils ) diff --git a/Source/Tools/FEXLoader/LinuxSyscalls/FileManagement.cpp b/Source/Tools/FEXLoader/LinuxSyscalls/FileManagement.cpp index 8be9f1afe..e5325b9c6 100644 --- a/Source/Tools/FEXLoader/LinuxSyscalls/FileManagement.cpp +++ b/Source/Tools/FEXLoader/LinuxSyscalls/FileManagement.cpp @@ -5,6 +5,7 @@ desc: Rootfs overlay logic $end_info$ */ +#include "Common/Config.h" #include "Common/FDUtils.h" #include "FEXCore/Config/Config.h" @@ -13,7 +14,6 @@ $end_info$ #include "LinuxSyscalls/Syscalls.h" #include "LinuxSyscalls/x64/Syscalls.h" -#include #include #include #include @@ -116,7 +116,7 @@ static void LoadThunkDatabase(fextl::unordered_map return; } - std::string_view HomeDirectory = FEXCore::Paths::GetHomeDirectory(); + std::string_view HomeDirectory = FEX::Config::GetHomeDirectory(); for( json_t const* Library = json_getChild( DB ); Library != nullptr; Library = json_getSibling( Library )) { // Get the user defined name for the library