Merge branch 'niftestbsatool' into 'master'

bsatool and NIF debugging improvements

See merge request OpenMW/openmw!3619
macos_ci_fix
Evil Eye 1 year ago
commit 7cdf702a14

@ -194,7 +194,8 @@ int extract(std::unique_ptr<File>& bsa, Arguments& info)
// Get a stream for the file to extract // Get a stream for the file to extract
for (auto it = bsa->getList().rbegin(); it != bsa->getList().rend(); ++it) for (auto it = bsa->getList().rbegin(); it != bsa->getList().rend(); ++it)
{ {
if (Misc::StringUtils::ciEqual(Misc::StringUtils::stringToU8String(it->name()), archivePath)) auto streamPath = Misc::StringUtils::stringToU8String(it->name());
if (Misc::StringUtils::ciEqual(streamPath, archivePath) || Misc::StringUtils::ciEqual(streamPath, extractPath))
{ {
stream = bsa->getFile(&*it); stream = bsa->getFile(&*it);
break; break;

@ -45,9 +45,6 @@ std::unique_ptr<VFS::Archive> makeBsaArchive(const std::filesystem::path& path)
{ {
switch (Bsa::BSAFile::detectVersion(path)) switch (Bsa::BSAFile::detectVersion(path))
{ {
case Bsa::BSAVER_UNKNOWN:
std::cerr << '"' << path << "\" is unknown BSA archive" << std::endl;
return nullptr;
case Bsa::BSAVER_COMPRESSED: case Bsa::BSAVER_COMPRESSED:
return std::make_unique<VFS::ArchiveSelector<Bsa::BSAVER_COMPRESSED>::type>(path); return std::make_unique<VFS::ArchiveSelector<Bsa::BSAVER_COMPRESSED>::type>(path);
case Bsa::BSAVER_BA2_GNRL: case Bsa::BSAVER_BA2_GNRL:
@ -56,12 +53,12 @@ std::unique_ptr<VFS::Archive> makeBsaArchive(const std::filesystem::path& path)
return std::make_unique<VFS::ArchiveSelector<Bsa::BSAVER_BA2_DX10>::type>(path); return std::make_unique<VFS::ArchiveSelector<Bsa::BSAVER_BA2_DX10>::type>(path);
case Bsa::BSAVER_UNCOMPRESSED: case Bsa::BSAVER_UNCOMPRESSED:
return std::make_unique<VFS::ArchiveSelector<Bsa::BSAVER_UNCOMPRESSED>::type>(path); return std::make_unique<VFS::ArchiveSelector<Bsa::BSAVER_UNCOMPRESSED>::type>(path);
} case Bsa::BSAVER_UNKNOWN:
default:
std::cerr << '"' << path << "\" is unsupported BSA archive" << std::endl; std::cerr << "'" << Files::pathToUnicodeString(path) << "' is not a recognized BSA archive" << std::endl;
return nullptr; return nullptr;
} }
}
std::unique_ptr<VFS::Archive> makeArchive(const std::filesystem::path& path) std::unique_ptr<VFS::Archive> makeArchive(const std::filesystem::path& path)
{ {
@ -72,58 +69,86 @@ std::unique_ptr<VFS::Archive> makeArchive(const std::filesystem::path& path)
return nullptr; return nullptr;
} }
void readNIF(
const std::filesystem::path& source, const std::filesystem::path& path, const VFS::Manager* vfs, bool quiet)
{
const std::string pathStr = Files::pathToUnicodeString(path);
if (!quiet)
{
std::cout << "Reading NIF file '" << pathStr << "'";
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);
Nif::Reader reader(file);
if (vfs != nullptr)
reader.parse(vfs->get(pathStr));
else
reader.parse(Files::openConstrainedFileStream(fullPath));
}
catch (std::exception& e)
{
std::cerr << "Failed to read '" << pathStr << "':" << std::endl << e.what() << std::endl;
}
}
/// Check all the nif files in a given VFS::Archive /// Check all the nif files in a given VFS::Archive
/// \note Can not read a bsa file inside of a bsa file. /// \note Can not read a bsa file inside of a bsa file.
void readVFS(std::unique_ptr<VFS::Archive>&& anArchive, const std::filesystem::path& archivePath = {}) void readVFS(std::unique_ptr<VFS::Archive>&& archive, const std::filesystem::path& archivePath, bool quiet)
{ {
if (anArchive == nullptr) if (archive == nullptr)
return; return;
VFS::Manager myManager; if (!quiet)
myManager.addArchive(std::move(anArchive)); std::cout << "Reading data source '" << Files::pathToUnicodeString(archivePath) << "'" << std::endl;
myManager.buildIndex();
for (const auto& name : myManager.getRecursiveDirectoryIterator("")) VFS::Manager vfs;
{ vfs.addArchive(std::move(archive));
try vfs.buildIndex();
for (const auto& name : vfs.getRecursiveDirectoryIterator(""))
{ {
if (isNIF(name)) if (isNIF(name))
{ {
// std::cout << "Decoding: " << name << std::endl; readNIF(archivePath, name, &vfs, quiet);
Nif::NIFFile file(archivePath / name);
Nif::Reader reader(file);
reader.parse(myManager.get(name));
} }
else if (isBSA(name)) }
{
if (!archivePath.empty() && !isBSA(archivePath)) if (!archivePath.empty() && !isBSA(archivePath))
{ {
// std::cout << "Reading BSA File: " << name << std::endl; Files::PathContainer dataDirs = { archivePath };
readVFS(makeBsaArchive(archivePath / name), archivePath / name); const Files::Collections fileCollections = Files::Collections(dataDirs);
// std::cout << "Done with BSA File: " << name << std::endl; 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);
} }
catch (std::exception& e) for (auto& file : ba2Col)
{ {
std::cerr << "ERROR, an exception has occurred: " << e.what() << std::endl; readVFS(makeBsaArchive(file.second), file.second, quiet);
} }
} }
} }
bool parseOptions(int argc, char** argv, std::vector<Files::MaybeQuotedPath>& files, bool& writeDebugLog, bool parseOptions(int argc, char** argv, Files::PathContainer& files, Files::PathContainer& archives,
std::vector<Files::MaybeQuotedPath>& archives) bool& writeDebugLog, bool& quiet)
{ {
bpo::options_description desc(R"(Ensure that OpenMW can use the provided NIF and BSA files bpo::options_description desc(R"(Ensure that OpenMW can use the provided NIF and BSA files
Usages: Usages:
niftool <nif files, BSA files, or directories> niftest <nif files, BSA files, or directories>
Scan the file or directories for nif errors. Scan the file or directories for NIF errors.
Allowed options)"); Allowed options)");
auto addOption = desc.add_options(); auto addOption = desc.add_options();
addOption("help,h", "print help message."); addOption("help,h", "print help message.");
addOption("write-debug-log,v", "write debug log for unsupported nif files"); addOption("write-debug-log,v", "write debug log for unsupported nif files");
addOption("quiet,q", "do not log read archives/files");
addOption("archives", bpo::value<Files::MaybeQuotedPathContainer>(), "path to archive files to provide files"); addOption("archives", bpo::value<Files::MaybeQuotedPathContainer>(), "path to archive files to provide files");
addOption("input-file", bpo::value<Files::MaybeQuotedPathContainer>(), "input file"); addOption("input-file", bpo::value<Files::MaybeQuotedPathContainer>(), "input file");
@ -143,17 +168,18 @@ Allowed options)");
return false; return false;
} }
writeDebugLog = variables.count("write-debug-log") > 0; writeDebugLog = variables.count("write-debug-log") > 0;
quiet = variables.count("quiet") > 0;
if (variables.count("input-file")) if (variables.count("input-file"))
{ {
files = variables["input-file"].as<Files::MaybeQuotedPathContainer>(); files = asPathContainer(variables["input-file"].as<Files::MaybeQuotedPathContainer>());
if (const auto it = variables.find("archives"); it != variables.end()) if (const auto it = variables.find("archives"); it != variables.end())
archives = it->second.as<Files::MaybeQuotedPathContainer>(); archives = asPathContainer(it->second.as<Files::MaybeQuotedPathContainer>());
return true; return true;
} }
} }
catch (std::exception& e) catch (std::exception& e)
{ {
std::cout << "ERROR parsing arguments: " << e.what() << "\n\n" << desc << std::endl; std::cout << "Error parsing arguments: " << e.what() << "\n\n" << desc << std::endl;
return false; return false;
} }
@ -164,64 +190,62 @@ Allowed options)");
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
std::vector<Files::MaybeQuotedPath> files; Files::PathContainer files, sources;
bool writeDebugLog = false; bool writeDebugLog = false;
std::vector<Files::MaybeQuotedPath> archives; bool quiet = false;
if (!parseOptions(argc, argv, files, writeDebugLog, archives)) if (!parseOptions(argc, argv, files, sources, writeDebugLog, quiet))
return 1; return 1;
Nif::Reader::setLoadUnsupportedFiles(true); Nif::Reader::setLoadUnsupportedFiles(true);
Nif::Reader::setWriteNifDebugLog(writeDebugLog); Nif::Reader::setWriteNifDebugLog(writeDebugLog);
std::unique_ptr<VFS::Manager> vfs; std::unique_ptr<VFS::Manager> vfs;
if (!archives.empty()) if (!sources.empty())
{ {
vfs = std::make_unique<VFS::Manager>(); vfs = std::make_unique<VFS::Manager>();
for (const std::filesystem::path& path : archives) for (const std::filesystem::path& path : sources)
{ {
const std::string pathStr = Files::pathToUnicodeString(path);
if (!quiet)
std::cout << "Adding data source '" << pathStr << "'" << std::endl;
try try
{ {
if (auto archive = makeArchive(path)) if (auto archive = makeArchive(path))
vfs->addArchive(std::move(archive)); vfs->addArchive(std::move(archive));
else else
std::cerr << '"' << path << "\" is unsupported archive" << std::endl; std::cerr << "Error: '" << pathStr << "' is not an archive or directory" << std::endl;
vfs->buildIndex();
} }
catch (std::exception& e) catch (std::exception& e)
{ {
std::cerr << "ERROR, an exception has occurred: " << e.what() << std::endl; std::cerr << "Failed to add data source '" << pathStr << "': " << e.what() << std::endl;
} }
} }
vfs->buildIndex();
} }
// std::cout << "Reading Files" << std::endl;
for (const auto& path : files) for (const auto& path : files)
{ {
const std::string pathStr = Files::pathToUnicodeString(path);
try try
{ {
if (isNIF(path)) if (isNIF(path))
{ {
// std::cout << "Decoding: " << name << std::endl; readNIF({}, path, vfs.get(), quiet);
Nif::NIFFile file(path);
Nif::Reader reader(file);
if (vfs != nullptr)
reader.parse(vfs->get(Files::pathToUnicodeString(path)));
else
reader.parse(Files::openConstrainedFileStream(path));
} }
else if (auto archive = makeArchive(path)) else if (auto archive = makeArchive(path))
{ {
readVFS(std::move(archive), path); readVFS(std::move(archive), path, quiet);
} }
else else
{ {
std::cerr << "ERROR: \"" << Files::pathToUnicodeString(path) std::cerr << "Error: '" << pathStr << "' is not a NIF file, BSA/BA2 archive, or directory" << std::endl;
<< "\" is not a nif file, bsa/ba2 file, or directory!" << std::endl;
} }
} }
catch (std::exception& e) catch (std::exception& e)
{ {
std::cerr << "ERROR, an exception has occurred: " << e.what() << std::endl; std::cerr << "Failed to read '" << pathStr << "': " << e.what() << std::endl;
} }
} }
return 0; return 0;

