Skip to content

Commit

Permalink
feat(bsppp): add lump patch file creation method
Browse files Browse the repository at this point in the history
  • Loading branch information
craftablescience committed Jul 17, 2024
1 parent 99de94a commit d9957e7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/bsppp/bsppp.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ class BSP {

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

void createLumpPatchFile(BSPLump lumpIndex) const;

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);
Expand Down
28 changes: 28 additions & 0 deletions src/bsppp/bsppp.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <bsppp/bsppp.h>

#include <filesystem>
#include <limits>
#include <utility>

Expand Down Expand Up @@ -73,6 +74,33 @@ void BSP::writeLump(BSPLump lumpIndex, const std::vector<std::byte>& data) {
.write(data);
}

void BSP::createLumpPatchFile(BSPLump lumpIndex) const {
auto lumpData = this->readLump(lumpIndex);
if (!lumpData) {
return;
}

auto& lump = this->header.lumps.at(static_cast<int32_t>(lumpIndex));

const auto fsPath = std::filesystem::path{this->path};
const auto fsStem = (fsPath.parent_path() / fsPath.stem()).string() + "_l_";
int nonexistentNumber;
for (nonexistentNumber = 0; true; nonexistentNumber++) {
if (!std::filesystem::exists(fsStem + std::to_string(nonexistentNumber) + ".lmp")) {
break;
}
}

FileStream writer{fsStem + std::to_string(nonexistentNumber) + ".lmp", FileStream::OPT_TRUNCATE | FileStream::OPT_CREATE_IF_NONEXISTENT};
writer
.write<int32_t>(sizeof(int32_t) * 5)
.write(lumpIndex)
.write(lump.version)
.write(lump.length)
.write(this->header.mapRevision)
.write(*lumpData);
}

void BSP::moveLumpToWritableSpace(BSPLump lumpIndex, int newSize) {
auto lumpToMove = static_cast<uint32_t>(lumpIndex);
this->header.lumps[lumpToMove].length = newSize;
Expand Down

0 comments on commit d9957e7

Please sign in to comment.