Remove unused remnants of NIFStream from BGSMStream

master
Alexei Kotov 2 weeks ago
parent ea5e101821
commit 6be2bb70c3

@ -11,7 +11,7 @@ namespace Bgsm
{
void Reader::parse(Files::IStreamPtr&& inputStream)
{
BGSMStream stream(*this, std::move(inputStream));
BGSMStream stream(std::move(inputStream));
std::array<char, 4> signature;
stream.readArray(signature);

@ -19,8 +19,6 @@ namespace Bgsm
public:
void parse(Files::IStreamPtr&& stream);
std::uint32_t getVersion() const { return mFile->mVersion; }
std::unique_ptr<MaterialFile>& getFile() { return mFile; }
};
}

@ -1,66 +1,8 @@
#include "stream.hpp"
#include <span>
#include "reader.hpp"
namespace
{
// Read a range of elements into a dynamic buffer per-element
// This one should be used if the type cannot be read contiguously
// (e.g. quaternions)
template <class T>
void readRange(Bgsm::BGSMStream& stream, T* dest, size_t size)
{
for (T& value : std::span(dest, size))
stream.read(value);
}
// Read a range of elements into a dynamic buffer
// This one should be used if the type can be read contiguously as an array of a different type
// (e.g. osg::VecXf can be read as a float array of X elements)
template <class elementType, size_t numElements, class T>
void readAlignedRange(Files::IStreamPtr& stream, T* dest, size_t size)
{
static_assert(std::is_standard_layout_v<T>);
static_assert(std::alignment_of_v<T> == std::alignment_of_v<elementType>);
static_assert(sizeof(T) == sizeof(elementType) * numElements);
Bgsm::readDynamicBufferOfType(stream, reinterpret_cast<elementType*>(dest), size * numElements);
}
}
namespace Bgsm
{
std::uint32_t BGSMStream::getVersion() const
{
return mReader.getVersion();
}
std::string BGSMStream::getSizedString(size_t length)
{
// Prevent potential memory allocation freezes; strings this long are not expected in BGSM
if (length > 1024)
throw std::runtime_error("Requested string length is too large: " + std::to_string(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;
}
void BGSMStream::getSizedStrings(std::vector<std::string>& vec, size_t size)
{
vec.resize(size);
for (size_t i = 0; i < vec.size(); i++)
vec[i] = getSizedString();
}
template <>
void BGSMStream::read<osg::Vec2f>(osg::Vec2f& vec)
{
@ -82,32 +24,18 @@ namespace Bgsm
template <>
void BGSMStream::read<std::string>(std::string& str)
{
str = getSizedString();
}
template <>
void BGSMStream::read<osg::Vec2f>(osg::Vec2f* dest, size_t size)
{
readAlignedRange<float, 2>(mStream, dest, size);
}
template <>
void BGSMStream::read<osg::Vec3f>(osg::Vec3f* dest, size_t size)
{
readAlignedRange<float, 3>(mStream, dest, size);
}
template <>
void BGSMStream::read<osg::Vec4f>(osg::Vec4f* dest, size_t size)
{
readAlignedRange<float, 4>(mStream, dest, size);
}
template <>
void BGSMStream::read<std::string>(std::string* dest, size_t size)
{
for (std::string& value : std::span(dest, size))
value = getSizedString();
std::uint32_t length;
read(length);
// Prevent potential memory allocation freezes; strings this long are not expected in BGSM
if (length > 1024)
throw std::runtime_error("Requested string length is too large: " + std::to_string(length));
str = std::string(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");
std::size_t end = str.find('\0');
if (end != std::string::npos)
str.erase(end);
}
}

@ -8,23 +8,21 @@
#include <stdexcept>
#include <string>
#include <type_traits>
#include <vector>
#include <components/files/istreamptr.hpp>
#include <components/misc/endianness.hpp>
#include <osg/Vec2f>
#include <osg/Vec3f>
#include <osg/Vec4f>
namespace Bgsm
{
class Reader;
template <std::size_t numInstances, typename T>
inline void readBufferOfType(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(reinterpret_cast<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");
@ -39,35 +37,16 @@ namespace Bgsm
readBufferOfType<numInstances>(pIStream, static_cast<T*>(dest));
}
template <typename T>
inline void readDynamicBufferOfType(Files::IStreamPtr& pIStream, T* dest, std::size_t numInstances)
{
static_assert(std::is_arithmetic_v<T>, "Buffer element type is not arithmetic");
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]);
}
class BGSMStream
{
const Reader& mReader;
Files::IStreamPtr mStream;
public:
explicit BGSMStream(const Reader& reader, Files::IStreamPtr&& stream)
: mReader(reader)
, mStream(std::move(stream))
explicit BGSMStream(Files::IStreamPtr&& stream)
: mStream(std::move(stream))
{
}
const Reader& getFile() const { return mReader; }
std::uint32_t getVersion() const;
void skip(size_t size) { mStream->ignore(size); }
/// Read into a single instance of type
@ -83,41 +62,6 @@ namespace Bgsm
{
readBufferOfType<size>(mStream, arr.data());
}
/// Read instances of type into a dynamic buffer
template <class T>
void read(T* dest, size_t size)
{
readDynamicBufferOfType<T>(mStream, dest, size);
}
/// Read multiple instances of type into a vector
template <class T>
void readVector(std::vector<T>& vec, size_t size)
{
if (size == 0)
return;
vec.resize(size);
read(vec.data(), size);
}
/// Extract an instance of type
template <class T>
T get()
{
T data;
read(data);
return data;
}
/// Read a string of the given length
std::string getSizedString(size_t length);
/// Read a string of the length specified in the file
std::string getSizedString() { return getSizedString(get<uint32_t>()); }
/// Read a list of strings
void getSizedStrings(std::vector<std::string>& vec, size_t size);
};
template <>
@ -128,15 +72,6 @@ namespace Bgsm
void BGSMStream::read<osg::Vec4f>(osg::Vec4f& vec);
template <>
void BGSMStream::read<std::string>(std::string& str);
template <>
void BGSMStream::read<osg::Vec2f>(osg::Vec2f* dest, size_t size);
template <>
void BGSMStream::read<osg::Vec3f>(osg::Vec3f* dest, size_t size);
template <>
void BGSMStream::read<osg::Vec4f>(osg::Vec4f* dest, size_t size);
template <>
void BGSMStream::read<std::string>(std::string* dest, size_t size);
}
#endif

Loading…
Cancel
Save