Skip to content

Commit

Permalink
VOXELFORMAT: CUBZH: fixed loading the node names
Browse files Browse the repository at this point in the history
the string chunk length for the name is given in a single byte, not in an int
  • Loading branch information
mgerhardy committed May 31, 2024
1 parent 678a679 commit beaf53b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
38 changes: 22 additions & 16 deletions src/modules/voxelformat/private/cubzh/CubzhFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ bool CubzhFormat::loadShape5(const core::String &filename, const Header &header,
int64_t startPos = stream.pos();
while (stream.pos() < startPos + chunk.chunkSize - 5) {
Chunk subChunk;
wrapBool(loadSubChunkHeader(stream, subChunk))
wrapBool(loadSubChunkHeader(header, stream, subChunk))
core::DynamicArray<uint8_t> volumeBuffer; // used in case the size chunk is late
switch (subChunk.chunkId) {
case priv::CHUNK_ID_SHAPE_SIZE_V5:
Expand Down Expand Up @@ -406,15 +406,8 @@ bool CubzhFormat::loadPCubes(const core::String &filename, const Header &header,
}

bool CubzhFormat::loadChunkHeader(const Header &header, io::ReadStream &stream, Chunk &chunk) const {
wrap(stream.readUInt8(chunk.chunkId))
if (header.version == 6 && chunk.chunkId == priv::CHUNK_ID_SHAPE_NAME_V6) {
uint8_t chunkSize;
wrap(stream.readUInt8(chunkSize))
chunk.chunkSize = chunkSize;
} else {
wrap(stream.readUInt32(chunk.chunkSize))
}
Log::debug("Chunk id %u with size %u", chunk.chunkId, chunk.chunkSize);
wrapBool(loadSubChunkHeader(header, stream, chunk))
Log::debug("Mainchunk id %u with size %u", chunk.chunkId, chunk.chunkSize);
if (header.version == 6u && chunk.supportsCompression()) {
wrap(stream.readUInt8(chunk.compressed))
wrap(stream.readUInt32(chunk.uncompressedSize))
Expand All @@ -424,10 +417,16 @@ bool CubzhFormat::loadChunkHeader(const Header &header, io::ReadStream &stream,
return true;
}

bool CubzhFormat::loadSubChunkHeader(io::ReadStream &stream, Chunk &chunk) const {
bool CubzhFormat::loadSubChunkHeader(const Header &header, io::ReadStream &stream, Chunk &chunk) const {
wrap(stream.readUInt8(chunk.chunkId))
wrap(stream.readUInt32(chunk.chunkSize))
Log::debug("Subchunk id %u with size %u", chunk.chunkId, chunk.chunkSize);
if (header.version == 6 && chunk.chunkId == priv::CHUNK_ID_SHAPE_NAME_V6) {
uint8_t chunkSize;
wrap(stream.readUInt8(chunkSize))
chunk.chunkSize = chunkSize;
} else {
wrap(stream.readUInt32(chunk.chunkSize))
}
Log::debug("Chunk id %u with size %u", chunk.chunkId, chunk.chunkSize);
return true;
}

Expand All @@ -447,10 +446,16 @@ bool CubzhFormat::loadShape6(const core::String &filename, const Header &header,
bool hasPivot = false;
bool sizeChunkFound = false;
bool paletteFound = false;
bool nameFound = false;
while (!stream.eos()) {
if (stream.remaining() == 4 && nameFound) {
// there is a bug in the calculation of the uncompressed size in cubzh that writes a few bytes
// too much for the size of the name chunk
break;
}
Log::debug("Remaining sub stream data: %d", (int)stream.remaining());
Chunk subChunk;
wrapBool(loadSubChunkHeader(stream, subChunk))
wrapBool(loadSubChunkHeader(header, stream, subChunk))
core::DynamicArray<uint8_t> volumeBuffer; // used in case the size chunk is late
switch (subChunk.chunkId) {
case priv::CHUNK_ID_SHAPE_ID_V6:
Expand All @@ -463,7 +468,7 @@ bool CubzhFormat::loadShape6(const core::String &filename, const Header &header,
Log::debug("Load parent id %u", parentShapeId);
break;
case priv::CHUNK_ID_SHAPE_TRANSFORM_V6: {
Log::debug("Load transform");
Log::debug("Load local transform");
wrap(stream.readFloat(pos.x))
wrap(stream.readFloat(pos.y))
wrap(stream.readFloat(pos.z))
Expand Down Expand Up @@ -508,10 +513,11 @@ bool CubzhFormat::loadShape6(const core::String &filename, const Header &header,
}
case priv::CHUNK_ID_SHAPE_NAME_V6: {
core::String name;
stream.readString(subChunk.chunkSize, name);
wrapBool(stream.readString(subChunk.chunkSize, name));
if (!name.empty()) {
node.setName(name);
}
nameFound = true;
Log::debug("Load node name: %s", name.c_str());
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/voxelformat/private/cubzh/CubzhFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ class CubzhFormat : public PaletteFormat {
scenegraph::SceneGraph &sceneGraph, palette::Palette &palette,
const LoadContext &ctx) override;
bool loadChunkHeader(const Header &header, io::ReadStream &stream, Chunk &chunk) const;
bool loadSubChunkHeader(io::ReadStream &stream, Chunk &chunk) const;
bool loadSubChunkHeader(const Header &header, io::ReadStream &stream, Chunk &chunk) const;
bool loadHeader(io::SeekableReadStream &stream, Header &header) const;
bool loadSkipChunk(const Header &header, const Chunk &chunk, io::ReadStream &stream) const;
bool loadSkipSubChunk(const Chunk &chunk, io::ReadStream &stream) const;
Expand Down

0 comments on commit beaf53b

Please sign in to comment.