|
|
@ -7,6 +7,8 @@
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <vector>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <typeinfo>
|
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
|
|
|
|
#include <components/files/constrainedfilestream.hpp>
|
|
|
|
#include <components/files/constrainedfilestream.hpp>
|
|
|
|
#include <components/misc/endianness.hpp>
|
|
|
|
#include <components/misc/endianness.hpp>
|
|
|
@ -22,31 +24,32 @@ namespace Nif
|
|
|
|
|
|
|
|
|
|
|
|
class NIFFile;
|
|
|
|
class NIFFile;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
template <std::size_t numInstances, typename T> inline void readLittleEndianBufferOfType(Files::IStreamPtr &pIStream, T* dest)
|
|
|
|
readLittleEndianBufferOfType: This template should only be used with arithmetic types
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
template <uint32_t numInstances, typename T> inline void readLittleEndianBufferOfType(Files::IStreamPtr &pIStream, T* dest)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
static_assert(std::is_arithmetic_v<T>, "Buffer element type is not arithmetic");
|
|
|
|
pIStream->read((char*)dest, numInstances * sizeof(T));
|
|
|
|
pIStream->read((char*)dest, numInstances * sizeof(T));
|
|
|
|
|
|
|
|
if (pIStream->bad())
|
|
|
|
|
|
|
|
throw std::runtime_error("Failed to read little endian typed (" + std::string(typeid(T).name()) + ") buffer of "
|
|
|
|
|
|
|
|
+ std::to_string(numInstances) + " instances");
|
|
|
|
if constexpr (Misc::IS_BIG_ENDIAN)
|
|
|
|
if constexpr (Misc::IS_BIG_ENDIAN)
|
|
|
|
for (uint32_t i = 0; i < numInstances; i++)
|
|
|
|
for (std::size_t i = 0; i < numInstances; i++)
|
|
|
|
Misc::swapEndiannessInplace(dest[i]);
|
|
|
|
Misc::swapEndiannessInplace(dest[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
template <typename T> inline void readLittleEndianDynamicBufferOfType(Files::IStreamPtr &pIStream, T* dest, std::size_t numInstances)
|
|
|
|
readLittleEndianDynamicBufferOfType: This template should only be used with arithmetic types
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
template <typename T> inline void readLittleEndianDynamicBufferOfType(Files::IStreamPtr &pIStream, T* dest, uint32_t numInstances)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
static_assert(std::is_arithmetic_v<T>, "Buffer element type is not arithmetic");
|
|
|
|
pIStream->read((char*)dest, numInstances * sizeof(T));
|
|
|
|
pIStream->read((char*)dest, numInstances * sizeof(T));
|
|
|
|
|
|
|
|
if (pIStream->bad())
|
|
|
|
|
|
|
|
throw std::runtime_error("Failed to read little endian dynamic buffer of " + std::to_string(numInstances) + " instances");
|
|
|
|
if constexpr (Misc::IS_BIG_ENDIAN)
|
|
|
|
if constexpr (Misc::IS_BIG_ENDIAN)
|
|
|
|
for (uint32_t i = 0; i < numInstances; i++)
|
|
|
|
for (std::size_t i = 0; i < numInstances; i++)
|
|
|
|
Misc::swapEndiannessInplace(dest[i]);
|
|
|
|
Misc::swapEndiannessInplace(dest[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
template<typename type> type inline readLittleEndianType(Files::IStreamPtr &pIStream)
|
|
|
|
template<typename type> type inline readLittleEndianType(Files::IStreamPtr &pIStream)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
type val;
|
|
|
|
type val;
|
|
|
|
readLittleEndianBufferOfType<1, type>(pIStream, (type*)&val);
|
|
|
|
readLittleEndianBufferOfType<1, type>(pIStream, &val);
|
|
|
|
return val;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -96,21 +99,21 @@ public:
|
|
|
|
osg::Vec2f getVector2()
|
|
|
|
osg::Vec2f getVector2()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
osg::Vec2f vec;
|
|
|
|
osg::Vec2f vec;
|
|
|
|
readLittleEndianBufferOfType<2,float>(inp, (float*)&vec._v[0]);
|
|
|
|
readLittleEndianBufferOfType<2,float>(inp, vec._v);
|
|
|
|
return vec;
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
osg::Vec3f getVector3()
|
|
|
|
osg::Vec3f getVector3()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
osg::Vec3f vec;
|
|
|
|
osg::Vec3f vec;
|
|
|
|
readLittleEndianBufferOfType<3, float>(inp, (float*)&vec._v[0]);
|
|
|
|
readLittleEndianBufferOfType<3, float>(inp, vec._v);
|
|
|
|
return vec;
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
osg::Vec4f getVector4()
|
|
|
|
osg::Vec4f getVector4()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
osg::Vec4f vec;
|
|
|
|
osg::Vec4f vec;
|
|
|
|
readLittleEndianBufferOfType<4, float>(inp, (float*)&vec._v[0]);
|
|
|
|
readLittleEndianBufferOfType<4, float>(inp, vec._v);
|
|
|
|
return vec;
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -142,11 +145,11 @@ public:
|
|
|
|
///Read in a string of the given length
|
|
|
|
///Read in a string of the given length
|
|
|
|
std::string getSizedString(size_t length)
|
|
|
|
std::string getSizedString(size_t length)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::vector<char> str(length + 1, 0);
|
|
|
|
std::string str(length, '\0');
|
|
|
|
|
|
|
|
|
|
|
|
inp->read(str.data(), length);
|
|
|
|
inp->read(str.data(), length);
|
|
|
|
|
|
|
|
if (inp->bad())
|
|
|
|
return str.data();
|
|
|
|
throw std::runtime_error("Failed to read sized string of " + std::to_string(length) + " chars");
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
///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()
|
|
|
@ -167,6 +170,8 @@ public:
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
std::string result;
|
|
|
|
std::getline(*inp, result);
|
|
|
|
std::getline(*inp, result);
|
|
|
|
|
|
|
|
if (inp->bad())
|
|
|
|
|
|
|
|
throw std::runtime_error("Failed to read version string");
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|