Fix crash if LoadFile fails (#265)

This commit is contained in:
GaryOderNichts 2022-05-11 19:37:10 +02:00 committed by GitHub
parent fe3e534938
commit 108cd8efc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -54,11 +54,16 @@ namespace Ship {
std::shared_ptr<File> Archive::LoadFile(const std::string& filePath, bool includeParent, std::shared_ptr<File> FileToLoad) { std::shared_ptr<File> Archive::LoadFile(const std::string& filePath, bool includeParent, std::shared_ptr<File> FileToLoad) {
HANDLE fileHandle = NULL; HANDLE fileHandle = NULL;
if (FileToLoad == nullptr) {
FileToLoad = std::make_shared<File>();
FileToLoad->path = filePath;
}
if (!SFileOpenFileEx(mainMPQ, filePath.c_str(), 0, &fileHandle)) { if (!SFileOpenFileEx(mainMPQ, filePath.c_str(), 0, &fileHandle)) {
SPDLOG_ERROR("({}) Failed to open file {} from mpq archive {}", GetLastError(), filePath.c_str(), MainPath.c_str()); SPDLOG_ERROR("({}) Failed to open file {} from mpq archive {}", GetLastError(), filePath.c_str(), MainPath.c_str());
std::unique_lock<std::mutex> Lock(FileToLoad->FileLoadMutex); std::unique_lock<std::mutex> Lock(FileToLoad->FileLoadMutex);
FileToLoad->bHasLoadError = true; FileToLoad->bHasLoadError = true;
return nullptr; return FileToLoad;
} }
DWORD dwFileSize = SFileGetFileSize(fileHandle, 0); DWORD dwFileSize = SFileGetFileSize(fileHandle, 0);
@ -72,18 +77,13 @@ namespace Ship {
} }
std::unique_lock<std::mutex> Lock(FileToLoad->FileLoadMutex); std::unique_lock<std::mutex> Lock(FileToLoad->FileLoadMutex);
FileToLoad->bHasLoadError = true; FileToLoad->bHasLoadError = true;
return nullptr; return FileToLoad;
} }
if (!SFileCloseFile(fileHandle)) { if (!SFileCloseFile(fileHandle)) {
SPDLOG_ERROR("({}) Failed to close file {} from mpq archive {}", GetLastError(), filePath.c_str(), MainPath.c_str()); SPDLOG_ERROR("({}) Failed to close file {} from mpq archive {}", GetLastError(), filePath.c_str(), MainPath.c_str());
} }
if (FileToLoad == nullptr) {
FileToLoad = std::make_shared<File>();
FileToLoad->path = filePath;
}
std::unique_lock<std::mutex> Lock(FileToLoad->FileLoadMutex); std::unique_lock<std::mutex> Lock(FileToLoad->FileLoadMutex);
FileToLoad->parent = includeParent ? shared_from_this() : nullptr; FileToLoad->parent = includeParent ? shared_from_this() : nullptr;
FileToLoad->buffer = fileData; FileToLoad->buffer = fileData;
@ -97,6 +97,11 @@ namespace Ship {
HANDLE fileHandle = NULL; HANDLE fileHandle = NULL;
HANDLE mpqHandle = NULL; HANDLE mpqHandle = NULL;
if (FileToLoad == nullptr) {
FileToLoad = std::make_shared<File>();
FileToLoad->path = filePath;
}
for(auto [path, handle] : mpqHandles) { for(auto [path, handle] : mpqHandles) {
if (SFileOpenFileEx(mpqHandle, filePath.c_str(), 0, &fileHandle)) { if (SFileOpenFileEx(mpqHandle, filePath.c_str(), 0, &fileHandle)) {
std::unique_lock Lock(FileToLoad->FileLoadMutex); std::unique_lock Lock(FileToLoad->FileLoadMutex);
@ -121,18 +126,13 @@ namespace Ship {
} }
std::unique_lock<std::mutex> Lock(FileToLoad->FileLoadMutex); std::unique_lock<std::mutex> Lock(FileToLoad->FileLoadMutex);
FileToLoad->bHasLoadError = true; FileToLoad->bHasLoadError = true;
return nullptr; return FileToLoad;
} }
if (!SFileCloseFile(fileHandle)) { if (!SFileCloseFile(fileHandle)) {
SPDLOG_ERROR("({}) Failed to close file {} from mpq archive {}", GetLastError(), filePath.c_str(), MainPath.c_str()); SPDLOG_ERROR("({}) Failed to close file {} from mpq archive {}", GetLastError(), filePath.c_str(), MainPath.c_str());
} }
if (FileToLoad == nullptr) {
FileToLoad = std::make_shared<File>();
FileToLoad->path = filePath;
}
std::unique_lock<std::mutex> Lock(FileToLoad->FileLoadMutex); std::unique_lock<std::mutex> Lock(FileToLoad->FileLoadMutex);
FileToLoad->parent = includeParent ? shared_from_this() : nullptr; FileToLoad->parent = includeParent ? shared_from_this() : nullptr;
FileToLoad->buffer = fileData; FileToLoad->buffer = fileData;