Something went wrong. Try again.
OpenMW is an open implementation of the Morrowind engine
Something went wrong. Try again.
3.8 kB · 133 lines
C++
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134#include "linuxpath.hpp"
#if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__)
#include <array>#include <cstring>#include <pwd.h>#include <unistd.h>
#include <components/misc/strings/lower.hpp>
#include "wineutils.hpp"
namespace{ std::filesystem::path getUserHome() { const char* dir = getenv("HOME"); if (dir == nullptr) { struct passwd* pwd = getpwuid(getuid()); if (pwd != nullptr) { dir = pwd->pw_dir; } } if (dir == nullptr) return {}; else return dir; }
std::filesystem::path getEnv(const std::string& envVariable, const std::filesystem::path& fallback) { const char* result = getenv(envVariable.c_str()); if (!result) return fallback; std::filesystem::path dir(result); if (dir.empty()) return fallback; else return dir; }}
/** * \namespace Files */namespace Files{
LinuxPath::LinuxPath(const std::string& applicationName) : mName(applicationName) { }
std::filesystem::path LinuxPath::getUserConfigPath() const { return getEnv("XDG_CONFIG_HOME", getUserHome() / ".config") / mName; }
std::filesystem::path LinuxPath::getUserDataPath() const { return getEnv("XDG_DATA_HOME", getUserHome() / ".local/share") / mName; }
std::filesystem::path LinuxPath::getCachePath() const { return getEnv("XDG_CACHE_HOME", getUserHome() / ".cache") / mName; }
std::filesystem::path LinuxPath::getGlobalConfigPath() const { std::filesystem::path globalPath(GLOBAL_CONFIG_PATH); return globalPath / mName; }
std::filesystem::path LinuxPath::getLocalPath() const { auto localPath = std::filesystem::current_path() / "";
static const std::filesystem::path statusPaths[] = { "/proc/self/exe", "/proc/self/file", "/proc/curproc/exe", "/proc/curproc/file" };
for (const auto& path : statusPaths) { std::error_code ec; const auto binPath = read_symlink(path, ec); if (ec.value() != -1) { localPath = binPath.parent_path() / ""; break; } }
return localPath; }
std::filesystem::path LinuxPath::getGlobalDataPath() const { std::filesystem::path globalDataPath(GLOBAL_DATA_PATH); return globalDataPath / mName; }
std::vector<std::filesystem::path> LinuxPath::getInstallPaths() const { std::filesystem::path homePath = getUserHome(); if (homePath.empty()) return {};
std::vector<std::filesystem::path> paths(Wine::getInstallPaths(homePath)); std::array steamPaths{ // Default (~/.steam/steam can be a symlink or a real directory) homePath / ".steam/steam/steamapps/common/Morrowind", // Snap homePath / "snap/steam/common/.local/share/Steam/steamapps/common/Morrowind", // Flatpak homePath / ".var/app/com.valvesoftware.Steam/.local/share/Steam/steamapps/common/Morrowind", }; for (const std::filesystem::path& steam : steamPaths) { if (std::filesystem::is_directory(steam)) paths.emplace_back(steam); } std::ranges::sort(paths); const auto [first, last] = std::ranges::unique(paths); paths.erase(first, last); return paths; }
} /* namespace Files */
#endif /* defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) */