|
@@ -130,34 +130,37 @@ void FileAccessCompressed::_close() {
|
|
f->store_32(0); //compressed sizes, will update later
|
|
f->store_32(0); //compressed sizes, will update later
|
|
}
|
|
}
|
|
|
|
|
|
- Vector<int> block_sizes;
|
|
|
|
|
|
+ uint32_t last_block_size = write_max % block_size;
|
|
|
|
+
|
|
|
|
+ // Temporary buffer for compressed data blocks.
|
|
|
|
+ LocalVector<uint8_t> temp_cblock;
|
|
|
|
+ temp_cblock.resize(Compression::get_max_compressed_buffer_size(bc == 1 ? last_block_size : block_size, cmode));
|
|
|
|
+ uint8_t *temp_cblock_ptr = temp_cblock.ptr();
|
|
|
|
+
|
|
|
|
+ // Compress and store the blocks.
|
|
|
|
+ LocalVector<uint32_t> block_sizes;
|
|
for (uint32_t i = 0; i < bc; i++) {
|
|
for (uint32_t i = 0; i < bc; i++) {
|
|
- uint32_t bl = i == (bc - 1) ? write_max % block_size : block_size;
|
|
|
|
|
|
+ uint32_t bl = i == (bc - 1) ? last_block_size : block_size;
|
|
uint8_t *bp = &write_ptr[i * block_size];
|
|
uint8_t *bp = &write_ptr[i * block_size];
|
|
|
|
|
|
- Vector<uint8_t> cblock;
|
|
|
|
- cblock.resize(Compression::get_max_compressed_buffer_size(bl, cmode));
|
|
|
|
- const int64_t compressed_size = Compression::compress(cblock.ptrw(), bp, bl, cmode);
|
|
|
|
|
|
+ const int64_t compressed_size = Compression::compress(temp_cblock_ptr, bp, bl, cmode);
|
|
ERR_FAIL_COND_MSG(compressed_size < 0, "FileAccessCompressed: Error compressing data.");
|
|
ERR_FAIL_COND_MSG(compressed_size < 0, "FileAccessCompressed: Error compressing data.");
|
|
|
|
|
|
- f->store_buffer(cblock.ptr(), (uint64_t)compressed_size);
|
|
|
|
|
|
+ f->store_buffer(temp_cblock_ptr, (uint64_t)compressed_size);
|
|
block_sizes.push_back(compressed_size);
|
|
block_sizes.push_back(compressed_size);
|
|
}
|
|
}
|
|
|
|
|
|
f->seek(16); //ok write block sizes
|
|
f->seek(16); //ok write block sizes
|
|
for (uint32_t i = 0; i < bc; i++) {
|
|
for (uint32_t i = 0; i < bc; i++) {
|
|
- f->store_32(uint32_t(block_sizes[i]));
|
|
|
|
|
|
+ f->store_32(block_sizes[i]);
|
|
}
|
|
}
|
|
f->seek_end();
|
|
f->seek_end();
|
|
f->store_buffer((const uint8_t *)mgc.get_data(), mgc.length()); //magic at the end too
|
|
f->store_buffer((const uint8_t *)mgc.get_data(), mgc.length()); //magic at the end too
|
|
-
|
|
|
|
- buffer.clear();
|
|
|
|
-
|
|
|
|
} else {
|
|
} else {
|
|
comp_buffer.clear();
|
|
comp_buffer.clear();
|
|
- buffer.clear();
|
|
|
|
read_blocks.clear();
|
|
read_blocks.clear();
|
|
}
|
|
}
|
|
|
|
+ buffer.clear();
|
|
f.unref();
|
|
f.unref();
|
|
}
|
|
}
|
|
|
|
|