From 6be2bb70c3cbf478b21d40405e031bfada9e57e4 Mon Sep 17 00:00:00 2001 From: Alexei Kotov Date: Sat, 20 Apr 2024 18:11:56 +0300 Subject: [PATCH] Remove unused remnants of NIFStream from BGSMStream --- components/bgsm/reader.cpp | 2 +- components/bgsm/reader.hpp | 2 - components/bgsm/stream.cpp | 96 +++++--------------------------------- components/bgsm/stream.hpp | 73 ++--------------------------- 4 files changed, 17 insertions(+), 156 deletions(-) diff --git a/components/bgsm/reader.cpp b/components/bgsm/reader.cpp index c89d872bd7..47c052fb81 100644 --- a/components/bgsm/reader.cpp +++ b/components/bgsm/reader.cpp @@ -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 signature; stream.readArray(signature); diff --git a/components/bgsm/reader.hpp b/components/bgsm/reader.hpp index 2d669900ad..2d8a0ed481 100644 --- a/components/bgsm/reader.hpp +++ b/components/bgsm/reader.hpp @@ -19,8 +19,6 @@ namespace Bgsm public: void parse(Files::IStreamPtr&& stream); - std::uint32_t getVersion() const { return mFile->mVersion; } - std::unique_ptr& getFile() { return mFile; } }; } diff --git a/components/bgsm/stream.cpp b/components/bgsm/stream.cpp index 00cc382d3f..2c11066709 100644 --- a/components/bgsm/stream.cpp +++ b/components/bgsm/stream.cpp @@ -1,66 +1,8 @@ #include "stream.hpp" -#include - -#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 - 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 - void readAlignedRange(Files::IStreamPtr& stream, T* dest, size_t size) - { - static_assert(std::is_standard_layout_v); - static_assert(std::alignment_of_v == std::alignment_of_v); - static_assert(sizeof(T) == sizeof(elementType) * numElements); - Bgsm::readDynamicBufferOfType(stream, reinterpret_cast(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& 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& vec) { @@ -82,32 +24,18 @@ namespace Bgsm template <> void BGSMStream::read(std::string& str) { - str = getSizedString(); - } - - template <> - void BGSMStream::read(osg::Vec2f* dest, size_t size) - { - readAlignedRange(mStream, dest, size); - } - - template <> - void BGSMStream::read(osg::Vec3f* dest, size_t size) - { - readAlignedRange(mStream, dest, size); - } - - template <> - void BGSMStream::read(osg::Vec4f* dest, size_t size) - { - readAlignedRange(mStream, dest, size); - } - - template <> - void BGSMStream::read(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); } } diff --git a/components/bgsm/stream.hpp b/components/bgsm/stream.hpp index 2e03a52dd4..a355523367 100644 --- a/components/bgsm/stream.hpp +++ b/components/bgsm/stream.hpp @@ -8,23 +8,21 @@ #include #include #include -#include #include #include +#include #include #include namespace Bgsm { - class Reader; - template inline void readBufferOfType(Files::IStreamPtr& pIStream, T* dest) { static_assert(std::is_arithmetic_v, "Buffer element type is not arithmetic"); - pIStream->read((char*)dest, numInstances * sizeof(T)); + pIStream->read(reinterpret_cast(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(pIStream, static_cast(dest)); } - template - inline void readDynamicBufferOfType(Files::IStreamPtr& pIStream, T* dest, std::size_t numInstances) - { - static_assert(std::is_arithmetic_v, "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(mStream, arr.data()); } - - /// Read instances of type into a dynamic buffer - template - void read(T* dest, size_t size) - { - readDynamicBufferOfType(mStream, dest, size); - } - - /// Read multiple instances of type into a vector - template - void readVector(std::vector& vec, size_t size) - { - if (size == 0) - return; - vec.resize(size); - read(vec.data(), size); - } - - /// Extract an instance of type - template - 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()); } - - /// Read a list of strings - void getSizedStrings(std::vector& vec, size_t size); }; template <> @@ -128,15 +72,6 @@ namespace Bgsm void BGSMStream::read(osg::Vec4f& vec); template <> void BGSMStream::read(std::string& str); - - template <> - void BGSMStream::read(osg::Vec2f* dest, size_t size); - template <> - void BGSMStream::read(osg::Vec3f* dest, size_t size); - template <> - void BGSMStream::read(osg::Vec4f* dest, size_t size); - template <> - void BGSMStream::read(std::string* dest, size_t size); } #endif