88
99namespace Nif
1010{
11+ namespace
12+ {
13+ void readNiSkinDataVertWeight (NIFStream& stream, NiSkinData::VertWeight& value)
14+ {
15+ auto & [vertex, weight] = value;
16+ stream.read (vertex);
17+ stream.read (weight);
18+ }
19+
20+ struct ReadNiSkinDataBoneInfo
21+ {
22+ bool mHasVertexWeights ;
23+
24+ void operator ()(NIFStream& stream, NiSkinData::BoneInfo& value) const
25+ {
26+ stream.read (value.mTransform );
27+ stream.read (value.mBoundSphere );
28+
29+ const uint16_t numVertices = stream.get <uint16_t >();
30+
31+ if (!mHasVertexWeights )
32+ return ;
33+
34+ stream.readVectorOfRecords (numVertices, readNiSkinDataVertWeight, value.mWeights );
35+ }
36+ };
37+
38+ struct ReadNiMorphDataMorphData
39+ {
40+ uint32_t mNumVerts ;
41+
42+ void operator ()(NIFStream& stream, NiMorphData::MorphData& value) const
43+ {
44+ value.mKeyFrames = std::make_shared<FloatKeyMap>();
45+ value.mKeyFrames ->read (&stream, /* morph*/ true );
46+ stream.readVector (value.mVertices , mNumVerts );
47+ }
48+ };
49+ }
50+
1151 void NiGeometryData::read (NIFStream* nif)
1252 {
1353 if (nif->getVersion () >= NIFStream::generateVersion (10 , 1 , 0 , 114 ))
@@ -215,20 +255,21 @@ namespace Nif
215255 nif->read (mSigned );
216256 }
217257
258+ void NiPixelData::Mipmap::read (NIFStream* nif)
259+ {
260+ nif->read (mWidth );
261+ nif->read (mHeight );
262+ nif->read (mOffset );
263+ }
264+
218265 void NiPixelData::read (NIFStream* nif)
219266 {
220267 mPixelFormat .read (nif);
221268 mPalette .read (nif);
222- mMipmaps . resize ( nif->get <uint32_t >() );
269+ const uint32_t mipmapsCount = nif->get <uint32_t >();
223270 nif->read (mBytesPerPixel );
224- for (Mipmap& mip : mMipmaps )
225- {
226- nif->read (mip.mWidth );
227- nif->read (mip.mHeight );
228- nif->read (mip.mOffset );
229- }
230- uint32_t numPixels;
231- nif->read (numPixels);
271+ nif->readVectorOfRecords (mipmapsCount, mMipmaps );
272+ const uint32_t numPixels = nif->get <uint32_t >();
232273 if (nif->getVersion () >= NIFStream::generateVersion (10 , 4 , 0 , 2 ))
233274 nif->read (mNumFaces );
234275 nif->readVector (mData , numPixels * mNumFaces );
@@ -247,12 +288,14 @@ namespace Nif
247288
248289 void NiVisData::read (NIFStream* nif)
249290 {
250- mKeys = std::make_shared<std::vector<std::pair<float , bool >>>(nif->get <uint32_t >());
251- for (auto & [time, value] : *mKeys )
252- {
253- nif->read (time);
254- value = nif->get <uint8_t >() != 0 ;
255- }
291+ const auto readPair = [](NIFStream& stream, std::pair<float , bool >& value) {
292+ stream.read (value.first );
293+ value.second = stream.get <uint8_t >() != 0 ;
294+ };
295+
296+ std::vector<std::pair<float , bool >> keys;
297+ nif->readVectorOfRecords <uint32_t >(readPair, keys);
298+ mKeys = std::make_shared<std::vector<std::pair<float , bool >>>(std::move (keys));
256299 }
257300
258301 void NiSkinInstance::read (NIFStream* nif)
@@ -296,16 +339,17 @@ namespace Nif
296339 return partitions;
297340 }
298341
342+ void BSDismemberSkinInstance::BodyPart::read (NIFStream* nif)
343+ {
344+ nif->read (mFlags );
345+ nif->read (mType );
346+ }
347+
299348 void BSDismemberSkinInstance::read (NIFStream* nif)
300349 {
301350 NiSkinInstance::read (nif);
302351
303- mParts .resize (nif->get <uint32_t >());
304- for (BodyPart& part : mParts )
305- {
306- nif->read (part.mFlags );
307- nif->read (part.mType );
308- }
352+ nif->readVectorOfRecords <uint32_t >(mParts );
309353 }
310354
311355 void BSSkinInstance::read (NIFStream* nif)
@@ -339,8 +383,7 @@ namespace Nif
339383 {
340384 nif->read (mTransform );
341385
342- uint32_t numBones;
343- nif->read (numBones);
386+ const uint32_t numBones = nif->get <uint32_t >();
344387 bool hasVertexWeights = true ;
345388 if (nif->getVersion () >= NIFFile::NIFVersion::VER_MW)
346389 {
@@ -351,25 +394,8 @@ namespace Nif
351394 nif->read (hasVertexWeights);
352395 }
353396
354- mBones .resize (numBones);
355- for (BoneInfo& bi : mBones )
356- {
357- nif->read (bi.mTransform );
358- nif->read (bi.mBoundSphere );
359-
360- uint16_t numVertices;
361- nif->read (numVertices);
362-
363- if (!hasVertexWeights)
364- continue ;
365-
366- bi.mWeights .resize (numVertices);
367- for (auto & [vertex, weight] : bi.mWeights )
368- {
369- nif->read (vertex);
370- nif->read (weight);
371- }
372- }
397+ const ReadNiSkinDataBoneInfo readBoneInfo{ .mHasVertexWeights = hasVertexWeights };
398+ nif->readVectorOfRecords (numBones, readBoneInfo, mBones );
373399 }
374400
375401 void NiSkinData::post (Reader& nif)
@@ -467,18 +493,12 @@ namespace Nif
467493
468494 void NiMorphData::read (NIFStream* nif)
469495 {
470- uint32_t numMorphs, numVerts;
471- nif->read (numMorphs);
472- nif->read (numVerts);
496+ const uint32_t numMorphs = nif->get <uint32_t >();
497+ const uint32_t numVerts = nif->get <uint32_t >();
473498 nif->read (mRelativeTargets );
474499
475- mMorphs .resize (numMorphs);
476- for (MorphData& morph : mMorphs )
477- {
478- morph.mKeyFrames = std::make_shared<FloatKeyMap>();
479- morph.mKeyFrames ->read (nif, /* morph*/ true );
480- nif->readVector (morph.mVertices , numVerts);
481- }
500+ const ReadNiMorphDataMorphData readMorph{ .mNumVerts = numVerts };
501+ nif->readVectorOfRecords (numMorphs, readMorph, mMorphs );
482502 }
483503
484504 void NiKeyframeData::read (NIFStream* nif)
0 commit comments