From 19fbba080da522ec4fb3373911088d289d4a4d62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Mocquillon?= Date: Mon, 6 Mar 2023 21:26:51 +0100 Subject: [PATCH] Add a check if the requested file is not found --- components/bsa/ba2dx10file.cpp | 16 +++++++++------- components/bsa/ba2dx10file.hpp | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/components/bsa/ba2dx10file.cpp b/components/bsa/ba2dx10file.cpp index 4ec8359b85..af49c277ad 100644 --- a/components/bsa/ba2dx10file.cpp +++ b/components/bsa/ba2dx10file.cpp @@ -141,7 +141,7 @@ namespace Bsa mIsLoaded = true; } - BA2DX10File::FileRecord BA2DX10File::getFileRecord(const std::string& str) const + std::optional BA2DX10File::getFileRecord(const std::string& str) const { for (const auto c : str) { @@ -169,13 +169,13 @@ namespace Bsa uint32_t folderHash = generateHash(folder); auto it = mFolders.find(folderHash); if (it == mFolders.end()) - return FileRecord(); // folder not found, return default which has offset of sInvalidOffset + return std::nullopt; // folder not found uint32_t fileHash = generateHash(fileName); uint32_t extHash = *reinterpret_cast(ext.data() + 1); auto iter = it->second.find({ fileHash, extHash }); if (iter == it->second.end()) - return FileRecord(); // file not found, return default which has offset of sInvalidOffset + return std::nullopt; // file not found return iter->second; } @@ -221,8 +221,9 @@ namespace Bsa Files::IStreamPtr BA2DX10File::getFile(const FileStruct* file) { - FileRecord fileRec = getFileRecord(file->name()); - return getFile(fileRec); + if (auto fileRec = getFileRecord(file->name()); fileRec) + return getFile(*fileRec); + fail("File not found: " + std::string(file->name())); } void BA2DX10File::addFile(const std::string& filename, std::istream& file) @@ -233,8 +234,9 @@ namespace Bsa Files::IStreamPtr BA2DX10File::getFile(const char* file) { - FileRecord fileRec = getFileRecord(file); - return getFile(fileRec); + if (auto fileRec = getFileRecord(file); fileRec) + return getFile(*fileRec); + fail("File not found: " + std::string(file)); } constexpr const uint32_t DDSD_CAPS = 0x00000001; diff --git a/components/bsa/ba2dx10file.hpp b/components/bsa/ba2dx10file.hpp index 398e555d30..59db10745b 100644 --- a/components/bsa/ba2dx10file.hpp +++ b/components/bsa/ba2dx10file.hpp @@ -41,7 +41,7 @@ namespace Bsa std::list> mFileNames; - FileRecord getFileRecord(const std::string& str) const; + std::optional getFileRecord(const std::string& str) const; Files::IStreamPtr getFile(const FileRecord& fileRecord);