Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 42 additions & 13 deletions src/client/minimap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,8 @@ void Minimap::saveOtmm(const std::string& fileName)
try {
stdext::timer saveTimer;

#ifndef ANDROID
std::string tmpFileName = fileName;
tmpFileName += ".tmp";
std::string tmpFileName = fileName + ".tmp";
FileStreamPtr fin = g_resources.createFile(tmpFileName);
#else
FileStreamPtr fin = g_resources.createFile(fileName);
#endif

//TODO: compression flag with zlib
uint32 flags = 0;
Expand Down Expand Up @@ -437,17 +432,51 @@ void Minimap::saveOtmm(const std::string& fileName)
fin->addU16(invalidPos.y);
fin->addU8(invalidPos.z);

fin->addU32(OTMM_VALID); // Flags the file completed.

fin->flush();

fin->close();
#ifndef ANDROID
std::filesystem::path filePath(g_resources.getWriteDir()), tmpFilePath(g_resources.getWriteDir());
filePath += fileName;
tmpFilePath += tmpFileName;
if(std::filesystem::file_size(tmpFilePath) > 1024) {
std::filesystem::rename(tmpFilePath, filePath);


std::string writeDir = g_resources.getWriteDir();
std::string finalPath = writeDir + fileName;
std::string tempPath = writeDir + tmpFileName;

bool isTempValid = false;

FileStreamPtr checkFile = g_resources.openFile(tmpFileName);
if (checkFile && checkFile->size() >= 4) {
checkFile->seek(checkFile->size() - 4);
uint32 marker = checkFile->getU32();
if (marker == OTMM_VALID) {
isTempValid = true;
}
else {
throw stdext::exception("Failed to validate OTMM temp file.");
}
}
#endif

// If temp file is valid.
if (isTempValid) {
if (std::filesystem::exists(finalPath)) {
auto existingSize = std::filesystem::file_size(finalPath);
auto newSize = std::filesystem::file_size(tempPath);
if (newSize > existingSize) { // MC-handling, check if the size of the otmm map is bigg
std::filesystem::rename(tempPath, finalPath);
}
else {
std::filesystem::remove(tempPath);
}
}
else {
std::filesystem::rename(tempPath, finalPath);
}
}




} catch (stdext::exception& e) {
g_logger.error(stdext::format("failed to save OTMM minimap: %s", e.what()));
} catch (std::exception& e) {
Expand Down
3 changes: 2 additions & 1 deletion src/client/minimap.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
enum {
MMBLOCK_SIZE = 64,
OTMM_SIGNATURE = 0x4D4d544F,
OTMM_VERSION = 1
OTMM_VERSION = 1,
OTMM_VALID = 0x444F4E45
};

enum MinimapTileFlags {
Expand Down