1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-19 22:23:51 +00:00

Use misc/endianness.hpp in components/nif/nifstream

This commit is contained in:
Petr Mikheev 2021-01-08 23:02:19 +01:00
parent 14dd11372f
commit eaaa2f4a1c
2 changed files with 35 additions and 65 deletions

View file

@ -7,7 +7,7 @@ namespace Nif
osg::Quat NIFStream::getQuaternion() osg::Quat NIFStream::getQuaternion()
{ {
float f[4]; float f[4];
readLittleEndianBufferOfType<4, float,uint32_t>(inp, (float*)&f); readLittleEndianBufferOfType<4, float>(inp, (float*)&f);
osg::Quat quat; osg::Quat quat;
quat.w() = f[0]; quat.w() = f[0];
quat.x() = f[1]; quat.x() = f[1];

View file

@ -9,6 +9,7 @@
#include <vector> #include <vector>
#include <components/files/constrainedfilestream.hpp> #include <components/files/constrainedfilestream.hpp>
#include <components/misc/endianness.hpp>
#include <osg/Vec3f> #include <osg/Vec3f>
#include <osg/Vec4f> #include <osg/Vec4f>
@ -22,61 +23,30 @@ namespace Nif
class NIFFile; class NIFFile;
/* /*
readLittleEndianBufferOfType: This template should only be used with non POD data types readLittleEndianBufferOfType: This template should only be used with arithmetic types
*/ */
template <uint32_t numInstances, typename T, typename IntegerT> inline void readLittleEndianBufferOfType(Files::IStreamPtr &pIStream, T* dest) template <uint32_t numInstances, typename T> inline void readLittleEndianBufferOfType(Files::IStreamPtr &pIStream, T* dest)
{ {
#if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86)
pIStream->read((char*)dest, numInstances * sizeof(T)); pIStream->read((char*)dest, numInstances * sizeof(T));
#else if constexpr (Misc::IS_BIG_ENDIAN)
uint8_t* destByteBuffer = (uint8_t*)dest;
pIStream->read((char*)dest, numInstances * sizeof(T));
/*
Due to the loop iterations being known at compile time,
this nested loop will most likely be unrolled
For example, for 2 instances of a 4 byte data type, you should get the below result
*/
union {
IntegerT i;
T t;
} u;
for (uint32_t i = 0; i < numInstances; i++) for (uint32_t i = 0; i < numInstances; i++)
{ Misc::swapEndiannessInplace(dest[i]);
u = { 0 };
for (uint32_t byte = 0; byte < sizeof(T); byte++)
u.i |= (((IntegerT)destByteBuffer[i * sizeof(T) + byte]) << (byte * 8));
dest[i] = u.t;
}
#endif
} }
/* /*
readLittleEndianDynamicBufferOfType: This template should only be used with non POD data types readLittleEndianDynamicBufferOfType: This template should only be used with arithmetic types
*/ */
template <typename T, typename IntegerT> inline void readLittleEndianDynamicBufferOfType(Files::IStreamPtr &pIStream, T* dest, uint32_t numInstances) template <typename T> inline void readLittleEndianDynamicBufferOfType(Files::IStreamPtr &pIStream, T* dest, uint32_t numInstances)
{ {
#if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86)
pIStream->read((char*)dest, numInstances * sizeof(T)); pIStream->read((char*)dest, numInstances * sizeof(T));
#else if constexpr (Misc::IS_BIG_ENDIAN)
uint8_t* destByteBuffer = (uint8_t*)dest;
pIStream->read((char*)dest, numInstances * sizeof(T));
union {
IntegerT i;
T t;
} u;
for (uint32_t i = 0; i < numInstances; i++) for (uint32_t i = 0; i < numInstances; i++)
{ Misc::swapEndiannessInplace(dest[i]);
u.i = 0;
for (uint32_t byte = 0; byte < sizeof(T); byte++)
u.i |= ((IntegerT)destByteBuffer[i * sizeof(T) + byte]) << (byte * 8);
dest[i] = u.t;
}
#endif
} }
template<typename type, typename IntegerT> type inline readLittleEndianType(Files::IStreamPtr &pIStream) template<typename type> type inline readLittleEndianType(Files::IStreamPtr &pIStream)
{ {
type val; type val;
readLittleEndianBufferOfType<1,type,IntegerT>(pIStream, (type*)&val); readLittleEndianBufferOfType<1, type>(pIStream, (type*)&val);
return val; return val;
} }
@ -95,59 +65,59 @@ public:
char getChar() char getChar()
{ {
return readLittleEndianType<char,char>(inp); return readLittleEndianType<char>(inp);
} }
short getShort() short getShort()
{ {
return readLittleEndianType<short,short>(inp); return readLittleEndianType<short>(inp);
} }
unsigned short getUShort() unsigned short getUShort()
{ {
return readLittleEndianType<unsigned short,unsigned short>(inp); return readLittleEndianType<unsigned short>(inp);
} }
int getInt() int getInt()
{ {
return readLittleEndianType<int,int>(inp); return readLittleEndianType<int>(inp);
} }
unsigned int getUInt() unsigned int getUInt()
{ {
return readLittleEndianType<unsigned int,unsigned int>(inp); return readLittleEndianType<unsigned int>(inp);
} }
float getFloat() float getFloat()
{ {
return readLittleEndianType<float,uint32_t>(inp); return readLittleEndianType<float>(inp);
} }
osg::Vec2f getVector2() osg::Vec2f getVector2()
{ {
osg::Vec2f vec; osg::Vec2f vec;
readLittleEndianBufferOfType<2,float,uint32_t>(inp, (float*)&vec._v[0]); readLittleEndianBufferOfType<2,float>(inp, (float*)&vec._v[0]);
return vec; return vec;
} }
osg::Vec3f getVector3() osg::Vec3f getVector3()
{ {
osg::Vec3f vec; osg::Vec3f vec;
readLittleEndianBufferOfType<3, float,uint32_t>(inp, (float*)&vec._v[0]); readLittleEndianBufferOfType<3, float>(inp, (float*)&vec._v[0]);
return vec; return vec;
} }
osg::Vec4f getVector4() osg::Vec4f getVector4()
{ {
osg::Vec4f vec; osg::Vec4f vec;
readLittleEndianBufferOfType<4, float,uint32_t>(inp, (float*)&vec._v[0]); readLittleEndianBufferOfType<4, float>(inp, (float*)&vec._v[0]);
return vec; return vec;
} }
Matrix3 getMatrix3() Matrix3 getMatrix3()
{ {
Matrix3 mat; Matrix3 mat;
readLittleEndianBufferOfType<9, float,uint32_t>(inp, (float*)&mat.mValues); readLittleEndianBufferOfType<9, float>(inp, (float*)&mat.mValues);
return mat; return mat;
} }
@ -181,14 +151,14 @@ public:
///Read in a string of the length specified in the file ///Read in a string of the length specified in the file
std::string getSizedString() std::string getSizedString()
{ {
size_t size = readLittleEndianType<uint32_t,uint32_t>(inp); size_t size = readLittleEndianType<uint32_t>(inp);
return getSizedString(size); return getSizedString(size);
} }
///Specific to Bethesda headers, uses a byte for length ///Specific to Bethesda headers, uses a byte for length
std::string getExportString() std::string getExportString()
{ {
size_t size = static_cast<size_t>(readLittleEndianType<uint8_t,uint8_t>(inp)); size_t size = static_cast<size_t>(readLittleEndianType<uint8_t>(inp));
return getSizedString(size); return getSizedString(size);
} }
@ -203,58 +173,58 @@ public:
void getChars(std::vector<char> &vec, size_t size) void getChars(std::vector<char> &vec, size_t size)
{ {
vec.resize(size); vec.resize(size);
readLittleEndianDynamicBufferOfType<char,char>(inp, vec.data(), size); readLittleEndianDynamicBufferOfType<char>(inp, vec.data(), size);
} }
void getUChars(std::vector<unsigned char> &vec, size_t size) void getUChars(std::vector<unsigned char> &vec, size_t size)
{ {
vec.resize(size); vec.resize(size);
readLittleEndianDynamicBufferOfType<unsigned char,unsigned char>(inp, vec.data(), size); readLittleEndianDynamicBufferOfType<unsigned char>(inp, vec.data(), size);
} }
void getUShorts(std::vector<unsigned short> &vec, size_t size) void getUShorts(std::vector<unsigned short> &vec, size_t size)
{ {
vec.resize(size); vec.resize(size);
readLittleEndianDynamicBufferOfType<unsigned short,unsigned short>(inp, vec.data(), size); readLittleEndianDynamicBufferOfType<unsigned short>(inp, vec.data(), size);
} }
void getFloats(std::vector<float> &vec, size_t size) void getFloats(std::vector<float> &vec, size_t size)
{ {
vec.resize(size); vec.resize(size);
readLittleEndianDynamicBufferOfType<float,uint32_t>(inp, vec.data(), size); readLittleEndianDynamicBufferOfType<float>(inp, vec.data(), size);
} }
void getInts(std::vector<int> &vec, size_t size) void getInts(std::vector<int> &vec, size_t size)
{ {
vec.resize(size); vec.resize(size);
readLittleEndianDynamicBufferOfType<int,int>(inp, vec.data(), size); readLittleEndianDynamicBufferOfType<int>(inp, vec.data(), size);
} }
void getUInts(std::vector<unsigned int> &vec, size_t size) void getUInts(std::vector<unsigned int> &vec, size_t size)
{ {
vec.resize(size); vec.resize(size);
readLittleEndianDynamicBufferOfType<unsigned int,unsigned int>(inp, vec.data(), size); readLittleEndianDynamicBufferOfType<unsigned int>(inp, vec.data(), size);
} }
void getVector2s(std::vector<osg::Vec2f> &vec, size_t size) void getVector2s(std::vector<osg::Vec2f> &vec, size_t size)
{ {
vec.resize(size); vec.resize(size);
/* The packed storage of each Vec2f is 2 floats exactly */ /* The packed storage of each Vec2f is 2 floats exactly */
readLittleEndianDynamicBufferOfType<float,uint32_t>(inp,(float*)vec.data(), size*2); readLittleEndianDynamicBufferOfType<float>(inp,(float*)vec.data(), size*2);
} }
void getVector3s(std::vector<osg::Vec3f> &vec, size_t size) void getVector3s(std::vector<osg::Vec3f> &vec, size_t size)
{ {
vec.resize(size); vec.resize(size);
/* The packed storage of each Vec3f is 3 floats exactly */ /* The packed storage of each Vec3f is 3 floats exactly */
readLittleEndianDynamicBufferOfType<float,uint32_t>(inp, (float*)vec.data(), size*3); readLittleEndianDynamicBufferOfType<float>(inp, (float*)vec.data(), size*3);
} }
void getVector4s(std::vector<osg::Vec4f> &vec, size_t size) void getVector4s(std::vector<osg::Vec4f> &vec, size_t size)
{ {
vec.resize(size); vec.resize(size);
/* The packed storage of each Vec4f is 4 floats exactly */ /* The packed storage of each Vec4f is 4 floats exactly */
readLittleEndianDynamicBufferOfType<float,uint32_t>(inp, (float*)vec.data(), size*4); readLittleEndianDynamicBufferOfType<float>(inp, (float*)vec.data(), size*4);
} }
void getQuaternions(std::vector<osg::Quat> &quat, size_t size) void getQuaternions(std::vector<osg::Quat> &quat, size_t size)