From 10f8f46ca621729638387bc378b826f1bfc0f7d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Wed, 18 Nov 2015 22:22:09 +0100 Subject: [PATCH] Fixed CanvasPortion using the same temp files across processes --- TODO | 1 - src/render/software/CanvasPortion.cpp | 17 ++++++++++++++++- src/system/FileSystem.cpp | 7 +++++++ src/system/FileSystem.h | 5 +++++ src/system/PackStream.cpp | 2 +- src/system/System.cpp | 5 +++++ src/system/System.h | 8 ++++++++ 7 files changed, 42 insertions(+), 3 deletions(-) diff --git a/TODO b/TODO index 2b172a0..c88912f 100644 --- a/TODO +++ b/TODO @@ -6,7 +6,6 @@ Technlology Preview 2 : - Refactor medium traversal to unify clouds, atmosphere and god rays. - Fix potential holes in land rendering (OpenGL and software). - Fix sun size not being consistent between opengl and software -- Fix CanvasPortion saves colliding on disk when running several instances Technology Preview 3 : - Alter aerial perspective using estimation of the amount of light left after cloud layers traversal. diff --git a/src/render/software/CanvasPortion.cpp b/src/render/software/CanvasPortion.cpp index 932424d..1af36cc 100644 --- a/src/render/software/CanvasPortion.cpp +++ b/src/render/software/CanvasPortion.cpp @@ -1,11 +1,14 @@ #include "CanvasPortion.h" #include +#include +#include #include "CanvasPixel.h" #include "CanvasPreview.h" #include "PackStream.h" #include "FileSystem.h" +#include "System.h" #define CHECK_COORDINATES() \ assert(x >= 0); \ @@ -14,6 +17,15 @@ assert(y < height); \ assert(pixels != NULL) +// Keep track of created files to erase them at program exit +static std::vector _files; +static void clean_all_files() { + for (auto &filepath : _files) { + FileSystem::removeFile(filepath); + } +} +static int _atexit = std::atexit(clean_all_files); + CanvasPortion::CanvasPortion(int index, CanvasPreview *preview) : index(index), preview(preview) { width = 1; height = 1; @@ -73,7 +85,9 @@ void CanvasPortion::discardPixels(bool save) { void CanvasPortion::saveToDisk() { if (pixels) { - filepath = FileSystem::getTempFile("paysages_portion_" + std::to_string(index) + ".dat"); + auto pid = System::getProcessId(); + filepath = + FileSystem::getTempFile("paysages_portion_" + std::to_string(index) + "_" + std::to_string(pid) + ".dat"); PackStream stream; stream.bindToFile(filepath, true); stream.write(&width); @@ -83,6 +97,7 @@ void CanvasPortion::saveToDisk() { pixels[y * width + x].getComposite().save(&stream); } } + _files.push_back(filepath); } } diff --git a/src/system/FileSystem.cpp b/src/system/FileSystem.cpp index 11d31ec..8d1d961 100644 --- a/src/system/FileSystem.cpp +++ b/src/system/FileSystem.cpp @@ -2,6 +2,7 @@ #include #include +#include std::string FileSystem::getTempFile(const std::string &filename) { return QDir::temp().filePath(QString::fromStdString(filename)).toStdString(); @@ -10,3 +11,9 @@ std::string FileSystem::getTempFile(const std::string &filename) { bool FileSystem::isFile(const std::string &filepath) { return QFileInfo(QString::fromStdString(filepath)).exists(); } + +bool FileSystem::removeFile(const std::string &filepath) { + if (FileSystem::isFile(filepath)) { + remove(filepath.c_str()); + } +} diff --git a/src/system/FileSystem.h b/src/system/FileSystem.h index 5e8963a..2acd06c 100644 --- a/src/system/FileSystem.h +++ b/src/system/FileSystem.h @@ -19,6 +19,11 @@ class SYSTEMSHARED_EXPORT FileSystem { * Returns true if the given path points to a file. */ static bool isFile(const std::string &filepath); + + /** + * Remove a file by its absolute path. + */ + static bool removeFile(const std::string &filepath); }; } } diff --git a/src/system/PackStream.cpp b/src/system/PackStream.cpp index 0e90ef4..1716866 100644 --- a/src/system/PackStream.cpp +++ b/src/system/PackStream.cpp @@ -133,6 +133,6 @@ void PackStream::skip(const double &value, int count) { stream->skipRawData(sizeof(value) * count); } -void paysages::system::PackStream::skipBytes(int bytes) { +void PackStream::skipBytes(int bytes) { stream->skipRawData(bytes); } diff --git a/src/system/System.cpp b/src/system/System.cpp index 3eb559e..7a79d7a 100644 --- a/src/system/System.cpp +++ b/src/system/System.cpp @@ -1,6 +1,7 @@ #include "System.h" #include +#include static int core_count = -1; @@ -18,3 +19,7 @@ int System::getCoreCount() { } return core_count; } + +int System::getProcessId() { + return QCoreApplication::applicationPid(); +} diff --git a/src/system/System.h b/src/system/System.h index 9c5d4d6..4ea3ba9 100644 --- a/src/system/System.h +++ b/src/system/System.h @@ -11,7 +11,15 @@ namespace system { */ class SYSTEMSHARED_EXPORT System { public: + /** + * Get the number of CPU cores available on current system. + */ static int getCoreCount(); + + /** + * Get an identifying number for the current process. + */ + static int getProcessId(); }; } }