From b61249841eaaaa2f193c598ad9b59116fb9e056d Mon Sep 17 00:00:00 2001 From: CedricMocquillon Date: Wed, 19 May 2021 18:38:42 +0200 Subject: [PATCH 1/2] Update lookup only after sorting files --- components/bsa/bsa_file.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/components/bsa/bsa_file.cpp b/components/bsa/bsa_file.cpp index 3375882f5..4dfaa8658 100644 --- a/components/bsa/bsa_file.cpp +++ b/components/bsa/bsa_file.cpp @@ -182,8 +182,6 @@ void BSAFile::readHeader() if(fs.offset + fs.fileSize > fsize) fail("Archive contains offsets outside itself"); - // Add the file name to the lookup - mLookup[fs.name()] = i; } mStringBuf.resize(endOfNameBuffer); @@ -191,6 +189,13 @@ void BSAFile::readHeader() return left.offset < right.offset; }); + for (size_t i = 0; i < filenum; i++) + { + FileStruct& fs = mFiles[i]; + // Add the file name to the lookup + mLookup[fs.name()] = i; + } + mIsLoaded = true; } From 86d137363e86a087287305bfc3f8a281ee2645ef Mon Sep 17 00:00:00 2001 From: CedricMocquillon Date: Thu, 20 May 2021 21:28:52 +0200 Subject: [PATCH 2/2] Check if the archive is opened before adding a file to it and clear state on close --- components/bsa/bsa_file.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/components/bsa/bsa_file.cpp b/components/bsa/bsa_file.cpp index 4dfaa8658..129c2bf45 100644 --- a/components/bsa/bsa_file.cpp +++ b/components/bsa/bsa_file.cpp @@ -252,6 +252,9 @@ int BSAFile::getIndex(const char *str) const /// Open an archive file. void BSAFile::open(const std::string &file) { + if (mIsLoaded) + close(); + mFilename = file; if(boost::filesystem::exists(file)) readHeader(); @@ -259,16 +262,20 @@ void BSAFile::open(const std::string &file) { { boost::filesystem::fstream(mFilename, std::ios::binary | std::ios::out); } writeHeader(); + mIsLoaded = true; } } /// Close the archive, write the updated headers to the file void Bsa::BSAFile::close() { - if (!mHasChanged) - return; + if (mHasChanged) + writeHeader(); - writeHeader(); + mFiles.clear(); + mStringBuf.clear(); + mLookup.clear(); + mIsLoaded = false; } Files::IStreamPtr BSAFile::getFile(const char *file) @@ -290,6 +297,8 @@ Files::IStreamPtr BSAFile::getFile(const FileStruct *file) void Bsa::BSAFile::addFile(const std::string& filename, std::istream& file) { + if (!mIsLoaded) + fail("Unable to add file " + filename + " the archive is not opened"); namespace bfs = boost::filesystem; auto newStartOfDataBuffer = 12 + (12 + 8) * (mFiles.size() + 1) + mStringBuf.size() + filename.size() + 1;