Skip to content

Commit

Permalink
feat(bsppp): add BSP editing library, use as the backend of the BSP f…
Browse files Browse the repository at this point in the history
…ormat in vpkpp
  • Loading branch information
craftablescience committed Jul 17, 2024
1 parent 279aebf commit efbde2e
Show file tree
Hide file tree
Showing 8 changed files with 303 additions and 212 deletions.
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)


# Options
option(SOURCEPP_USE_BSPPP "Build bsppp library" ON)
option(SOURCEPP_USE_DMXPP "Build dmxpp library" ON)
option(SOURCEPP_USE_FGDPP "Build fgdpp library" ON)
option(SOURCEPP_USE_KVPP "Build kvpp library" ON)
Expand All @@ -28,7 +29,11 @@ option(SOURCEPP_USE_STATIC_MSVC_RUNTIME "Link to static MSVC runtime library"


# Option overrides
if(SOURCEPP_USE_STEAMPP OR SOURCEPP_USE_VPKPP)
if(SOURCEPP_USE_STEAMPP)
set(SOURCEPP_USE_KVPP ON CACHE INTERNAL "")
endif()
if(SOURCEPP_USE_VPKPP)
set(SOURCEPP_USE_BSPPP ON CACHE INTERNAL "")
set(SOURCEPP_USE_KVPP ON CACHE INTERNAL "")
endif()

Expand Down Expand Up @@ -80,6 +85,7 @@ endif()


# Add libraries
add_sourcepp_library(bsppp NO_TEST)
add_sourcepp_library(dmxpp)
add_sourcepp_library(fgdpp)
add_sourcepp_library(kvpp)
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ Several modern C++20 libraries for sanely parsing Valve formats, rolled into one
<th>Wrappers</th>
<th>Special Thanks</th>
</tr>
<tr>
<td>bsppp</td>
<td>
<ul>
<li><a href="https://developer.valvesoftware.com/wiki/BSP_(Source)">BSP</a> v17-27</li>
</ul>
</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center"></td>
<td></td>
</tr>
<tr>
<td>dmxpp</td>
<td>
Expand Down
142 changes: 142 additions & 0 deletions include/bsppp/bsppp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#pragma once

#include <array>
#include <cstddef>
#include <optional>
#include <string>
#include <vector>

#include <sourcepp/parser/Binary.h>

namespace bsppp {

constexpr auto BSP_SIGNATURE = sourcepp::parser::binary::makeFourCC("VBSP");

enum class BSPLump : int32_t {
UNKNOWN = -1,
ENTITIES = 0,
PLANES,
TEXDATA,
VERTEXES,
VISIBILITY,
NODES,
TEXINFO,
FACES,
LIGHTING,
OCCLUSION,
LEAFS,
FACEIDS,
EDGES,
SURFEDGES,
MODELS,
WORLDLIGHTS,
LEAFFACES,
LEAFBRUSHES,
BRUSHES,
BRUSHSIDES,
AREAS,
AREAPORTALS,
S2004_PORTALS,
UNUSED0 = S2004_PORTALS,
SL4D2_PROPCOLLISION = S2004_PORTALS,
S2004_CLUSTERS,
UNUSED1 = S2004_CLUSTERS,
SL4D2_PROPHULLS = S2004_CLUSTERS,
S2004_PORTALVERTS,
UNUSED2 = S2004_PORTALVERTS,
SL4D2_PROPHULLVERTS = S2004_PORTALVERTS,
S2004_CLUSTERPORTALS,
UNUSED3 = S2004_CLUSTERPORTALS,
SL4D2_PROPTRIS = S2004_CLUSTERPORTALS,
DISPINFO,
ORIGINALFACES,
PHYSDISP,
PHYSCOLLIDE,
VERTNORMALS,
VERTNORMALINDICES,
S2004_DISP_LIGHTMAP_ALPHAS,
UNUSED4 = S2004_DISP_LIGHTMAP_ALPHAS,
DISP_VERTS,
DISP_LIGHTMAP_SAMPLE_POSITIONS,
GAME_LUMP,
LEAFWATERDATA,
PRIMITIVES,
PRIMVERTS,
PRIMINDICES,
PAKFILE,
CLIPPORTALVERTS,
CUBEMAPS,
TEXDATA_STRING_DATA,
TEXDATA_STRING_TABLE,
OVERLAYS,
LEAFMINDISTTOWATER,
FACE_MACRO_TEXTURE_INFO,
DISP_TRIS,
S2004_PHYSCOLLIDESURFACE,
UNUSED5 = S2004_PHYSCOLLIDESURFACE,
SL4D2_PROP_BLOB = S2004_PHYSCOLLIDESURFACE,
WATEROVERLAYS,
S2006_XBOX_LIGHTMAPPAGES,
LEAF_AMBIENT_INDEX_HDR = S2006_XBOX_LIGHTMAPPAGES,
S2006_XBOX_LIGHTMAPPAGEINFOS,
LEAF_AMBIENT_INDEX = S2006_XBOX_LIGHTMAPPAGEINFOS,
LIGHTING_HDR,
WORLDLIGHTS_HDR,
LEAF_AMBIENT_LIGHTING_HDR,
LEAF_AMBIENT_LIGHTING,
XBOX_XZIPPAKFILE,
FACES_HDR,
MAP_FLAGS,
OVERLAY_FADES,
L4D_OVERLAY_SYSTEM_LEVELS,
UNUSED6 = L4D_OVERLAY_SYSTEM_LEVELS,
L4D2_PHYSLEVEL,
UNUSED7 = L4D2_PHYSLEVEL,
ASW_DISP_MULTIBLEND,
UNUSED8 = ASW_DISP_MULTIBLEND,

COUNT,
};
static_assert(static_cast<int32_t>(BSPLump::COUNT) == 64, "Incorrect lump count!");

class BSP {
struct Lump {
/// Byte offset into file
int32_t offset;
/// Length of lump data
int32_t length;
/// Lump format version
int32_t version;
/// Uncompressed size, or 0
int32_t fourCC;
};

struct Header {
/// Version of the BSP file
int32_t version;
/// Lump metadata
std::array<Lump, static_cast<uint32_t>(BSPLump::COUNT)> lumps;
/// Map version number
int32_t mapRevision;
};

public:
explicit BSP(std::string path_);

explicit operator bool() const;

[[nodiscard]] static BSP create(std::string path, int32_t version = 21, int32_t mapRevision = 0);

[[nodiscard]] std::optional<std::vector<std::byte>> readLump(BSPLump lumpIndex) const;

void writeLump(BSPLump lumpIndex, const std::vector<std::byte>& data);

protected:
/// If the lump is too big where it is, shift it to the end of the file, otherwise its fine
void moveLumpToWritableSpace(BSPLump lumpIndex, int newSize);

std::string path;
Header header{};
};

} // namespace bsppp
125 changes: 5 additions & 120 deletions include/vpkpp/format/BSP.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,125 +2,16 @@

#include <array>

#include <sourcepp/parser/Binary.h>
#include <bsppp/bsppp.h>

#include "ZIP.h"

namespace vpkpp {

constexpr auto BSP_SIGNATURE = sourcepp::parser::binary::makeFourCC("VBSP");
using bsppp::BSP_SIGNATURE;
constexpr std::string_view BSP_EXTENSION = ".bsp";

enum class BSPLump : int32_t {
UNKNOWN = -1,
ENTITIES = 0,
PLANES,
TEXDATA,
VERTEXES,
VISIBILITY,
NODES,
TEXINFO,
FACES,
LIGHTING,
OCCLUSION,
LEAFS,
FACEIDS,
EDGES,
SURFEDGES,
MODELS,
WORLDLIGHTS,
LEAFFACES,
LEAFBRUSHES,
BRUSHES,
BRUSHSIDES,
AREAS,
AREAPORTALS,
S2004_PORTALS,
UNUSED0 = S2004_PORTALS,
SL4D2_PROPCOLLISION = S2004_PORTALS,
S2004_CLUSTERS,
UNUSED1 = S2004_CLUSTERS,
SL4D2_PROPHULLS = S2004_CLUSTERS,
S2004_PORTALVERTS,
UNUSED2 = S2004_PORTALVERTS,
SL4D2_PROPHULLVERTS = S2004_PORTALVERTS,
S2004_CLUSTERPORTALS,
UNUSED3 = S2004_CLUSTERPORTALS,
SL4D2_PROPTRIS = S2004_CLUSTERPORTALS,
DISPINFO,
ORIGINALFACES,
PHYSDISP,
PHYSCOLLIDE,
VERTNORMALS,
VERTNORMALINDICES,
S2004_DISP_LIGHTMAP_ALPHAS,
UNUSED4 = S2004_DISP_LIGHTMAP_ALPHAS,
DISP_VERTS,
DISP_LIGHTMAP_SAMPLE_POSITIONS,
GAME_LUMP,
LEAFWATERDATA,
PRIMITIVES,
PRIMVERTS,
PRIMINDICES,
PAKFILE,
CLIPPORTALVERTS,
CUBEMAPS,
TEXDATA_STRING_DATA,
TEXDATA_STRING_TABLE,
OVERLAYS,
LEAFMINDISTTOWATER,
FACE_MACRO_TEXTURE_INFO,
DISP_TRIS,
S2004_PHYSCOLLIDESURFACE,
UNUSED5 = S2004_PHYSCOLLIDESURFACE,
SL4D2_PROP_BLOB = S2004_PHYSCOLLIDESURFACE,
WATEROVERLAYS,
S2006_XBOX_LIGHTMAPPAGES,
LEAF_AMBIENT_INDEX_HDR = S2006_XBOX_LIGHTMAPPAGES,
S2006_XBOX_LIGHTMAPPAGEINFOS,
LEAF_AMBIENT_INDEX = S2006_XBOX_LIGHTMAPPAGEINFOS,
LIGHTING_HDR,
WORLDLIGHTS_HDR,
LEAF_AMBIENT_LIGHTING_HDR,
LEAF_AMBIENT_LIGHTING,
XBOX_XZIPPAKFILE,
FACES_HDR,
MAP_FLAGS,
OVERLAY_FADES,
L4D_OVERLAY_SYSTEM_LEVELS,
UNUSED6 = L4D_OVERLAY_SYSTEM_LEVELS,
L4D2_PHYSLEVEL,
UNUSED7 = L4D2_PHYSLEVEL,
ASW_DISP_MULTIBLEND,
UNUSED8 = ASW_DISP_MULTIBLEND,

COUNT,
};
static_assert(static_cast<int32_t>(BSPLump::COUNT) == 64, "Incorrect lump count!");

class BSP : public ZIP {
struct Lump {
/// Byte offset into file
int32_t offset;
/// Length of lump data
int32_t length;
/// Lump format version
int32_t version;
/// Uncompressed size, or 0
int32_t fourCC;
};

struct Header {
/// BSP_ID
int32_t signature;
/// Version of the BSP file
int32_t version;
/// Lump metadata
std::array<Lump, static_cast<uint32_t>(BSPLump::COUNT)> lumps;
/// Map version number
int32_t mapRevision;
};

class BSP : public ZIP, private bsppp::BSP {
public:
/// Open a BSP file
[[nodiscard]] static std::unique_ptr<PackFile> open(const std::string& path, PackFileOptions options = {}, const Callback& callback = nullptr);
Expand All @@ -133,19 +24,13 @@ class BSP : public ZIP {

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

[[nodiscard]] std::optional<std::vector<std::byte>> readLump(BSPLump lumpIndex) const;

void writeLump(BSPLump lumpIndex, const std::vector<std::byte>& data);

protected:
BSP(const std::string& fullFilePath_, PackFileOptions options_);

/// If the lump is too big where it is, shift it to the end of the file, otherwise its fine
void moveLumpToWritableSpace(BSPLump lumpIndex, int newSize);

const std::string tempBSPPakLumpPath;

Header header{};
private:
using bsppp::BSP::operator bool;

private:
VPKPP_REGISTER_PACKFILE_OPEN(BSP_EXTENSION, &BSP::open);
Expand Down
3 changes: 3 additions & 0 deletions src/bsppp/_bsppp.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_pretty_parser(bsppp DEPS vpkpp SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/include/bsppp/bsppp.h"
"${CMAKE_CURRENT_LIST_DIR}/bsppp.cpp")
Loading

0 comments on commit efbde2e

Please sign in to comment.