Skip to content
Open
Changes from 5 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
84 changes: 76 additions & 8 deletions src/client/minimap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,73 @@

Minimap g_minimap;

namespace {
Comment thread
divinity76 marked this conversation as resolved.
class FlockGuard {
private:
FlockGuard(const FlockGuard&) = delete;
FlockGuard& operator=(const FlockGuard&) = delete;
bool successfullyLocked = false;
#ifdef WIN32
HANDLE fileHandle = INVALID_HANDLE_VALUE;
#else
int fileHandle = -1;
#endif
public:
FlockGuard(const std::string& filename, const bool exclusive) {
#ifdef WIN32
this->fileHandle = CreateFileA(filename.c_str(), GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (this->fileHandle == INVALID_HANDLE_VALUE) {
g_logger.error(stdext::format("Failed to open file for locking: %s", filename.data()));
return;
}
OVERLAPPED ov = { 0 };
Comment thread
divinity76 marked this conversation as resolved.
DWORD flags = exclusive ? LOCKFILE_EXCLUSIVE_LOCK : 0;
if (!LockFileEx(this->fileHandle, flags, 0, MAXDWORD, MAXDWORD, &ov)) {
g_logger.error(stdext::format("Failed to lock file: %s", filename.data()));
CloseHandle(this->fileHandle);
Comment thread
divinity76 marked this conversation as resolved.
this->fileHandle = INVALID_HANDLE_VALUE;
Comment thread
divinity76 marked this conversation as resolved.
Outdated
return;
}
this->successfullyLocked = true;
Comment thread
divinity76 marked this conversation as resolved.
#else
this->fileHandle = open(filename.c_str(), O_RDWR | O_CREAT | O_CLOEXEC, 0666);
if (this->fileHandle < 0) {
g_logger.error(stdext::format("Failed to open file for locking: %s", filename.data()));
return;
}
int lockSuccess = flock(this->fileHandle, exclusive ? LOCK_EX : LOCK_SH);
if (lockSuccess != 0) {
g_logger.error(stdext::format("Failed to lock file: %s", filename.data()));
close(this->fileHandle);
this->fileHandle = -1;
Comment thread
divinity76 marked this conversation as resolved.
Outdated
return;
}
Comment thread
divinity76 marked this conversation as resolved.
this->successfullyLocked = true;
#endif
Comment thread
divinity76 marked this conversation as resolved.
}
Comment thread
divinity76 marked this conversation as resolved.
~FlockGuard() {
#ifdef WIN32
if (this->successfullyLocked) {
OVERLAPPED ov = { 0 };
UnlockFileEx(this->fileHandle, 0, MAXDWORD, MAXDWORD, &ov);
}
if (this->fileHandle != INVALID_HANDLE_VALUE) {
CloseHandle(this->fileHandle);
this->fileHandle = INVALID_HANDLE_VALUE;
Comment thread
divinity76 marked this conversation as resolved.
Outdated
}
#else
if (this->successfullyLocked) {
flock(this->fileHandle, LOCK_UN);
}
if (this->fileHandle >= 0) {
close(this->fileHandle);
this->fileHandle = -1;
Comment thread
divinity76 marked this conversation as resolved.
Outdated
}
#endif
}
};
Comment thread
divinity76 marked this conversation as resolved.
Comment thread
divinity76 marked this conversation as resolved.
}
Comment thread
divinity76 marked this conversation as resolved.
void MinimapBlock::clean()
{
m_tiles.fill(MinimapTile());
Expand Down Expand Up @@ -332,8 +399,10 @@ void Minimap::saveImage(const std::string& fileName, int minX, int minY, int max

bool Minimap::loadOtmm(const std::string& fileName)
{
const std::string filePath = g_resources.resolvePath(fileName);
auto flockGuard = FlockGuard(filePath, false);
Comment thread
divinity76 marked this conversation as resolved.
Outdated
Comment thread
divinity76 marked this conversation as resolved.
Outdated
try {
FileStreamPtr fin = g_resources.openFile(fileName, g_game.getFeature(Otc::GameDontCacheFiles));
FileStreamPtr fin = g_resources.openFile(filePath, g_game.getFeature(Otc::GameDontCacheFiles));
Comment thread
divinity76 marked this conversation as resolved.
if(!fin)
stdext::throw_exception("unable to open file");
Comment thread
divinity76 marked this conversation as resolved.
Outdated

Expand Down Expand Up @@ -393,15 +462,17 @@ bool Minimap::loadOtmm(const std::string& fileName)

void Minimap::saveOtmm(const std::string& fileName)
{
const std::string filePath = g_resources.resolvePath(fileName);
auto flockGuard = FlockGuard(filePath, true);
Comment thread
divinity76 marked this conversation as resolved.
Outdated
Comment thread
divinity76 marked this conversation as resolved.
Outdated
try {
stdext::timer saveTimer;

#ifndef ANDROID
std::string tmpFileName = fileName;
tmpFileName += ".tmp";
FileStreamPtr fin = g_resources.createFile(tmpFileName);
std::string tmpFilePath = filePath;
tmpFilePath += ".tmp";
FileStreamPtr fin = g_resources.createFile(tmpFilePath);
#else
FileStreamPtr fin = g_resources.createFile(fileName);
FileStreamPtr fin = g_resources.createFile(filePath);
#endif
Comment thread
divinity76 marked this conversation as resolved.
Outdated

//TODO: compression flag with zlib
Expand Down Expand Up @@ -456,9 +527,6 @@ void Minimap::saveOtmm(const std::string& fileName)

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);
}
Expand Down
Loading