Skip to content

Commit

Permalink
Merge branch 'niftestbsatool' into 'master'
Browse files Browse the repository at this point in the history
bsatool and NIF debugging improvements

See merge request OpenMW/openmw!3619
  • Loading branch information
Assumeru committed Dec 4, 2023
2 parents 53f5e4d + b96600a commit 7cdf702
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 77 deletions.
3 changes: 2 additions & 1 deletion apps/bsatool/bsatool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ int extract(std::unique_ptr<File>& bsa, Arguments& info)
// Get a stream for the file to extract
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);
break;
Expand Down
142 changes: 83 additions & 59 deletions apps/niftest/niftest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ std::unique_ptr<VFS::Archive> makeBsaArchive(const std::filesystem::path& 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:
return std::make_unique<VFS::ArchiveSelector<Bsa::BSAVER_COMPRESSED>::type>(path);
case Bsa::BSAVER_BA2_GNRL:
Expand All @@ -56,11 +53,11 @@ std::unique_ptr<VFS::Archive> makeBsaArchive(const std::filesystem::path& path)
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);
case Bsa::BSAVER_UNKNOWN:
default:
std::cerr << "'" << Files::pathToUnicodeString(path) << "' is not a recognized BSA archive" << std::endl;
return nullptr;
}

std::cerr << '"' << path << "\" is unsupported BSA archive" << std::endl;

return nullptr;
}

std::unique_ptr<VFS::Archive> makeArchive(const std::filesystem::path& path)
Expand All @@ -72,58 +69,86 @@ std::unique_ptr<VFS::Archive> makeArchive(const std::filesystem::path& path)
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
/// \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;

VFS::Manager myManager;
myManager.addArchive(std::move(anArchive));
myManager.buildIndex();
if (!quiet)
std::cout << "Reading data source '" << Files::pathToUnicodeString(archivePath) << "'" << std::endl;

VFS::Manager vfs;
vfs.addArchive(std::move(archive));
vfs.buildIndex();

for (const auto& name : myManager.getRecursiveDirectoryIterator(""))
for (const auto& name : vfs.getRecursiveDirectoryIterator(""))
{
try
if (isNIF(name))
{
if (isNIF(name))
{
// std::cout << "Decoding: " << name << std::endl;
Nif::NIFFile file(archivePath / name);
Nif::Reader reader(file);
reader.parse(myManager.get(name));
}
else if (isBSA(name))
{
if (!archivePath.empty() && !isBSA(archivePath))
{
// std::cout << "Reading BSA File: " << name << std::endl;
readVFS(makeBsaArchive(archivePath / name), archivePath / name);
// std::cout << "Done with BSA File: " << name << std::endl;
}
}
readNIF(archivePath, name, &vfs, quiet);
}
catch (std::exception& e)
}

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)
{
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,
std::vector<Files::MaybeQuotedPath>& archives)
bool parseOptions(int argc, char** argv, Files::PathContainer& files, Files::PathContainer& archives,
bool& writeDebugLog, bool& quiet)
{
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.
niftest <nif files, BSA files, or directories>
Scan the file or directories for NIF errors.
Allowed options)");
auto addOption = desc.add_options();
addOption("help,h", "print help message.");
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("input-file", bpo::value<Files::MaybeQuotedPathContainer>(), "input file");

Expand All @@ -143,17 +168,18 @@ Allowed options)");
return false;
}
writeDebugLog = variables.count("write-debug-log") > 0;
quiet = variables.count("quiet") > 0;
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())
archives = it->second.as<Files::MaybeQuotedPathContainer>();
archives = asPathContainer(it->second.as<Files::MaybeQuotedPathContainer>());
return true;
}
}
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;
}

Expand All @@ -164,64 +190,62 @@ Allowed options)");

