Skip to content

Commit db92b04

Browse files
committed
Fixed my shitcode that wasn't threadsafe at all
1 parent 01b7a78 commit db92b04

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

Source/Managers/ActivityMan.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ bool ActivityMan::SaveCurrentGame(const std::string& fileName) {
185185
zipWriteInFileInZip(zippedSaveFile, mainStreamView.data(), mainStreamView.size());
186186
zipCloseFileInZip(zippedSaveFile);
187187

188+
std::vector<SDL_IOStream*> pngStreams;
189+
pngStreams.resize(sceneLayerInfos->size());
190+
191+
// Generates PNGs (this is thread-safe)
188192
std::for_each(std::execution::par_unseq,
189193
sceneLayerInfos->begin(), sceneLayerInfos->end(),
190194
[&](const SceneLayerInfo& layerInfo) {
@@ -206,20 +210,28 @@ bool ActivityMan::SaveCurrentGame(const std::string& fileName) {
206210
return;
207211
}
208212

209-
// Actually get the memory
210-
void* buffer = SDL_GetPointerProperty(SDL_GetIOProperties(stream), SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, nullptr);
211-
size_t size = static_cast<size_t>(SDL_GetIOSize(stream));
212-
if (!buffer || size < 0) {
213-
g_ConsoleMan.PrintString("ERROR: Failed to save scenelayers to PNG!");
214-
return;
215-
}
213+
size_t i = &layerInfo - &(*sceneLayerInfos->begin());
214+
pngStreams[i] = stream;
215+
});
216216

217-
zipOpenNewFileInZip(zippedSaveFile, ("Save " + layerInfo.name + ".png").c_str(), &zfi, nullptr, 0, nullptr, 0, nullptr, HACK_MZ_COMPRESS_METHOD_STORE, HACK_MZ_COMPRESS_LEVEL_FAST);
218-
zipWriteInFileInZip(zippedSaveFile, static_cast<const char*>(buffer), size);
219-
zipCloseFileInZip(zippedSaveFile);
217+
// Actually save to the zip (this bit isn't thread-safe)
218+
for (int i = 0; i < pngStreams.size(); ++i) {
219+
SDL_IOStream* stream = pngStreams[i];
220+
221+
// Actually get the memory
222+
void* buffer = SDL_GetPointerProperty(SDL_GetIOProperties(stream), SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, nullptr);
223+
size_t size = static_cast<size_t>(SDL_GetIOSize(stream));
224+
if (!stream || size < 0) {
225+
g_ConsoleMan.PrintString("ERROR: Failed to save scenelayers to PNG!");
226+
continue;
227+
}
220228

221-
SDL_CloseIO(stream);
222-
});
229+
zipOpenNewFileInZip(zippedSaveFile, ("Save " + (*sceneLayerInfos)[i].name + ".png").c_str(), &zfi, nullptr, 0, nullptr, 0, nullptr, HACK_MZ_COMPRESS_METHOD_STORE, HACK_MZ_COMPRESS_LEVEL_FAST);
230+
zipWriteInFileInZip(zippedSaveFile, static_cast<const char*>(buffer), size);
231+
zipCloseFileInZip(zippedSaveFile);
232+
233+
SDL_CloseIO(stream);
234+
}
223235

224236
zipClose(zippedSaveFile, fileName.c_str());
225237

0 commit comments

Comments
 (0)