From 4583dc05db823e7aed92ec9fe0c3386513bc8b6e Mon Sep 17 00:00:00 2001 From: Arthur Moore Date: Tue, 28 Oct 2014 03:12:36 -0400 Subject: [PATCH] Move NIFStream functions into a cpp file where they belong --- components/CMakeLists.txt | 2 +- components/nif/nifstream.cpp | 137 ++++++++++++++++++++++++++++++++ components/nif/nifstream.hpp | 146 ++++++----------------------------- 3 files changed, 160 insertions(+), 125 deletions(-) create mode 100644 components/nif/nifstream.cpp diff --git a/components/CMakeLists.txt b/components/CMakeLists.txt index c107b58e2..9bb0a557c 100644 --- a/components/CMakeLists.txt +++ b/components/CMakeLists.txt @@ -19,7 +19,7 @@ add_component_dir (bsa ) add_component_dir (nif - controlled effect niftypes record controller extra node record_ptr data niffile property nifkey data node base + controlled effect niftypes record controller extra node record_ptr data niffile property nifkey data node base nifstream ) add_component_dir (nifcache diff --git a/components/nif/nifstream.cpp b/components/nif/nifstream.cpp new file mode 100644 index 000000000..527d1a2af --- /dev/null +++ b/components/nif/nifstream.cpp @@ -0,0 +1,137 @@ +#include "nifstream.hpp" +//For error reporting +#include "niffile.hpp" + +namespace Nif +{ + +//Private functions +uint8_t NIFStream::read_byte() +{ + uint8_t byte; + if(inp->read(&byte, 1) != 1) return 0; + return byte; +} +uint16_t NIFStream::read_le16() +{ + uint8_t buffer[2]; + if(inp->read(buffer, 2) != 2) return 0; + return buffer[0] | (buffer[1]<<8); +} +uint32_t NIFStream::read_le32() +{ + uint8_t buffer[4]; + if(inp->read(buffer, 4) != 4) return 0; + return buffer[0] | (buffer[1]<<8) | (buffer[2]<<16) | (buffer[3]<<24); +} +float NIFStream::read_le32f() +{ + union { + uint32_t i; + float f; + } u = { read_le32() }; + return u.f; +} + +//Public functions +Ogre::Vector2 NIFStream::getVector2() +{ + float a[2]; + for(size_t i = 0;i < 2;i++) + a[i] = getFloat(); + return Ogre::Vector2(a); +} +Ogre::Vector3 NIFStream::getVector3() +{ + float a[3]; + for(size_t i = 0;i < 3;i++) + a[i] = getFloat(); + return Ogre::Vector3(a); +} +Ogre::Vector4 NIFStream::getVector4() +{ + float a[4]; + for(size_t i = 0;i < 4;i++) + a[i] = getFloat(); + return Ogre::Vector4(a); +} +Ogre::Matrix3 NIFStream::getMatrix3() +{ + Ogre::Real a[3][3]; + for(size_t i = 0;i < 3;i++) + { + for(size_t j = 0;j < 3;j++) + a[i][j] = Ogre::Real(getFloat()); + } + return Ogre::Matrix3(a); +} +Ogre::Quaternion NIFStream::getQuaternion() +{ + float a[4]; + for(size_t i = 0;i < 4;i++) + a[i] = getFloat(); + return Ogre::Quaternion(a); +} +Transformation NIFStream::getTrafo() +{ + Transformation t; + t.pos = getVector3(); + t.rotationScale = getMatrix3(); + t.scale = getFloat(); + return t; +} + +std::string NIFStream::getString(size_t length) +{ + std::vector str (length+1, 0); + + if(inp->read(&str[0], length) != length) + throw std::runtime_error (": String length in NIF file "+ file->getFilename() +" does not match! Expected length: " + + Ogre::StringConverter::toString(length)); + + return &str[0]; +} +std::string NIFStream::getString() +{ + size_t size = read_le32(); + return getString(size); +} + +void NIFStream::getShorts(std::vector &vec, size_t size) +{ + vec.resize(size); + for(size_t i = 0;i < vec.size();i++) + vec[i] = getShort(); +} +void NIFStream::getFloats(std::vector &vec, size_t size) +{ + vec.resize(size); + for(size_t i = 0;i < vec.size();i++) + vec[i] = getFloat(); +} +void NIFStream::getVector2s(std::vector &vec, size_t size) +{ + vec.resize(size); + for(size_t i = 0;i < vec.size();i++) + vec[i] = getVector2(); +} +void NIFStream::getVector3s(std::vector &vec, size_t size) +{ + vec.resize(size); + for(size_t i = 0;i < vec.size();i++) + vec[i] = getVector3(); +} +void NIFStream::getVector4s(std::vector &vec, size_t size) +{ + vec.resize(size); + for(size_t i = 0;i < vec.size();i++) + vec[i] = getVector4(); +} +void NIFStream::getQuaternions(std::vector &quat, size_t size) +{ + quat.resize(size); + for(size_t i = 0;i < quat.size();i++) + quat[i] = getQuaternion(); +} + +} diff --git a/components/nif/nifstream.hpp b/components/nif/nifstream.hpp index c1b169ac8..fcee0045e 100644 --- a/components/nif/nifstream.hpp +++ b/components/nif/nifstream.hpp @@ -26,32 +26,10 @@ class NIFStream { /// Input stream Ogre::DataStreamPtr inp; - uint8_t read_byte() - { - uint8_t byte; - if(inp->read(&byte, 1) != 1) return 0; - return byte; - } - uint16_t read_le16() - { - uint8_t buffer[2]; - if(inp->read(buffer, 2) != 2) return 0; - return buffer[0] | (buffer[1]<<8); - } - uint32_t read_le32() - { - uint8_t buffer[4]; - if(inp->read(buffer, 4) != 4) return 0; - return buffer[0] | (buffer[1]<<8) | (buffer[2]<<16) | (buffer[3]<<24); - } - float read_le32f() - { - union { - uint32_t i; - float f; - } u = { read_le32() }; - return u.f; - } + uint8_t read_byte(); + uint16_t read_le16(); + uint32_t read_le32(); + float read_le32f(); public: @@ -91,105 +69,25 @@ public: int getInt() { return read_le32(); } int getUInt() { return read_le32(); } float getFloat() { return read_le32f(); } - Ogre::Vector2 getVector2() - { - float a[2]; - for(size_t i = 0;i < 2;i++) - a[i] = getFloat(); - return Ogre::Vector2(a); - } - Ogre::Vector3 getVector3() - { - float a[3]; - for(size_t i = 0;i < 3;i++) - a[i] = getFloat(); - return Ogre::Vector3(a); - } - Ogre::Vector4 getVector4() - { - float a[4]; - for(size_t i = 0;i < 4;i++) - a[i] = getFloat(); - return Ogre::Vector4(a); - } - Ogre::Matrix3 getMatrix3() - { - Ogre::Real a[3][3]; - for(size_t i = 0;i < 3;i++) - { - for(size_t j = 0;j < 3;j++) - a[i][j] = Ogre::Real(getFloat()); - } - return Ogre::Matrix3(a); - } - Ogre::Quaternion getQuaternion() - { - float a[4]; - for(size_t i = 0;i < 4;i++) - a[i] = getFloat(); - return Ogre::Quaternion(a); - } - Transformation getTrafo() - { - Transformation t; - t.pos = getVector3(); - t.rotationScale = getMatrix3(); - t.scale = getFloat(); - return t; - } - - std::string getString(size_t length) - { - std::vector str (length+1, 0); - - if(inp->read(&str[0], length) != length) - throw std::runtime_error (": String length in NIF file "+ file->getFilename() +" does not match! Expected length: " - + Ogre::StringConverter::toString(length)); - - return &str[0]; - } - std::string getString() - { - size_t size = read_le32(); - return getString(size); - } - void getShorts(std::vector &vec, size_t size) - { - vec.resize(size); - for(size_t i = 0;i < vec.size();i++) - vec[i] = getShort(); - } - void getFloats(std::vector &vec, size_t size) - { - vec.resize(size); - for(size_t i = 0;i < vec.size();i++) - vec[i] = getFloat(); - } - void getVector2s(std::vector &vec, size_t size) - { - vec.resize(size); - for(size_t i = 0;i < vec.size();i++) - vec[i] = getVector2(); - } - void getVector3s(std::vector &vec, size_t size) - { - vec.resize(size); - for(size_t i = 0;i < vec.size();i++) - vec[i] = getVector3(); - } - void getVector4s(std::vector &vec, size_t size) - { - vec.resize(size); - for(size_t i = 0;i < vec.size();i++) - vec[i] = getVector4(); - } - void getQuaternions(std::vector &quat, size_t size) - { - quat.resize(size); - for(size_t i = 0;i < quat.size();i++) - quat[i] = getQuaternion(); - } + Ogre::Vector2 getVector2(); + Ogre::Vector3 getVector3(); + Ogre::Vector4 getVector4(); + Ogre::Matrix3 getMatrix3(); + Ogre::Quaternion getQuaternion(); + Transformation getTrafo(); + + ///Read in a string of the given length + std::string getString(size_t length); + ///Read in a string of the length specified in the file + std::string getString(); + + void getShorts(std::vector &vec, size_t size); + void getFloats(std::vector &vec, size_t size); + void getVector2s(std::vector &vec, size_t size); + void getVector3s(std::vector &vec, size_t size); + void getVector4s(std::vector &vec, size_t size); + void getQuaternions(std::vector &quat, size_t size); }; }