|
|
|
/// Functions used to read raw binary data from .nif files
|
|
|
|
|
|
|
|
#ifndef OPENMW_COMPONENTS_NIF_NIFSTREAM_HPP
|
|
|
|
#define OPENMW_COMPONENTS_NIF_NIFSTREAM_HPP
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <cassert>
|
|
|
|
#include <istream>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <components/files/istreamptr.hpp>
|
|
|
|
#include <components/misc/endianness.hpp>
|
|
|
|
#include <components/misc/float16.hpp>
|
|
|
|
|
|
|
|
#include <osg/Quat>
|
|
|
|
#include <osg/Vec3f>
|
|
|
|
#include <osg/Vec4f>
|
|
|
|
|
|
|
|
#include "niftypes.hpp"
|
|
|
|
|
|
|
|
namespace Nif
|
|
|
|
{
|
|
|
|
|
|
|
|
class Reader;
|
|
|
|
|
|
|
|
template <std::size_t numInstances, typename T>
|
|
|
|
inline void readBufferOfType(Files::IStreamPtr& pIStream, T* dest)
|
|
|
|
{
|
|
|
|
static_assert(
|
|
|
|
std::is_arithmetic_v<T> || std::is_same_v<T, Misc::float16_t>, "Buffer element type is not arithmetic");
|
|
|
|
static_assert(!std::is_same_v<T, bool>, "Buffer element type is boolean");
|
|
|
|
pIStream->read((char*)dest, numInstances * sizeof(T));
|
|
|
|
if (pIStream->bad())
|
|
|
|
throw std::runtime_error("Failed to read typed (" + std::string(typeid(T).name()) + ") buffer of "
|
|
|
|
+ std::to_string(numInstances) + " instances");
|
|
|
|
if constexpr (Misc::IS_BIG_ENDIAN)
|
|
|
|
for (std::size_t i = 0; i < numInstances; i++)
|
|
|
|
Misc::swapEndiannessInplace(dest[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline void readDynamicBufferOfType(Files::IStreamPtr& pIStream, T* dest, std::size_t numInstances)
|
|
|
|
{
|
|
|
|
static_assert(
|
|
|
|
std::is_arithmetic_v<T> || std::is_same_v<T, Misc::float16_t>, "Buffer element type is not arithmetic");
|
|
|
|
static_assert(!std::is_same_v<T, bool>, "Buffer element type is boolean");
|
|
|
|
pIStream->read((char*)dest, numInstances * sizeof(T));
|
|
|
|
if (pIStream->bad())
|
|
|
|
throw std::runtime_error("Failed to read typed (" + std::string(typeid(T).name()) + ") dynamic buffer of "
|
|
|
|
+ std::to_string(numInstances) + " instances");
|
|
|
|
if constexpr (Misc::IS_BIG_ENDIAN)
|
|
|
|
for (std::size_t i = 0; i < numInstances; i++)
|
|
|
|
Misc::swapEndiannessInplace(dest[i]);
|
|
|
|
}
|
|
|
|
template <typename type>
|
|
|
|
type inline readType(Files::IStreamPtr& pIStream)
|
|
|
|
{
|
|
|
|
type val;
|
|
|
|
readBufferOfType<1, type>(pIStream, &val);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
class NIFStream
|
|
|
|
{
|
|
|
|
const Reader& mReader;
|
|
|
|
Files::IStreamPtr mStream;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit NIFStream(const Reader& reader, Files::IStreamPtr&& stream)
|
|
|
|
: mReader(reader)
|
|
|
|
, mStream(std::move(stream))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const Reader& getFile() const { return mReader; }
|
|
|
|
|
|
|
|
void skip(size_t size) { mStream->ignore(size); }
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void read(T& data)
|
|
|
|
{
|
|
|
|
data = readType<T>(mStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
void read(osg::Vec3f& data) { readBufferOfType<3, float>(mStream, data._v); }
|
|
|
|
void read(osg::Vec4f& data) { readBufferOfType<4, float>(mStream, data._v); }
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
T get()
|
|
|
|
{
|
|
|
|
return readType<T>(mStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void readVector(std::vector<T>& vec, size_t size)
|
|
|
|
{
|
|
|
|
vec.resize(size);
|
|
|
|
readDynamicBufferOfType<T>(mStream, vec.data(), size);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, size_t size>
|
|
|
|
void readArray(std::array<T, size>& arr)
|
|
|
|
{
|
|
|
|
readBufferOfType<T, size>(mStream, arr.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
// DEPRECATED: Use read() or get() whenever relevant
|
|
|
|
char getChar() { return readType<char>(mStream); }
|
|
|
|
|
|
|
|
// DEPRECATED: Use read() or get() whenever relevant
|
|
|
|
short getShort() { return readType<short>(mStream); }
|
|
|
|
|
|
|
|
// DEPRECATED: Use read() or get() whenever relevant
|
|
|
|
unsigned short getUShort() { return readType<unsigned short>(mStream); }
|
|
|
|
|
|
|
|
// DEPRECATED: Use read() or get() whenever relevant
|
|
|
|
int getInt() { return readType<int>(mStream); }
|
|
|
|
|
|
|
|
// DEPRECATED: Use read() or get() whenever relevant
|
|
|
|
unsigned int getUInt() { return readType<unsigned int>(mStream); }
|
|
|
|
|
|
|
|
// DEPRECATED: Use read() or get() whenever relevant
|
|
|
|
float getFloat() { return readType<float>(mStream); }
|
|
|
|
|
|
|
|
osg::Vec2f getVector2()
|
|
|
|
{
|
|
|
|
osg::Vec2f vec;
|
|
|
|
readBufferOfType<2, float>(mStream, vec._v);
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
// DEPRECATED: Use read() whenever relevant
|
|
|
|
osg::Vec3f getVector3()
|
|
|
|
{
|
|
|
|
osg::Vec3f vec;
|
|
|
|
readBufferOfType<3, float>(mStream, vec._v);
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
osg::Vec4f getVector4()
|
|
|
|
{
|
|
|
|
osg::Vec4f vec;
|
|
|
|
readBufferOfType<4, float>(mStream, vec._v);
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix3 getMatrix3()
|
|
|
|
{
|
|
|
|
Matrix3 mat;
|
|
|
|
readBufferOfType<9, float>(mStream, (float*)&mat.mValues);
|
|
|
|
return mat;
|
|
|
|
}
|
|
|
|
|
|
|
|
osg::Quat getQuaternion();
|
|
|
|
|
|
|
|
Transformation getTrafo();
|
|
|
|
|
|
|
|
bool getBoolean();
|
|
|
|
|
|
|
|
std::string getString();
|
|
|
|
|
|
|
|
unsigned int getVersion() const;
|
|
|
|
unsigned int getUserVersion() const;
|
|
|
|
unsigned int getBethVersion() const;
|
|
|
|
|
|
|
|
// Convert human-readable version numbers into a number that can be compared.
|
|
|
|
static constexpr uint32_t generateVersion(uint8_t major, uint8_t minor, uint8_t patch, uint8_t rev)
|
|
|
|
{
|
|
|
|
return (major << 24) + (minor << 16) + (patch << 8) + rev;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read in a string of the given length
|
|
|
|
std::string getSizedString(size_t length)
|
|
|
|
{
|
|
|
|
std::string str(length, '\0');
|
|
|
|
mStream->read(str.data(), length);
|
|
|
|
if (mStream->bad())
|
|
|
|
throw std::runtime_error("Failed to read sized string of " + std::to_string(length) + " chars");
|
|
|
|
size_t end = str.find('\0');
|
|
|
|
if (end != std::string::npos)
|
|
|
|
str.erase(end);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
/// Read in a string of the length specified in the file
|
|
|
|
std::string getSizedString()
|
|
|
|
{
|
|
|
|
size_t size = readType<uint32_t>(mStream);
|
|
|
|
return getSizedString(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Specific to Bethesda headers, uses a byte for length
|
|
|
|
std::string getExportString()
|
|
|
|
{
|
|
|
|
size_t size = static_cast<size_t>(readType<uint8_t>(mStream));
|
|
|
|
return getSizedString(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This is special since the version string doesn't start with a number, and ends with "\n"
|
|
|
|
std::string getVersionString()
|
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
std::getline(*mStream, result);
|
|
|
|
if (mStream->bad())
|
|
|
|
throw std::runtime_error("Failed to read version string");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read a sequence of null-terminated strings
|
|
|
|
std::string getStringPalette()
|
|
|
|
{
|
|
|
|
size_t size = readType<uint32_t>(mStream);
|
|
|
|
std::string str(size, '\0');
|
|
|
|
mStream->read(str.data(), size);
|
|
|
|
if (mStream->bad())
|
|
|
|
throw std::runtime_error("Failed to read string palette of " + std::to_string(size) + " chars");
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
// DEPRECATED: Use readVector()
|
|
|
|
void getChars(std::vector<char>& vec, size_t size)
|
|
|
|
{
|
|
|
|
vec.resize(size);
|
|
|
|
readDynamicBufferOfType<char>(mStream, vec.data(), size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// DEPRECATED: Use readVector()
|
|
|
|
void getUChars(std::vector<unsigned char>& vec, size_t size)
|
|
|
|
{
|
|
|
|
vec.resize(size);
|
|
|
|
readDynamicBufferOfType<unsigned char>(mStream, vec.data(), size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// DEPRECATED: Use readVector()
|
|
|
|
void getUShorts(std::vector<unsigned short>& vec, size_t size)
|
|
|
|
{
|
|
|
|
vec.resize(size);
|
|
|
|
readDynamicBufferOfType<unsigned short>(mStream, vec.data(), size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// DEPRECATED: Use readVector()
|
|
|
|
void getFloats(std::vector<float>& vec, size_t size)
|
|
|
|
{
|
|
|
|
vec.resize(size);
|
|
|
|
readDynamicBufferOfType<float>(mStream, vec.data(), size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// DEPRECATED: Use readVector()
|
|
|
|
void getInts(std::vector<int>& vec, size_t size)
|
|
|
|
{
|
|
|
|
vec.resize(size);
|
|
|
|
readDynamicBufferOfType<int>(mStream, vec.data(), size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// DEPRECATED: Use readVector()
|
|
|
|
void getUInts(std::vector<unsigned int>& vec, size_t size)
|
|
|
|
{
|
|
|
|
vec.resize(size);
|
|
|
|
readDynamicBufferOfType<unsigned int>(mStream, vec.data(), size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void getVector2s(std::vector<osg::Vec2f>& vec, size_t size)
|
|
|
|
{
|
|
|
|
vec.resize(size);
|
|
|
|
/* The packed storage of each Vec2f is 2 floats exactly */
|
|
|
|
readDynamicBufferOfType<float>(mStream, (float*)vec.data(), size * 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void getVector3s(std::vector<osg::Vec3f>& vec, size_t size)
|
|
|
|
{
|
|
|
|
vec.resize(size);
|
|
|
|
/* The packed storage of each Vec3f is 3 floats exactly */
|
|
|
|
readDynamicBufferOfType<float>(mStream, (float*)vec.data(), size * 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void getVector4s(std::vector<osg::Vec4f>& vec, size_t size)
|
|
|
|
{
|
|
|
|
vec.resize(size);
|
|
|
|
/* The packed storage of each Vec4f is 4 floats exactly */
|
|
|
|
readDynamicBufferOfType<float>(mStream, (float*)vec.data(), size * 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
void getQuaternions(std::vector<osg::Quat>& quat, size_t size)
|
|
|
|
{
|
|
|
|
quat.resize(size);
|
|
|
|
for (size_t i = 0; i < quat.size(); i++)
|
|
|
|
quat[i] = getQuaternion();
|
|
|
|
}
|
|
|
|
|
|
|
|
void getStrings(std::vector<std::string>& vec, size_t size)
|
|
|
|
{
|
|
|
|
vec.resize(size);
|
|
|
|
for (size_t i = 0; i < vec.size(); i++)
|
|
|
|
vec[i] = getString();
|
|
|
|
}
|
|
|
|
/// We need to use this when the string table isn't actually initialized.
|
|
|
|
void getSizedStrings(std::vector<std::string>& vec, size_t size)
|
|
|
|
{
|
|
|
|
vec.resize(size);
|
|
|
|
for (size_t i = 0; i < vec.size(); i++)
|
|
|
|
vec[i] = getSizedString();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|