1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-05-09 20:11:25 +00:00

Remove NIFFile::warn and NIFFile::fail functions

These functions use NIFFile only as context, they are not really a part of
either reading nor state invariant. And they only confuse reader because it's
not immediatelly obvious that no code is executed after fail.
This commit is contained in:
elsid 2022-09-18 13:19:21 +02:00
parent 6d3de520e8
commit b39ccbeeef
No known key found for this signature in database
GPG key ID: 4DE04C198CBA7625
7 changed files with 59 additions and 43 deletions

View file

@ -1,7 +1,10 @@
#include "data.hpp" #include "data.hpp"
#include "exception.hpp"
#include "nifkey.hpp" #include "nifkey.hpp"
#include "node.hpp" #include "node.hpp"
#include <components/debug/debuglog.hpp>
namespace Nif namespace Nif
{ {
void NiSkinInstance::read(NIFStream* nif) void NiSkinInstance::read(NIFStream* nif)
@ -21,16 +24,16 @@ namespace Nif
bones.post(nif); bones.post(nif);
if (data.empty() || root.empty()) if (data.empty() || root.empty())
nif->fail("NiSkinInstance missing root or data"); throw Nif::Exception("NiSkinInstance missing root or data", nif->getFilename());
size_t bnum = bones.length(); size_t bnum = bones.length();
if (bnum != data->bones.size()) if (bnum != data->bones.size())
nif->fail("Mismatch in NiSkinData bone count"); throw Nif::Exception("Mismatch in NiSkinData bone count", nif->getFilename());
for (size_t i = 0; i < bnum; i++) for (size_t i = 0; i < bnum; i++)
{ {
if (bones[i].empty()) if (bones[i].empty())
nif->fail("Oops: Missing bone! Don't know how to handle this."); throw Nif::Exception("Oops: Missing bone! Don't know how to handle this.", nif->getFilename());
bones[i]->setBone(); bones[i]->setBone();
} }
} }
@ -470,7 +473,8 @@ namespace Nif
{ {
palette = nif->getString(); palette = nif->getString();
if (nif->getUInt() != palette.size()) if (nif->getUInt() != palette.size())
nif->file->warn("Failed size check in NiStringPalette"); Log(Debug::Warning) << "NIFFile Warning: Failed size check in NiStringPalette. File: "
<< nif->file->getFilename();
} }
void NiBoolData::read(NIFStream* nif) void NiBoolData::read(NIFStream* nif)

View file

@ -0,0 +1,21 @@
#ifndef OPENMW_COMPONENTS_NIF_EXCEPTION_HPP
#define OPENMW_COMPONENTS_NIF_EXCEPTION_HPP
#include <filesystem>
#include <stdexcept>
#include <string>
#include <components/files/conversion.hpp>
namespace Nif
{
struct Exception : std::runtime_error
{
explicit Exception(const std::string& message, const std::filesystem::path& path)
: std::runtime_error("NIFFile Error: " + message + " when reading " + Files::pathToUnicodeString(path))
{
}
};
}
#endif

View file

