From c2d64874dfeb76ed572c0bb964afa236e56f0b70 Mon Sep 17 00:00:00 2001 From: codereader Date: Fri, 20 Nov 2020 13:11:34 +0100 Subject: [PATCH] #5108: Extend IArchiveFileInfoProvider interface. Add corresponding unit tests. --- include/iarchive.h | 5 ++++- include/ifilesystem.h | 8 ++++++++ radiantcore/vfs/DirectoryArchive.cpp | 5 +++++ radiantcore/vfs/DirectoryArchive.h | 1 + radiantcore/vfs/ZipArchive.cpp | 5 +++++ radiantcore/vfs/ZipArchive.h | 1 + test/VFS.cpp | 6 ++++++ 7 files changed, 30 insertions(+), 1 deletion(-) diff --git a/include/iarchive.h b/include/iarchive.h index 3384f60e0f..e9dfd240ec 100644 --- a/include/iarchive.h +++ b/include/iarchive.h @@ -25,11 +25,14 @@ class IArchiveFileInfoProvider public: virtual ~IArchiveFileInfoProvider() {} - // Get file size of the file given by the relative path (like "def/func.def") + // Get file size of the file given by the relative path (like "def/func.def") in bytes virtual std::size_t getFileSize(const std::string& relativePath) = 0; // Returns true if this file is an actual file on disk (as opposed to a file in a PAK) virtual bool getIsPhysical(const std::string& relativePath) = 0; + + // Returns the absolute file system path to the archive the given file is located in + virtual std::string getArchivePath(const std::string& relativePath) = 0; }; /** diff --git a/include/ifilesystem.h b/include/ifilesystem.h index f9b8b1a279..726bbe610b 100644 --- a/include/ifilesystem.h +++ b/include/ifilesystem.h @@ -99,16 +99,24 @@ class FileInfo return topDir + (topDir.back() == '/' ? "" : "/") + name; } + // See IArchiveFileInfoProvider::getFileSize std::size_t getSize() const { return _infoProvider ? _infoProvider->getFileSize(fullPath()) : 0; } + // See IArchiveFileInfoProvider::getIsPhysicalFile bool getIsPhysicalFile() const { return _infoProvider ? _infoProvider->getIsPhysical(fullPath()) : false; } + // See IArchiveFileInfoProvider::getArchivePath + std::string getArchivePath() const + { + return _infoProvider ? _infoProvider->getArchivePath(fullPath()) : ""; + } + /// Equality comparison with another FileInfo bool operator== (const FileInfo& rhs) const { diff --git a/radiantcore/vfs/DirectoryArchive.cpp b/radiantcore/vfs/DirectoryArchive.cpp index 191262a445..959ffecf13 100644 --- a/radiantcore/vfs/DirectoryArchive.cpp +++ b/radiantcore/vfs/DirectoryArchive.cpp @@ -110,3 +110,8 @@ bool DirectoryArchive::getIsPhysical(const std::string& relativePath) // this whole class represents a physical directory, we don't even check return true; } + +std::string DirectoryArchive::getArchivePath(const std::string& relativePath) +{ + return _root; +} \ No newline at end of file diff --git a/radiantcore/vfs/DirectoryArchive.h b/radiantcore/vfs/DirectoryArchive.h index f8f8397769..c99bdeb1c5 100644 --- a/radiantcore/vfs/DirectoryArchive.h +++ b/radiantcore/vfs/DirectoryArchive.h @@ -31,5 +31,6 @@ class DirectoryArchive final : std::size_t getFileSize(const std::string& relativePath) override; bool getIsPhysical(const std::string& relativePath) override; + std::string getArchivePath(const std::string& relativePath) override; }; typedef std::shared_ptr DirectoryArchivePtr; diff --git a/radiantcore/vfs/ZipArchive.cpp b/radiantcore/vfs/ZipArchive.cpp index 3050ee3080..6a7ee5eb6b 100644 --- a/radiantcore/vfs/ZipArchive.cpp +++ b/radiantcore/vfs/ZipArchive.cpp @@ -158,6 +158,11 @@ bool ZipArchive::getIsPhysical(const std::string& relativePath) return false; } +std::string ZipArchive::getArchivePath(const std::string& relativePath) +{ + return _fullPath; +} + void ZipArchive::readZipRecord() { ZipMagic magic; diff --git a/radiantcore/vfs/ZipArchive.h b/radiantcore/vfs/ZipArchive.h index 73835254ca..ffb5eabceb 100644 --- a/radiantcore/vfs/ZipArchive.h +++ b/radiantcore/vfs/ZipArchive.h @@ -67,6 +67,7 @@ class ZipArchive final : std::size_t getFileSize(const std::string& relativePath) override; bool getIsPhysical(const std::string& relativePath) override; + std::string getArchivePath(const std::string& relativePath) override; private: void readZipRecord(); diff --git a/test/VFS.cpp b/test/VFS.cpp index 83db0df534..00d7fbb679 100644 --- a/test/VFS.cpp +++ b/test/VFS.cpp @@ -62,11 +62,17 @@ TEST_F(VfsTest, GetArchiveFileInfo) fs::path physicalFilePath = _context.getTestResourcePath(); physicalFilePath /= physicalFile; + fs::path pk4Path = _context.getTestResourcePath(); + pk4Path /= "tdm_example_mtrs.pk4"; + EXPECT_EQ(foundFiles.find(physicalFile)->second.getSize(), os::getFileSize(physicalFilePath.string())); EXPECT_EQ(foundFiles.find(physicalFile)->second.getIsPhysicalFile(), true); + EXPECT_EQ(foundFiles.find(physicalFile)->second.getArchivePath(), _context.getTestResourcePath()); EXPECT_EQ(foundFiles.find(fileInPak)->second.getSize(), 1096); // that file should have 1096 bytes EXPECT_EQ(foundFiles.find(fileInPak)->second.getIsPhysicalFile(), false); + // The PK4 file is located right in the test resources folder + EXPECT_EQ(foundFiles.find(fileInPak)->second.getArchivePath(), pk4Path.string()); } TEST_F(VfsTest, VisitMaterialsFolderOnly)