int main(int argc, char** argv)
{
std::vector<Files::MaybeQuotedPath> files;
Files::PathContainer files, sources;
bool writeDebugLog = false;
std::vector<Files::MaybeQuotedPath> archives;
if (!parseOptions(argc, argv, files, writeDebugLog, archives))
bool quiet = false;
if (!parseOptions(argc, argv, files, sources, writeDebugLog, quiet))
return 1;

Nif::Reader::setLoadUnsupportedFiles(true);
Nif::Reader::setWriteNifDebugLog(writeDebugLog);

std::unique_ptr<VFS::Manager> vfs;
if (!archives.empty())
if (!sources.empty())
{
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
{
if (auto archive = makeArchive(path))
vfs->addArchive(std::move(archive));
else
std::cerr << '"' << path << "\" is unsupported archive" << std::endl;
vfs->buildIndex();
std::cerr << "Error: '" << pathStr << "' is not an archive or directory" << std::endl;
}
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)
{
const std::string pathStr = Files::pathToUnicodeString(path);
try
{
if (isNIF(path))
{
// std::cout << "Decoding: " << name << std::endl;
Nif::NIFFile file(path);
Nif::Reader reader(file);
if (vfs != nullptr)
reader.parse(vfs->get(Files::pathToUnicodeString(path)));
else
reader.parse(Files::openConstrainedFileStream(path));
readNIF({}, path, vfs.get(), quiet);
}
else if (auto archive = makeArchive(path))
{
readVFS(std::move(archive), path);
readVFS(std::move(archive), path, quiet);
}
else
{
std::cerr << "ERROR: \"" << Files::pathToUnicodeString(path)
<< "\" is not a nif file, bsa/ba2 file, or directory!" << std::endl;
std::cerr << "Error: '" << pathStr << "' is not a NIF file, BSA/BA2 archive, or directory" << std::endl;
}
}
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;
Expand Down
32 changes: 20 additions & 12 deletions components/nif/niffile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,10 @@ namespace Nif

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);
mHash.append(reinterpret_cast<const char*>(fileHash.data()), fileHash.size() * sizeof(std::uint64_t));

Expand All @@ -538,15 +542,9 @@ namespace Nif
};
const bool supportedVersion
= std::find(supportedVers.begin(), supportedVers.end(), mVersion) != supportedVers.end();
const bool writeDebugLog = sWriteNifDebugLog;
if (!supportedVersion)
{
if (!sLoadUnsupportedFiles)
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;
}

if (!supportedVersion && !sLoadUnsupportedFiles)
throw Nif::Exception("Unsupported NIF version: " + versionToString(mVersion), mFilename);

const bool hasEndianness = mVersion >= NIFStream::generateVersion(20, 0, 0, 4);
const bool hasUserVersion = mVersion >= NIFStream::generateVersion(10, 0, 1, 8);
Expand Down Expand Up @@ -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)
{
// TODO: 20.3.1.2 uses DJB hashes instead of strings
Expand Down Expand Up @@ -658,9 +667,8 @@ namespace Nif

r = entry->second();

if (!supportedVersion && writeDebugLog)
Log(Debug::Verbose) << "NIF Debug: Reading record of type " << rec << ", index " << i << " ("
<< mFilename << ")";
if (writeDebug)
Log(Debug::Verbose) << "NIF Debug: Reading record of type " << rec << ", index " << i;

assert(r != nullptr);
assert(r->recType != RC_MISSING);
Expand Down
5 changes: 2 additions & 3 deletions docs/source/reference/modding/settings/models.rst
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ write nif debug log

:Type: boolean
:Range: True/False
:Default: True
:Default: False

If enabled, log the loading process of unsupported NIF files.
:ref:`load unsupported nif files` setting must be enabled for this setting to have any effect.
If enabled, log the loading process of NIF files.
4 changes: 2 additions & 2 deletions files/settings-default.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -1136,8 +1136,8 @@ weathersnow = meshes/snow.nif
# Blizzard weather effect
weatherblizzard = meshes/blizzard.nif

# Enable to write logs when loading unsupported nif file
write nif debug log = true
# Enable to write logs when loading NIF files
write nif debug log = false

[Groundcover]

Expand Down

0 comments on commit 7cdf702

Please sign in to comment.