diff --git a/components/nif/nifstream.cpp b/components/nif/nifstream.cpp index 489548f52c..55b0cb713a 100644 --- a/components/nif/nifstream.cpp +++ b/components/nif/nifstream.cpp @@ -1,5 +1,5 @@ #include "nifstream.hpp" -// For error reporting + #include "niffile.hpp" namespace Nif @@ -25,19 +25,16 @@ namespace Nif return t; } - /// Booleans in 4.0.0.2 (Morrowind format) and earlier are 4 byte, while in 4.1.0.0+ they're 1 byte. bool NIFStream::getBoolean() { return getVersion() < generateVersion(4, 1, 0, 0) ? getInt() != 0 : getChar() != 0; } - /// Read in a string, either from the string table using the index or from the stream using the specified length std::string NIFStream::getString() { return getVersion() < generateVersion(20, 1, 0, 1) ? getSizedString() : mReader.getString(getUInt()); } - // Convenience utility functions: get the versions of the currently read file unsigned int NIFStream::getVersion() const { return mReader.getVersion(); diff --git a/components/nif/nifstream.hpp b/components/nif/nifstream.hpp index 05cffa8596..1826ae125c 100644 --- a/components/nif/nifstream.hpp +++ b/components/nif/nifstream.hpp @@ -79,6 +79,7 @@ namespace Nif void skip(size_t size) { mStream->ignore(size); } + /// Read into a single instance of type template void read(T& data) { @@ -88,12 +89,14 @@ namespace Nif void read(osg::Vec3f& data) { readBufferOfType<3, float>(mStream, data._v); } void read(osg::Vec4f& data) { readBufferOfType<4, float>(mStream, data._v); } + /// Extract an instance of type template T get() { return readType(mStream); } + /// Read multiple instances of type into a vector template void readVector(std::vector& vec, size_t size) { @@ -101,6 +104,7 @@ namespace Nif readDynamicBufferOfType(mStream, vec.data(), size); } + /// Read multiple instances of type into an array template void readArray(std::array& arr) { @@ -158,21 +162,23 @@ namespace Nif Transformation getTrafo(); + /// Read in a boolean. Boolean serialization format differs between versions bool getBoolean(); + /// Read in a string, either from the string table or from the stream depending on the version 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. + /// 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 + /// Read a string of the given length std::string getSizedString(size_t length) { std::string str(length, '\0'); @@ -184,21 +190,22 @@ namespace Nif str.erase(end); return str; } - /// Read in a string of the length specified in the file + + /// Read a string of the length specified in the file std::string getSizedString() { size_t size = readType(mStream); return getSizedString(size); } - /// Specific to Bethesda headers, uses a byte for length + /// Read a Bethesda header string that uses a byte for length std::string getExportString() { size_t size = static_cast(readType(mStream)); return getSizedString(size); } - /// This is special since the version string doesn't start with a number, and ends with "\n" + /// Read the version string which doesn't start with a number and ends with "\n" std::string getVersionString() { std::string result; @@ -264,21 +271,21 @@ namespace Nif void getVector2s(std::vector& vec, size_t size) { vec.resize(size); - /* The packed storage of each Vec2f is 2 floats exactly */ + // The packed storage of each Vec2f is 2 floats exactly readDynamicBufferOfType(mStream, (float*)vec.data(), size * 2); } void getVector3s(std::vector& vec, size_t size) { vec.resize(size); - /* The packed storage of each Vec3f is 3 floats exactly */ + // The packed storage of each Vec3f is 3 floats exactly readDynamicBufferOfType(mStream, (float*)vec.data(), size * 3); } void getVector4s(std::vector& vec, size_t size) { vec.resize(size); - /* The packed storage of each Vec4f is 4 floats exactly */ + // The packed storage of each Vec4f is 4 floats exactly readDynamicBufferOfType(mStream, (float*)vec.data(), size * 4); } @@ -295,7 +302,8 @@ namespace Nif 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. + + /// Read a list of strings without using the string table, e.g. the string table itself void getSizedStrings(std::vector& vec, size_t size) { vec.resize(size);