Skip to content

vtfpp: add support for xtf #71

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 0 additions & 2 deletions include/bsppp/EntityLump.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include <vector>

#include <BufferStream.h>
#include <sourcepp/parser/Text.h>
#include <sourcepp/String.h>

namespace bsppp {

Expand Down
37 changes: 37 additions & 0 deletions include/bsppp/PakLumpXbox.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include <vpkpp/format/XZP.h>

#include "PakLump.h"

namespace bsppp {

/// Utility class for vpkpp integration - you need to include this header in a C++ file
/// somewhere in your executable for automatic registration to work! Thanks C++
class PakLumpXbox : public vpkpp::XZP {
public:
~PakLumpXbox() override;

/// Open a BSP file
[[nodiscard]] static std::unique_ptr<PackFile> open(const std::string& path, const EntryCallback& callback = nullptr);

static constexpr inline std::string_view GUID = "E0B02EF72F044612B80648AEBB92451A";

[[nodiscard]] constexpr std::string_view getGUID() const override {
return PakLump::GUID;
}

[[nodiscard]] explicit operator std::string() const override;

protected:
explicit PakLumpXbox(const std::string& fullFilePath_);

const std::string tempPakLumpPath;
uint32_t version{};
uint32_t mapRevision{};

private:
VPKPP_REGISTER_PACKFILE_OPEN(BSP_EXTENSION, &PakLumpXbox::open);
};

} // namespace bsppp
2 changes: 2 additions & 0 deletions include/bsppp/bsppp.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
*/

#include "BSP.h"
#include "EntityLump.h"
#include "LumpData.h"
#include "PakLump.h"
#include "PakLumpXbox.h"
25 changes: 23 additions & 2 deletions include/vtfpp/ImageFormats.h
Original file line number Diff line number Diff line change
Expand Up @@ -723,16 +723,37 @@ namespace ImageFormatDetails {
if (i == mip && j == frame && k == face && l == slice) {
length = imageSize;
return true;
} else {
offset += imageSize;
}
offset += imageSize;
}
}
}
}
return false;
}

