From 10072f74b45bda86df7244bade4e1466c7e9853f Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Tue, 10 Jul 2012 00:27:13 -0700 Subject: [PATCH] Remove NIFFile::getType --- components/nif/data.hpp | 18 ++++-- components/nif/effect.hpp | 10 ++- components/nif/extra.hpp | 2 +- components/nif/nif_file.hpp | 124 ++++++++++++++++++++++++++++++------ components/nif/node.hpp | 19 +++++- components/nif/property.hpp | 23 ++++++- 6 files changed, 166 insertions(+), 30 deletions(-) diff --git a/components/nif/data.hpp b/components/nif/data.hpp index a9a5895f4..1b5fa029b 100644 --- a/components/nif/data.hpp +++ b/components/nif/data.hpp @@ -105,16 +105,16 @@ public: int verts = nif->getShort(); if(nif->getInt()) - vertices = nif->getFloatLen(verts*3); + vertices = nif->getArrayLen(verts*3); if(nif->getInt()) - normals = nif->getFloatLen(verts*3); + normals = nif->getArrayLen(verts*3); center = nif->getVector(); radius = nif->getFloat(); if(nif->getInt()) - colors = nif->getFloatLen(verts*4); + colors = nif->getArrayLen(verts*4); int uvs = nif->getShort(); @@ -123,7 +123,7 @@ public: uvs &= 0x3f; if(nif->getInt()) - uvlist = nif->getFloatLen(uvs*verts*2); + uvlist = nif->getArrayLen(uvs*verts*2); } }; @@ -181,7 +181,7 @@ public: if(nif->getInt()) { // Particle sizes - nif->getFloatLen(activeCount); + nif->getArrayLen(activeCount); } } }; @@ -414,7 +414,9 @@ public: assert(sizeof(BoneTrafo) == 4*(9+3+1)); assert(sizeof(VertWeight) == 6); - trafo = nif->getType(); + trafo.rotation = nif->getMatrix(); + trafo.trans = nif->getVector(); + trafo.scale = nif->getFloat(); int boneNum = nif->getInt(); nif->getInt(); // -1 @@ -424,7 +426,9 @@ public: { BoneInfo &bi = bones[i]; - bi.trafo = nif->getType(); + bi.trafo.rotation = nif->getMatrix(); + bi.trafo.trans = nif->getVector(); + bi.trafo.scale = nif->getFloat(); bi.unknown = nif->getVector4(); // Number of vertex weights diff --git a/components/nif/effect.hpp b/components/nif/effect.hpp index 6ecc7c61a..f48049ec4 100644 --- a/components/nif/effect.hpp +++ b/components/nif/effect.hpp @@ -41,6 +41,14 @@ struct NiLight : Effect Vector ambient; Vector diffuse; Vector specular; + + void read(NIFFile *nif) + { + nif->load(dimmer); + ambient = nif->getVector(); + diffuse = nif->getVector(); + specular = nif->getVector(); + } }; SLight light; @@ -50,7 +58,7 @@ struct NiLight : Effect nif->getInt(); // 1 nif->getInt(); // 1? - light = nif->getType(); + light.read(nif); } }; diff --git a/components/nif/extra.hpp b/components/nif/extra.hpp index e5829fdfc..5615d833e 100644 --- a/components/nif/extra.hpp +++ b/components/nif/extra.hpp @@ -56,7 +56,7 @@ public: /*int i =*/ nif->getInt(); int s = nif->getShort(); // number of vertices - nif->getFloatLen(s); // vertex weights I guess + nif->getArrayLen(s); // vertex weights I guess } }; diff --git a/components/nif/nif_file.hpp b/components/nif/nif_file.hpp index a2246e00e..c4b138232 100644 --- a/components/nif/nif_file.hpp +++ b/components/nif/nif_file.hpp @@ -99,20 +99,70 @@ public: void skip(size_t size) { inp->skip(size); } - template X getType() + uint32_t read_le32() { - X obj; - if(inp->read(&obj, sizeof(X)) != sizeof(X)) - fail("Failed to read from NIF"); - return obj; + uint8_t buffer[4]; + if(inp->read(buffer, 4) != 4) return 0; + return buffer[0] | (buffer[1]<<8) | (buffer[2]<<16) | (buffer[3]<<24); + } + uint16_t read_le16() + { + uint8_t buffer[2]; + if(inp->read(buffer, 2) != 2) return 0; + return buffer[0] | (buffer[1]<<8); + } + uint8_t read_byte() + { + uint8_t byte; + if(inp->read(&byte, 1) != 1) return 0; + return byte; + } + std::string read_string(size_t length) + { + std::string str; + str.resize(length); + if(inp->read(&str[0], length) != length) + return std::string(); + return str.substr(0, str.find('\0')); } - unsigned short getShort() { return getType(); } - int getInt() { return getType(); } - float getFloat() { return getType(); } - char getByte() { return getType(); } - template - std::vector getArrayLen(int num) + + char& load(char &c) { c = read_byte(); return c; } + unsigned char& load(unsigned char &c) { c = read_byte(); return c; } + short& load(short &s) { s = read_le16(); return s; } + unsigned short& load(unsigned short &s) { s = read_le16(); return s; } + int& load(int &i) { i = read_le32(); return i; } + unsigned int& load(unsigned int &i) { i = read_le32(); return i; } + float& load(float &f) + { + union { + int i; + float f; + } u = { read_le32() }; + f = u.f; + return f; + } + + template + T* load(T (&a)[N]) + { + for(size_t i = 0;i < N;i++) + load(a[i]); + return a; + } + + template + std::vector& load(std::vector &v, size_t size) + { + v.resize(size); + for(size_t i = 0;i < size;i++) + load(v[i]); + return v; + } + + + template + std::vector getArrayLen(size_t num) { std::vector v(num); if(inp->read(&v[0], num*sizeof(X)) != num*sizeof(X)) @@ -120,20 +170,47 @@ public: return v; } - template + template std::vector getArray() { - int len = getInt(); + size_t len = read_le32(); return getArrayLen(len); } - Vector getVector() { return getType(); } - Matrix getMatrix() { return getType(); } - Transformation getTrafo() { return getType(); } - Vector4 getVector4() { return getType(); } + char getByte() { char c; return load(c); } + unsigned short getShort() { unsigned short s; return load(s); } + int getInt() { int i; return load(i); } + float getFloat() { float f; return load(f); } + Vector getVector() + { + Vector v; + load(v.array); + return v; + } + Vector4 getVector4() + { + Vector4 v; + load(v.array); + return v; + } + Matrix getMatrix() + { + Matrix m; + m.v[0] = getVector(); + m.v[1] = getVector(); + m.v[2] = getVector(); + return m; + } + Transformation getTrafo() + { + Transformation t; + t.pos = getVector(); + t.rotation = getMatrix(); + load(t.scale); + t.velocity = getVector(); + return t; + } - std::vector getFloatLen(int num) - { return getArrayLen(num); } // For fixed-size strings where you already know the size std::string getString(size_t size) @@ -151,5 +228,14 @@ public: } }; +template<> +inline std::vector NIFFile::getArrayLen(size_t num) +{ + std::vector v(num); + for(size_t i = 0;i < num;i++) + load(v[i]); + return v; +} + } // Namespace #endif diff --git a/components/nif/node.hpp b/components/nif/node.hpp index 8a6af5777..d5cd8fe82 100644 --- a/components/nif/node.hpp +++ b/components/nif/node.hpp @@ -218,6 +218,23 @@ struct NiCamera : Node // Level of detail modifier float LOD; + + void read(NIFFile *nif) + { + nif->load(left); + nif->load(right); + nif->load(top); + nif->load(bottom); + nif->load(nearDist); + nif->load(farDist); + + nif->load(vleft); + nif->load(vright); + nif->load(vtop); + nif->load(vbottom); + + nif->load(LOD); + } }; Camera cam; @@ -225,7 +242,7 @@ struct NiCamera : Node { Node::read(nif); - cam = nif->getType(); + cam.read(nif); nif->getInt(); // -1 nif->getInt(); // 0 diff --git a/components/nif/property.hpp b/components/nif/property.hpp index 8485d978f..87e3ae5f2 100644 --- a/components/nif/property.hpp +++ b/components/nif/property.hpp @@ -160,7 +160,7 @@ struct StructPropT : Property void read(NIFFile *nif) { Property::read(nif); - data = nif->getType(); + data.read(nif); } }; @@ -169,6 +169,16 @@ struct S_MaterialProperty // The vector components are R,G,B Vector ambient, diffuse, specular, emissive; float glossiness, alpha; + + void read(NIFFile *nif) + { + ambient = nif->getVector(); + diffuse = nif->getVector(); + specular = nif->getVector(); + emissive = nif->getVector(); + nif->load(glossiness); + nif->load(alpha); + } }; struct S_VertexColorProperty @@ -183,6 +193,12 @@ struct S_VertexColorProperty 1 - lighting emmisive ambient/diffuse */ int vertmode, lightmode; + + void read(NIFFile *nif) + { + nif->load(vertmode); + nif->load(lightmode); + } }; struct S_AlphaProperty @@ -234,6 +250,11 @@ struct S_AlphaProperty // Tested against when certain flags are set (see above.) unsigned char threshold; + + void read(NIFFile *nif) + { + nif->load(threshold); + } }; typedef StructPropT NiAlphaProperty;