diff --git a/niflib.cpp b/niflib.cpp index 7cb750903b285db702fcad3179c09895fed730b7..b272d3b2f8431efea8bc59f055aa6c4d50928e4c 100644 --- a/niflib.cpp +++ b/niflib.cpp @@ -120,6 +120,35 @@ blk_ref FindRoot( vector<blk_ref> const & blocks ) { return root; } +unsigned int CheckNifHeader( string const & file_name ) { + //--Open File--// + ifstream in( file_name.c_str(), ifstream::binary ); + + //--Read Header--// + char header_string[64]; + in.getline( header_string, 64 ); + string headerstr(header_string); + + // make sure this is a NIF file + if ( ( headerstr.substr(0, 22) != "NetImmerse File Format" ) + && ( headerstr.substr(0, 20) != "Gamebryo File Format" ) ) + return VER_INVALID; + + // supported versions + if ( headerstr == "NetImmerse File Format, Version 4.0.0.2" ) return VER_4_0_0_2; + if ( headerstr == "NetImmerse File Format, Version 4.1.0.12" ) return VER_4_1_0_12; + if ( headerstr == "NetImmerse File Format, Version 4.2.0.2" ) return VER_4_2_0_2; + if ( headerstr == "NetImmerse File Format, Version 4.2.1.0" ) return VER_4_2_1_0; + if ( headerstr == "NetImmerse File Format, Version 4.2.2.0" ) return VER_4_2_2_0; + if ( headerstr == "NetImmerse File Format, Version 10.0.1.0" ) return VER_10_0_1_0; + if ( headerstr == "Gamebryo File Format, Version 10.1.0.0" ) return VER_10_1_0_0; + if ( headerstr == "Gamebryo File Format, Version 10.2.0.0" ) return VER_10_2_0_0; + if ( headerstr == "Gamebryo File Format, Version 20.0.0.4" ) return VER_20_0_0_4; + + // anything else: unsupported + return VER_UNSUPPORTED; +} + //Reads the given file by file name and returns a vector of block references vector<blk_ref> ReadNifList( string const & file_name ) { diff --git a/niflib.h b/niflib.h index 4ca29c70cd309ebc5ebbdcdc60e1ca3b993f89a9..848b9708438cb63a45246f230d29031c741ece77 100644 --- a/niflib.h +++ b/niflib.h @@ -147,15 +147,17 @@ enum AttrType { }; //NIF Versions -const unsigned int VER_4_0_0_2 = 0x04000002; /*!< Nif Version 4.0.0.2 */ -const unsigned int VER_4_1_0_12 = 0x0401000C; /*!< Nif Version 4.1.0.12 */ -const unsigned int VER_4_2_0_2 = 0x04020002; /*!< Nif Version 4.2.0.2 */ -const unsigned int VER_4_2_1_0 = 0x04020100; /*!< Nif Version 4.2.1.0 */ -const unsigned int VER_4_2_2_0 = 0x04020200; /*!< Nif Version 4.2.2.0 */ -const unsigned int VER_10_0_1_0 = 0x0A000100; /*!< Nif Version 10.0.1.0 */ -const unsigned int VER_10_1_0_0 = 0x0A010000; /*!< Nif Version 10.1.0.0 */ -const unsigned int VER_10_2_0_0 = 0x0A020000; /*!< Nif Version 10.2.0.0 */ -const unsigned int VER_20_0_0_4 = 0x14000004; /*!< Nif Version 20.0.0.4 */ +const unsigned int VER_4_0_0_2 = 0x04000002; /*!< Nif Version 4.0.0.2 */ +const unsigned int VER_4_1_0_12 = 0x0401000C; /*!< Nif Version 4.1.0.12 */ +const unsigned int VER_4_2_0_2 = 0x04020002; /*!< Nif Version 4.2.0.2 */ +const unsigned int VER_4_2_1_0 = 0x04020100; /*!< Nif Version 4.2.1.0 */ +const unsigned int VER_4_2_2_0 = 0x04020200; /*!< Nif Version 4.2.2.0 */ +const unsigned int VER_10_0_1_0 = 0x0A000100; /*!< Nif Version 10.0.1.0 */ +const unsigned int VER_10_1_0_0 = 0x0A010000; /*!< Nif Version 10.1.0.0 */ +const unsigned int VER_10_2_0_0 = 0x0A020000; /*!< Nif Version 10.2.0.0 */ +const unsigned int VER_20_0_0_4 = 0x14000004; /*!< Nif Version 20.0.0.4 */ +const unsigned int VER_UNSUPPORTED = 0xFFFFFFFF; /*!< Unsupported Nif Version */ +const unsigned int VER_INVALID = 0xFFFFFFFE; /*!< Not a Nif file */ /*! Lists the basic texture types availiable from the ITexturingProperty interface*/ enum TexType { @@ -220,6 +222,24 @@ enum PixelFormat { //--Main Functions--// +/*! + * Reads the header of the given file by file name and returns the NIF version. Call this + * function prior to calling ReadNifList or ReadNifTree, if you need to make sure that the NIF file is supported. + * \param file_name The name of the file to load, or the complete path if it is not in the working directory. + * \return The NIF version of the file. + * + * <b>Example:</b> + * \code + * unsigned int ver = CheckNifHeader("test_in.nif"); + * \endcode + * + * <b>In Python:</b> + * \code + * ver = CheckNifHeader("test_in.nif") + * \endcode + */ +unsigned int CheckNifHeader( string const & file_name ); + /*! * Reads the given file by file name and returns a vector of block references * \param file_name The name of the file to load, or the complete path if it is not in the working directory.