Skip to content

Commit

Permalink
#5108: Extend IArchiveFileInfoProvider interface. Add corresponding u…
Browse files Browse the repository at this point in the history
…nit tests.
  • Loading branch information
codereader committed Nov 20, 2020
1 parent 49b5033 commit c2d6487
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 1 deletion.
5 changes: 4 additions & 1 deletion include/iarchive.h
Expand Up @@ -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;
};

/**
Expand Down
8 changes: 8 additions & 0 deletions include/ifilesystem.h
Expand Up @@ -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
{
Expand Down
5 changes: 5 additions & 0 deletions radiantcore/vfs/DirectoryArchive.cpp
Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions radiantcore/vfs/DirectoryArchive.h
Expand Up @@ -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<DirectoryArchive> DirectoryArchivePtr;
5 changes: 5 additions & 0 deletions radiantcore/vfs/ZipArchive.cpp
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions radiantcore/vfs/ZipArchive.h
Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions test/VFS.cpp
Expand Up @@ -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)
Expand Down

0 comments on commit c2d6487

Please sign in to comment.