// XTF (PLATFORM_XBOX) is more like DDS layout
[[nodiscard]] inline bool getDataPositionXbox(uint32_t& offset, uint32_t& length, ImageFormat format, uint8_t mip, uint8_t mipCount, uint16_t frame, uint16_t frameCount, uint8_t face, uint8_t faceCount, uint16_t width, uint16_t height, uint16_t slice = 0, uint16_t sliceCount = 1) {
offset = 0;
length = 0;
for (int j = 0; j < frameCount; j++) {
for (int k = 0; k < faceCount; k++) {
for (int l = 0; l < sliceCount; l++) {
for (int i = 0; i < mipCount; i++) {
const auto imageSize = ImageFormatDetails::getDataLength(format, ImageDimensions::getMipDim(i, width), ImageDimensions::getMipDim(i, height));
if (i == mip && j == frame && k == face && l == slice) {
length = imageSize;
return true;
}
offset += imageSize;
}
}
}
offset += sourcepp::math::paddingForAlignment(512, offset);
}
return false;
}

} // namespace ImageFormatDetails

} // namespace vtfpp
2 changes: 2 additions & 0 deletions include/vtfpp/VTF.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace vtfpp {
constexpr uint32_t VTF_SIGNATURE = sourcepp::parser::binary::makeFourCC("VTF\0");
constexpr uint32_t VTFX_SIGNATURE = sourcepp::parser::binary::makeFourCC("VTFX");
constexpr uint32_t VTF3_SIGNATURE = sourcepp::parser::binary::makeFourCC("VTF3");
constexpr uint32_t XTF_SIGNATURE = sourcepp::parser::binary::makeFourCC("XTF\0");

enum class CompressionMethod : int16_t {
// Strata Source v7.6 defines
Expand Down Expand Up @@ -171,6 +172,7 @@ class VTF {
PLATFORM_PC = 0x001,
PLATFORM_PS3_PORTAL2 = 0x003,
PLATFORM_PS3_ORANGEBOX = 0x333,
PLATFORM_XBOX = 0x359,
PLATFORM_X360 = 0x360,
};

Expand Down
3 changes: 3 additions & 0 deletions src/bsppp/EntityLump.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include <bsppp/EntityLump.h>

#include <sourcepp/parser/Text.h>
#include <sourcepp/String.h>

using namespace bsppp;
using namespace sourcepp;

Expand Down
3 changes: 3 additions & 0 deletions src/bsppp/PakLump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ std::unique_ptr<PackFile> PakLump::open(const std::string& path, const EntryCall
// Extract the paklump to a temp file
FileStream writer{bsp->tempPakLumpPath, FileStream::OPT_TRUNCATE | FileStream::OPT_CREATE_IF_NONEXISTENT};
writer.write(*pakFileLump);
} else if (auto xzpLump = reader.getLumpData(BSPLump::XBOX_XZIPPAKFILE); xzpLump && bsp->version == 19) {
// Xbox pak lump, bail so other code can handle it
return nullptr;
} else {
// No paklump, create an empty zip
if (!ZIP::create(bsp->tempPakLumpPath)) {
Expand Down
66 changes: 66 additions & 0 deletions src/bsppp/PakLumpXbox.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <bsppp/PakLumpXbox.h>

#include <filesystem>

#include <bsppp/BSP.h>
#include <FileStream.h>
#include <sourcepp/String.h>
#include <vpkpp/format/XZP.h>

using namespace bsppp;
using namespace sourcepp;
using namespace vpkpp;

PakLumpXbox::PakLumpXbox(const std::string& fullFilePath_)
: XZP(fullFilePath_)
, tempPakLumpPath((std::filesystem::temp_directory_path() / (string::generateUUIDv4() + ".xzp")).string()) {}

PakLumpXbox::~PakLumpXbox() {
std::filesystem::remove(this->tempPakLumpPath);
}

std::unique_ptr<PackFile> PakLumpXbox::open(const std::string& path, const EntryCallback& callback) {
if (!std::filesystem::exists(path)) {
// File does not exist
return nullptr;
}

auto* bsp = new PakLumpXbox{path};
auto packFile = std::unique_ptr<PackFile>(bsp);

{
bsppp::BSP reader{path};
if (!reader) {
// File failed to load, or has an invalid signature
return nullptr;
}

bsp->version = reader.getVersion();
bsp->mapRevision = reader.getMapRevision();

if (auto xzpLump = reader.getLumpData(BSPLump::XBOX_XZIPPAKFILE); xzpLump && bsp->version == 19) {
// Extract the paklump to a temp file
FileStream writer{bsp->tempPakLumpPath, FileStream::OPT_TRUNCATE | FileStream::OPT_CREATE_IF_NONEXISTENT};
writer.write(*xzpLump);
} else {
// No Xbox paklump
return nullptr;
}
}

const auto xzp = XZP::open(bsp->tempPakLumpPath, [bsp, &callback](const std::string& entryPath, const Entry& entry) {
bsp->entries.emplace(entryPath, entry);

if (callback) {
callback(entryPath, entry);
}
});

return packFile;
}

PakLumpXbox::operator std::string() const {
return XZP::operator std::string() +
" | Version v" + std::to_string(this->version) +
" | Map Revision " + std::to_string(this->mapRevision);
}
4 changes: 3 additions & 1 deletion src/bsppp/_bsppp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ add_pretty_parser(bsppp
"${CMAKE_CURRENT_SOURCE_DIR}/include/bsppp/EntityLump.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/bsppp/LumpData.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/bsppp/PakLump.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/bsppp/PakLumpXbox.h"
SOURCES
"${CMAKE_CURRENT_LIST_DIR}/BSP.cpp"
"${CMAKE_CURRENT_LIST_DIR}/EntityLump.cpp"
"${CMAKE_CURRENT_LIST_DIR}/PakLump.cpp")
"${CMAKE_CURRENT_LIST_DIR}/PakLump.cpp"
"${CMAKE_CURRENT_LIST_DIR}/PakLumpXbox.cpp")
Loading
Loading