Skip to content

Commit

Permalink
fix(Vfs): always write catalog directly after header for compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
lmichaelis committed Jun 9, 2024
1 parent 18318ec commit 748962d
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/Vfs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,26 @@ namespace zenkit {
return const_cast<VfsNode*>(const_cast<Vfs const*>(this)->find(name));
}

static uint32_t count_nodes(VfsNode const* node) {
uint32_t count = 1; /* self */

if (node->type() == VfsNodeType::DIRECTORY) {
for (auto& child : node->children()) {
count += count_nodes(&child);
}
}

return count;
}

void Vfs::save(Write* w, GameVersion version) const {
std::vector<std::byte> catalog;
auto write_catalog = Write::to(&catalog);

// Skip the header, we'll write it at the end.
w->seek(256 + 16 + 6 * 4, Whence::BEG);
unsigned header_size = 256 + 16 + 6 * 4;
unsigned catalog_size = (count_nodes(&_m_root) - 1) * (64 + 4 * 4); // -1 because the root node is not counted
w->seek(header_size + catalog_size, Whence::BEG);

std::vector<std::byte> cache;
std::string name;
Expand Down Expand Up @@ -334,9 +348,9 @@ namespace zenkit {
w->write_uint(index);
w->write_uint(0);
w->write_uint(off + catalog.size());
w->write_uint(off);
w->write_uint(header_size);
w->write_uint(80);
w->seek(static_cast<ssize_t>(off), Whence::BEG);
w->seek(static_cast<ssize_t>(header_size), Whence::BEG);
w->write(catalog.data(), catalog.size());
}

Expand Down

0 comments on commit 748962d

Please sign in to comment.