@ -512,6 +512,10 @@ namespace Nif
void Reader::parse(Files::IStreamPtr&& stream) void Reader::parse(Files::IStreamPtr&& stream)
{ {
const bool writeDebug = sWriteNifDebugLog;
if (writeDebug)
Log(Debug::Verbose) << "NIF Debug: Reading file: '" << mFilename << "'";
const std::array<std::uint64_t, 2> fileHash = Files::getHash(mFilename, *stream); const std::array<std::uint64_t, 2> fileHash = Files::getHash(mFilename, *stream);
mHash.append(reinterpret_cast<const char*>(fileHash.data()), fileHash.size() * sizeof(std::uint64_t)); mHash.append(reinterpret_cast<const char*>(fileHash.data()), fileHash.size() * sizeof(std::uint64_t));
@ -538,15 +542,9 @@ namespace Nif
}; };
const bool supportedVersion const bool supportedVersion
= std::find(supportedVers.begin(), supportedVers.end(), mVersion) != supportedVers.end(); = std::find(supportedVers.begin(), supportedVers.end(), mVersion) != supportedVers.end();
const bool writeDebugLog = sWriteNifDebugLog;
if (!supportedVersion) if (!supportedVersion && !sLoadUnsupportedFiles)
{
if (!sLoadUnsupportedFiles)
throw Nif::Exception("Unsupported NIF version: " + versionToString(mVersion), mFilename); throw Nif::Exception("Unsupported NIF version: " + versionToString(mVersion), mFilename);
if (writeDebugLog)
Log(Debug::Warning) << " NIFFile Warning: Unsupported NIF version: " << versionToString(mVersion)
<< ". Proceed with caution! File: " << mFilename;
}
const bool hasEndianness = mVersion >= NIFStream::generateVersion(20, 0, 0, 4); const bool hasEndianness = mVersion >= NIFStream::generateVersion(20, 0, 0, 4);
const bool hasUserVersion = mVersion >= NIFStream::generateVersion(10, 0, 1, 8); const bool hasUserVersion = mVersion >= NIFStream::generateVersion(10, 0, 1, 8);
@ -603,6 +601,17 @@ namespace Nif
} }
} }
if (writeDebug)
{
std::stringstream versionInfo;
versionInfo << "NIF Debug: Version: " << versionToString(mVersion);
if (mUserVersion)
versionInfo << "\nUser version: " << mUserVersion;
if (mBethVersion)
versionInfo << "\nBSStream version: " << mBethVersion;
Log(Debug::Verbose) << versionInfo.str();
}
if (hasRecTypeListings) if (hasRecTypeListings)
{ {
// TODO: 20.3.1.2 uses DJB hashes instead of strings // TODO: 20.3.1.2 uses DJB hashes instead of strings
@ -658,9 +667,8 @@ namespace Nif
r = entry->second(); r = entry->second();
if (!supportedVersion && writeDebugLog) if (writeDebug)
Log(Debug::Verbose) << "NIF Debug: Reading record of type " << rec << ", index " << i << " (" Log(Debug::Verbose) << "NIF Debug: Reading record of type " << rec << ", index " << i;
<< mFilename << ")";
assert(r != nullptr); assert(r != nullptr);
assert(r->recType != RC_MISSING); assert(r->recType != RC_MISSING);

@ -269,7 +269,6 @@ write nif debug log
:Type: boolean :Type: boolean
:Range: True/False :Range: True/False
:Default: True :Default: False
If enabled, log the loading process of unsupported NIF files. If enabled, log the loading process of NIF files.
:ref:`load unsupported nif files` setting must be enabled for this setting to have any effect.

@ -1136,8 +1136,8 @@ weathersnow = meshes/snow.nif
# Blizzard weather effect # Blizzard weather effect
weatherblizzard = meshes/blizzard.nif weatherblizzard = meshes/blizzard.nif
# Enable to write logs when loading unsupported nif file # Enable to write logs when loading NIF files
write nif debug log = true write nif debug log = false
[Groundcover] [Groundcover]

Loading…
Cancel
Save