2015-07-21 22:08:37 -04:00
|
|
|
///Program to test .nif files both on the FileSystem and in BSA archives.
|
|
|
|
|
|
|
|
#include <iostream>
|
2022-05-22 18:56:14 +02:00
|
|
|
#include <filesystem>
|
2015-07-21 22:08:37 -04:00
|
|
|
|
2022-08-03 00:00:54 +02:00
|
|
|
#include <components/misc/strings/algorithm.hpp>
|
2015-07-21 22:08:37 -04:00
|
|
|
#include <components/nif/niffile.hpp>
|
|
|
|
#include <components/files/constrainedfilestream.hpp>
|
|
|
|
#include <components/vfs/manager.hpp>
|
|
|
|
#include <components/vfs/bsaarchive.hpp>
|
2015-08-07 20:20:31 -04:00
|
|
|
#include <components/vfs/filesystemarchive.hpp>
|
2022-06-20 20:48:06 +02:00
|
|
|
#include <components/files/configurationmanager.hpp>
|
2022-07-03 00:02:29 +02:00
|
|
|
#include <components/files/conversion.hpp>
|
2015-07-21 22:08:37 -04:00
|
|
|
|
2015-08-07 20:06:33 -04:00
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
|
|
|
|
// Create local aliases for brevity
|
|
|
|
namespace bpo = boost::program_options;
|
2015-07-21 22:08:37 -04:00
|
|
|
|
|
|
|
///See if the file has the named extension
|
2022-06-19 13:28:33 +02:00
|
|
|
bool hasExtension(const std::filesystem::path& filename, const std::string& extensionToFind)
|
2015-07-21 22:08:37 -04:00
|
|
|
{
|
2022-07-03 00:02:29 +02:00
|
|
|
const auto extension = Files::pathToUnicodeString(filename.extension());
|
2021-11-10 21:25:16 +01:00
|
|
|
return Misc::StringUtils::ciEqual(extension, extensionToFind);
|
2015-07-21 22:08:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
///See if the file has the "nif" extension.
|
2022-06-19 13:28:33 +02:00
|
|
|
bool isNIF(const std::filesystem::path &filename)
|
2015-07-21 22:08:37 -04:00
|
|
|
{
|
|
|
|
return hasExtension(filename,"nif");
|
|
|
|
}
|
|
|
|
///See if the file has the "bsa" extension.
|
2022-06-19 13:28:33 +02:00
|
|
|
bool isBSA(const std::filesystem::path &filename)
|
2015-07-21 22:08:37 -04:00
|
|
|
{
|
|
|
|
return hasExtension(filename,"bsa");
|
|
|
|
}
|
|
|
|
|
2015-08-07 20:20:31 -04:00
|
|
|
/// Check all the nif files in a given VFS::Archive
|
2015-08-07 20:49:52 -04:00
|
|
|
/// \note Can not read a bsa file inside of a bsa file.
|
2022-06-19 13:28:33 +02:00
|
|
|
void readVFS(std::unique_ptr<VFS::Archive>&& anArchive, const std::filesystem::path& archivePath = {})
|
2015-07-21 22:08:37 -04:00
|
|
|
{
|
2015-08-07 20:39:43 -04:00
|
|
|
VFS::Manager myManager(true);
|
2022-07-16 12:14:20 +02:00
|
|
|
myManager.addArchive(std::move(anArchive));
|
2015-07-21 22:08:37 -04:00
|
|
|
myManager.buildIndex();
|
|
|
|
|
2022-07-03 00:02:29 +02:00
|
|
|
for(const auto& name : myManager.getRecursiveDirectoryIterator(""))
|
2015-07-21 22:08:37 -04:00
|
|
|
{
|
2015-08-07 20:20:31 -04:00
|
|
|
try{
|
|
|
|
if(isNIF(name))
|
|
|
|
{
|
|
|
|
// std::cout << "Decoding: " << name << std::endl;
|
2022-06-19 13:28:33 +02:00
|
|
|
Nif::NIFFile temp_nif(myManager.get(name),archivePath / name);
|
2015-08-07 20:20:31 -04:00
|
|
|
}
|
2015-08-07 20:39:43 -04:00
|
|
|
else if(isBSA(name))
|
|
|
|
{
|
2015-08-07 20:49:52 -04:00
|
|
|
if(!archivePath.empty() && !isBSA(archivePath))
|
2015-08-07 20:39:43 -04:00
|
|
|
{
|
2015-08-07 20:49:52 -04:00
|
|
|
// std::cout << "Reading BSA File: " << name << std::endl;
|
2022-06-19 13:28:33 +02:00
|
|
|
readVFS(std::make_unique<VFS::BsaArchive>(archivePath / name), archivePath / name);
|
2015-08-07 20:49:52 -04:00
|
|
|
// std::cout << "Done with BSA File: " << name << std::endl;
|
2015-08-07 20:39:43 -04:00
|
|
|
}
|
|
|
|
}
|
2015-08-07 20:20:31 -04:00
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
|
|
|
std::cerr << "ERROR, an exception has occurred: " << e.what() << std::endl;
|
|
|
|
}
|
2015-07-21 22:08:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-20 20:48:06 +02:00
|
|
|
bool parseOptions (int argc, char** argv, std::vector<Files::MaybeQuotedPath> &files)
|
2015-08-07 20:06:33 -04:00
|
|
|
{
|
2022-09-12 16:55:17 +02:00
|
|
|
bpo::options_description desc(R"(Ensure that OpenMW can use the provided NIF and BSA files
|
|
|
|
|
|
|
|
Usages:
|
|
|
|
niftool <nif files, BSA files, or directories>
|
|
|
|
Scan the file or directories for nif errors.
|
|
|
|
|
|
|
|
Allowed options)");
|
2022-09-12 16:48:15 +02:00
|
|
|
auto addOption = desc.add_options();
|
|
|
|
addOption("help,h", "print help message.");
|
|
|
|
addOption("input-file", bpo::value< Files::MaybeQuotedPathContainer >(), "input file");
|
2015-08-07 20:06:33 -04: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 09:11:14 +04:00
|
|
|
bpo::notify(variables);
|
|
|
|
if (variables.count ("help"))
|
|
|
|
{
|
|
|
|
std::cout << desc << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (variables.count("input-file"))
|
|
|
|
{
|
2022-08-06 18:09:50 +02:00
|
|
|
files = variables["input-file"].as< Files::MaybeQuotedPathContainer >();
|
2019-03-19 09:11:14 +04:00
|
|
|
return true;
|
|
|
|
}
|
2015-08-07 20:06:33 -04:00
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
|
|
|
std::cout << "ERROR parsing arguments: " << e.what() << "\n\n"
|
|
|
|
<< desc << std::endl;
|
2018-11-15 18:10:19 +04:00
|
|
|
return false;
|
2015-08-07 20:06:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "No input files or directories specified!" << std::endl;
|
|
|
|
std::cout << desc << std::endl;
|
2018-11-15 18:10:19 +04:00
|
|
|
return false;
|
2015-08-07 20:06:33 -04:00
|
|
|
}
|
|
|
|
|
2015-07-21 22:08:37 -04:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2022-06-20 20:48:06 +02:00
|
|
|
std::vector<Files::MaybeQuotedPath> files;
|
2018-11-15 18:10:19 +04:00
|
|
|
if(!parseOptions (argc, argv, files))
|
|
|
|
return 1;
|
2015-07-21 22:08:37 -04:00
|
|
|
|
2020-11-09 14:22:48 +03:00
|
|
|
Nif::NIFFile::setLoadUnsupportedFiles(true);
|
2015-08-07 20:49:52 -04:00
|
|
|
// std::cout << "Reading Files" << std::endl;
|
2022-06-20 20:48:06 +02:00
|
|
|
for(const auto& path : files)
|
2015-08-07 20:06:33 -04:00
|
|
|
{
|
2018-11-14 15:52:36 +04:00
|
|
|
try
|
|
|
|
{
|
2022-06-19 13:28:33 +02:00
|
|
|
if(isNIF(path))
|
2015-07-21 22:08:37 -04:00
|
|
|
{
|
|
|
|
//std::cout << "Decoding: " << name << std::endl;
|
2022-06-19 13:28:33 +02:00
|
|
|
Nif::NIFFile temp_nif(Files::openConstrainedFileStream(path), path);
|
2015-07-21 22:08:37 -04:00
|
|
|
}
|
2022-06-19 13:28:33 +02:00
|
|
|
else if(isBSA(path))
|
2015-07-21 22:08:37 -04:00
|
|
|
{
|
2015-08-07 20:49:52 -04:00
|
|
|
// std::cout << "Reading BSA File: " << name << std::endl;
|
2022-06-19 13:28:33 +02:00
|
|
|
readVFS(std::make_unique<VFS::BsaArchive>(path));
|
2015-08-07 20:20:31 -04:00
|
|
|
}
|
2022-06-19 13:28:33 +02:00
|
|
|
else if(std::filesystem::is_directory(path))
|
2015-08-07 20:20:31 -04:00
|
|
|
{
|
2015-08-07 20:49:52 -04:00
|
|
|
// std::cout << "Reading All Files in: " << name << std::endl;
|
2022-06-19 13:28:33 +02:00
|
|
|
readVFS(std::make_unique<VFS::FileSystemArchive>(path), path);
|
2015-07-21 22:08:37 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-03 00:02:29 +02:00
|
|
|
std::cerr << "ERROR: \"" << Files::pathToUnicodeString(path) << "\" is not a nif file, bsa file, or directory!" << std::endl;
|
2015-07-21 22:08:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
|
|
|
std::cerr << "ERROR, an exception has occurred: " << e.what() << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|