@ -15,6 +15,7 @@
#include "controller.hpp" #include "controller.hpp"
#include "data.hpp" #include "data.hpp"
#include "effect.hpp" #include "effect.hpp"
#include "exception.hpp"
#include "extra.hpp" #include "extra.hpp"
#include "physics.hpp" #include "physics.hpp"
#include "property.hpp" #include "property.hpp"
@ -200,7 +201,7 @@ namespace Nif
const bool supportedHeader = std::any_of(verStrings.begin(), verStrings.end(), const bool supportedHeader = std::any_of(verStrings.begin(), verStrings.end(),
[&](const std::string& verString) { return head.compare(0, verString.size(), verString) == 0; }); [&](const std::string& verString) { return head.compare(0, verString.size(), verString) == 0; });
if (!supportedHeader) if (!supportedHeader)
fail("Invalid NIF header: " + head); throw Nif::Exception("Invalid NIF header: " + head, filename);
// Get BCD version // Get BCD version
ver = nif.getUInt(); ver = nif.getUInt();
@ -214,9 +215,10 @@ namespace Nif
if (!supportedVersion) if (!supportedVersion)
{ {
if (sLoadUnsupportedFiles) if (sLoadUnsupportedFiles)
warn("Unsupported NIF version: " + printVersion(ver) + ". Proceed with caution!"); Log(Debug::Warning) << " NIFFile Warning: Unsupported NIF version: " << printVersion(ver)
<< ". Proceed with caution! File: " << filename;
else else
fail("Unsupported NIF version: " + printVersion(ver)); throw Nif::Exception("Unsupported NIF version: " + printVersion(ver), filename);
} }
// NIF data endianness // NIF data endianness
@ -224,7 +226,7 @@ namespace Nif
{ {
unsigned char endianness = nif.getChar(); unsigned char endianness = nif.getChar();
if (endianness == 0) if (endianness == 0)
fail("Big endian NIF files are unsupported"); throw Nif::Exception("Big endian NIF files are unsupported", filename);
} }
// User version // User version
@ -296,24 +298,19 @@ namespace Nif
{ {
std::stringstream error; std::stringstream error;
error << "Record number " << i << " out of " << recNum << " is blank."; error << "Record number " << i << " out of " << recNum << " is blank.";
fail(error.str()); throw Nif::Exception(error.str(), filename);
} }
// Record separator. Some Havok records in Oblivion do not have it. // Record separator. Some Havok records in Oblivion do not have it.
if (hasRecordSeparators && rec.compare(0, 3, "bhk")) if (hasRecordSeparators && rec.compare(0, 3, "bhk"))
{
if (nif.getInt()) if (nif.getInt())
{ Log(Debug::Warning) << "NIFFile Warning: Record number " << i << " out of " << recNum
std::stringstream warning; << " is preceded by a non-zero separator. File: " << filename;
warning << "Record number " << i << " out of " << recNum << " is preceded by a non-zero separator.";
warn(warning.str());
}
}
const auto entry = factories.find(rec); const auto entry = factories.find(rec);
if (entry == factories.end()) if (entry == factories.end())
fail("Unknown record type " + rec); throw Nif::Exception("Unknown record type " + rec, filename);
r = entry->second(); r = entry->second();
@ -343,7 +340,8 @@ namespace Nif
else else
{ {
roots[i] = nullptr; roots[i] = nullptr;
warn("Root " + std::to_string(i + 1) + " does not point to a record: index " + std::to_string(idx)); Log(Debug::Warning) << "NIFFile Warning: Root " << i + 1 << " does not point to a record: index " << idx
<< ". File: " << filename;
} }
} }
@ -369,16 +367,6 @@ namespace Nif
sLoadUnsupportedFiles = load; sLoadUnsupportedFiles = load;
} }
void NIFFile::warn(const std::string& msg) const
{
Log(Debug::Warning) << " NIFFile Warning: " << msg << "\nFile: " << filename;
}
[[noreturn]] void NIFFile::fail(const std::string& msg) const
{
throw std::runtime_error(" NIFFile Error: " + msg + "\nFile: " + Files::pathToUnicodeString(filename));
}
std::string NIFFile::getString(uint32_t index) const std::string NIFFile::getString(uint32_t index) const
{ {
if (index == std::numeric_limits<uint32_t>::max()) if (index == std::numeric_limits<uint32_t>::max())

View file

@ -94,12 +94,6 @@ namespace Nif
BETHVER_FO4 = 130 // Fallout 4 BETHVER_FO4 = 130 // Fallout 4
}; };
/// Used if file parsing fails
[[noreturn]] void fail(const std::string& msg) const;
/// Used when something goes wrong, but not catastrophically so
void warn(const std::string& msg) const;
/// Open a NIF stream. The name is used for error messages. /// Open a NIF stream. The name is used for error messages.
NIFFile(Files::IStreamPtr&& stream, const std::filesystem::path& name); NIFFile(Files::IStreamPtr&& stream, const std::filesystem::path& name);

View file

