FileSystem.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "FileSystem.h"
  2. #include "WallpaperEngine/Logging/CLog.h"
  3. #include <climits>
  4. #include <cstdlib>
  5. #include <filesystem>
  6. #include <sstream>
  7. #include <sys/stat.h>
  8. const char* assets_default_paths [] = {".steam/steam/steamapps/common", ".local/share/Steam/steamapps/common",
  9. ".var/app/com.valvesoftware.Steam/.local/share/Steam/steamapps/common",
  10. "snap/steam/common/.local/share/Steam/steamapps/common", nullptr};
  11. const char* workshop_content_default_paths [] = {
  12. ".local/share/Steam/steamapps/workshop/content", ".steam/steam/steamapps/workshop/content",
  13. ".var/app/com.valvesoftware.Steam/.local/share/Steam/steamapps/workshop/content",
  14. "snap/steam/common/.local/share/Steam/steamapps/workshop/content", nullptr};
  15. std::filesystem::path detectHomepath () {
  16. char* home = getenv ("HOME");
  17. if (home == nullptr)
  18. sLog.exception ("Cannot find home directory for the current user");
  19. const std::filesystem::path path = home;
  20. if (!std::filesystem::is_directory (path))
  21. sLog.exception ("Cannot find home directory for current user, ", home, " is not a directory");
  22. return home;
  23. }
  24. std::filesystem::path Steam::FileSystem::workshopDirectory (int appID, const std::string& contentID) {
  25. auto homepath = detectHomepath ();
  26. for (const char** current = workshop_content_default_paths; *current != nullptr; current++) {
  27. auto currentpath = std::filesystem::path (homepath) / *current / std::to_string (appID) / contentID;
  28. if (!std::filesystem::exists (currentpath) || !std::filesystem::is_directory (currentpath))
  29. continue;
  30. return currentpath;
  31. }
  32. sLog.exception ("Cannot find workshop directory for steam app ", appID, " and content ", contentID);
  33. }
  34. std::filesystem::path Steam::FileSystem::appDirectory (const std::string& appDirectory, const std::string& path) {
  35. auto homepath = detectHomepath ();
  36. for (const char** current = assets_default_paths; *current != nullptr; current++) {
  37. auto currentpath = std::filesystem::path (homepath) / *current / appDirectory / path;
  38. if (!std::filesystem::exists (currentpath) || !std::filesystem::is_directory (currentpath))
  39. continue;
  40. return currentpath;
  41. }
  42. sLog.exception ("Cannot find directory for steam app ", appDirectory, ": ", path);
  43. }