2015-07-22 02:08:37 +00:00
|
|
|
/// Program to test .nif files both on the FileSystem and in BSA archives.
|
|
|
|
|
2022-10-10 11:41:36 +00:00
|
|
|
#include <exception>
|
2022-05-22 16:56:14 +00:00
|
|
|
#include <filesystem>
|
2015-07-22 02:08:37 +00:00
|
|
|
#include <iostream>
|
2022-10-10 11:41:36 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2015-07-22 02:08:37 +00:00
|
|
|
|
2022-06-20 18:48:06 +00:00
|
|
|
#include <components/files/configurationmanager.hpp>
|
2015-07-22 02:08:37 +00:00
|
|
|
#include <components/files/constrainedfilestream.hpp>
|
2022-07-02 22:02:29 +00:00
|
|
|
#include <components/files/conversion.hpp>
|
2022-08-02 22:00:54 +00:00
|
|
|
#include <components/misc/strings/algorithm.hpp>
|
2015-07-22 02:08:37 +00:00
|
|
|
#include <components/nif/niffile.hpp>
|
2022-10-10 11:41:36 +00:00
|
|
|
#include <components/vfs/archive.hpp>
|
2015-07-22 02:08:37 +00:00
|
|
|
#include <components/vfs/bsaarchive.hpp>
|
2015-08-08 00:20:31 +00:00
|
|
|
#include <components/vfs/filesystemarchive.hpp>
|
2022-06-20 18:48:06 +00:00
|
|
|
#include <components/vfs/manager.hpp>
|
2024-01-15 23:30:41 +00:00
|
|
|
#include <components/vfs/recursivedirectoryiterator.hpp>
|
2015-07-22 02:08:37 +00:00
|
|
|
|
2015-08-08 00:06:33 +00:00
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
|
|
|
|
// Create local aliases for brevity
|
|
|
|
namespace bpo = boost::program_options;
|
2015-07-22 02:08:37 +00:00
|
|
|
|
|
|
|
/// See if the file has the named extension
|
2022-06-19 11:28:33 +00:00
|
|
|
bool hasExtension(const std::filesystem::path& filename, const std::string& extensionToFind)
|
2015-07-22 02:08:37 +00:00
|
|
|
{
|
2022-07-02 22:02:29 +00:00
|
|
|
const auto extension = Files::pathToUnicodeString(filename.extension());
|
2021-11-10 20:25:16 +00:00
|
|
|
return Misc::StringUtils::ciEqual(extension, extensionToFind);
|
2015-07-22 02:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// See if the file has the "nif" extension.
|
2022-06-19 11:28:33 +00:00
|
|
|
bool isNIF(const std::filesystem::path& filename)
|
2015-07-22 02:08:37 +00:00
|
|
|
{
|
2023-02-08 20:25:01 +00:00
|
|
|
return hasExtension(filename, ".nif");
|
2015-07-22 02:08:37 +00:00
|
|
|
}
|
|
|
|
/// See if the file has the "bsa" extension.
|
2022-06-19 11:28:33 +00:00
|
|
|
bool isBSA(const std::filesystem::path& filename)
|
2015-07-22 02:08:37 +00:00
|
|
|
{
|
2023-09-17 15:58:37 +00:00
|
|
|
return hasExtension(filename, ".bsa") || hasExtension(filename, ".ba2");
|
2015-07-22 02:08:37 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 20:25:26 +00:00
|
|
|
std::unique_ptr<VFS::Archive> makeBsaArchive(const std::filesystem::path& path)
|
|
|
|
{
|
2023-03-02 15:49:09 +00:00
|
|
|
switch (Bsa::BSAFile::detectVersion(path))
|
2023-02-08 20:25:26 +00:00
|
|
|
{
|
|
|
|
case Bsa::BSAVER_COMPRESSED:
|
2023-03-02 19:47:53 +00:00
|
|
|
return std::make_unique<VFS::ArchiveSelector<Bsa::BSAVER_COMPRESSED>::type>(path);
|
2023-03-02 15:49:09 +00:00
|
|
|
case Bsa::BSAVER_BA2_GNRL:
|
2023-03-02 19:47:53 +00:00
|
|
|
return std::make_unique<VFS::ArchiveSelector<Bsa::BSAVER_BA2_GNRL>::type>(path);
|
2023-03-02 15:51:04 +00:00
|
|
|
case Bsa::BSAVER_BA2_DX10:
|
2023-03-02 19:47:53 +00:00
|
|
|
return std::make_unique<VFS::ArchiveSelector<Bsa::BSAVER_BA2_DX10>::type>(path);
|
|
|
|
case Bsa::BSAVER_UNCOMPRESSED:
|
|
|
|
return std::make_unique<VFS::ArchiveSelector<Bsa::BSAVER_UNCOMPRESSED>::type>(path);
|
2023-11-26 18:57:41 +00:00
|
|
|
case Bsa::BSAVER_UNKNOWN:
|
|
|
|
default:
|
|
|
|
std::cerr << "'" << Files::pathToUnicodeString(path) << "' is not a recognized BSA archive" << std::endl;
|
|
|
|
return nullptr;
|
2023-02-08 20:25:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-08 20:53:44 +00:00
|
|
|
std::unique_ptr<VFS::Archive> makeArchive(const std::filesystem::path& path)
|
|
|
|
{
|
|
|
|
if (isBSA(path))
|
|
|
|
return makeBsaArchive(path);
|
|
|
|
if (std::filesystem::is_directory(path))
|
|
|
|
return std::make_unique<VFS::FileSystemArchive>(path);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-11-26 18:57:41 +00:00
|
|
|
void readNIF(
|
|
|
|
const std::filesystem::path& source, const std::filesystem::path& path, const VFS::Manager* vfs, bool quiet)
|
|
|
|
{
|
2023-11-28 18:50:40 +00:00
|
|
|
const std::string pathStr = Files::pathToUnicodeString(path);
|
2023-11-26 18:57:41 +00:00
|
|
|
if (!quiet)
|
|
|
|
{
|
2023-11-28 18:50:40 +00:00
|
|
|
std::cout << "Reading NIF file '" << pathStr << "'";
|
2023-11-26 18:57:41 +00:00
|
|
|
if (!source.empty())
|
|
|
|
std::cout << " from '" << Files::pathToUnicodeString(isBSA(source) ? source.filename() : source) << "'";
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
std::filesystem::path fullPath = !source.empty() ? source / path : path;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Nif::NIFFile file(fullPath);
|
2024-01-16 19:56:58 +00:00
|
|
|
Nif::Reader reader(file, nullptr);
|
2023-11-26 18:57:41 +00:00
|
|
|
if (vfs != nullptr)
|
2023-11-28 18:50:40 +00:00
|
|
|
reader.parse(vfs->get(pathStr));
|
2023-11-26 18:57:41 +00:00
|
|
|
else
|
|
|
|
reader.parse(Files::openConstrainedFileStream(fullPath));
|
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
2023-11-28 18:50:40 +00:00
|
|
|
std::cerr << "Failed to read '" << pathStr << "':" << std::endl << e.what() << std::endl;
|
2023-11-26 18:57:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-08 00:20:31 +00:00
|
|
|
/// Check all the nif files in a given VFS::Archive
|
2015-08-08 00:49:52 +00:00
|
|
|
/// \note Can not read a bsa file inside of a bsa file.
|
2023-11-26 18:57:41 +00:00
|
|
|
void readVFS(std::unique_ptr<VFS::Archive>&& archive, const std::filesystem::path& archivePath, bool quiet)
|
2015-07-22 02:08:37 +00:00
|
|
|
{
|
2023-11-26 18:57:41 +00:00
|
|
|
if (archive == nullptr)
|
2023-02-08 20:25:26 +00:00
|
|
|
return;
|
|
|
|
|
2023-11-26 18:57:41 +00:00
|
|
|
if (!quiet)
|
|
|
|
std::cout << "Reading data source '" << Files::pathToUnicodeString(archivePath) << "'" << std::endl;
|
|
|
|
|
|
|
|
VFS::Manager vfs;
|
|
|
|
vfs.addArchive(std::move(archive));
|
|
|
|
vfs.buildIndex();
|
2015-07-22 02:08:37 +00:00
|
|
|
|
2023-11-26 18:57:41 +00:00
|
|
|
for (const auto& name : vfs.getRecursiveDirectoryIterator(""))
|
2015-07-22 02:08:37 +00:00
|
|
|
{
|
2023-11-26 18:57:41 +00:00
|
|
|
if (isNIF(name))
|
2015-08-08 00:20:31 +00:00
|
|
|
{
|
2023-11-26 18:57:41 +00:00
|
|
|
readNIF(archivePath, name, &vfs, quiet);
|
2015-08-08 00:20:31 +00:00
|
|
|
}
|
2023-11-26 18:57:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!archivePath.empty() && !isBSA(archivePath))
|
|
|
|
{
|
|
|
|
Files::PathContainer dataDirs = { archivePath };
|
|
|
|
const Files::Collections fileCollections = Files::Collections(dataDirs);
|
|
|
|
const Files::MultiDirCollection& bsaCol = fileCollections.getCollection(".bsa");
|
|
|
|
const Files::MultiDirCollection& ba2Col = fileCollections.getCollection(".ba2");
|
|
|
|
for (auto& file : bsaCol)
|
|
|
|
{
|
|
|
|
readVFS(makeBsaArchive(file.second), file.second, quiet);
|
|
|
|
}
|
|
|
|
for (auto& file : ba2Col)
|
2015-08-08 00:20:31 +00:00
|
|
|
{
|
2023-11-26 18:57:41 +00:00
|
|
|
readVFS(makeBsaArchive(file.second), file.second, quiet);
|
2015-08-08 00:20:31 +00:00
|
|
|
}
|
2015-07-22 02:08:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-26 18:57:41 +00:00
|
|
|
bool parseOptions(int argc, char** argv, Files::PathContainer& files, Files::PathContainer& archives,
|
|
|
|
bool& writeDebugLog, bool& quiet)
|
2015-08-08 00:06:33 +00:00
|
|
|
{
|
2022-09-12 14:55:17 +00:00
|
|
|
bpo::options_description desc(R"(Ensure that OpenMW can use the provided NIF and BSA files
|
|
|
|
|
|
|
|
Usages:
|
2023-11-26 18:57:41 +00:00
|
|
|
niftest <nif files, BSA files, or directories>
|
|
|
|
Scan the file or directories for NIF errors.
|
2022-09-12 14:55:17 +00:00
|
|
|
|
|
|
|
Allowed options)");
|
2022-09-12 14:48:15 +00:00
|
|
|
auto addOption = desc.add_options();
|
|
|
|
addOption("help,h", "print help message.");
|
2023-02-08 20:36:05 +00:00
|
|
|
addOption("write-debug-log,v", "write debug log for unsupported nif files");
|
2023-11-26 18:57:41 +00:00
|
|
|
addOption("quiet,q", "do not log read archives/files");
|
2023-02-08 20:53:44 +00:00
|
|
|
addOption("archives", bpo::value<Files::MaybeQuotedPathContainer>(), "path to archive files to provide files");
|
2022-09-12 14:48:15 +00:00
|
|
|
addOption("input-file", bpo::value<Files::MaybeQuotedPathContainer>(), "input file");
|
2015-08-08 00:06:33 +00:00
|
|
|
|
|
|
|
// Default option if none provided
|
|
|
|
bpo::positional_options_description p;
|
|
|
|
p.add("input-file", -1);
|
|
|
|
|
|
|
|
bpo::variables_map variables;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
bpo::parsed_options valid_opts = bpo::command_line_parser(argc, argv).options(desc).positional(p).run();
|
|
|
|
bpo::store(valid_opts, variables);
|
2019-03-19 05:11:14 +00:00
|
|
|
bpo::notify(variables);
|
|
|
|
if (variables.count("help"))
|
|
|
|
{
|
|
|
|
std::cout << desc << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
2023-02-08 20:36:05 +00:00
|
|
|
writeDebugLog = variables.count("write-debug-log") > 0;
|
2023-11-26 18:57:41 +00:00
|
|
|
quiet = variables.count("quiet") > 0;
|
2019-03-19 05:11:14 +00:00
|
|
|
if (variables.count("input-file"))
|
|
|
|
{
|
2023-11-26 18:57:41 +00:00
|
|
|
files = asPathContainer(variables["input-file"].as<Files::MaybeQuotedPathContainer>());
|
2023-02-08 20:53:44 +00:00
|
|
|
if (const auto it = variables.find("archives"); it != variables.end())
|
2023-11-26 18:57:41 +00:00
|
|
|
archives = asPathContainer(it->second.as<Files::MaybeQuotedPathContainer>());
|
2019-03-19 05:11:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
2015-08-08 00:06:33 +00:00
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
2023-11-26 18:57:41 +00:00
|
|
|
std::cout << "Error parsing arguments: " << e.what() << "\n\n" << desc << std::endl;
|
2018-11-15 14:10:19 +00:00
|
|
|
return false;
|
2015-08-08 00:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "No input files or directories specified!" << std::endl;
|
|
|
|
std::cout << desc << std::endl;
|
2018-11-15 14:10:19 +00:00
|
|
|
return false;
|
2015-08-08 00:06:33 +00:00
|
|
|
}
|
|
|
|
|
2015-07-22 02:08:37 +00:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2023-11-26 18:57:41 +00:00
|
|
|
Files::PathContainer files, sources;
|
2023-02-08 20:36:05 +00:00
|
|
|
bool writeDebugLog = false;
|
2023-11-26 18:57:41 +00:00
|
|
|
bool quiet = false;
|
|
|
|
if (!parseOptions(argc, argv, files, sources, writeDebugLog, quiet))
|
2018-11-15 14:10:19 +00:00
|
|
|
return 1;
|
2015-07-22 02:08:37 +00:00
|
|
|
|
2022-09-17 17:24:42 +00:00
|
|
|
Nif::Reader::setLoadUnsupportedFiles(true);
|
2023-02-08 20:36:05 +00:00
|
|
|
Nif::Reader::setWriteNifDebugLog(writeDebugLog);
|
|
|
|
|
2023-02-08 20:53:44 +00:00
|
|
|
std::unique_ptr<VFS::Manager> vfs;
|
2023-11-26 18:57:41 +00:00
|
|
|
if (!sources.empty())
|
2023-02-08 20:53:44 +00:00
|
|
|
{
|
2023-05-31 21:11:03 +00:00
|
|
|
vfs = std::make_unique<VFS::Manager>();
|
2023-11-26 18:57:41 +00:00
|
|
|
for (const std::filesystem::path& path : sources)
|
2023-07-31 11:21:10 +00:00
|
|
|
{
|
2023-11-28 18:50:40 +00:00
|
|
|
const std::string pathStr = Files::pathToUnicodeString(path);
|
2023-11-26 18:57:41 +00:00
|
|
|
if (!quiet)
|
2023-11-28 18:50:40 +00:00
|
|
|
std::cout << "Adding data source '" << pathStr << "'" << std::endl;
|
2023-11-26 18:57:41 +00:00
|
|
|
|
2023-07-31 11:21:10 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if (auto archive = makeArchive(path))
|
|
|
|
vfs->addArchive(std::move(archive));
|
|
|
|
else
|
2023-11-28 18:50:40 +00:00
|
|
|
std::cerr << "Error: '" << pathStr << "' is not an archive or directory" << std::endl;
|
2023-07-31 11:21:10 +00:00
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
2023-11-28 18:50:40 +00:00
|
|
|
std::cerr << "Failed to add data source '" << pathStr << "': " << e.what() << std::endl;
|
2023-07-31 11:21:10 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-26 18:57:41 +00:00
|
|
|
|
|
|
|
vfs->buildIndex();
|
2023-02-08 20:53:44 +00:00
|
|
|
}
|
|
|
|
|
2022-06-20 18:48:06 +00:00
|
|
|
for (const auto& path : files)
|
2015-08-08 00:06:33 +00:00
|
|
|
{
|
2023-11-28 18:50:40 +00:00
|
|
|
const std::string pathStr = Files::pathToUnicodeString(path);
|
2018-11-14 11:52:36 +00:00
|
|
|
try
|
|
|
|
{
|
2022-06-19 11:28:33 +00:00
|
|
|
if (isNIF(path))
|
2015-07-22 02:08:37 +00:00
|
|
|
{
|
2023-11-26 18:57:41 +00:00
|
|
|
readNIF({}, path, vfs.get(), quiet);
|
2015-08-08 00:20:31 +00:00
|
|
|
}
|
2023-02-08 20:53:44 +00:00
|
|
|
else if (auto archive = makeArchive(path))
|
2015-08-08 00:20:31 +00:00
|
|
|
{
|
2023-11-26 18:57:41 +00:00
|
|
|
readVFS(std::move(archive), path, quiet);
|
2015-07-22 02:08:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-11-28 18:50:40 +00:00
|
|
|
std::cerr << "Error: '" << pathStr << "' is not a NIF file, BSA/BA2 archive, or directory" << std::endl;
|
2015-07-22 02:08:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
2023-11-28 18:50:40 +00:00
|
|
|
std::cerr << "Failed to read '" << pathStr << "': " << e.what() << std::endl;
|
2015-07-22 02:08:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|