Skip to content

Commit

Permalink
feat(vpkpp): support latest PCK flag addition, removing files without…
Browse files Browse the repository at this point in the history
… removing their contents
  • Loading branch information
craftablescience committed Nov 21, 2024
1 parent 9b44cc1 commit 206e571
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
16 changes: 11 additions & 5 deletions include/vpkpp/format/PCK.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,24 @@ constexpr std::string_view PCK_EXTENSION = ".pck";

class PCK : public PackFile {
protected:
enum FlagsV2 : uint32_t {
FLAG_NONE = 0,
FLAG_ENCRYPTED = 1 << 0,
FLAG_RELATIVE_FILE_DATA = 1 << 1,
enum FlagsDirV2 : uint32_t {
FLAG_DIR_NONE = 0,
FLAG_DIR_ENCRYPTED = 1 << 0,
FLAG_DIR_RELATIVE_FILE_DATA = 1 << 1,
};

enum FlagsFileV2 : uint32_t {
FLAG_FILE_NONE = 0,
FLAG_FILE_ENCRYPTED = 1 << 0,
FLAG_FILE_REMOVED = 1 << 1,
};

struct Header {
uint32_t packVersion;
uint32_t godotVersionMajor;
uint32_t godotVersionMinor;
uint32_t godotVersionPatch;
FlagsV2 flags; // packVersion >= 2
FlagsDirV2 flags; // packVersion >= 2
};

public:
Expand Down
19 changes: 11 additions & 8 deletions src/vpkpp/format/PCK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ std::unique_ptr<PackFile> PCK::create(const std::string& path, uint32_t version,

if (version > 1) {
stream
.write(FlagsV2::FLAG_NONE)
.write(FLAG_DIR_NONE)
.write<uint64_t>(0);
}

Expand Down Expand Up @@ -80,20 +80,20 @@ std::unique_ptr<PackFile> PCK::open(const std::string& path, const EntryCallback
reader.read(pck->header.godotVersionMinor);
reader.read(pck->header.godotVersionPatch);

pck->header.flags = FLAG_NONE;
pck->header.flags = FLAG_DIR_NONE;
std::size_t extraEntryContentsOffset = 0;
if (pck->header.packVersion > 1) {
pck->header.flags = reader.read<FlagsV2>();
pck->header.flags = reader.read<FlagsDirV2>();
extraEntryContentsOffset = reader.read<uint64_t>();
}

if (pck->header.flags & FLAG_ENCRYPTED) {
if (pck->header.flags & FLAG_DIR_ENCRYPTED) {
// File directory is encrypted
return nullptr;
}
if (pck->header.flags & FLAG_RELATIVE_FILE_DATA) {
if (pck->header.flags & FLAG_DIR_RELATIVE_FILE_DATA) {
extraEntryContentsOffset += pck->startOffset;
pck->header.flags = static_cast<FlagsV2>(pck->header.flags & ~FLAG_RELATIVE_FILE_DATA);
pck->header.flags = static_cast<FlagsDirV2>(pck->header.flags & ~FLAG_DIR_RELATIVE_FILE_DATA);
}

// Reserved
Expand All @@ -115,6 +115,9 @@ std::unique_ptr<PackFile> PCK::open(const std::string& path, const EntryCallback

if (pck->header.packVersion > 1) {
entry.flags = reader.read<uint32_t>();
if (entry.flags & FLAG_FILE_REMOVED) {
continue;
}
}

pck->entries.emplace(entryPath, entry);
Expand All @@ -141,7 +144,7 @@ std::optional<std::vector<std::byte>> PCK::readEntry(const std::string& path_) c
}

// It's baked into the file on disk
if (entry->flags & FLAG_ENCRYPTED) {
if (entry->flags & FLAG_FILE_ENCRYPTED) {
// File is encrypted
return std::nullopt;
}
Expand Down Expand Up @@ -297,7 +300,7 @@ PCK::operator std::string() const {
if (this->startOffset > 0) {
out += " | Embedded";
}
if (this->header.flags & FLAG_ENCRYPTED) {
if (this->header.flags & FLAG_DIR_ENCRYPTED) {
out += " | Encrypted";
}
return out;
Expand Down

0 comments on commit 206e571

Please sign in to comment.