diff --git a/apps/niftest/niftest.cpp b/apps/niftest/niftest.cpp index e18da8e3f6..fe60238cd5 100644 --- a/apps/niftest/niftest.cpp +++ b/apps/niftest/niftest.cpp @@ -42,29 +42,10 @@ bool isBSA(const std::filesystem::path& filename) return hasExtension(filename, ".bsa") || hasExtension(filename, ".ba2"); } -std::unique_ptr makeBsaArchive(const std::filesystem::path& path) -{ - switch (Bsa::BSAFile::detectVersion(path)) - { - case Bsa::BSAVER_COMPRESSED: - return std::make_unique::type>(path); - case Bsa::BSAVER_BA2_GNRL: - return std::make_unique::type>(path); - case Bsa::BSAVER_BA2_DX10: - return std::make_unique::type>(path); - case Bsa::BSAVER_UNCOMPRESSED: - return std::make_unique::type>(path); - case Bsa::BSAVER_UNKNOWN: - default: - std::cerr << "'" << Files::pathToUnicodeString(path) << "' is not a recognized BSA archive" << std::endl; - return nullptr; - } -} - std::unique_ptr makeArchive(const std::filesystem::path& path) { if (isBSA(path)) - return makeBsaArchive(path); + return VFS::makeBsaArchive(path); if (std::filesystem::is_directory(path)) return std::make_unique(path); return nullptr; @@ -124,17 +105,23 @@ void readVFS(std::unique_ptr&& archive, const std::filesystem::pat if (!archivePath.empty() && !isBSA(archivePath)) { - Files::PathContainer dataDirs = { archivePath }; - const Files::Collections fileCollections = Files::Collections(dataDirs); + const Files::Collections fileCollections({ archivePath }); const Files::MultiDirCollection& bsaCol = fileCollections.getCollection(".bsa"); const Files::MultiDirCollection& ba2Col = fileCollections.getCollection(".ba2"); - for (auto& file : bsaCol) + for (const Files::MultiDirCollection& collection : { bsaCol, ba2Col }) { - readVFS(makeBsaArchive(file.second), file.second, quiet); - } - for (auto& file : ba2Col) - { - readVFS(makeBsaArchive(file.second), file.second, quiet); + for (auto& file : collection) + { + try + { + readVFS(VFS::makeBsaArchive(file.second), file.second, quiet); + } + catch (const std::exception& e) + { + std::cerr << "Failed to read archive file '" << Files::pathToUnicodeString(file.second) + << "': " << e.what() << std::endl; + } + } } } } diff --git a/components/vfs/bsaarchive.hpp b/components/vfs/bsaarchive.hpp index 2276933684..83a68a0589 100644 --- a/components/vfs/bsaarchive.hpp +++ b/components/vfs/bsaarchive.hpp @@ -11,6 +11,8 @@ #include #include +#include +#include namespace VFS { @@ -73,34 +75,24 @@ namespace VFS std::vector mFiles; }; - template - struct ArchiveSelector + inline std::unique_ptr makeBsaArchive(const std::filesystem::path& path) { - }; - - template <> - struct ArchiveSelector - { - using type = BsaArchive; - }; - - template <> - struct ArchiveSelector - { - using type = BsaArchive; - }; - - template <> - struct ArchiveSelector - { - using type = BsaArchive; - }; + switch (Bsa::BSAFile::detectVersion(path)) + { + case Bsa::BSAVER_UNKNOWN: + break; + case Bsa::BSAVER_UNCOMPRESSED: + return std::make_unique>(path); + case Bsa::BSAVER_COMPRESSED: + return std::make_unique>(path); + case Bsa::BSAVER_BA2_GNRL: + return std::make_unique>(path); + case Bsa::BSAVER_BA2_DX10: + return std::make_unique>(path); + } - template <> - struct ArchiveSelector - { - using type = BsaArchive; - }; + throw std::runtime_error("Unknown archive type '" + Files::pathToUnicodeString(path) + "'"); + } } #endif diff --git a/components/vfs/registerarchives.cpp b/components/vfs/registerarchives.cpp index 9dbe878bca..f017b5f73c 100644 --- a/components/vfs/registerarchives.cpp +++ b/components/vfs/registerarchives.cpp @@ -25,18 +25,7 @@ namespace VFS // Last BSA has the highest priority const auto archivePath = collections.getPath(*archive); Log(Debug::Info) << "Adding BSA archive " << archivePath; - Bsa::BsaVersion bsaVersion = Bsa::BSAFile::detectVersion(archivePath); - - if (bsaVersion == Bsa::BSAVER_COMPRESSED) - vfs->addArchive(std::make_unique::type>(archivePath)); - else if (bsaVersion == Bsa::BSAVER_BA2_GNRL) - vfs->addArchive(std::make_unique::type>(archivePath)); - else if (bsaVersion == Bsa::BSAVER_BA2_DX10) - vfs->addArchive(std::make_unique::type>(archivePath)); - else if (bsaVersion == Bsa::BSAVER_UNCOMPRESSED) - vfs->addArchive(std::make_unique::type>(archivePath)); - else - throw std::runtime_error("Unknown archive type '" + *archive + "'"); + vfs->addArchive(makeBsaArchive(archivePath)); } else {