Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support reading of Godot4 .pck files #32

Merged
merged 2 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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