Separate record list reading from its state

To make it possible to set record list state independently from reading NIF
file.
coverity_clang_test
elsid 2 years ago
parent 4b760e8846
commit 0688b6de40
No known key found for this signature in database
GPG Key ID: 4DE04C198CBA7625

@ -19,14 +19,14 @@ namespace Nif
if (nif->getVersion() < NIFStream::generateVersion(10, 0, 1, 0))
extra.read(nif);
else
extralist.read(nif);
readRecordList(nif, extralist);
controller.read(nif);
}
void Named::post(Reader& nif)
{
extra.post(nif);
extralist.post(nif);
postRecordList(nif, extralist);
controller.post(nif);
}
}

@ -209,7 +209,7 @@ namespace Nif
void NiMultiTargetTransformController::post(Reader& nif)
{
NiInterpController::post(nif);
mExtraTargets.post(nif);
postRecordList(nif, mExtraTargets);
}
void NiAlphaController::read(NIFStream* nif)
@ -251,7 +251,7 @@ namespace Nif
{
if (nif->getVersion() <= NIFFile::NIFVersion::VER_OB)
{
mInterpolators.read(nif);
readRecordList(nif, mInterpolators);
if (nif->getVersion() >= NIFStream::generateVersion(10, 2, 0, 0) && nif->getBethVersion() > 9)
{
unsigned int numUnknown = nif->getUInt();
@ -279,7 +279,7 @@ namespace Nif
{
NiInterpController::post(nif);
mData.post(nif);
mInterpolators.post(nif);
postRecordList(nif, mInterpolators);
}
void NiVisController::read(NIFStream* nif)
@ -304,13 +304,13 @@ namespace Nif
timeStart = nif->getFloat();
mDelta = nif->getFloat();
}
mSources.read(nif);
readRecordList(nif, mSources);
}
void NiFlipController::post(Reader& nif)
{
NiFloatInterpController::post(nif);
mSources.post(nif);
postRecordList(nif, mSources);
}
void bhkBlendController::read(NIFStream* nif)

@ -13,7 +13,7 @@ namespace Nif
if (nif->getVersion() >= NIFStream::generateVersion(10, 1, 0, 101))
partitions.read(nif);
root.read(nif);
bones.read(nif);
readRecordList(nif, bones);
}
void NiSkinInstance::post(Reader& nif)
@ -21,20 +21,19 @@ namespace Nif
data.post(nif);
partitions.post(nif);
root.post(nif);
bones.post(nif);
postRecordList(nif, bones);
if (data.empty() || root.empty())
throw Nif::Exception("NiSkinInstance missing root or data", nif.getFilename());
size_t bnum = bones.length();
if (bnum != data->bones.size())
if (bones.size() != data->bones.size())
throw Nif::Exception("Mismatch in NiSkinData bone count", nif.getFilename());
for (size_t i = 0; i < bnum; i++)
for (auto& bone : bones)
{
if (bones[i].empty())
if (bone.empty())
throw Nif::Exception("Oops: Missing bone! Don't know how to handle this.", nif.getFilename());
bones[i]->setBone();
bone->setBone();
}
}

