linux-wallpaperengine/src/Steam/FileSystem/FileSystem.cpp
Alexis Maiquez 432814f956 hopefully added snap directories too (#166)
Signed-off-by: Alexis Maiquez <almamu@almamu.com>
2023-12-12 01:23:49 +01:00

72 lines
2.3 KiB
C++

#include "FileSystem.h"
#include "WallpaperEngine/Logging/CLog.h"
#include <climits>
#include <cstdlib>
#include <sstream>
#include <filesystem>
#include <sys/stat.h>
const char* assets_default_paths [] = {
".steam/steam/steamapps/common",
".local/share/Steam/steamapps/common",
".var/app/com.valvesoftware.Steam/.local/share/Steam/steamapps/common",
"snap/steam/common/.local/share/Steam/steamapps/common",
nullptr
};
const char* workshop_content_default_paths [] = {
".local/share/Steam/steamapps/workshop/content",
".steam/steam/steamapps/workshop/content",
".var/app/com.valvesoftware.Steam/.local/share/Steam/steamapps/workshop/content",
"snap/steam/common/.local/share/Steam/steamapps/workshop/content",
nullptr
};
std::filesystem::path detectHomepath ()
{
char* home = getenv ("HOME");
if (home == nullptr)
sLog.exception ("Cannot find home directory for the current user");
std::filesystem::path path = home;
if (!std::filesystem::is_directory (path))
sLog.exception ("Cannot find home directory for current user, ", home, " is not a directory");
return home;
}
std::filesystem::path Steam::FileSystem::workshopDirectory (int appID, const std::string& contentID)
{
auto homepath = detectHomepath ();
for (const char** current = workshop_content_default_paths; *current != nullptr; current ++)
{
auto currentpath = std::filesystem::path (homepath) / *current / std::to_string (appID) / contentID;
if (!std::filesystem::exists (currentpath) || !std::filesystem::is_directory (currentpath))
continue;
return currentpath;
}
sLog.exception ("Cannot find workshop directory for steam app ", appID, " and content ", contentID);
}
std::filesystem::path Steam::FileSystem::appDirectory (const std::string& appDirectory, const std::string& path)
{
auto homepath = detectHomepath ();
for (const char** current = assets_default_paths; *current != nullptr; current ++)
{
auto currentpath = std::filesystem::path (homepath) / *current / appDirectory / path;
if (!std::filesystem::exists (currentpath) || !std::filesystem::is_directory (currentpath))
continue;
return currentpath;
}
sLog.exception ("Cannot find directory for steam app ", appDirectory, ": ", path);
}