Skip to content

Commit

Permalink
Merge pull request #32 from rgson/read-godot4-pcks
Browse files Browse the repository at this point in the history
Support reading of Godot4 .pck files
  • Loading branch information
hhyyrylainen authored Aug 5, 2023
2 parents 9ffd988 + d9800c1 commit 117ff02
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
23 changes: 21 additions & 2 deletions src/pck/PckFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,21 @@ bool PckFile::Load()
MinorGodotVersion = Read32();
PatchGodotVersion = Read32();

if(FormatVersion > MAX_SUPPORTED_PCK_VERSION) {
if(FormatVersion > MAX_SUPPORTED_PCK_VERSION_LOAD) {
std::cout << "ERROR: pck is unsupported version: " << FormatVersion << "\n";
return false;
}

if(FormatVersion == 2) {
Flags = Read32();
FileOffsetBase = Read64();
}

if(Flags & PACK_DIR_ENCRYPTED) {
std::cout << "ERROR: pck is encrypted\n";
return false;
}

// Reserved
for(int i = 0; i < 16; i++) {
Read32();
Expand All @@ -70,11 +80,15 @@ bool PckFile::Load()
while(!entry.Path.empty() && entry.Path.back() == '\0')
entry.Path.pop_back();

entry.Offset = Read64();
entry.Offset = FileOffsetBase + Read64();
entry.Size = Read64();

File->read(reinterpret_cast<char*>(entry.MD5.data()), sizeof(entry.MD5));

if(FormatVersion == 2) {
entry.Flags = Read32();
}

entry.GetData = [offset = entry.Offset, size = entry.Size, this]() {
return ReadContainedFileContents(offset, size);
};
Expand All @@ -98,6 +112,11 @@ bool PckFile::Load()
// ------------------------------------ //
bool PckFile::Save()
{
if(FormatVersion > MAX_SUPPORTED_PCK_VERSION_SAVE) {
std::cout << "ERROR: cannot save pck version: " << FormatVersion << "\n";
return false;
}

const auto tmpWrite = Path + ".write";

File = std::fstream(tmpWrite, std::ios::trunc | std::ios::out | std::ios::binary);
Expand Down
8 changes: 7 additions & 1 deletion src/pck/PckFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ namespace pcktool {

// Pck magic
constexpr uint32_t PCK_HEADER_MAGIC = 0x43504447;
constexpr uint32_t PACK_DIR_ENCRYPTED = 1;

// Highest pck version supported by this tool
constexpr int MAX_SUPPORTED_PCK_VERSION = 1;
constexpr int MAX_SUPPORTED_PCK_VERSION_LOAD = 2;
constexpr int MAX_SUPPORTED_PCK_VERSION_SAVE = 1;

//! \brief A single pck file object. Handles reading and writing
//!
Expand All @@ -28,6 +30,7 @@ class PckFile {
uint64_t Offset;
uint64_t Size;
std::array<uint8_t, 16> MD5 = {0};
uint32_t Flags = 0;

std::function<std::string()> GetData;
};
Expand Down Expand Up @@ -109,6 +112,9 @@ class PckFile {
uint32_t MinorGodotVersion = 0;
uint32_t PatchGodotVersion = 0;

uint32_t Flags = 0;
uint64_t FileOffsetBase = 0;

//! Add trailing null bytes to the length of a path until it is a multiple of this size
size_t PadPathsToMultipleWithNULLS = 4;

Expand Down

0 comments on commit 117ff02

Please sign in to comment.