@ -84,7 +84,7 @@ namespace Nif
if (nif->getVersion() <= NIFStream::generateVersion(4, 2, 2, 0))
velocity = nif->getVector3();
if (nif->getBethVersion() <= NIFFile::BethVersion::BETHVER_FO3)
props.read(nif);
readRecordList(nif, props);
if (nif->getVersion() <= NIFStream::generateVersion(4, 2, 2, 0))
hasBounds = nif->getBoolean();
@ -102,7 +102,7 @@ namespace Nif
void Node::post(Reader& nif)
{
Named::post(nif);
props.post(nif);
postRecordList(nif, props);
collision.post(nif);
}
@ -114,9 +114,9 @@ namespace Nif
void NiNode::read(NIFStream* nif)
{
Node::read(nif);
children.read(nif);
readRecordList(nif, children);
if (nif->getBethVersion() < NIFFile::BethVersion::BETHVER_FO4)
effects.read(nif);
readRecordList(nif, effects);
// Discard transformations for the root node, otherwise some meshes
// occasionally get wrong orientation. Only for NiNode-s for now, but
@ -131,14 +131,14 @@ namespace Nif
void NiNode::post(Reader& nif)
{
Node::post(nif);
children.post(nif);
effects.post(nif);
postRecordList(nif, children);
postRecordList(nif, effects);
for (size_t i = 0; i < children.length(); i++)
for (auto& child : children)
{
// Why would a unique list of children contain empty refs?
if (!children[i].empty())
children[i]->parents.push_back(this);
if (!child.empty())
child->parents.push_back(this);
}
}

@ -202,14 +202,14 @@ namespace Nif
mGrowBy = nif->getUInt();
if (nif->getVersion() >= NIFStream::generateVersion(10, 1, 0, 0))
mScale = nif->getVector4();
mData.read(nif);
readRecordList(nif, mData);
unsigned int numFilters = nif->getUInt();
nif->getUInts(mFilters, numFilters);
}
void bhkNiTriStripsShape::post(Reader& nif)
{
mData.post(nif);
postRecordList(nif, mData);
}
void bhkPackedNiTriStripsShape::read(NIFStream* nif)
@ -301,7 +301,7 @@ namespace Nif
void bhkListShape::read(NIFStream* nif)
{
mSubshapes.read(nif);
readRecordList(nif, mSubshapes);
mHavokMaterial.read(nif);
mChildShapeProperty.read(nif);
mChildFilterProperty.read(nif);
@ -315,7 +315,7 @@ namespace Nif
{
bhkEntity::read(nif);
mInfo.read(nif);
mConstraints.read(nif);
readRecordList(nif, mConstraints);
if (nif->getBethVersion() < 76)
mBodyFlags = nif->getUInt();
else

@ -85,39 +85,25 @@ namespace Nif
implementation.
*/
template <class X>
class RecordListT
{
typedef RecordPtrT<X> Ptr;
std::vector<Ptr> list;
public:
RecordListT() = default;
RecordListT(std::vector<Ptr> list)
: list(std::move(list))
{
}
void read(NIFStream* nif)
{
int len = nif->getInt();
list.resize(len);
using RecordListT = std::vector<RecordPtrT<X>>;
for (size_t i = 0; i < list.size(); i++)
list[i].read(nif);
}
template <class T>
void readRecordList(NIFStream* nif, RecordListT<T>& list)
{
const int length = nif->getInt();
void post(Reader& nif)
{
for (size_t i = 0; i < list.size(); i++)
list[i].post(nif);
}
list.resize(static_cast<std::size_t>(length));
const Ptr& operator[](size_t index) const { return list.at(index); }
Ptr& operator[](size_t index) { return list.at(index); }
for (auto& value : list)
value.read(nif);
}
size_t length() const { return list.size(); }
};
template <class T>
void postRecordList(Reader& nif, RecordListT<T>& list)
{
for (auto& value : list)
value.post(nif);
}
struct Node;
struct Extra;

@ -284,14 +284,9 @@ namespace NifBullet
if (const Nif::NiNode* ninode = dynamic_cast<const Nif::NiNode*>(&node))
{
const Nif::NodeList& list = ninode->children;
for (size_t i = 0; i < list.length(); i++)
{
if (!list[i].empty())
{
if (findBoundingBox(list[i].get(), filename))
return true;
}
}
for (const auto& child : list)
if (!child.empty() && findBoundingBox(child.get(), filename))
return true;
}
return false;
}
@ -300,12 +295,11 @@ namespace NifBullet
{
if (const Nif::NiNode* ninode = dynamic_cast<const Nif::NiNode*>(&rootNode))
{
const Nif::NodeList& list = ninode->children;
for (size_t i = 0; i < list.length(); i++)
for (const auto& child : ninode->children)
{
if (list[i].empty())
if (child.empty())
continue;
if (list[i].getPtr()->recType == Nif::RC_RootCollisionNode)
if (child.getPtr()->recType == Nif::RC_RootCollisionNode)
return true;
}
}
@ -316,17 +310,16 @@ namespace NifBullet
{
if (const Nif::NiNode* ninode = dynamic_cast<const Nif::NiNode*>(&rootNode))
{
const Nif::NodeList& list = ninode->children;
for (size_t i = 0; i < list.length(); i++)
for (const auto& child : ninode->children)
{
if (list[i].empty())
if (child.empty())
continue;
const Nif::Node* childNode = list[i].getPtr();
const Nif::Node* childNode = child.getPtr();
if (childNode->recType != Nif::RC_RootCollisionNode)
continue;
const Nif::NiNode* niChildnode
= static_cast<const Nif::NiNode*>(childNode); // RootCollisionNode is always a NiNode
if (childNode->hasBounds || niChildnode->children.length() > 0)
if (childNode->hasBounds || niChildnode->children.size() > 0)
return false;
}
}
@ -412,13 +405,13 @@ namespace NifBullet
{
const Nif::NodeList& list = ninode->children;
const Nif::Parent currentParent{ *ninode, parent };
for (size_t i = 0; i < list.length(); i++)
for (const auto& child : list)
{
if (list[i].empty())
if (child.empty())
continue;
assert(std::find(list[i]->parents.begin(), list[i]->parents.end(), ninode) != list[i]->parents.end());
handleNode(fileName, list[i].get(), &currentParent, flags, isCollisionNode, isAnimated, autogenerated,
assert(std::find(child->parents.begin(), child->parents.end(), ninode) != child->parents.end());
handleNode(fileName, child.get(), &currentParent, flags, isCollisionNode, isAnimated, autogenerated,
avoid, visualCollisionType);
}
}

@ -199,7 +199,7 @@ namespace NifOsg
GeomMorpherController::GeomMorpherController(const Nif::NiGeomMorpherController* ctrl)
{
if (ctrl->mInterpolators.length() == 0)
if (ctrl->mInterpolators.size() == 0)
{
if (!ctrl->mData.empty())
{
@ -209,10 +209,10 @@ namespace NifOsg
return;
}
mKeyFrames.resize(ctrl->mInterpolators.length());
mKeyFrames.resize(ctrl->mInterpolators.size());
mWeights = ctrl->mWeights;
for (size_t i = 0; i < ctrl->mInterpolators.length(); ++i)
for (std::size_t i = 0, n = ctrl->mInterpolators.size(); i < n; ++i)
{
if (!ctrl->mInterpolators[i].empty() && ctrl->mInterpolators[i]->recType == Nif::RC_NiFloatInterpolator)
{

@ -76,9 +76,9 @@ namespace
if (const Nif::NiNode* ninode = dynamic_cast<const Nif::NiNode*>(node))
{
outIndices.push_back(ninode->recIndex);
for (unsigned int i = 0; i < ninode->children.length(); ++i)
if (!ninode->children[i].empty())
getAllNiNodes(ninode->children[i].getPtr(), outIndices);
for (const auto& child : ninode->children)
if (!child.empty())
getAllNiNodes(child.getPtr(), outIndices);
}
}
@ -102,18 +102,17 @@ namespace
{
if (parent != nullptr)
collectDrawableProperties(&parent->mNiNode, parent->mParent, out);
const Nif::PropertyList& props = nifNode->props;
for (size_t i = 0; i < props.length(); ++i)
for (const auto& property : nifNode->props)
{
if (!props[i].empty())
if (!property.empty())
{
switch (props[i]->recType)
switch (property->recType)
{
case Nif::RC_NiMaterialProperty:
case Nif::RC_NiVertexColorProperty:
case Nif::RC_NiSpecularProperty:
case Nif::RC_NiAlphaProperty:
out.push_back(props[i].getPtr());
out.push_back(property.getPtr());
break;
default:
break;
@ -375,19 +374,17 @@ namespace NifOsg
SceneUtil::CompositeStateSetUpdater* composite, Resource::ImageManager* imageManager,
std::vector<unsigned int>& boundTextures, int animflags)
{
const Nif::PropertyList& props = nifNode->props;
bool hasStencilProperty = false;
for (size_t i = 0; i < props.length(); ++i)
for (const auto& property : nifNode->props)
{
if (props[i].empty())
if (property.empty())
continue;
if (props[i].getPtr()->recType == Nif::RC_NiStencilProperty)
if (property.getPtr()->recType == Nif::RC_NiStencilProperty)
{
const Nif::NiStencilProperty* stencilprop
= static_cast<const Nif::NiStencilProperty*>(props[i].getPtr());
= static_cast<const Nif::NiStencilProperty*>(property.getPtr());
if (stencilprop->data.enabled != 0)
{
hasStencilProperty = true;
@ -396,24 +393,24 @@ namespace NifOsg
}
}
for (size_t i = 0; i < props.length(); ++i)
for (const auto& property : nifNode->props)
{
if (!props[i].empty())
if (!property.empty())
{
// Get the lowest numbered recIndex of the NiTexturingProperty root node.
// This is what is overridden when a spell effect "particle texture" is used.
if (nifNode->parents.empty() && !mFoundFirstRootTexturingProperty
&& props[i].getPtr()->recType == Nif::RC_NiTexturingProperty)
&& property.getPtr()->recType == Nif::RC_NiTexturingProperty)
{
mFirstRootTextureIndex = props[i].getPtr()->recIndex;
mFirstRootTextureIndex = property.getPtr()->recIndex;
mFoundFirstRootTexturingProperty = true;
}
else if (props[i].getPtr()->recType == Nif::RC_NiTexturingProperty)
else if (property.getPtr()->recType == Nif::RC_NiTexturingProperty)
{
if (props[i].getPtr()->recIndex == mFirstRootTextureIndex)
if (property.getPtr()->recIndex == mFirstRootTextureIndex)
applyTo->setUserValue("overrideFx", 1);
}
handleProperty(props[i].getPtr(), applyTo, composite, imageManager, boundTextures, animflags,
handleProperty(property.getPtr(), applyTo, composite, imageManager, boundTextures, animflags,
hasStencilProperty);
}
}
@ -463,13 +460,13 @@ namespace NifOsg
const Nif::NiFltAnimationNode* niFltAnimationNode = static_cast<const Nif::NiFltAnimationNode*>(nifNode);
osg::ref_ptr<osg::Sequence> sequenceNode(new osg::Sequence);
sequenceNode->setName(niFltAnimationNode->name);
if (niFltAnimationNode->children.length() != 0)
if (!niFltAnimationNode->children.empty())
{
if (niFltAnimationNode->swing())
sequenceNode->setDefaultTime(
niFltAnimationNode->mDuration / (niFltAnimationNode->children.length() * 2));
niFltAnimationNode->mDuration / (niFltAnimationNode->children.size() * 2));
else
sequenceNode->setDefaultTime(niFltAnimationNode->mDuration / niFltAnimationNode->children.length());
sequenceNode->setDefaultTime(niFltAnimationNode->mDuration / niFltAnimationNode->children.size());
}
return sequenceNode;
}
@ -630,12 +627,9 @@ namespace NifOsg
for (Nif::ExtraPtr e = nifNode->extra; !e.empty(); e = e->next)
extraCollection.emplace_back(e);
for (size_t i = 0; i < nifNode->extralist.length(); ++i)
{
Nif::ExtraPtr e = nifNode->extralist[i];
if (!e.empty())
extraCollection.emplace_back(e);
}
for (const auto& extraNode : nifNode->extralist)
if (!extraNode.empty())
extraCollection.emplace_back(extraNode);
for (const auto& e : extraCollection)
{
@ -805,20 +799,16 @@ namespace NifOsg
if (ninode)
{
const Nif::NodeList& effects = ninode->effects;
for (size_t i = 0; i < effects.length(); ++i)
{
if (!effects[i].empty())
handleEffect(effects[i].getPtr(), currentNode, imageManager);
}
for (const auto& effect : effects)
if (!effect.empty())
handleEffect(effect.getPtr(), currentNode, imageManager);
const Nif::NodeList& children = ninode->children;
const Nif::Parent currentParent{ *ninode, parent };
for (size_t i = 0; i < children.length(); ++i)
{
if (!children[i].empty())
handleNode(children[i].getPtr(), &currentParent, currentNode, imageManager, boundTextures,
animflags, skipMeshes, hasMarkers, hasAnimatedParents, textKeys, rootNode);
}
for (const auto& child : children)
if (!child.empty())
handleNode(child.getPtr(), &currentParent, currentNode, imageManager, boundTextures, animflags,
skipMeshes, hasMarkers, hasAnimatedParents, textKeys, rootNode);
}
if (nifNode->recType == Nif::RC_NiFltAnimationNode)
@ -1012,13 +1002,12 @@ namespace NifOsg
wrapT = inherit->getWrap(osg::Texture2D::WRAP_T);
}
for (unsigned int i = 0; i < flipctrl->mSources.length(); ++i)
for (const auto& source : flipctrl->mSources)
{
Nif::NiSourceTexturePtr st = flipctrl->mSources[i];
if (st.empty())
if (source.empty())
continue;
osg::ref_ptr<osg::Image> image(handleSourceTexture(st.getPtr(), imageManager));
osg::ref_ptr<osg::Image> image(handleSourceTexture(source.getPtr(), imageManager));
osg::ref_ptr<osg::Texture2D> texture(new osg::Texture2D(image));
if (image)
texture->setTextureSize(image->s(), image->t());
@ -1480,7 +1469,7 @@ namespace NifOsg
const Nif::NiSkinInstance* skin = static_cast<const Nif::NiGeometry*>(nifNode)->skin.getPtr();
const Nif::NiSkinData* data = skin->data.getPtr();
const Nif::NodeList& bones = skin->bones;
for (size_t i = 0; i < bones.length(); i++)
for (std::size_t i = 0, n = bones.size(); i < n; ++i)
{
std::string boneName = Misc::StringUtils::lowerCase(bones[i].getPtr()->name);

Loading…
Cancel
Save