Skip to content

Commit ec12fe4

Browse files
bsppp: add support for console BSPs
1 parent 7d4484d commit ec12fe4

File tree

5 files changed

+46
-11
lines changed

5 files changed

+46
-11
lines changed

include/bsppp/BSP.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace bsppp {
1919

2020
constexpr auto BSP_SIGNATURE = sourcepp::parser::binary::makeFourCC("VBSP");
21+
constexpr auto BSP_CONSOLE_SIGNATURE = sourcepp::parser::binary::makeFourCC("PSBV");
2122

2223
enum class BSPLump : int32_t {
2324
UNKNOWN = -1,
@@ -156,6 +157,14 @@ class BSP {
156157

157158
void setMapRevision(uint32_t mapRevision);
158159

160+
[[nodiscard]] bool isL4D2() const;
161+
162+
void setL4D2(bool isL4D2);
163+
164+
[[nodiscard]] bool isConsole() const;
165+
166+
void setConsole(bool isConsole);
167+
159168
[[nodiscard]] bool hasLump(BSPLump lumpIndex) const;
160169

161170
[[nodiscard]] bool isLumpCompressed(BSPLump lumpIndex) const;
@@ -258,7 +267,9 @@ class BSP {
258267
uint32_t stagedMapRevision{};
259268

260269
// Slightly different header despite using the same version just to be quirky
261-
bool isL4D2 = false;
270+
bool l4d2 = false;
271+
// BSP is mostly big-endian
272+
bool console = false;
262273
};
263274

264275
} // namespace bsppp

lang/python/cfg/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from ._sourcepp_impl import __doc__, gamepp, sourcepp, steampp, toolpp, vcryptpp, vtfpp
1+
from ._sourcepp_impl import __doc__, bsppp, gamepp, sourcepp, steampp, toolpp, vcryptpp, vtfpp
22

33
__author__ = "craftablescience"
44
__version__ = "${SOURCEPP_PYTHON_VERSION}"
5-
__all__ = ['__author__', '__doc__', '__version__', 'gamepp', 'sourcepp', 'steampp', 'toolpp', 'vcryptpp', 'vtfpp']
5+
__all__ = ['__author__', '__doc__', '__version__', 'bsppp', 'gamepp', 'sourcepp', 'steampp', 'toolpp', 'vcryptpp', 'vtfpp']

lang/python/src/bsppp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ inline void register_python(py::module_& m) {
270270
.def_static("create", &BSP::create, py::arg("path"), py::arg("version") = 21, py::arg("map_revision") = 0)
271271
.def_prop_rw("version", &BSP::getVersion, &BSP::setVersion)
272272
.def_prop_rw("map_revision", &BSP::getMapRevision, &BSP::setMapRevision)
273+
.def_prop_rw("l4d2", &BSP::isL4D2, &BSP::setL4D2)
274+
.def_prop_rw("console", &BSP::isConsole, &BSP::setConsole)
273275
.def("has_lump", &BSP::hasLump, py::arg("lump_index"))
274276
.def("is_lump_compressed", &BSP::isLumpCompressed, py::arg("lump_index"))
275277
.def("get_lump_version", &BSP::getLumpVersion, py::arg("lump_index"))

src/bsppp/BSP.cpp

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ template<BufferStreamPODType T>
2323
}
2424

2525
BufferStreamReadOnly stream{*data};
26+
stream.set_big_endian(bsp.isConsole());
2627
std::vector<T> out;
2728
callback(bsp, stream, out);
2829
return out;
@@ -104,6 +105,22 @@ void BSP::setMapRevision(uint32_t mapRevision) {
104105
this->stagedMapRevision = mapRevision;
105106
}
106107

108+
bool BSP::isL4D2() const {
109+
return this->l4d2;
110+
}
111+
112+
void BSP::setL4D2(bool isL4D2) {
113+
this->l4d2 = isL4D2;
114+
}
115+
116+
bool BSP::isConsole() const {
117+
return this->console;
118+
}
119+
120+
void BSP::setConsole(bool isConsole) {
121+
this->console = isConsole;
122+
}
123+
107124
bool BSP::hasLump(BSPLump lumpIndex) const {
108125
if (this->path.empty()) {
109126
return false;
@@ -351,6 +368,7 @@ bool BSP::bake(std::string_view outputPath) {
351368

352369
std::vector<std::byte> out;
353370
BufferStream stream{out};
371+
stream.set_big_endian(this->console);
354372

355373
stream << BSP_SIGNATURE << this->stagedVersion;
356374
if (this->stagedVersion == 27) {
@@ -421,7 +439,7 @@ bool BSP::bake(std::string_view outputPath) {
421439

422440
const auto curPos = stream.tell();
423441
stream.seek_u(lumpsHeaderOffset + (i * sizeof(Lump)));
424-
if (!this->isL4D2) {
442+
if (!this->l4d2) {
425443
stream
426444
.write<uint32_t>(gameLumpOffset)
427445
.write<uint32_t>(curPos - gameLumpOffset)
@@ -442,7 +460,7 @@ bool BSP::bake(std::string_view outputPath) {
442460
const auto& lumpPair = this->stagedLumps.at(i);
443461
const auto curPos = stream.tell();
444462
stream.seek_u(lumpsHeaderOffset + (i * sizeof(Lump)));
445-
if (!this->isL4D2) {
463+
if (!this->l4d2) {
446464
stream
447465
.write<uint32_t>(curPos)
448466
.write<uint32_t>(lumpPair.second.size())
@@ -466,7 +484,7 @@ bool BSP::bake(std::string_view outputPath) {
466484
stream.seek_u(lumpsHeaderOffset + (i * sizeof(Lump)));
467485

468486
auto& lump = this->header.lumps[i];
469-
if (!this->isL4D2) {
487+
if (!this->l4d2) {
470488
stream
471489
.write<uint32_t>(curPos)
472490
.write<uint32_t>(lump.length)
@@ -494,7 +512,7 @@ bool BSP::bake(std::string_view outputPath) {
494512
}
495513
}
496514

497-
out.resize(stream.tell());
515+
out.resize(stream.size());
498516
if (!fs::writeFileBuffer(outputPath.empty() ? this->path : std::string{outputPath}, out)) {
499517
return false;
500518
}
@@ -511,9 +529,12 @@ bool BSP::readHeader() {
511529
}
512530
reader.seek_in(0);
513531

514-
if (reader.read<uint32_t>() != BSP_SIGNATURE) {
532+
if (auto signature = reader.read<uint32_t>(); signature != BSP_SIGNATURE && signature != BSP_CONSOLE_SIGNATURE) {
515533
// File is not a BSP
516534
return false;
535+
} else if (signature == BSP_CONSOLE_SIGNATURE) {
536+
this->console = true;
537+
reader.set_big_endian(true);
517538
}
518539
this->header.version = reader.read<uint32_t>();
519540

@@ -539,8 +560,8 @@ bool BSP::readHeader() {
539560
break;
540561
}
541562
}
542-
this->isL4D2 = i == BSP_LUMP_COUNT;
543-
if (this->isL4D2) {
563+
this->l4d2 = i == BSP_LUMP_COUNT;
564+
if (this->l4d2) {
544565
// Swap fields around
545566
for (i = 0; i < BSP_LUMP_COUNT; i++) {
546567
std::swap(this->header.lumps[i].offset, this->header.lumps[i].version);
@@ -693,6 +714,7 @@ std::vector<BSPGameLump> BSP::parseGameLumps(bool decompress) const {
693714
return lumps;
694715
}
695716
BufferStreamReadOnly stream{*gameLumpData};
717+
stream.set_big_endian(this->console);
696718

697719
lumps.resize(stream.read<uint32_t>());
698720
for (auto& lump : lumps) {

0 commit comments

Comments
 (0)