summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGaryOderNichts <12049776+GaryOderNichts@users.noreply.github.com>2022-05-11 19:37:10 +0200
committerGitHub <noreply@github.com>2022-05-11 13:37:10 -0400
commit108cd8efc8801ecaaa10a1ebc3abd8312b87ecc6 (patch)
treef679a92ef6949c5bf6bc5b33d54a8cdb62ae48dd
parentfe3e53493850c2f7f897d27a31dc8bee916ce50c (diff)
Fix crash if LoadFile fails (#265)
-rw-r--r--libultraship/libultraship/Archive.cpp26
1 files changed, 13 insertions, 13 deletions
diff --git a/libultraship/libultraship/Archive.cpp b/libultraship/libultraship/Archive.cpp
index 67ec8831a..1e8f8d597 100644
--- a/libultraship/libultraship/Archive.cpp
+++ b/libultraship/libultraship/Archive.cpp
@@ -54,11 +54,16 @@ namespace Ship {
std::shared_ptr<File> Archive::LoadFile(const std::string& filePath, bool includeParent, std::shared_ptr<File> FileToLoad) {
HANDLE fileHandle = NULL;
+ if (FileToLoad == nullptr) {
+ FileToLoad = std::make_shared<File>();
+ FileToLoad->path = filePath;
+ }
+
if (!SFileOpenFileEx(mainMPQ, filePath.c_str(), 0, &fileHandle)) {
SPDLOG_ERROR("({}) Failed to open file {} from mpq archive {}", GetLastError(), filePath.c_str(), MainPath.c_str());
std::unique_lock<std::mutex> Lock(FileToLoad->FileLoadMutex);
FileToLoad->bHasLoadError = true;
- return nullptr;
+ return FileToLoad;
}
DWORD dwFileSize = SFileGetFileSize(fileHandle, 0);
@@ -72,18 +77,13 @@ namespace Ship {
}
std::unique_lock<std::mutex> Lock(FileToLoad->FileLoadMutex);
FileToLoad->bHasLoadError = true;
- return nullptr;
+ return FileToLoad;
}
if (!SFileCloseFile(fileHandle)) {
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);
FileToLoad->parent = includeParent ? shared_from_this() : nullptr;
FileToLoad->buffer = fileData;
@@ -97,6 +97,11 @@ namespace Ship {
HANDLE fileHandle = NULL;
HANDLE mpqHandle = NULL;
+ if (FileToLoad == nullptr) {
+ FileToLoad = std::make_shared<File>();
+ FileToLoad->path = filePath;
+ }
+
for(auto [path, handle] : mpqHandles) {
if (SFileOpenFileEx(mpqHandle, filePath.c_str(), 0, &fileHandle)) {
std::unique_lock Lock(FileToLoad->FileLoadMutex);
@@ -121,18 +126,13 @@ namespace Ship {
}
std::unique_lock<std::mutex> Lock(FileToLoad->FileLoadMutex);
FileToLoad->bHasLoadError = true;
- return nullptr;
+ return FileToLoad;
}
if (!SFileCloseFile(fileHandle)) {
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);
FileToLoad->parent = includeParent ? shared_from_this() : nullptr;
FileToLoad->buffer = fileData;