Skip to content

Commit 99de94a

Browse files
feat(bsppp): add BSP editing library, use as the backend of the BSP format in vpkpp
1 parent 90f7153 commit 99de94a

File tree

8 files changed

+303
-212
lines changed

8 files changed

+303
-212
lines changed

CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
1414

1515

1616
# Options
17+
option(SOURCEPP_USE_BSPPP "Build bsppp library" ON)
1718
option(SOURCEPP_USE_DMXPP "Build dmxpp library" ON)
1819
option(SOURCEPP_USE_FGDPP "Build fgdpp library" ON)
1920
option(SOURCEPP_USE_KVPP "Build kvpp library" ON)
@@ -28,7 +29,11 @@ option(SOURCEPP_USE_STATIC_MSVC_RUNTIME "Link to static MSVC runtime library"
2829

2930

3031
# Option overrides
31-
if(SOURCEPP_USE_STEAMPP OR SOURCEPP_USE_VPKPP)
32+
if(SOURCEPP_USE_STEAMPP)
33+
set(SOURCEPP_USE_KVPP ON CACHE INTERNAL "")
34+
endif()
35+
if(SOURCEPP_USE_VPKPP)
36+
set(SOURCEPP_USE_BSPPP ON CACHE INTERNAL "")
3237
set(SOURCEPP_USE_KVPP ON CACHE INTERNAL "")
3338
endif()
3439

@@ -80,6 +85,7 @@ endif()
8085

8186

8287
# Add libraries
88+
add_sourcepp_library(bsppp NO_TEST)
8389
add_sourcepp_library(dmxpp)
8490
add_sourcepp_library(fgdpp)
8591
add_sourcepp_library(kvpp)

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ Several modern C++20 libraries for sanely parsing Valve formats, rolled into one
2222
<th>Wrappers</th>
2323
<th>Special Thanks</th>
2424
</tr>
25+
<tr>
26+
<td>bsppp</td>
27+
<td>
28+
<ul>
29+
<li><a href="https://developer.valvesoftware.com/wiki/BSP_(Source)">BSP</a> v17-27</li>
30+
</ul>
31+
</td>
32+
<td align="center">✅</td>
33+
<td align="center">✅</td>
34+
<td align="center">✅</td>
35+
<td align="center"></td>
36+
<td></td>
37+
</tr>
2538
<tr>
2639
<td>dmxpp</td>
2740
<td>

include/bsppp/bsppp.h

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#pragma once
2+
3+
#include <array>
4+
#include <cstddef>
5+
#include <optional>
6+
#include <string>
7+
#include <vector>
8+
9+
#include <sourcepp/parser/Binary.h>
10+
11+
namespace bsppp {
12+
13+
constexpr auto BSP_SIGNATURE = sourcepp::parser::binary::makeFourCC("VBSP");
14+
15+
enum class BSPLump : int32_t {
16+
UNKNOWN = -1,
17+
ENTITIES = 0,
18+
PLANES,
19+
TEXDATA,
20+
VERTEXES,
21+
VISIBILITY,
22+
NODES,
23+
TEXINFO,
24+
FACES,
25+
LIGHTING,
26+
OCCLUSION,
27+
LEAFS,
28+
FACEIDS,
29+
EDGES,
30+
SURFEDGES,
31+
MODELS,
32+
WORLDLIGHTS,
33+
LEAFFACES,
34+
LEAFBRUSHES,
35+
BRUSHES,
36+
BRUSHSIDES,
37+
AREAS,
38+
AREAPORTALS,
39+
S2004_PORTALS,
40+
UNUSED0 = S2004_PORTALS,
41+
SL4D2_PROPCOLLISION = S2004_PORTALS,
42+
S2004_CLUSTERS,
43+
UNUSED1 = S2004_CLUSTERS,
44+
SL4D2_PROPHULLS = S2004_CLUSTERS,
45+
S2004_PORTALVERTS,
46+
UNUSED2 = S2004_PORTALVERTS,
47+
SL4D2_PROPHULLVERTS = S2004_PORTALVERTS,
48+
S2004_CLUSTERPORTALS,
49+
UNUSED3 = S2004_CLUSTERPORTALS,
50+
SL4D2_PROPTRIS = S2004_CLUSTERPORTALS,
51+
DISPINFO,
52+
ORIGINALFACES,
53+
PHYSDISP,
54+
PHYSCOLLIDE,
55+
VERTNORMALS,
56+
VERTNORMALINDICES,
57+
S2004_DISP_LIGHTMAP_ALPHAS,
58+
UNUSED4 = S2004_DISP_LIGHTMAP_ALPHAS,
59+
DISP_VERTS,
60+
DISP_LIGHTMAP_SAMPLE_POSITIONS,
61+
GAME_LUMP,
62+
LEAFWATERDATA,
63+
PRIMITIVES,
64+
PRIMVERTS,
65+
PRIMINDICES,
66+
PAKFILE,
67+
CLIPPORTALVERTS,
68+
CUBEMAPS,
69+
TEXDATA_STRING_DATA,
70+
TEXDATA_STRING_TABLE,
71+
OVERLAYS,
72+
LEAFMINDISTTOWATER,
73+
FACE_MACRO_TEXTURE_INFO,
74+
DISP_TRIS,
75+
S2004_PHYSCOLLIDESURFACE,
76+
UNUSED5 = S2004_PHYSCOLLIDESURFACE,
77+
SL4D2_PROP_BLOB = S2004_PHYSCOLLIDESURFACE,
78+
WATEROVERLAYS,
79+
S2006_XBOX_LIGHTMAPPAGES,
80+
LEAF_AMBIENT_INDEX_HDR = S2006_XBOX_LIGHTMAPPAGES,
81+
S2006_XBOX_LIGHTMAPPAGEINFOS,
82+
LEAF_AMBIENT_INDEX = S2006_XBOX_LIGHTMAPPAGEINFOS,
83+
LIGHTING_HDR,
84+
WORLDLIGHTS_HDR,
85+
LEAF_AMBIENT_LIGHTING_HDR,
86+
LEAF_AMBIENT_LIGHTING,
87+
XBOX_XZIPPAKFILE,
88+
FACES_HDR,
89+
MAP_FLAGS,
90+
OVERLAY_FADES,
91+
L4D_OVERLAY_SYSTEM_LEVELS,
92+
UNUSED6 = L4D_OVERLAY_SYSTEM_LEVELS,
93+
L4D2_PHYSLEVEL,
94+
UNUSED7 = L4D2_PHYSLEVEL,
95+
ASW_DISP_MULTIBLEND,
96+
UNUSED8 = ASW_DISP_MULTIBLEND,
97+
98+
COUNT,
99+
};
100+
static_assert(static_cast<int32_t>(BSPLump::COUNT) == 64, "Incorrect lump count!");
101+
102+
class BSP {
103+
struct Lump {
104+
/// Byte offset into file
105+
int32_t offset;
106+
/// Length of lump data
107+
int32_t length;
108+
/// Lump format version
109+
int32_t version;
110+
/// Uncompressed size, or 0
111+
int32_t fourCC;
112+
};
113+
114+
struct Header {
115+
/// Version of the BSP file
116+
int32_t version;
117+
/// Lump metadata
118+
std::array<Lump, static_cast<uint32_t>(BSPLump::COUNT)> lumps;
119+
/// Map version number
120+
int32_t mapRevision;
121+
};
122+
123+
public:
124+
explicit BSP(std::string path_);
125+
126+
explicit operator bool() const;
127+
128+
[[nodiscard]] static BSP create(std::string path, int32_t version = 21, int32_t mapRevision = 0);
129+
130+
[[nodiscard]] std::optional<std::vector<std::byte>> readLump(BSPLump lumpIndex) const;
131+
132+
void writeLump(BSPLump lumpIndex, const std::vector<std::byte>& data);
133+
134+
protected:
135+
/// If the lump is too big where it is, shift it to the end of the file, otherwise its fine
136+
void moveLumpToWritableSpace(BSPLump lumpIndex, int newSize);
137+
138+
std::string path;
139+
Header header{};
140+
};
141+
142+
} // namespace bsppp

include/vpkpp/format/BSP.h

Lines changed: 5 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -2,125 +2,16 @@
22

33
#include <array>
44

5-
#include <sourcepp/parser/Binary.h>
5+
#include <bsppp/bsppp.h>
66

77
#include "ZIP.h"
88

99
namespace vpkpp {
1010

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

14-
enum class BSPLump : int32_t {
15-
UNKNOWN = -1,
16-
ENTITIES = 0,
17-
PLANES,
18-
TEXDATA,
19-
VERTEXES,
20-
VISIBILITY,
21-
NODES,
22-
TEXINFO,
23-
FACES,
24-
LIGHTING,
25-
OCCLUSION,
26-
LEAFS,
27-
FACEIDS,
28-
EDGES,
29-
SURFEDGES,
30-
MODELS,
31-
WORLDLIGHTS,
32-
LEAFFACES,
33-
LEAFBRUSHES,
34-
BRUSHES,
35-
BRUSHSIDES,
36-
AREAS,
37-
AREAPORTALS,
38-
S2004_PORTALS,
39-
UNUSED0 = S2004_PORTALS,
40-
SL4D2_PROPCOLLISION = S2004_PORTALS,
41-
S2004_CLUSTERS,
42-
UNUSED1 = S2004_CLUSTERS,
43-
SL4D2_PROPHULLS = S2004_CLUSTERS,
44-
S2004_PORTALVERTS,
45-
UNUSED2 = S2004_PORTALVERTS,
46-
SL4D2_PROPHULLVERTS = S2004_PORTALVERTS,
47-
S2004_CLUSTERPORTALS,
48-
UNUSED3 = S2004_CLUSTERPORTALS,
49-
SL4D2_PROPTRIS = S2004_CLUSTERPORTALS,
50-
DISPINFO,
51-
ORIGINALFACES,
52-
PHYSDISP,
53-
PHYSCOLLIDE,
54-
VERTNORMALS,
55-
VERTNORMALINDICES,
56-
S2004_DISP_LIGHTMAP_ALPHAS,
57-
UNUSED4 = S2004_DISP_LIGHTMAP_ALPHAS,
58-
DISP_VERTS,
59-
DISP_LIGHTMAP_SAMPLE_POSITIONS,
60-
GAME_LUMP,
61-
LEAFWATERDATA,
62-
PRIMITIVES,
63-
PRIMVERTS,
64-
PRIMINDICES,
65-
PAKFILE,
66-
CLIPPORTALVERTS,
67-
CUBEMAPS,
68-
TEXDATA_STRING_DATA,
69-
TEXDATA_STRING_TABLE,
70-
OVERLAYS,
71-
LEAFMINDISTTOWATER,
72-
FACE_MACRO_TEXTURE_INFO,
73-
DISP_TRIS,
74-
S2004_PHYSCOLLIDESURFACE,
75-
UNUSED5 = S2004_PHYSCOLLIDESURFACE,
76-
SL4D2_PROP_BLOB = S2004_PHYSCOLLIDESURFACE,
77-
WATEROVERLAYS,
78-
S2006_XBOX_LIGHTMAPPAGES,
79-
LEAF_AMBIENT_INDEX_HDR = S2006_XBOX_LIGHTMAPPAGES,
80-
S2006_XBOX_LIGHTMAPPAGEINFOS,
81-
LEAF_AMBIENT_INDEX = S2006_XBOX_LIGHTMAPPAGEINFOS,
82-
LIGHTING_HDR,
83-
WORLDLIGHTS_HDR,
84-
LEAF_AMBIENT_LIGHTING_HDR,
85-
LEAF_AMBIENT_LIGHTING,
86-
XBOX_XZIPPAKFILE,
87-
FACES_HDR,
88-
MAP_FLAGS,
89-
OVERLAY_FADES,
90-
L4D_OVERLAY_SYSTEM_LEVELS,
91-
UNUSED6 = L4D_OVERLAY_SYSTEM_LEVELS,
92-
L4D2_PHYSLEVEL,
93-
UNUSED7 = L4D2_PHYSLEVEL,
94-
ASW_DISP_MULTIBLEND,
95-
UNUSED8 = ASW_DISP_MULTIBLEND,
96-
97-
COUNT,
98-
};
99-
static_assert(static_cast<int32_t>(BSPLump::COUNT) == 64, "Incorrect lump count!");
100-
101-
class BSP : public ZIP {
102-
struct Lump {
103-
/// Byte offset into file
104-
int32_t offset;
105-
/// Length of lump data
106-
int32_t length;
107-
/// Lump format version
108-
int32_t version;
109-
/// Uncompressed size, or 0
110-
int32_t fourCC;
111-
};
112-
113-
struct Header {
114-
/// BSP_ID
115-
int32_t signature;
116-
/// Version of the BSP file
117-
int32_t version;
118-
/// Lump metadata
119-
std::array<Lump, static_cast<uint32_t>(BSPLump::COUNT)> lumps;
120-
/// Map version number
121-
int32_t mapRevision;
122-
};
123-
14+
class BSP : public ZIP, private bsppp::BSP {
12415
public:
12516
/// Open a BSP file
12617
[[nodiscard]] static std::unique_ptr<PackFile> open(const std::string& path, PackFileOptions options = {}, const Callback& callback = nullptr);
@@ -133,19 +24,13 @@ class BSP : public ZIP {
13324

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

136-
[[nodiscard]] std::optional<std::vector<std::byte>> readLump(BSPLump lumpIndex) const;
137-
138-
void writeLump(BSPLump lumpIndex, const std::vector<std::byte>& data);
139-
14027
protected:
14128
BSP(const std::string& fullFilePath_, PackFileOptions options_);
14229

143-
/// If the lump is too big where it is, shift it to the end of the file, otherwise its fine
144-
void moveLumpToWritableSpace(BSPLump lumpIndex, int newSize);
145-
14630
const std::string tempBSPPakLumpPath;
14731

148-
Header header{};
32+
private:
33+
using bsppp::BSP::operator bool;
14934

15035
private:
15136
VPKPP_REGISTER_PACKFILE_OPEN(BSP_EXTENSION, &BSP::open);

src/bsppp/_bsppp.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
add_pretty_parser(bsppp DEPS vpkpp SOURCES
2+
"${CMAKE_CURRENT_SOURCE_DIR}/include/bsppp/bsppp.h"
3+
"${CMAKE_CURRENT_LIST_DIR}/bsppp.cpp")

0 commit comments

Comments
 (0)