@ -5,6 +5,7 @@
#include <map> #include <map>
#include "exception.hpp"
#include "niffile.hpp" #include "niffile.hpp"
#include "nifstream.hpp" #include "nifstream.hpp"
@ -112,7 +113,8 @@ namespace Nif
} }
else if (count != 0) else if (count != 0)
{ {
nif->file->fail("Unhandled interpolation type: " + std::to_string(mInterpolationType)); throw Nif::Exception(
"Unhandled interpolation type: " + std::to_string(mInterpolationType), nif->file->getFilename());
} }
} }

View file

@ -3,6 +3,7 @@
#include <components/misc/algorithm.hpp> #include <components/misc/algorithm.hpp>
#include "data.hpp" #include "data.hpp"
#include "exception.hpp"
#include "physics.hpp" #include "physics.hpp"
#include "property.hpp" #include "property.hpp"
@ -68,7 +69,8 @@ namespace Nif
} }
default: default:
{ {
nif->file->fail("Unhandled NiBoundingVolume type: " + std::to_string(type)); throw Nif::Exception(
"Unhandled NiBoundingVolume type: " + std::to_string(type), nif->file->getFilename());
} }
} }
} }

View file

@ -42,6 +42,7 @@
#include <components/nif/controlled.hpp> #include <components/nif/controlled.hpp>
#include <components/nif/effect.hpp> #include <components/nif/effect.hpp>
#include <components/nif/exception.hpp>
#include <components/nif/extra.hpp> #include <components/nif/extra.hpp>
#include <components/nif/node.hpp> #include <components/nif/node.hpp>
#include <components/nif/property.hpp> #include <components/nif/property.hpp>
@ -265,15 +266,17 @@ namespace NifOsg
if (!seq) if (!seq)
{ {
nif->warn("Found no NiSequenceStreamHelper root record"); Log(Debug::Warning) << "NIFFile Warning: Found no NiSequenceStreamHelper root record. File: "
<< nif->getFilename();
return; return;
} }
Nif::ExtraPtr extra = seq->extra; Nif::ExtraPtr extra = seq->extra;
if (extra.empty() || extra->recType != Nif::RC_NiTextKeyExtraData) if (extra.empty() || extra->recType != Nif::RC_NiTextKeyExtraData)
{ {
nif->warn("First extra data was not a NiTextKeyExtraData, but a " Log(Debug::Warning) << "NIFFile Warning: First extra data was not a NiTextKeyExtraData, but a "
+ (extra.empty() ? std::string("nil") : extra->recName) + "."); << (extra.empty() ? std::string_view("nil") : std::string_view(extra->recName))
<< ". File: " << nif->getFilename();
return; return;
} }
@ -285,7 +288,8 @@ namespace NifOsg
{ {
if (extra->recType != Nif::RC_NiStringExtraData || ctrl->recType != Nif::RC_NiKeyframeController) if (extra->recType != Nif::RC_NiStringExtraData || ctrl->recType != Nif::RC_NiKeyframeController)
{ {
nif->warn("Unexpected extra data " + extra->recName + " with controller " + ctrl->recName); Log(Debug::Warning) << "NIFFile Warning: Unexpected extra data " << extra->recName
<< " with controller " << ctrl->recName << ". File: " << nif->getFilename();
continue; continue;
} }
@ -328,7 +332,7 @@ namespace NifOsg
roots.emplace_back(nifNode); roots.emplace_back(nifNode);
} }
if (roots.empty()) if (roots.empty())
nif->fail("Found no root nodes"); throw Nif::Exception("Found no root nodes", nif->getFilename());
osg::ref_ptr<SceneUtil::TextKeyMapHolder> textkeys(new SceneUtil::TextKeyMapHolder); osg::ref_ptr<SceneUtil::TextKeyMapHolder> textkeys(new SceneUtil::TextKeyMapHolder);
@ -1198,8 +1202,9 @@ namespace NifOsg
osg::Group* emitterNode = findEmitterNode.mFound; osg::Group* emitterNode = findEmitterNode.mFound;
if (!emitterNode) if (!emitterNode)
{ {
nif->warn("Failed to find particle emitter emitter node (node record index " Log(Debug::Warning)
+ std::to_string(recIndex) + ")"); << "NIFFile Warning: Failed to find particle emitter emitter node (node record index "
<< recIndex << "). File: " << nif->getFilename();
continue; continue;
} }