2014-08-24 17:27:09 +00:00
|
|
|
/// Functions used to read raw binary data from .nif files
|
|
|
|
|
2013-02-24 21:51:56 +00:00
|
|
|
#ifndef OPENMW_COMPONENTS_NIF_NIFSTREAM_HPP
|
|
|
|
#define OPENMW_COMPONENTS_NIF_NIFSTREAM_HPP
|
2017-09-22 03:37:19 +00:00
|
|
|
|
2017-05-05 19:21:11 +00:00
|
|
|
#include <cassert>
|
2014-08-24 17:27:09 +00:00
|
|
|
#include <istream>
|
|
|
|
#include <stdexcept>
|
2015-02-17 16:08:55 +00:00
|
|
|
#include <stdint.h>
|
2021-05-04 20:58:01 +00:00
|
|
|
#include <type_traits>
|
2022-07-16 16:19:56 +00:00
|
|
|
#include <vector>
|
2014-08-24 17:27:09 +00:00
|
|
|
|
2022-07-16 16:19:56 +00:00
|
|
|
#include <components/files/istreamptr.hpp>
|
2021-01-08 22:02:19 +00:00
|
|
|
#include <components/misc/endianness.hpp>
|
2015-02-17 16:08:55 +00:00
|
|
|
|
|
|
|
#include <osg/Quat>
|
|
|
|
#include <osg/Vec3f>
|
|
|
|
#include <osg/Vec4f>
|
2014-08-24 17:27:09 +00:00
|
|
|
|
|
|
|
#include "niftypes.hpp"
|
|
|
|
|
2013-01-05 21:03:05 +00:00
|
|
|
namespace Nif
|
|
|
|
{
|
|
|
|
|
2022-09-17 17:24:42 +00:00
|
|
|
class Reader;
|
2020-05-12 22:25:39 +00:00
|
|
|
|
2018-07-10 09:32:52 +00:00
|
|
|
template <std::size_t numInstances, typename T>
|
|
|
|
inline void readLittleEndianBufferOfType(Files::IStreamPtr& pIStream, T* dest)
|
|
|
|
{
|
2021-01-08 22:02:19 +00:00
|
|
|
static_assert(std::is_arithmetic_v<T>, "Buffer element type is not arithmetic");
|
|
|
|
pIStream->read((char*)dest, numInstances * sizeof(T));
|
2021-05-04 20:58:01 +00:00
|
|
|
if (pIStream->bad())
|
2021-01-08 22:02:19 +00:00
|
|
|
throw std::runtime_error("Failed to read little endian typed (" + std::string(typeid(T).name())
|
2021-05-04 20:58:01 +00:00
|
|
|
+ ") buffer of " + std::to_string(numInstances) + " instances");
|
2021-01-08 22:02:19 +00:00
|
|
|
if constexpr (Misc::IS_BIG_ENDIAN)
|
2021-05-04 20:58:01 +00:00
|
|
|
for (std::size_t i = 0; i < numInstances; i++)
|
2021-01-08 22:02:19 +00:00
|
|
|
Misc::swapEndiannessInplace(dest[i]);
|
2017-09-21 04:53:12 +00:00
|
|
|
}
|
2018-07-10 09:32:52 +00:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline void readLittleEndianDynamicBufferOfType(Files::IStreamPtr& pIStream, T* dest, std::size_t numInstances)
|
|
|
|
{
|
2021-01-08 22:02:19 +00:00
|
|
|
static_assert(std::is_arithmetic_v<T>, "Buffer element type is not arithmetic");
|
|
|
|
pIStream->read((char*)dest, numInstances * sizeof(T));
|
2019-12-29 12:53:44 +00:00
|
|
|
if (pIStream->bad())
|
|
|
|
throw std::runtime_error(
|
2021-01-08 22:02:19 +00:00
|
|
|
"Failed to read little endian dynamic buffer of " + std::to_string(numInstances) + " instances");
|
|
|
|
if constexpr (Misc::IS_BIG_ENDIAN)
|
2018-07-10 09:32:52 +00:00
|
|
|
for (std::size_t i = 0; i < numInstances; i++)
|
2021-01-08 22:02:19 +00:00
|
|
|
Misc::swapEndiannessInplace(dest[i]);
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
2021-01-08 22:02:19 +00:00
|
|
|
template <typename type>
|
|
|
|
type inline readLittleEndianType(Files::IStreamPtr& pIStream)
|
2018-07-10 09:32:52 +00:00
|
|
|
{
|
2017-09-22 03:37:19 +00:00
|
|
|
type val;
|
2021-05-04 20:58:01 +00:00
|
|
|
readLittleEndianBufferOfType<1, type>(pIStream, &val);
|
2017-09-22 03:37:19 +00:00
|
|
|
return val;
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 09:32:52 +00:00
|
|
|
class NIFStream
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2022-09-17 17:24:42 +00:00
|
|
|
const Reader& file;
|
|
|
|
|
2013-01-05 21:03:05 +00:00
|
|
|
/// Input stream
|
2017-09-21 04:53:12 +00:00
|
|
|
Files::IStreamPtr inp;
|
2022-09-22 18:26:05 +00:00
|
|
|
|
|
|
|
public:
|
2022-09-17 17:24:42 +00:00
|
|
|
explicit NIFStream(const Reader& file, Files::IStreamPtr&& inp)
|
|
|
|
: file(file)
|
|
|
|
, inp(std::move(inp))
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-09-17 17:24:42 +00:00
|
|
|
const Reader& getFile() const { return file; }
|
|
|
|
|
2017-09-21 04:53:12 +00:00
|
|
|
void skip(size_t size) { inp->ignore(size); }
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2023-06-28 09:27:22 +00:00
|
|
|
template <class T>
|
|
|
|
void read(T& data)
|
|
|
|
{
|
|
|
|
data = readLittleEndianType<T>(inp);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
T get()
|
|
|
|
{
|
|
|
|
return readLittleEndianType<T>(inp);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void readVector(std::vector<T>& vec, size_t size)
|
|
|
|
{
|
|
|
|
vec.resize(size);
|
|
|
|
readLittleEndianDynamicBufferOfType<T>(inp, vec.data(), size);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, size_t size>
|
|
|
|
void readArray(std::array<T, size>& arr)
|
|
|
|
{
|
|
|
|
readLittleEndianDynamicBufferOfType<T>(inp, arr.data(), size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// DEPRECATED: Use read() or get() whenever relevant
|
2017-09-23 02:08:25 +00:00
|
|
|
char getChar() { return readLittleEndianType<char>(inp); }
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2023-06-28 09:27:22 +00:00
|
|
|
// DEPRECATED: Use read() or get() whenever relevant
|
2017-09-23 02:08:25 +00:00
|
|
|
short getShort() { return readLittleEndianType<short>(inp); }
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2023-06-28 09:27:22 +00:00
|
|
|
// DEPRECATED: Use read() or get() whenever relevant
|
2017-09-23 02:08:25 +00:00
|
|
|
unsigned short getUShort() { return readLittleEndianType<unsigned short>(inp); }
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2023-06-28 09:27:22 +00:00
|
|
|
// DEPRECATED: Use read() or get() whenever relevant
|
2017-09-23 02:08:25 +00:00
|
|
|
int getInt() { return readLittleEndianType<int>(inp); }
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2023-06-28 09:27:22 +00:00
|
|
|
// DEPRECATED: Use read() or get() whenever relevant
|
2021-01-08 22:02:19 +00:00
|
|
|
unsigned int getUInt() { return readLittleEndianType<unsigned int>(inp); }
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2023-06-28 09:27:22 +00:00
|
|
|
// DEPRECATED: Use read() or get() whenever relevant
|
2021-01-08 22:02:19 +00:00
|
|
|
float getFloat() { return readLittleEndianType<float>(inp); }
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2017-09-23 02:08:25 +00:00
|
|
|
osg::Vec2f getVector2()
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2017-09-23 02:08:25 +00:00
|
|
|
osg::Vec2f vec;
|
|
|
|
readLittleEndianBufferOfType<2, float>(inp, vec._v);
|
2017-09-21 04:53:12 +00:00
|
|
|
return vec;
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 09:32:52 +00:00
|
|
|
osg::Vec3f getVector3()
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2017-09-23 02:08:25 +00:00
|
|
|
osg::Vec3f vec;
|
2021-01-08 22:02:19 +00:00
|
|
|
readLittleEndianBufferOfType<3, float>(inp, vec._v);
|
2017-09-21 04:53:12 +00:00
|
|
|
return vec;
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
|
|
|
|
2021-01-08 22:02:19 +00:00
|
|
|
osg::Vec4f getVector4()
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2017-09-21 04:53:12 +00:00
|
|
|
osg::Vec4f vec;
|
2021-05-04 20:58:01 +00:00
|
|
|
readLittleEndianBufferOfType<4, float>(inp, vec._v);
|
2017-09-21 04:53:12 +00:00
|
|
|
return vec;
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 09:32:52 +00:00
|
|
|
Matrix3 getMatrix3()
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2017-09-21 04:53:12 +00:00
|
|
|
Matrix3 mat;
|
2021-01-08 22:02:19 +00:00
|
|
|
readLittleEndianBufferOfType<9, float>(inp, (float*)&mat.mValues);
|
2017-09-21 04:53:12 +00:00
|
|
|
return mat;
|
|
|
|
}
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2018-07-10 09:32:52 +00:00
|
|
|
osg::Quat getQuaternion();
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2018-07-10 09:32:52 +00:00
|
|
|
Transformation getTrafo();
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2019-12-29 12:53:44 +00:00
|
|
|
bool getBoolean();
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2019-12-29 12:53:44 +00:00
|
|
|
std::string getString();
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2018-07-10 09:32:52 +00:00
|
|
|
unsigned int getVersion() const;
|
2020-02-02 15:40:11 +00:00
|
|
|
unsigned int getUserVersion() const;
|
|
|
|
unsigned int getBethVersion() const;
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2018-07-10 09:32:52 +00:00
|
|
|
// 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)
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2020-02-02 14:08:17 +00:00
|
|
|
return (major << 24) + (minor << 16) + (patch << 8) + rev;
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
|
|
|
|
2014-10-28 07:12:36 +00:00
|
|
|
/// Read in a string of the given length
|
2018-07-10 09:32:52 +00:00
|
|
|
std::string getSizedString(size_t length)
|
|
|
|
{
|
2021-05-04 20:58:01 +00:00
|
|
|
std::string str(length, '\0');
|
2017-09-21 04:53:12 +00:00
|
|
|
inp->read(str.data(), length);
|
2021-05-04 20:58:01 +00:00
|
|
|
if (inp->bad())
|
2017-09-21 04:53:12 +00:00
|
|
|
throw std::runtime_error("Failed to read sized string of " + std::to_string(length) + " chars");
|
|
|
|
size_t end = str.find('\0');
|
2017-09-23 02:08:25 +00:00
|
|
|
if (end != std::string::npos)
|
|
|
|
str.erase(end);
|
2021-05-04 20:58:01 +00:00
|
|
|
return str;
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
2017-09-23 02:08:25 +00:00
|
|
|
/// Read in a string of the length specified in the file
|
2019-12-29 12:53:44 +00:00
|
|
|
std::string getSizedString()
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2017-09-23 02:08:25 +00:00
|
|
|
size_t size = readLittleEndianType<uint32_t>(inp);
|
2019-12-29 12:53:44 +00:00
|
|
|
return getSizedString(size);
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 02:08:25 +00:00
|
|
|
/// Specific to Bethesda headers, uses a byte for length
|
|
|
|
std::string getExportString()
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2021-01-08 22:02:19 +00:00
|
|
|
size_t size = static_cast<size_t>(readLittleEndianType<uint8_t>(inp));
|
|
|
|
return getSizedString(size);
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
|
|
|
|
2021-01-08 22:02:19 +00:00
|
|
|
/// This is special since the version string doesn't start with a number, and ends with "\n"
|
2018-07-10 09:32:52 +00:00
|
|
|
std::string getVersionString()
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2017-09-21 04:53:12 +00:00
|
|
|
std::string result;
|
|
|
|
std::getline(*inp, result);
|
2021-05-04 20:58:01 +00:00
|
|
|
if (inp->bad())
|
|
|
|
throw std::runtime_error("Failed to read version string");
|
2018-07-10 09:32:52 +00:00
|
|
|
return result;
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
|
|
|
|
2023-02-09 17:58:08 +00:00
|
|
|
/// Read a sequence of null-terminated strings
|
|
|
|
std::string getStringPalette()
|
|
|
|
{
|
|
|
|
size_t size = readLittleEndianType<uint32_t>(inp);
|
|
|
|
std::string str(size, '\0');
|
|
|
|
inp->read(str.data(), size);
|
|
|
|
if (inp->bad())
|
|
|
|
throw std::runtime_error("Failed to read string palette of " + std::to_string(size) + " chars");
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2023-06-28 09:27:22 +00:00
|
|
|
// DEPRECATED: Use readVector()
|
2020-05-12 22:25:39 +00:00
|
|
|
void getChars(std::vector<char>& vec, size_t size)
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2020-05-12 22:25:39 +00:00
|
|
|
vec.resize(size);
|
2018-07-10 09:32:52 +00:00
|
|
|
readLittleEndianDynamicBufferOfType<char>(inp, vec.data(), size);
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
|
|
|
|
2023-06-28 09:27:22 +00:00
|
|
|
// DEPRECATED: Use readVector()
|
2020-05-12 22:25:39 +00:00
|
|
|
void getUChars(std::vector<unsigned char>& vec, size_t size)
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2020-05-12 22:25:39 +00:00
|
|
|
vec.resize(size);
|
2021-01-08 22:02:19 +00:00
|
|
|
readLittleEndianDynamicBufferOfType<unsigned char>(inp, vec.data(), size);
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
|
|
|
|
2023-06-28 09:27:22 +00:00
|
|
|
// DEPRECATED: Use readVector()
|
2020-05-12 22:25:39 +00:00
|
|
|
void getUShorts(std::vector<unsigned short>& vec, size_t size)
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2020-05-12 22:25:39 +00:00
|
|
|
vec.resize(size);
|
2021-01-08 22:02:19 +00:00
|
|
|
readLittleEndianDynamicBufferOfType<unsigned short>(inp, vec.data(), size);
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
|
|
|
|
2023-06-28 09:27:22 +00:00
|
|
|
// DEPRECATED: Use readVector()
|
2018-07-10 09:32:52 +00:00
|
|
|
void getFloats(std::vector<float>& vec, size_t size)
|
|
|
|
{
|
2017-09-21 04:53:12 +00:00
|
|
|
vec.resize(size);
|
|
|
|
readLittleEndianDynamicBufferOfType<float>(inp, vec.data(), size);
|
|
|
|
}
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2023-06-28 09:27:22 +00:00
|
|
|
// DEPRECATED: Use readVector()
|
2019-12-29 12:53:44 +00:00
|
|
|
void getInts(std::vector<int>& vec, size_t size)
|
|
|
|
{
|
|
|
|
vec.resize(size);
|
2021-01-08 22:02:19 +00:00
|
|
|
readLittleEndianDynamicBufferOfType<int>(inp, vec.data(), size);
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
|
|
|
|
2023-06-28 09:27:22 +00:00
|
|
|
// DEPRECATED: Use readVector()
|
2020-05-12 22:25:39 +00:00
|
|
|
void getUInts(std::vector<unsigned int>& vec, size_t size)
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2017-09-21 04:53:12 +00:00
|
|
|
vec.resize(size);
|
2021-01-08 22:02:19 +00:00
|
|
|
readLittleEndianDynamicBufferOfType<unsigned int>(inp, vec.data(), size);
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 09:32:52 +00:00
|
|
|
void getVector2s(std::vector<osg::Vec2f>& vec, size_t size)
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2019-12-29 12:53:44 +00:00
|
|
|
vec.resize(size);
|
2017-09-23 02:08:25 +00:00
|
|
|
/* The packed storage of each Vec2f is 2 floats exactly */
|
2021-01-08 22:02:19 +00:00
|
|
|
readLittleEndianDynamicBufferOfType<float>(inp, (float*)vec.data(), size * 2);
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 09:32:52 +00:00
|
|
|
void getVector3s(std::vector<osg::Vec3f>& vec, size_t size)
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2019-12-29 12:53:44 +00:00
|
|
|
vec.resize(size);
|
2017-09-23 02:08:25 +00:00
|
|
|
/* The packed storage of each Vec3f is 3 floats exactly */
|
2021-01-08 22:02:19 +00:00
|
|
|
readLittleEndianDynamicBufferOfType<float>(inp, (float*)vec.data(), size * 3);
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 09:32:52 +00:00
|
|
|
void getVector4s(std::vector<osg::Vec4f>& vec, size_t size)
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2017-09-21 04:53:12 +00:00
|
|
|
vec.resize(size);
|
2017-09-23 02:08:25 +00:00
|
|
|
/* The packed storage of each Vec4f is 4 floats exactly */
|
2021-01-08 22:02:19 +00:00
|
|
|
readLittleEndianDynamicBufferOfType<float>(inp, (float*)vec.data(), size * 4);
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 09:32:52 +00:00
|
|
|
void getQuaternions(std::vector<osg::Quat>& quat, size_t size)
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2017-09-21 04:53:12 +00:00
|
|
|
quat.resize(size);
|
|
|
|
for (size_t i = 0; i < quat.size(); i++)
|
|
|
|
quat[i] = getQuaternion();
|
2022-09-22 18:26:05 +00:00
|
|
|
}
|
|
|
|
|
2019-12-29 12:53:44 +00:00
|
|
|
void getStrings(std::vector<std::string>& vec, size_t size)
|
2022-09-22 18:26:05 +00:00
|
|
|
{
|
2017-09-21 04:53:12 +00:00
|
|
|
vec.resize(size);
|
2019-12-29 12:53:44 +00:00
|
|
|
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();
|
|
|
|
}
|
2013-01-05 21:03:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|