From 08e2ec6c98342b401aa9e597609307cd4a9e619c Mon Sep 17 00:00:00 2001 From: Joshua Ashton Date: Mon, 18 Apr 2022 00:53:56 +0100 Subject: [PATCH] [util] Implement env helpers on non-Windows platforms --- src/util/util_env.cpp | 35 ++++++++++++++++++++++++++++++++++- src/util/util_env.h | 6 ++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/util/util_env.cpp b/src/util/util_env.cpp index 41b859bc..61f6326e 100644 --- a/src/util/util_env.cpp +++ b/src/util/util_env.cpp @@ -1,5 +1,13 @@ +#include +#include +#include #include +#ifdef __linux__ +#include +#include +#endif + #include "util_env.h" #include "./com/com_include.h" @@ -7,6 +15,7 @@ namespace dxvk::env { std::string getEnvVar(const char* name) { +#ifdef _WIN32 std::vector result; result.resize(MAX_PATH + 1); @@ -14,6 +23,10 @@ namespace dxvk::env { result.resize(len); return str::fromws(result.data()); +#else + const char* result = std::getenv(name); + return result ? result : ""; +#endif } @@ -36,7 +49,7 @@ namespace dxvk::env { std::string getExeName() { std::string fullPath = getExePath(); - auto n = fullPath.find_last_of('\\'); + auto n = fullPath.find_last_of(env::PlatformDirSlash); return (n != std::string::npos) ? fullPath.substr(n + 1) @@ -46,16 +59,19 @@ namespace dxvk::env { std::string getExeBaseName() { auto exeName = getExeName(); +#ifdef _WIN32 auto extp = matchFileExtension(exeName, "exe"); if (extp != std::string::npos) exeName.erase(extp); +#endif return exeName; } std::string getExePath() { +#if defined(_WIN32) std::vector exePath; exePath.resize(MAX_PATH + 1); @@ -63,10 +79,18 @@ namespace dxvk::env { exePath.resize(len); return str::fromws(exePath.data()); +#elif defined(__linux__) + std::array exePath = {}; + + size_t count = readlink("/proc/self/exe", exePath.data(), exePath.size()); + + return std::string(exePath.begin(), exePath.begin() + count); +#endif } void setThreadName(const std::string& name) { +#ifdef _WIN32 using SetThreadDescriptionProc = HRESULT (WINAPI *) (HANDLE, PCWSTR); static auto proc = reinterpret_cast( @@ -77,13 +101,22 @@ namespace dxvk::env { str::tows(name.c_str(), wideName.data(), wideName.size()); (*proc)(::GetCurrentThread(), wideName.data()); } +#else + std::array posixName = {}; + dxvk::str::strlcpy(posixName.data(), name.c_str(), 16); + ::pthread_setname_np(pthread_self(), posixName.data()); +#endif } bool createDirectory(const std::string& path) { +#ifdef _WIN32 WCHAR widePath[MAX_PATH]; str::tows(path.c_str(), widePath); return !!CreateDirectoryW(widePath, nullptr); +#else + return std::filesystem::create_directories(path); +#endif } } diff --git a/src/util/util_env.h b/src/util/util_env.h index cb4aa2a0..80efcbc9 100644 --- a/src/util/util_env.h +++ b/src/util/util_env.h @@ -4,6 +4,12 @@ namespace dxvk::env { +#ifdef _WIN32 + constexpr char PlatformDirSlash = '\\'; +#else + constexpr char PlatformDirSlash = '/'; +#endif + /** * \brief Checks whether the host platform is 32-bit */