mirror of
https://github.com/HarbourMasters/Shipwright.git
synced 2025-08-20 13:23:45 -07:00
Fix crash if LoadFile fails
This commit is contained in:
parent
ceef4a9453
commit
85ea0c6706
1 changed files with 13 additions and 13 deletions
|
@ -49,11 +49,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);
|
||||||
|
@ -67,18 +72,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;
|
||||||
|
@ -92,6 +92,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);
|
||||||
|
@ -116,18 +121,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;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue