Skip to content

Commit 7b0d4b5

Browse files
committed
refactor chunk compression
1 parent b6404aa commit 7b0d4b5

4 files changed

Lines changed: 24 additions & 38 deletions

File tree

libsave/src/GameTypes/Save/Blueprint.cpp

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,5 @@ void SatisfactorySave::Blueprint::save(const std::filesystem::path& filepath) {
6060

6161
fileAr << header;
6262

63-
// Split blob into chunks
64-
uint64_t blob_pos = 0;
65-
const std::byte* blob_buffer = ar.buffer_view().data();
66-
67-
while (blob_size > 0) {
68-
// Compress chunk
69-
int64_t chunk_size = std::min(static_cast<int64_t>(blob_size), ChunkHeader::COMPRESSION_CHUNK_SIZE);
70-
std::vector<std::byte> chunk_uncompressed{blob_buffer + blob_pos, blob_buffer + blob_pos + chunk_size};
71-
std::vector<std::byte> chunk_compressed = zlibCompress(chunk_uncompressed);
72-
73-
blob_pos += chunk_size;
74-
blob_size -= chunk_size;
75-
76-
// Chunk header
77-
ChunkHeader chunkHeader(static_cast<int64_t>(chunk_compressed.size()), chunk_size);
78-
fileAr << chunkHeader;
79-
80-
fileAr.write_buffer(chunk_compressed);
81-
}
63+
compressChunks(fileAr, ar.buffer_view());
8264
}

libsave/src/GameTypes/Save/ChunkHelper.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,20 @@ std::vector<std::byte> SatisfactorySave::decompressChunks(IStreamArchive& inAr)
2828

2929
return file_data_blob;
3030
}
31+
32+
void SatisfactorySave::compressChunks(OStreamArchive& outAr, std::span<const std::byte> blob) {
33+
// Split blob into chunks
34+
while (!blob.empty()) {
35+
// Compress chunk
36+
int64_t chunk_size = std::min(static_cast<int64_t>(blob.size()), ChunkHeader::COMPRESSION_CHUNK_SIZE);
37+
std::vector<std::byte> chunk_compressed = zlibCompress(blob.first(chunk_size));
38+
39+
blob = blob.subspan(chunk_size);
40+
41+
// Chunk header
42+
ChunkHeader chunkHeader(static_cast<int64_t>(chunk_compressed.size()), chunk_size);
43+
outAr << chunkHeader;
44+
45+
outAr.write_buffer(chunk_compressed);
46+
}
47+
}

libsave/src/GameTypes/Save/ChunkHelper.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
#include <cstddef>
44
#include <memory>
5+
#include <span>
56
#include <vector>
67

78
#include "ChunkHeader.h"
89
#include "IO/Archive/IStreamArchive.h"
10+
#include "IO/Archive/OStreamArchive.h"
911

1012
namespace SatisfactorySave {
1113

@@ -20,4 +22,6 @@ namespace SatisfactorySave {
2022
};
2123

2224
std::vector<std::byte> decompressChunks(IStreamArchive& inAr);
25+
26+
void compressChunks(OStreamArchive& outAr, std::span<const std::byte> blob);
2327
} // namespace SatisfactorySave

libsave/src/GameTypes/Save/SaveGame.cpp

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -150,25 +150,8 @@ void SatisfactorySave::SaveGame::save(const std::filesystem::path& filepath) {
150150
// Write header
151151
fileAr << mSaveHeader;
152152

153-
// Split blob into chunks
154-
uint64_t blob_pos = 0;
155-
const std::byte* blob_buffer = ar.buffer_view().data();
156-
157-
while (blob_size > 0) {
158-
// Compress chunk
159-
int64_t chunk_size = std::min(static_cast<int64_t>(blob_size), ChunkHeader::COMPRESSION_CHUNK_SIZE);
160-
std::vector<std::byte> chunk_uncompressed{blob_buffer + blob_pos, blob_buffer + blob_pos + chunk_size};
161-
std::vector<std::byte> chunk_compressed = zlibCompress(chunk_uncompressed);
162-
163-
blob_pos += chunk_size;
164-
blob_size -= chunk_size;
165-
166-
// Chunk header
167-
ChunkHeader chunkHeader(static_cast<int64_t>(chunk_compressed.size()), chunk_size);
168-
fileAr << chunkHeader;
169-
170-
fileAr.write_buffer(chunk_compressed);
171-
}
153+
// Write blob
154+
compressChunks(fileAr, ar.buffer_view());
172155
}
173156

174157
void SatisfactorySave::SaveGame::initAccessStructures() {

0 commit comments

Comments
 (0)