Skip to content

vpkpp: add support for Volition VPP version 4 #69

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion include/vpkpp/format/VPP.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class VPP : public PackFileReadOnly {
enum Flags : uint32_t {
FLAG_NONE = 0,
FLAG_COMPRESSED = 1 << 0,
FLAG_CONDENSED = 1 << 1
FLAG_CONDENSED = 1 << 1,
FLAG_UNK5 = 1 << 5, // v4.vpp_xbox2
};

/// Open a VPP file
Expand Down
70 changes: 56 additions & 14 deletions src/vpkpp/format/VPP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ std::unique_ptr<PackFile> VPP::open(const std::string& path, const EntryCallback

// Get file table offset
static constexpr uint32_t headerSize = sizeof(uint32_t) * 4;
reader.seek_in(headerSize + sourcepp::math::paddingForAlignment(VPP_ALIGNMENT, headerSize));
reader.seek_in(headerSize + math::paddingForAlignment(VPP_ALIGNMENT, headerSize));

// Get base file offset
const uint32_t fileTableSize = (60 + sizeof(uint32_t)) * entryCount;
vpp->entryBaseOffset = headerSize + sourcepp::math::paddingForAlignment(VPP_ALIGNMENT, headerSize)
+ fileTableSize + sourcepp::math::paddingForAlignment(VPP_ALIGNMENT, fileTableSize);
vpp->entryBaseOffset = headerSize + math::paddingForAlignment(VPP_ALIGNMENT, headerSize)
+ fileTableSize + math::paddingForAlignment(VPP_ALIGNMENT, fileTableSize);

// Get first file offset
uint32_t entryOffset = 0;
Expand All @@ -63,7 +63,7 @@ std::unique_ptr<PackFile> VPP::open(const std::string& path, const EntryCallback

// Calculate file offset
entry.offset = entryOffset;
entryOffset += entry.length + sourcepp::math::paddingForAlignment(VPP_ALIGNMENT, entry.length);
entryOffset += entry.length + math::paddingForAlignment(VPP_ALIGNMENT, entry.length);

// Put it in
vpp->entries.emplace(entryPath, entry);
Expand All @@ -83,12 +83,12 @@ std::unique_ptr<PackFile> VPP::open(const std::string& path, const EntryCallback

// Get file table offset
static constexpr uint32_t headerSize = sizeof(uint32_t) * 4;
reader.seek_in(headerSize + sourcepp::math::paddingForAlignment(VPP_ALIGNMENT, headerSize));
reader.seek_in(headerSize + math::paddingForAlignment(VPP_ALIGNMENT, headerSize));

// Get base file offset
const uint32_t fileTableSize = (24 + sizeof(uint32_t) * 2) * entryCount;
vpp->entryBaseOffset = headerSize + sourcepp::math::paddingForAlignment(VPP_ALIGNMENT, headerSize)
+ fileTableSize + sourcepp::math::paddingForAlignment(VPP_ALIGNMENT, fileTableSize);
vpp->entryBaseOffset = headerSize + math::paddingForAlignment(VPP_ALIGNMENT, headerSize)
+ fileTableSize + math::paddingForAlignment(VPP_ALIGNMENT, fileTableSize);

// Get first file offset
uint32_t entryOffset = 0;
Expand All @@ -112,7 +112,7 @@ std::unique_ptr<PackFile> VPP::open(const std::string& path, const EntryCallback

// Calculate file offset
entry.offset = entryOffset;
entryOffset += entry.length + sourcepp::math::paddingForAlignment(VPP_ALIGNMENT, entry.length);
entryOffset += entry.length + math::paddingForAlignment(VPP_ALIGNMENT, entry.length);

// Put it in
vpp->entries.emplace(entryPath, entry);
Expand All @@ -121,7 +121,7 @@ std::unique_ptr<PackFile> VPP::open(const std::string& path, const EntryCallback
callback(entryPath, entry);
}
}
} else if (version == 3) {
} else if (version == 3 || version == 4) {
// Skip unused header data
reader.skip_in(64 + 256 + sizeof(uint32_t));

Expand All @@ -147,15 +147,24 @@ std::unique_ptr<PackFile> VPP::open(const std::string& path, const EntryCallback
// Get sizes
const auto entryDirectorySize = reader.read<uint32_t>();
const auto entryNamesSize = reader.read<uint32_t>();
uint32_t entryExtensionsSize = 0;

if (version == 4) {
entryExtensionsSize = reader.read<uint32_t>();
}

// Check if we have compression
const auto entryDataSizeUncompressed = reader.read<uint32_t>();
const auto entryDataSizeCompressed = reader.read<uint32_t>();

// Set base data offset
vpp->entryBaseOffset = VPP_ALIGNMENT
+ entryDirectorySize + sourcepp::math::paddingForAlignment(VPP_ALIGNMENT, entryDirectorySize)
+ entryNamesSize + sourcepp::math::paddingForAlignment(VPP_ALIGNMENT, entryNamesSize);
+ entryDirectorySize + math::paddingForAlignment(VPP_ALIGNMENT, entryDirectorySize)
+ entryNamesSize + math::paddingForAlignment(VPP_ALIGNMENT, entryNamesSize);

if (version == 4) {
vpp->entryBaseOffset += entryExtensionsSize + math::paddingForAlignment(VPP_ALIGNMENT, entryExtensionsSize);
}

// Seek to file directory (alignment boundary)
reader.seek_in(VPP_ALIGNMENT);
Expand All @@ -166,6 +175,10 @@ std::unique_ptr<PackFile> VPP::open(const std::string& path, const EntryCallback

// Get file name offset
const auto entryNameOffset = reader.read<uint32_t>();
uint32_t entryExtensionOffset = 0;
if (version == 4) {
entryExtensionOffset = reader.read<uint32_t>();
}

// ??
reader.skip_in<uint32_t>();
Expand All @@ -174,7 +187,9 @@ std::unique_ptr<PackFile> VPP::open(const std::string& path, const EntryCallback
entry.offset = reader.read<uint32_t>();

// ??
reader.skip_in<uint32_t>();
if (version == 3) {
reader.skip_in<uint32_t>();
}

// Get file size
entry.length = reader.read<uint32_t>();
Expand All @@ -190,8 +205,35 @@ std::unique_ptr<PackFile> VPP::open(const std::string& path, const EntryCallback

// Get file name
const auto lastPos = reader.tell_in();
reader.seek_in(VPP_ALIGNMENT + entryDirectorySize + sourcepp::math::paddingForAlignment(VPP_ALIGNMENT, entryDirectorySize) + entryNameOffset);
const auto entryPath = vpp->cleanEntryPath(reader.read_string(entryNamesSize - entryNameOffset));

std::string entryPath;

if (version == 4) {
// Version 4
reader.seek_in(
VPP_ALIGNMENT +
entryDirectorySize + math::paddingForAlignment(VPP_ALIGNMENT, entryDirectorySize) +
entryNameOffset
);

std::string entryName = reader.read_string(entryNamesSize - entryNameOffset);

reader.seek_in(
VPP_ALIGNMENT +
entryDirectorySize + math::paddingForAlignment(VPP_ALIGNMENT, entryDirectorySize) +
entryNamesSize + math::paddingForAlignment(VPP_ALIGNMENT, entryNamesSize) +
entryExtensionOffset
);

std::string entryExtension = reader.read_string(entryExtensionsSize - entryExtensionOffset);

entryPath = vpp->cleanEntryPath(entryName + "." + entryExtension);
} else {
// Version 3
reader.seek_in(VPP_ALIGNMENT + entryDirectorySize + math::paddingForAlignment(VPP_ALIGNMENT, entryDirectorySize) + entryNameOffset);
entryPath = vpp->cleanEntryPath(reader.read_string(entryNamesSize - entryNameOffset));
}

reader.seek_in_u(lastPos);

// Put it in
Expand Down
16 changes: 16 additions & 0 deletions test/vpkpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,19 @@ TEST(vpkpp, vpp_v3_big_read) {
EXPECT_EQ(vpp->getEntryCount(), 20);
EXPECT_TRUE(vpp->hasEntry("pretty.txt"));
}

TEST(vpkpp, vpp_v4_lil_read) {
const auto vpp = PackFile::open(ASSET_ROOT "vpkpp/vpp/v4.vpp_pc");
ASSERT_TRUE(vpp);
VPKPP_PRINT_ALL_PATHS(vpp);
EXPECT_EQ(vpp->getEntryCount(), 730);
EXPECT_TRUE(vpp->hasEntry("sr2_skybox.hmap_pc"));
}

TEST(vpkpp, vpp_v4_big_read) {
const auto vpp = PackFile::open(ASSET_ROOT "vpkpp/vpp/v4.vpp_xbox2");
ASSERT_TRUE(vpp);
VPKPP_PRINT_ALL_PATHS(vpp);
EXPECT_EQ(vpp->getEntryCount(), 118);
EXPECT_TRUE(vpp->hasEntry("activity_level.lua"));
}
Loading