Package.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include <fstream>
  2. #include <memory>
  3. #include "Package.h"
  4. #include "WallpaperEngine/Assets/AssetLoadException.h"
  5. #include "WallpaperEngine/Data/Parsers/PackageParser.h"
  6. #include "WallpaperEngine/Data/Utils/BinaryReader.h"
  7. #include "WallpaperEngine/Data/Utils/MemoryStream.h"
  8. #include <algorithm>
  9. #include <cctype>
  10. using namespace WallpaperEngine::FileSystem;
  11. using namespace WallpaperEngine::FileSystem::Adapters;
  12. // scenes are authored on Windows, so fall back to a case-insensitive lookup when the exact path is missing
  13. static bool equalsIgnoreCase (const std::string& a, const std::string& b) {
  14. return std::ranges::equal (a, b, [] (unsigned char x, unsigned char y) {
  15. return std::tolower (x) == std::tolower (y);
  16. });
  17. }
  18. template <typename Files> static auto findFile (const Files& files, const std::filesystem::path& path) {
  19. const auto wanted = path.string ();
  20. const auto exact = std::ranges::find_if (files, [&wanted] (const auto& file) { return file->filename == wanted; });
  21. if (exact != files.end ()) {
  22. return exact;
  23. }
  24. return std::ranges::find_if (files, [&wanted] (const auto& file) {
  25. return equalsIgnoreCase (file->filename, wanted);
  26. });
  27. }
  28. ReadStreamSharedPtr PackageAdapter::open (const std::filesystem::path& path) const {
  29. const auto it = findFile (this->package->files, path);
  30. if (it == this->package->files.end ()) {
  31. throw std::filesystem::filesystem_error ("Cannot find file", path, std::error_code ());
  32. }
  33. const uint64_t start = static_cast<uint64_t> (it->get ()->offset) + this->package->baseOffset;
  34. // the stream is shared by every file of the package, nothing an earlier read left behind may carry over
  35. this->package->file->base ().clear ();
  36. // an entry that runs past the end (corrupt or truncated package) must not allocate its claimed size
  37. if (start + it->get ()->length > this->package->file->size ()) {
  38. throw std::filesystem::filesystem_error (
  39. "Package entry runs past the end of the package", path,
  40. std::make_error_code (std::errc::result_out_of_range)
  41. );
  42. }
  43. auto buffer = std::make_unique<char[]> (it->get ()->length);
  44. this->package->file->base ().seekg (static_cast<std::streamoff> (start), std::ios::beg);
  45. this->package->file->next (buffer.get (), it->get ()->length);
  46. return std::make_shared<MemoryStream> (std::move (buffer), it->get ()->length);
  47. }
  48. bool PackageAdapter::exists (const std::filesystem::path& path) const {
  49. return findFile (this->package->files, path) != this->package->files.end ();
  50. }
  51. std::filesystem::path PackageAdapter::physicalPath (const std::filesystem::path& path) const {
  52. throw std::filesystem::filesystem_error ("Package adapter does not support realpath", path, std::error_code ());
  53. }
  54. std::optional<std::filesystem::path>
  55. PackageAdapter::resolveWorkshopDependencyAlias (const std::filesystem::path& path) const {
  56. std::optional<std::filesystem::path> found = std::nullopt;
  57. for (const auto& file : this->package->files) {
  58. if (!isWorkshopDependencyAliasOf (file->filename, path)) {
  59. continue;
  60. }
  61. if (found.has_value ()) {
  62. // more than one dependency ships a same-named file under this path, refuse to guess
  63. return std::nullopt;
  64. }
  65. found = file->filename;
  66. }
  67. return found;
  68. }
  69. std::vector<std::string> PackageAdapter::listFiles (const std::filesystem::path& dir) const {
  70. std::vector<std::string> names;
  71. for (const auto& file : this->package->files) {
  72. const std::filesystem::path path = file->filename;
  73. if (equalsIgnoreCase (path.parent_path ().generic_string (), dir.generic_string ())) {
  74. names.push_back (path.filename ().string ());
  75. }
  76. }
  77. return names;
  78. }
  79. bool PackageFactory::handlesMountpoint (const std::filesystem::path& path) const {
  80. try {
  81. const auto finalpath = std::filesystem::canonical (path);
  82. const auto status = std::filesystem::status (finalpath);
  83. return std::filesystem::exists (finalpath) && std::filesystem::is_regular_file (status)
  84. && finalpath.extension () == ".pkg";
  85. } catch (std::filesystem::filesystem_error&) {
  86. return false;
  87. }
  88. }
  89. AdapterSharedPtr PackageFactory::create (const std::filesystem::path& path) const {
  90. const auto stream = std::make_shared<std::ifstream> (path, std::ios::binary);
  91. auto package = Data::Parsers::PackageParser::parse (stream);
  92. return std::make_unique<PackageAdapter> (std::move (package));
  93. }