From b00de10eb39a269b889936ef67dab1d12b053534 Mon Sep 17 00:00:00 2001 From: Krishty Date: Sat, 24 Apr 2021 00:17:50 +0200 Subject: [PATCH 01/30] Simplified importer search and fixed a few bugs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The search for a matching importer had a few issues, see #3791. There were two different mechanisms to determine whether an importer accepts a specific file extension: 1. `aiImporterDesc::mFileExtensions`, which was forwarded to the UI via `BaseImporter::GetExtensionList()`. 2. `BaseImporter::CanRead()` when called with `checkSig == false`, which determines whether to actually use that importer. Both were redundant and got out of sync repeatedly. I removed 2. completely and replaced it with 1., thereby syncing UI/import and shortening all `BaseImporter::CanRead()` implementations. Further bugfixes: - fixed glTF2 importer throwing exceptions when checking whether it can load a file - removed `BaseImporter::SimpleExtensionCheck()` because it is no longer used and had a bug with case sensitivity Since the `checkSig` parameter in `BaseImporter::CanRead()` is now useless, it can be removed completely. I’m not sure if this would break ABI compatiblity, so I’ll submit it with a later pull request. --- code/AssetLib/3DS/3DSLoader.cpp | 17 ++----- code/AssetLib/3MF/D3MFImporter.cpp | 20 ++------ code/AssetLib/AC/ACLoader.cpp | 15 ++---- code/AssetLib/AMF/AMFImporter.cpp | 16 ++---- code/AssetLib/ASE/ASELoader.cpp | 16 ++---- code/AssetLib/B3D/B3DImporter.cpp | 15 +----- code/AssetLib/BVH/BVHLoader.cpp | 15 ++---- code/AssetLib/Blender/BlenderLoader.cpp | 15 ++---- code/AssetLib/C4D/C4DImporter.cpp | 10 +--- code/AssetLib/COB/COBLoader.cpp | 14 ++---- code/AssetLib/CSM/CSMLoader.cpp | 15 ++---- code/AssetLib/Collada/ColladaLoader.cpp | 35 +++----------- code/AssetLib/DXF/DXFLoader.cpp | 15 ++---- code/AssetLib/FBX/FBXImporter.cpp | 16 ++---- code/AssetLib/HMP/HMPLoader.cpp | 21 +++----- code/AssetLib/IFC/IFCLoader.cpp | 19 +++----- code/AssetLib/Irr/IRRLoader.cpp | 20 ++------ code/AssetLib/Irr/IRRMeshLoader.cpp | 26 +++------- code/AssetLib/LWO/LWOLoader.cpp | 22 +++------ code/AssetLib/LWS/LWSLoader.cpp | 20 +++----- code/AssetLib/M3D/M3DImporter.cpp | 45 ++++++----------- code/AssetLib/MD2/MD2Loader.cpp | 15 ++---- code/AssetLib/MD3/MD3Loader.cpp | 15 ++---- code/AssetLib/MD5/MD5Loader.cpp | 17 ++----- code/AssetLib/MDC/MDCLoader.cpp | 16 ++---- code/AssetLib/MDL/MDLLoader.cpp | 29 +++++------ code/AssetLib/MMD/MMDImporter.cpp | 11 ++--- code/AssetLib/MS3D/MS3DLoader.cpp | 20 ++------ code/AssetLib/NDO/NDOLoader.cpp | 15 ++---- code/AssetLib/NFF/NFFLoader.cpp | 2 +- code/AssetLib/OFF/OFFLoader.cpp | 15 ++---- code/AssetLib/Obj/ObjFileImporter.cpp | 14 ++---- code/AssetLib/Ogre/OgreImporter.cpp | 12 ++--- code/AssetLib/OpenGEX/OpenGEXImporter.cpp | 13 ++--- code/AssetLib/Ply/PlyLoader.cpp | 21 ++------ code/AssetLib/Q3BSP/Q3BSPFileImporter.cpp | 8 +-- code/AssetLib/Q3D/Q3DLoader.cpp | 15 ++---- code/AssetLib/Raw/RawLoader.cpp | 4 +- code/AssetLib/SIB/SIBImporter.cpp | 4 +- code/AssetLib/SMD/SMDLoader.cpp | 4 +- code/AssetLib/STL/STLLoader.cpp | 17 ++----- code/AssetLib/Terragen/TerragenLoader.cpp | 24 ++------- code/AssetLib/Unreal/UnrealLoader.cpp | 4 +- code/AssetLib/X/XFileImporter.cpp | 14 ++---- code/AssetLib/X3D/X3DImporter.cpp | 9 +--- code/AssetLib/XGL/XGLLoader.cpp | 20 ++------ code/AssetLib/glTF/glTFImporter.cpp | 22 +++------ code/AssetLib/glTF2/glTF2Importer.cpp | 14 ++---- code/Common/BaseImporter.cpp | 30 +----------- code/Common/Importer.cpp | 59 ++++++++++++++++++++--- include/assimp/BaseImporter.h | 35 +++----------- 51 files changed, 246 insertions(+), 659 deletions(-) diff --git a/code/AssetLib/3DS/3DSLoader.cpp b/code/AssetLib/3DS/3DSLoader.cpp index 0a64f6870..b690f557d 100644 --- a/code/AssetLib/3DS/3DSLoader.cpp +++ b/code/AssetLib/3DS/3DSLoader.cpp @@ -111,20 +111,9 @@ Discreet3DSImporter::~Discreet3DSImporter() { // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. -bool Discreet3DSImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const { - std::string extension = GetExtension(pFile); - if (extension == "3ds" || extension == "prj") { - return true; - } - - if (!extension.length() || checkSig) { - uint16_t token[3]; - token[0] = 0x4d4d; - token[1] = 0x3dc2; - //token[2] = 0x3daa; - return CheckMagicToken(pIOHandler, pFile, token, 2, 0, 2); - } - return false; +bool Discreet3DSImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const { + static const uint16_t token[] = { 0x4d4d, 0x3dc2 /*, 0x3daa */ }; + return CheckMagicToken(pIOHandler, pFile, token, std::size(token), 0, sizeof token[0]); } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/3MF/D3MFImporter.cpp b/code/AssetLib/3MF/D3MFImporter.cpp index f4ddb6054..5a3813d2e 100644 --- a/code/AssetLib/3MF/D3MFImporter.cpp +++ b/code/AssetLib/3MF/D3MFImporter.cpp @@ -585,22 +585,12 @@ D3MFImporter::~D3MFImporter() { // empty } -bool D3MFImporter::CanRead(const std::string &filename, IOSystem *pIOHandler, bool checkSig) const { - const std::string extension(GetExtension(filename)); - if (extension == desc.mFileExtensions) { - return true; - } else if (!extension.length() || checkSig) { - if (nullptr == pIOHandler) { - return false; - } - if (!ZipArchiveIOSystem::isZipArchive(pIOHandler, filename)) { - return false; - } - D3MF::D3MFOpcPackage opcPackage(pIOHandler, filename); - return opcPackage.validate(); +bool D3MFImporter::CanRead(const std::string &filename, IOSystem *pIOHandler, bool /*checkSig*/) const { + if (!ZipArchiveIOSystem::isZipArchive(pIOHandler, filename)) { + return false; } - - return false; + D3MF::D3MFOpcPackage opcPackage(pIOHandler, filename); + return opcPackage.validate(); } void D3MFImporter::SetupProperties(const Importer * /*pImp*/) { diff --git a/code/AssetLib/AC/ACLoader.cpp b/code/AssetLib/AC/ACLoader.cpp index cba84e8b0..c259b2eb0 100644 --- a/code/AssetLib/AC/ACLoader.cpp +++ b/code/AssetLib/AC/ACLoader.cpp @@ -152,18 +152,9 @@ AC3DImporter::~AC3DImporter() { // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. -bool AC3DImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const { - std::string extension = GetExtension(pFile); - - // fixme: are acc and ac3d *really* used? Some sources say they are - if (extension == "ac" || extension == "ac3d" || extension == "acc") { - return true; - } - if (!extension.length() || checkSig) { - uint32_t token = AI_MAKE_MAGIC("AC3D"); - return CheckMagicToken(pIOHandler, pFile, &token, 1, 0); - } - return false; +bool AC3DImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const { + static const uint32_t tokens[] = { AI_MAKE_MAGIC("AC3D") }; + return CheckMagicToken(pIOHandler, pFile, tokens, std::size(tokens)); } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/AMF/AMFImporter.cpp b/code/AssetLib/AMF/AMFImporter.cpp index 1a3efba9a..0cc02a2c7 100644 --- a/code/AssetLib/AMF/AMFImporter.cpp +++ b/code/AssetLib/AMF/AMFImporter.cpp @@ -502,19 +502,9 @@ void AMFImporter::ParseNode_Metadata(XmlNode &node) { mNodeElement_List.push_back(ne); // and to node element list because its a new object in graph. } -bool AMFImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool pCheckSig) const { - const std::string extension = GetExtension(pFile); - - if (extension == "amf") { - return true; - } - - if (extension.empty() || pCheckSig) { - const char *tokens[] = { " pStream(pIOHandler->Open(pFile, "rb")); - unsigned char data[4]; - if (4 != pStream->Read(data, 1, 4)) { - return false; - } - return !memcmp(data, "3DMO", 4) /* bin */ -#ifdef M3D_ASCII - || !memcmp(data, "3dmo", 4) /* ASCII */ -#endif - ; +bool M3DImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const { + /* + * don't use CheckMagicToken because that checks with swapped bytes too, leading to false + * positives. This magic is not uint32_t, but char[4], so memcmp is the best way + */ + std::unique_ptr pStream(pIOHandler->Open(pFile, "rb")); + unsigned char data[4]; + if (4 != pStream->Read(data, 1, 4)) { + return false; } - return false; + return !memcmp(data, "3DMO", 4) /* bin */ +#ifdef M3D_ASCII + || !memcmp(data, "3dmo", 4) /* ASCII */ +#endif + ; } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/MD2/MD2Loader.cpp b/code/AssetLib/MD2/MD2Loader.cpp index 9ccbcdfca..77063be29 100644 --- a/code/AssetLib/MD2/MD2Loader.cpp +++ b/code/AssetLib/MD2/MD2Loader.cpp @@ -106,19 +106,10 @@ MD2Importer::~MD2Importer() // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. -bool MD2Importer::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const +bool MD2Importer::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool /*checkSig*/) const { - const std::string extension = GetExtension(pFile); - if (extension == "md2") - return true; - - // if check for extension is not enough, check for the magic tokens - if (!extension.length() || checkSig) { - uint32_t tokens[1]; - tokens[0] = AI_MD2_MAGIC_NUMBER_LE; - return CheckMagicToken(pIOHandler,pFile,tokens,1); - } - return false; + static const uint32_t tokens[] = { AI_MD2_MAGIC_NUMBER_LE }; + return CheckMagicToken(pIOHandler,pFile,tokens,std::size(tokens)); } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/MD3/MD3Loader.cpp b/code/AssetLib/MD3/MD3Loader.cpp index e27079766..ec47790d5 100644 --- a/code/AssetLib/MD3/MD3Loader.cpp +++ b/code/AssetLib/MD3/MD3Loader.cpp @@ -349,18 +349,9 @@ MD3Importer::~MD3Importer() {} // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. -bool MD3Importer::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const { - const std::string extension = GetExtension(pFile); - if (extension == "md3") - return true; - - // if check for extension is not enough, check for the magic tokens - if (!extension.length() || checkSig) { - uint32_t tokens[1]; - tokens[0] = AI_MD3_MAGIC_NUMBER_LE; - return CheckMagicToken(pIOHandler, pFile, tokens, 1); - } - return false; +bool MD3Importer::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const { + static const uint32_t tokens[] = { AI_MD3_MAGIC_NUMBER_LE }; + return CheckMagicToken(pIOHandler, pFile, tokens, std::size(tokens)); } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/MD5/MD5Loader.cpp b/code/AssetLib/MD5/MD5Loader.cpp index b99638cd2..b2dbba94a 100644 --- a/code/AssetLib/MD5/MD5Loader.cpp +++ b/code/AssetLib/MD5/MD5Loader.cpp @@ -100,20 +100,9 @@ MD5Importer::~MD5Importer() { // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. -bool MD5Importer::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const { - const std::string extension = GetExtension(pFile); - - if (extension == "md5anim" || extension == "md5mesh" || extension == "md5camera") - return true; - else if (!extension.length() || checkSig) { - if (!pIOHandler) { - return true; - } - const char *tokens[] = { "MD5Version" }; - return SearchFileHeaderForToken(pIOHandler, pFile, tokens, 1); - } - - return false; +bool MD5Importer::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const { + static const char *tokens[] = { "MD5Version" }; + return SearchFileHeaderForToken(pIOHandler, pFile, tokens, std::size(tokens)); } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/MDC/MDCLoader.cpp b/code/AssetLib/MDC/MDCLoader.cpp index 17a349768..df56c3cf6 100644 --- a/code/AssetLib/MDC/MDCLoader.cpp +++ b/code/AssetLib/MDC/MDCLoader.cpp @@ -110,19 +110,9 @@ MDCImporter::~MDCImporter() { // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. -bool MDCImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const { - const std::string extension = GetExtension(pFile); - if (extension == "mdc") { - return true; - } - - // if check for extension is not enough, check for the magic tokens - if (!extension.length() || checkSig) { - uint32_t tokens[1]; - tokens[0] = AI_MDC_MAGIC_NUMBER_LE; - return CheckMagicToken(pIOHandler, pFile, tokens, 1); - } - return false; +bool MDCImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const { + static const uint32_t tokens[] = { AI_MDC_MAGIC_NUMBER_LE }; + return CheckMagicToken(pIOHandler, pFile, tokens, std::size(tokens)); } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/MDL/MDLLoader.cpp b/code/AssetLib/MDL/MDLLoader.cpp index a4286a716..701d8add9 100644 --- a/code/AssetLib/MDL/MDLLoader.cpp +++ b/code/AssetLib/MDL/MDLLoader.cpp @@ -104,23 +104,18 @@ MDLImporter::~MDLImporter() { // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. -bool MDLImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const { - const std::string extension = GetExtension(pFile); - - // if check for extension is not enough, check for the magic tokens - if (extension == "mdl" || !extension.length() || checkSig) { - uint32_t tokens[8]; - tokens[0] = AI_MDL_MAGIC_NUMBER_LE_HL2a; - tokens[1] = AI_MDL_MAGIC_NUMBER_LE_HL2b; - tokens[2] = AI_MDL_MAGIC_NUMBER_LE_GS7; - tokens[3] = AI_MDL_MAGIC_NUMBER_LE_GS5b; - tokens[4] = AI_MDL_MAGIC_NUMBER_LE_GS5a; - tokens[5] = AI_MDL_MAGIC_NUMBER_LE_GS4; - tokens[6] = AI_MDL_MAGIC_NUMBER_LE_GS3; - tokens[7] = AI_MDL_MAGIC_NUMBER_LE; - return CheckMagicToken(pIOHandler, pFile, tokens, 8, 0); - } - return false; +bool MDLImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const { + static const uint32_t tokens[] = { + AI_MDL_MAGIC_NUMBER_LE_HL2a, + AI_MDL_MAGIC_NUMBER_LE_HL2b, + AI_MDL_MAGIC_NUMBER_LE_GS7, + AI_MDL_MAGIC_NUMBER_LE_GS5b, + AI_MDL_MAGIC_NUMBER_LE_GS5a, + AI_MDL_MAGIC_NUMBER_LE_GS4, + AI_MDL_MAGIC_NUMBER_LE_GS3, + AI_MDL_MAGIC_NUMBER_LE + }; + return CheckMagicToken(pIOHandler, pFile, tokens, std::size(tokens)); } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/MMD/MMDImporter.cpp b/code/AssetLib/MMD/MMDImporter.cpp index e4ecc1445..ab9e68177 100644 --- a/code/AssetLib/MMD/MMDImporter.cpp +++ b/code/AssetLib/MMD/MMDImporter.cpp @@ -89,14 +89,9 @@ MMDImporter::~MMDImporter() { // ------------------------------------------------------------------------------------------------ // Returns true, if file is an pmx file. bool MMDImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, - bool checkSig) const { - if (!checkSig) { - return SimpleExtensionCheck(pFile, "pmx"); - } else { - // Check file Header - static const char *pTokens[] = { "PMX " }; - return BaseImporter::SearchFileHeaderForToken(pIOHandler, pFile, pTokens, 1); - } + bool /*checkSig*/) const { + static const char *tokens[] = { "PMX " }; + return SearchFileHeaderForToken(pIOHandler, pFile, tokens, std::size(tokens)); } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/MS3D/MS3DLoader.cpp b/code/AssetLib/MS3D/MS3DLoader.cpp index 192bcbe41..70be25fef 100644 --- a/code/AssetLib/MS3D/MS3DLoader.cpp +++ b/code/AssetLib/MS3D/MS3DLoader.cpp @@ -88,26 +88,12 @@ MS3DImporter::MS3DImporter() // Destructor, private as well MS3DImporter::~MS3DImporter() {} - // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. -bool MS3DImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const +bool MS3DImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool /*checkSig*/) const { - // first call - simple extension check - const std::string extension = GetExtension(pFile); - if (extension == "ms3d") { - return true; - } - - // second call - check for magic identifiers - else if (!extension.length() || checkSig) { - if (!pIOHandler) { - return true; - } - const char* tokens[] = {"MS3D000000"}; - return SearchFileHeaderForToken(pIOHandler,pFile,tokens,1); - } - return false; + static const char* tokens[] = { "MS3D000000" }; + return SearchFileHeaderForToken(pIOHandler,pFile,tokens,std::size(tokens)); } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/NDO/NDOLoader.cpp b/code/AssetLib/NDO/NDOLoader.cpp index 77fe3e36c..05d50af1d 100644 --- a/code/AssetLib/NDO/NDOLoader.cpp +++ b/code/AssetLib/NDO/NDOLoader.cpp @@ -82,19 +82,10 @@ NDOImporter::~NDOImporter() // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. -bool NDOImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const +bool NDOImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool /*checkSig*/) const { - // check file extension - const std::string extension = GetExtension(pFile); - - if( extension == "ndo") - return true; - - if ((checkSig || !extension.length()) && pIOHandler) { - const char* tokens[] = {"nendo"}; - return SearchFileHeaderForToken(pIOHandler,pFile,tokens,1,5); - } - return false; + static const char* tokens[] = {"nendo"}; + return SearchFileHeaderForToken(pIOHandler,pFile,tokens,std::size(tokens),5); } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/NFF/NFFLoader.cpp b/code/AssetLib/NFF/NFFLoader.cpp index 11cce0990..f8b6a0acd 100644 --- a/code/AssetLib/NFF/NFFLoader.cpp +++ b/code/AssetLib/NFF/NFFLoader.cpp @@ -84,7 +84,7 @@ NFFImporter::~NFFImporter() {} // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. bool NFFImporter::CanRead(const std::string &pFile, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const { - return SimpleExtensionCheck(pFile, "nff", "enff"); + return false; } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/OFF/OFFLoader.cpp b/code/AssetLib/OFF/OFFLoader.cpp index 360ffc51b..d1566b4f7 100644 --- a/code/AssetLib/OFF/OFFLoader.cpp +++ b/code/AssetLib/OFF/OFFLoader.cpp @@ -85,19 +85,10 @@ OFFImporter::~OFFImporter() // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. -bool OFFImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const +bool OFFImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool /*checkSig*/) const { - const std::string extension = GetExtension(pFile); - - if (extension == "off") - return true; - else if (!extension.length() || checkSig) - { - if (!pIOHandler)return true; - const char* tokens[] = {"off"}; - return SearchFileHeaderForToken(pIOHandler,pFile,tokens,1,3); - } - return false; + static const char* tokens[] = { "off" }; + return SearchFileHeaderForToken(pIOHandler,pFile,tokens,std::size(tokens),3); } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/Obj/ObjFileImporter.cpp b/code/AssetLib/Obj/ObjFileImporter.cpp index d6232be81..fa5a77ea6 100644 --- a/code/AssetLib/Obj/ObjFileImporter.cpp +++ b/code/AssetLib/Obj/ObjFileImporter.cpp @@ -87,16 +87,10 @@ ObjFileImporter::~ObjFileImporter() { } // ------------------------------------------------------------------------------------------------ -// Returns true, if file is an obj file. -bool ObjFileImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const { - if (!checkSig) { - //Check File Extension - return SimpleExtensionCheck(pFile, "obj"); - } else { - // Check file Header - static const char *pTokens[] = { "mtllib", "usemtl", "v ", "vt ", "vn ", "o ", "g ", "s ", "f " }; - return BaseImporter::SearchFileHeaderForToken(pIOHandler, pFile, pTokens, 9, 200, false, true); - } +// Returns true if file is an obj file. +bool ObjFileImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const { + static const char *tokens[] = { "mtllib", "usemtl", "v ", "vt ", "vn ", "o ", "g ", "s ", "f " }; + return BaseImporter::SearchFileHeaderForToken(pIOHandler, pFile, tokens, std::size(tokens), 200, false, true); } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/Ogre/OgreImporter.cpp b/code/AssetLib/Ogre/OgreImporter.cpp index d139565e8..36ee6edff 100644 --- a/code/AssetLib/Ogre/OgreImporter.cpp +++ b/code/AssetLib/Ogre/OgreImporter.cpp @@ -73,17 +73,13 @@ void OgreImporter::SetupProperties(const Importer *pImp) { m_detectTextureTypeFromFilename = pImp->GetPropertyBool(AI_CONFIG_IMPORT_OGRE_TEXTURETYPE_FROM_FILENAME, false); } -bool OgreImporter::CanRead(const std::string &pFile, Assimp::IOSystem *pIOHandler, bool checkSig) const { - if (!checkSig) { - return EndsWith(pFile, ".mesh.xml", false) || EndsWith(pFile, ".mesh", false); - } - +bool OgreImporter::CanRead(const std::string &pFile, Assimp::IOSystem *pIOHandler, bool /*checkSig*/) const { if (EndsWith(pFile, ".mesh.xml", false)) { - const char *tokens[] = { "" }; - return SearchFileHeaderForToken(pIOHandler, pFile, tokens, 1); + static const char *tokens[] = { "" }; + return SearchFileHeaderForToken(pIOHandler, pFile, tokens, std::size(tokens)); } else { /// @todo Read and validate first header chunk? - return EndsWith(pFile, ".mesh", false); + return false; } } diff --git a/code/AssetLib/OpenGEX/OpenGEXImporter.cpp b/code/AssetLib/OpenGEX/OpenGEXImporter.cpp index 14c9ea37f..4ee9031d7 100644 --- a/code/AssetLib/OpenGEX/OpenGEXImporter.cpp +++ b/code/AssetLib/OpenGEX/OpenGEXImporter.cpp @@ -281,16 +281,9 @@ OpenGEXImporter::~OpenGEXImporter() { } //------------------------------------------------------------------------------------------------ -bool OpenGEXImporter::CanRead(const std::string &file, IOSystem *pIOHandler, bool checkSig) const { - bool canRead(false); - if (!checkSig) { - canRead = SimpleExtensionCheck(file, "ogex"); - } else { - static const char *token[] = { "Metric", "GeometryNode", "VertexArray (attrib", "IndexArray" }; - canRead = BaseImporter::SearchFileHeaderForToken(pIOHandler, file, token, 4); - } - - return canRead; +bool OpenGEXImporter::CanRead(const std::string &file, IOSystem *pIOHandler, bool /*checkSig*/) const { + static const char *tokens[] = { "Metric", "GeometryNode", "VertexArray (attrib", "IndexArray" }; + return SearchFileHeaderForToken(pIOHandler, file, tokens, std::size(tokens)); } //------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/Ply/PlyLoader.cpp b/code/AssetLib/Ply/PlyLoader.cpp index ce52636dd..d92ffacd1 100644 --- a/code/AssetLib/Ply/PlyLoader.cpp +++ b/code/AssetLib/Ply/PlyLoader.cpp @@ -100,24 +100,9 @@ PLYImporter::~PLYImporter() { // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. -bool PLYImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const { - const std::string extension = GetExtension(pFile); - - if (extension == "ply") { - return true; - } - - if (!extension.length() || checkSig) { - if (!pIOHandler) { - return true; - } - static const char *tokens[] = { - "ply" - }; - return SearchFileHeaderForToken(pIOHandler, pFile, tokens, 1); - } - - return false; +bool PLYImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const { + static const char *tokens[] = { "ply" }; + return SearchFileHeaderForToken(pIOHandler, pFile, tokens, std::size(tokens)); } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/Q3BSP/Q3BSPFileImporter.cpp b/code/AssetLib/Q3BSP/Q3BSPFileImporter.cpp index c8eaf85ce..738177037 100644 --- a/code/AssetLib/Q3BSP/Q3BSPFileImporter.cpp +++ b/code/AssetLib/Q3BSP/Q3BSPFileImporter.cpp @@ -160,12 +160,8 @@ Q3BSPFileImporter::~Q3BSPFileImporter() { } // ------------------------------------------------------------------------------------------------ -// Returns true, if the loader can read this. -bool Q3BSPFileImporter::CanRead(const std::string &rFile, IOSystem * /*pIOHandler*/, bool checkSig) const { - if (!checkSig) { - return SimpleExtensionCheck(rFile, "pk3", "bsp"); - } - +// Returns true if the loader can read this. +bool Q3BSPFileImporter::CanRead(const std::string &/*rFile*/, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const { return false; } diff --git a/code/AssetLib/Q3D/Q3DLoader.cpp b/code/AssetLib/Q3D/Q3DLoader.cpp index b52f86672..74ff01ca0 100644 --- a/code/AssetLib/Q3D/Q3DLoader.cpp +++ b/code/AssetLib/Q3D/Q3DLoader.cpp @@ -83,18 +83,9 @@ Q3DImporter::~Q3DImporter() { // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. -bool Q3DImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const { - const std::string extension = GetExtension(pFile); - - if (extension == "q3s" || extension == "q3o") - return true; - else if (!extension.length() || checkSig) { - if (!pIOHandler) - return true; - const char *tokens[] = { "quick3Do", "quick3Ds" }; - return SearchFileHeaderForToken(pIOHandler, pFile, tokens, 2); - } - return false; +bool Q3DImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const { + static const char *tokens[] = { "quick3Do", "quick3Ds" }; + return SearchFileHeaderForToken(pIOHandler, pFile, tokens, std::size(tokens)); } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/Raw/RawLoader.cpp b/code/AssetLib/Raw/RawLoader.cpp index 8e57912f5..27d37b7ac 100644 --- a/code/AssetLib/Raw/RawLoader.cpp +++ b/code/AssetLib/Raw/RawLoader.cpp @@ -84,8 +84,8 @@ RAWImporter::~RAWImporter() { // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. -bool RAWImporter::CanRead(const std::string &pFile, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const { - return SimpleExtensionCheck(pFile, "raw"); +bool RAWImporter::CanRead(const std::string & /*pFile*/, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const { + return false; } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/SIB/SIBImporter.cpp b/code/AssetLib/SIB/SIBImporter.cpp index 6898fb65c..469948a41 100644 --- a/code/AssetLib/SIB/SIBImporter.cpp +++ b/code/AssetLib/SIB/SIBImporter.cpp @@ -217,8 +217,8 @@ SIBImporter::~SIBImporter() { // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. -bool SIBImporter::CanRead(const std::string &pFile, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const { - return SimpleExtensionCheck(pFile, "sib"); +bool SIBImporter::CanRead(const std::string & /*pFile*/, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const { + return false; } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/SMD/SMDLoader.cpp b/code/AssetLib/SMD/SMDLoader.cpp index de9c65c4a..88353e85b 100644 --- a/code/AssetLib/SMD/SMDLoader.cpp +++ b/code/AssetLib/SMD/SMDLoader.cpp @@ -101,9 +101,9 @@ SMDImporter::~SMDImporter() { // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. -bool SMDImporter::CanRead( const std::string& pFile, IOSystem* /*pIOHandler*/, bool) const { +bool SMDImporter::CanRead( const std::string& /*pFile*/, IOSystem* /*pIOHandler*/, bool) const { // fixme: auto format detection - return SimpleExtensionCheck(pFile,"smd","vta"); + return false; } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/STL/STLLoader.cpp b/code/AssetLib/STL/STLLoader.cpp index 433fb14c7..65cfc7830 100644 --- a/code/AssetLib/STL/STLLoader.cpp +++ b/code/AssetLib/STL/STLLoader.cpp @@ -141,20 +141,9 @@ STLImporter::~STLImporter() { // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. -bool STLImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const { - const std::string extension = GetExtension(pFile); - - if (extension == "stl") { - return true; - } else if (!extension.length() || checkSig) { - if (!pIOHandler) { - return true; - } - const char *tokens[] = { "STL", "solid" }; - return SearchFileHeaderForToken(pIOHandler, pFile, tokens, 2); - } - - return false; +bool STLImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const { + static const char *tokens[] = { "STL", "solid" }; + return SearchFileHeaderForToken(pIOHandler, pFile, tokens, std::size(tokens)); } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/Terragen/TerragenLoader.cpp b/code/AssetLib/Terragen/TerragenLoader.cpp index e0bdbf026..16e5ac470 100644 --- a/code/AssetLib/Terragen/TerragenLoader.cpp +++ b/code/AssetLib/Terragen/TerragenLoader.cpp @@ -77,27 +77,9 @@ TerragenImporter::~TerragenImporter() {} // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. -bool TerragenImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const { - // check file extension - std::string extension = GetExtension(pFile); - - if (extension == "ter") - return true; - - if (!extension.length() || checkSig) { - /* If CanRead() is called in order to check whether we - * support a specific file extension in general pIOHandler - * might be nullptr and it's our duty to return true here. - */ - if (!pIOHandler) { - return true; - } - - const char *tokens[] = { "terragen" }; - return SearchFileHeaderForToken(pIOHandler, pFile, tokens, 1); - } - - return false; +bool TerragenImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const { + static const char *tokens[] = { "terragen" }; + return SearchFileHeaderForToken(pIOHandler, pFile, tokens, std::size(tokens)); } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/Unreal/UnrealLoader.cpp b/code/AssetLib/Unreal/UnrealLoader.cpp index 499b49c05..725fdcd7d 100644 --- a/code/AssetLib/Unreal/UnrealLoader.cpp +++ b/code/AssetLib/Unreal/UnrealLoader.cpp @@ -176,8 +176,8 @@ UnrealImporter::~UnrealImporter() {} // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. -bool UnrealImporter::CanRead(const std::string &pFile, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const { - return SimpleExtensionCheck(pFile, "3d", "uc"); +bool UnrealImporter::CanRead(const std::string & /*pFile*/, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const { + return false; } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/X/XFileImporter.cpp b/code/AssetLib/X/XFileImporter.cpp index cb245ed74..73cb1acda 100644 --- a/code/AssetLib/X/XFileImporter.cpp +++ b/code/AssetLib/X/XFileImporter.cpp @@ -89,17 +89,9 @@ XFileImporter::~XFileImporter() { // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. -bool XFileImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const { - std::string extension = GetExtension(pFile); - if(extension == "x") { - return true; - } - if (!extension.length() || checkSig) { - uint32_t token[1]; - token[0] = AI_MAKE_MAGIC("xof "); - return CheckMagicToken(pIOHandler,pFile,token,1,0); - } - return false; +bool XFileImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool /*checkSig*/) const { + static const uint32_t token[] = { AI_MAKE_MAGIC("xof ") }; + return CheckMagicToken(pIOHandler,pFile,token,std::size(token)); } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/X3D/X3DImporter.cpp b/code/AssetLib/X3D/X3DImporter.cpp index 121b7490e..6e4606909 100644 --- a/code/AssetLib/X3D/X3DImporter.cpp +++ b/code/AssetLib/X3D/X3DImporter.cpp @@ -156,14 +156,7 @@ void X3DImporter::ParseFile(const std::string &file, IOSystem *pIOHandler) { } } -bool X3DImporter::CanRead( const std::string &pFile, IOSystem * /*pIOHandler*/, bool checkSig ) const { - if (checkSig) { - std::string::size_type pos = pFile.find_last_of(".x3d"); - if (pos != std::string::npos) { - return true; - } - } - +bool X3DImporter::CanRead( const std::string & /*pFile*/, IOSystem * /*pIOHandler*/, bool /*checkSig*/ ) const { return false; } diff --git a/code/AssetLib/XGL/XGLLoader.cpp b/code/AssetLib/XGL/XGLLoader.cpp index 824f0c2c3..7e134a054 100644 --- a/code/AssetLib/XGL/XGLLoader.cpp +++ b/code/AssetLib/XGL/XGLLoader.cpp @@ -105,23 +105,9 @@ XGLImporter::~XGLImporter() { // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. -bool XGLImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const { - /* NOTE: A simple check for the file extension is not enough - * here. XGL and ZGL are ok, but xml is too generic - * and might be collada as well. So open the file and - * look for typical signal tokens. - */ - const std::string extension = GetExtension(pFile); - - if (extension == "xgl" || extension == "zgl") { - return true; - } else if (extension == "xml" || checkSig) { - ai_assert(pIOHandler != NULL); - - const char *tokens[] = { "", "", "" }; - return SearchFileHeaderForToken(pIOHandler, pFile, tokens, 3); - } - return false; +bool XGLImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const { + static const char *tokens[] = { "", "", "" }; + return SearchFileHeaderForToken(pIOHandler, pFile, tokens, std::size(tokens)); } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/glTF/glTFImporter.cpp b/code/AssetLib/glTF/glTFImporter.cpp index 5e072dd2a..0b22f78d4 100644 --- a/code/AssetLib/glTF/glTFImporter.cpp +++ b/code/AssetLib/glTF/glTFImporter.cpp @@ -93,24 +93,14 @@ const aiImporterDesc *glTFImporter::GetInfo() const { } bool glTFImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /* checkSig */) const { - const std::string &extension = GetExtension(pFile); - - if (extension != "gltf" && extension != "glb") { + glTF::Asset asset(pIOHandler); + try { + asset.Load(pFile, GetExtension(pFile) == "glb"); + std::string version = asset.asset.version; + return !version.empty() && version[0] == '1'; + } catch (...) { return false; } - - if (pIOHandler) { - glTF::Asset asset(pIOHandler); - try { - asset.Load(pFile, extension == "glb"); - std::string version = asset.asset.version; - return !version.empty() && version[0] == '1'; - } catch (...) { - return false; - } - } - - return false; } inline void SetMaterialColorProperty(std::vector &embeddedTexIdxs, Asset & /*r*/, glTF::TexProperty prop, aiMaterial *mat, diff --git a/code/AssetLib/glTF2/glTF2Importer.cpp b/code/AssetLib/glTF2/glTF2Importer.cpp index ab1f01bf8..841f61369 100644 --- a/code/AssetLib/glTF2/glTF2Importer.cpp +++ b/code/AssetLib/glTF2/glTF2Importer.cpp @@ -110,20 +110,14 @@ const aiImporterDesc *glTF2Importer::GetInfo() const { } bool glTF2Importer::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /* checkSig */) const { - const std::string &extension = GetExtension(pFile); - - if (extension != "gltf" && extension != "glb") { - return false; - } - - if (pIOHandler) { + try { glTF2::Asset asset(pIOHandler); - asset.Load(pFile, extension == "glb"); + asset.Load(pFile, GetExtension(pFile) == "glb"); std::string version = asset.asset.version; return !version.empty() && version[0] == '2'; + } catch(...) { + return false; } - - return false; } static aiTextureMapMode ConvertWrappingMode(SamplerWrap gltfWrapMode) { diff --git a/code/Common/BaseImporter.cpp b/code/Common/BaseImporter.cpp index 0657216cf..6ef287999 100644 --- a/code/Common/BaseImporter.cpp +++ b/code/Common/BaseImporter.cpp @@ -170,7 +170,7 @@ void BaseImporter::GetExtensionList(std::set &extensions) { /*static*/ bool BaseImporter::SearchFileHeaderForToken(IOSystem *pIOHandler, const std::string &pFile, const char **tokens, - unsigned int numTokens, + std::size_t numTokens, unsigned int searchBytes /* = 200 */, bool tokensSol /* false */, bool noAlphaBeforeTokens /* false */) { @@ -238,32 +238,6 @@ void BaseImporter::GetExtensionList(std::set &extensions) { return false; } -// ------------------------------------------------------------------------------------------------ -// Simple check for file extension -/*static*/ bool BaseImporter::SimpleExtensionCheck(const std::string &pFile, - const char *ext0, - const char *ext1, - const char *ext2) { - std::string::size_type pos = pFile.find_last_of('.'); - - // no file extension - can't read - if (pos == std::string::npos) - return false; - - const char *ext_real = &pFile[pos + 1]; - if (!ASSIMP_stricmp(ext_real, ext0)) - return true; - - // check for other, optional, file extensions - if (ext1 && !ASSIMP_stricmp(ext_real, ext1)) - return true; - - if (ext2 && !ASSIMP_stricmp(ext_real, ext2)) - return true; - - return false; -} - // ------------------------------------------------------------------------------------------------ // Get file extension from path std::string BaseImporter::GetExtension(const std::string &file) { @@ -284,7 +258,7 @@ std::string BaseImporter::GetExtension(const std::string &file) { // ------------------------------------------------------------------------------------------------ // Check for magic bytes at the beginning of the file. /* static */ bool BaseImporter::CheckMagicToken(IOSystem *pIOHandler, const std::string &pFile, - const void *_magic, unsigned int num, unsigned int offset, unsigned int size) { + const void *_magic, std::size_t num, unsigned int offset, unsigned int size) { ai_assert(size <= 16); ai_assert(_magic); diff --git a/code/Common/Importer.cpp b/code/Common/Importer.cpp index db7fc9e1c..36691bdcd 100644 --- a/code/Common/Importer.cpp +++ b/code/Common/Importer.cpp @@ -617,16 +617,61 @@ const aiScene* Importer::ReadFile( const char* _pFile, unsigned int pFlags) { profiler->BeginRegion("total"); } - // Find an worker class which can handle the file - BaseImporter* imp = nullptr; + // Find an worker class which can handle the file extension. + // Multiple importers may be able to handle the same extension (.xml!); gather them all. SetPropertyInteger("importerIndex", -1); - for( unsigned int a = 0; a < pimpl->mImporter.size(); a++) { + struct ImporterAndIndex { + BaseImporter * importer; + unsigned int index; + }; + std::vector possibleImporters; + for (unsigned int a = 0; a < pimpl->mImporter.size(); a++) { + + // Every importer has a list of supported extensions. + std::set extensions; + pimpl->mImporter[a]->GetExtensionList(extensions); + + // CAUTION: Do not just search for the extension! + // GetExtension() returns the part after the *last* dot, but some extensions have dots + // inside them, e.g. ogre.mesh.xml. Compare the entire end of the string. + for (std::set::const_iterator it = extensions.cbegin(); it != extensions.cend(); ++it) { + + // Yay for C++<20 not having std::string::ends_with() + std::string extension = "." + *it; + if (extension.length() <= pFile.length()) { + // Possible optimization: Fetch the lowercase filename! + if (0 == ASSIMP_stricmp(pFile.c_str() + pFile.length() - extension.length(), extension.c_str())) { + ImporterAndIndex candidate = { pimpl->mImporter[a], a }; + possibleImporters.push_back(candidate); + break; + } + } - if( pimpl->mImporter[a]->CanRead( pFile, pimpl->mIOHandler, false)) { - imp = pimpl->mImporter[a]; - SetPropertyInteger("importerIndex", a); - break; } + + } + + // If just one importer supports this extension, pick it and close the case. + BaseImporter* imp = nullptr; + if (1 == possibleImporters.size()) { + imp = possibleImporters[0].importer; + SetPropertyInteger("importerIndex", possibleImporters[0].index); + } + // If multiple importers claim this file extension, ask them to look at the actual file data to decide. + // This can happen e.g. with XML (COLLADA vs. Irrlicht). + else { + for (std::vector::const_iterator it = possibleImporters.begin(); it < possibleImporters.end(); ++it) { + BaseImporter & importer = *it->importer; + + ASSIMP_LOG_INFO("Found a possible importer: " + std::string(importer.GetInfo()->mName) + "; trying signature-based detection"); + if (importer.CanRead( pFile, pimpl->mIOHandler, true)) { + imp = &importer; + SetPropertyInteger("importerIndex", it->index); + break; + } + + } + } if (!imp) { diff --git a/include/assimp/BaseImporter.h b/include/assimp/BaseImporter.h index 86d7ba70f..166dbdc10 100644 --- a/include/assimp/BaseImporter.h +++ b/include/assimp/BaseImporter.h @@ -97,20 +97,15 @@ public: // ------------------------------------------------------------------- /** Returns whether the class can handle the format of the given file. * - * The implementation should be as quick as possible. A check for - * the file extension is enough. If no suitable loader is found with - * this strategy, CanRead() is called again, the 'checkSig' parameter - * set to true this time. Now the implementation is expected to - * perform a full check of the file structure, possibly searching the - * first bytes of the file for magic identifiers or keywords. + * The implementation is expected to perform a full check of the file + * structure, possibly searching the first bytes of the file for magic + * identifiers or keywords. * * @param pFile Path and file name of the file to be examined. * @param pIOHandler The IO handler to use for accessing any file. - * @param checkSig Set to true if this method is called a second time. - * This time, the implementation may take more time to examine the - * contents of the file to be loaded for magic bytes, keywords, etc - * to be able to load files with unknown/not existent file extensions. - * @return true if the class can read this file, false if not. + * @param checkSig Legacy; do not use. + * @return true if the class can read this file, false if not or if + * unsure. */ virtual bool CanRead( const std::string &pFile, @@ -295,25 +290,11 @@ public: // static utilities IOSystem *pIOSystem, const std::string &file, const char **tokens, - unsigned int numTokens, + std::size_t numTokens, unsigned int searchBytes = 200, bool tokensSol = false, bool noAlphaBeforeTokens = false); - // ------------------------------------------------------------------- - /** @brief Check whether a file has a specific file extension - * @param pFile Input file - * @param ext0 Extension to check for. Lowercase characters only, no dot! - * @param ext1 Optional second extension - * @param ext2 Optional third extension - * @note Case-insensitive - */ - static bool SimpleExtensionCheck( - const std::string &pFile, - const char *ext0, - const char *ext1 = nullptr, - const char *ext2 = nullptr); - // ------------------------------------------------------------------- /** @brief Extract file extension from a string * @param pFile Input file @@ -340,7 +321,7 @@ public: // static utilities IOSystem *pIOHandler, const std::string &pFile, const void *magic, - unsigned int num, + std::size_t num, unsigned int offset = 0, unsigned int size = 4); From 628cba9490c067aa811a24f1943529bcfb490c51 Mon Sep 17 00:00:00 2001 From: Krishty Date: Sat, 24 Apr 2021 00:44:50 +0200 Subject: [PATCH 02/30] fixed warning --- code/AssetLib/NFF/NFFLoader.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/AssetLib/NFF/NFFLoader.cpp b/code/AssetLib/NFF/NFFLoader.cpp index f8b6a0acd..695a360f2 100644 --- a/code/AssetLib/NFF/NFFLoader.cpp +++ b/code/AssetLib/NFF/NFFLoader.cpp @@ -83,7 +83,7 @@ NFFImporter::~NFFImporter() {} // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. -bool NFFImporter::CanRead(const std::string &pFile, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const { +bool NFFImporter::CanRead(const std::string & /*pFile*/, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const { return false; } From b87f67db4c10d568637d19a35bd986db49e92ceb Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Mon, 3 May 2021 20:49:14 +0200 Subject: [PATCH 03/30] Add missing include --- include/assimp/BaseImporter.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/assimp/BaseImporter.h b/include/assimp/BaseImporter.h index 166dbdc10..78ac532e3 100644 --- a/include/assimp/BaseImporter.h +++ b/include/assimp/BaseImporter.h @@ -58,6 +58,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include struct aiScene; struct aiImporterDesc; From 88d64388f8ea2538dc0597c00c6f8eab467da7df Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Mon, 3 May 2021 20:57:17 +0200 Subject: [PATCH 04/30] Update AMFImporter.cpp --- code/AssetLib/AMF/AMFImporter.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/code/AssetLib/AMF/AMFImporter.cpp b/code/AssetLib/AMF/AMFImporter.cpp index 0cc02a2c7..6c43944bf 100644 --- a/code/AssetLib/AMF/AMFImporter.cpp +++ b/code/AssetLib/AMF/AMFImporter.cpp @@ -504,7 +504,9 @@ void AMFImporter::ParseNode_Metadata(XmlNode &node) { bool AMFImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*pCheckSig*/) const { static const char *tokens[] = { " Date: Wed, 5 May 2021 00:08:54 +0200 Subject: [PATCH 05/30] replaced std::size() with AI_COUNT_OF for pre-C++20 compilers --- code/AssetLib/3DS/3DSLoader.cpp | 2 +- code/AssetLib/AC/ACLoader.cpp | 2 +- code/AssetLib/AMF/AMFImporter.cpp | 2 +- code/AssetLib/ASE/ASELoader.cpp | 2 +- code/AssetLib/BVH/BVHLoader.cpp | 2 +- code/AssetLib/Blender/BlenderLoader.cpp | 2 +- code/AssetLib/COB/COBLoader.cpp | 2 +- code/AssetLib/CSM/CSMLoader.cpp | 2 +- code/AssetLib/Collada/ColladaLoader.cpp | 2 +- code/AssetLib/DXF/DXFLoader.cpp | 2 +- code/AssetLib/FBX/FBXImporter.cpp | 2 +- code/AssetLib/HMP/HMPLoader.cpp | 2 +- code/AssetLib/IFC/IFCLoader.cpp | 2 +- code/AssetLib/Irr/IRRLoader.cpp | 2 +- code/AssetLib/Irr/IRRMeshLoader.cpp | 2 +- code/AssetLib/LWO/LWOLoader.cpp | 2 +- code/AssetLib/LWS/LWSLoader.cpp | 2 +- code/AssetLib/MD2/MD2Loader.cpp | 2 +- code/AssetLib/MD3/MD3Loader.cpp | 2 +- code/AssetLib/MD5/MD5Loader.cpp | 2 +- code/AssetLib/MDC/MDCLoader.cpp | 2 +- code/AssetLib/MDL/MDLLoader.cpp | 2 +- code/AssetLib/MMD/MMDImporter.cpp | 2 +- code/AssetLib/MS3D/MS3DLoader.cpp | 2 +- code/AssetLib/NDO/NDOLoader.cpp | 2 +- code/AssetLib/OFF/OFFLoader.cpp | 2 +- code/AssetLib/Ogre/OgreImporter.cpp | 2 +- code/AssetLib/OpenGEX/OpenGEXImporter.cpp | 2 +- code/AssetLib/Ply/PlyLoader.cpp | 2 +- code/AssetLib/Q3D/Q3DLoader.cpp | 2 +- code/AssetLib/STL/STLLoader.cpp | 2 +- code/AssetLib/Terragen/TerragenLoader.cpp | 2 +- code/AssetLib/X/XFileImporter.cpp | 2 +- code/AssetLib/XGL/XGLLoader.cpp | 2 +- include/assimp/defs.h | 2 ++ 35 files changed, 36 insertions(+), 34 deletions(-) diff --git a/code/AssetLib/3DS/3DSLoader.cpp b/code/AssetLib/3DS/3DSLoader.cpp index b690f557d..89a4fbc7e 100644 --- a/code/AssetLib/3DS/3DSLoader.cpp +++ b/code/AssetLib/3DS/3DSLoader.cpp @@ -113,7 +113,7 @@ Discreet3DSImporter::~Discreet3DSImporter() { // Returns whether the class can handle the format of the given file. bool Discreet3DSImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const { static const uint16_t token[] = { 0x4d4d, 0x3dc2 /*, 0x3daa */ }; - return CheckMagicToken(pIOHandler, pFile, token, std::size(token), 0, sizeof token[0]); + return CheckMagicToken(pIOHandler, pFile, token, AI_COUNT_OF(token), 0, sizeof token[0]); } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/AC/ACLoader.cpp b/code/AssetLib/AC/ACLoader.cpp index c259b2eb0..ea097ae0d 100644 --- a/code/AssetLib/AC/ACLoader.cpp +++ b/code/AssetLib/AC/ACLoader.cpp @@ -154,7 +154,7 @@ AC3DImporter::~AC3DImporter() { // Returns whether the class can handle the format of the given file. bool AC3DImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const { static const uint32_t tokens[] = { AI_MAKE_MAGIC("AC3D") }; - return CheckMagicToken(pIOHandler, pFile, tokens, std::size(tokens)); + return CheckMagicToken(pIOHandler, pFile, tokens, AI_COUNT_OF(tokens)); } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/AMF/AMFImporter.cpp b/code/AssetLib/AMF/AMFImporter.cpp index 0cc02a2c7..9e846d473 100644 --- a/code/AssetLib/AMF/AMFImporter.cpp +++ b/code/AssetLib/AMF/AMFImporter.cpp @@ -504,7 +504,7 @@ void AMFImporter::ParseNode_Metadata(XmlNode &node) { bool AMFImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*pCheckSig*/) const { static const char *tokens[] = { "" }; - return SearchFileHeaderForToken(pIOHandler, pFile, tokens, std::size(tokens)); + return SearchFileHeaderForToken(pIOHandler, pFile, tokens, AI_COUNT_OF(tokens)); } else { /// @todo Read and validate first header chunk? return false; diff --git a/code/AssetLib/OpenGEX/OpenGEXImporter.cpp b/code/AssetLib/OpenGEX/OpenGEXImporter.cpp index 4ee9031d7..429a03ad8 100644 --- a/code/AssetLib/OpenGEX/OpenGEXImporter.cpp +++ b/code/AssetLib/OpenGEX/OpenGEXImporter.cpp @@ -283,7 +283,7 @@ OpenGEXImporter::~OpenGEXImporter() { //------------------------------------------------------------------------------------------------ bool OpenGEXImporter::CanRead(const std::string &file, IOSystem *pIOHandler, bool /*checkSig*/) const { static const char *tokens[] = { "Metric", "GeometryNode", "VertexArray (attrib", "IndexArray" }; - return SearchFileHeaderForToken(pIOHandler, file, tokens, std::size(tokens)); + return SearchFileHeaderForToken(pIOHandler, file, tokens, AI_COUNT_OF(tokens)); } //------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/Ply/PlyLoader.cpp b/code/AssetLib/Ply/PlyLoader.cpp index d92ffacd1..86cf33703 100644 --- a/code/AssetLib/Ply/PlyLoader.cpp +++ b/code/AssetLib/Ply/PlyLoader.cpp @@ -102,7 +102,7 @@ PLYImporter::~PLYImporter() { // Returns whether the class can handle the format of the given file. bool PLYImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const { static const char *tokens[] = { "ply" }; - return SearchFileHeaderForToken(pIOHandler, pFile, tokens, std::size(tokens)); + return SearchFileHeaderForToken(pIOHandler, pFile, tokens, AI_COUNT_OF(tokens)); } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/Q3D/Q3DLoader.cpp b/code/AssetLib/Q3D/Q3DLoader.cpp index 74ff01ca0..9f41f190f 100644 --- a/code/AssetLib/Q3D/Q3DLoader.cpp +++ b/code/AssetLib/Q3D/Q3DLoader.cpp @@ -85,7 +85,7 @@ Q3DImporter::~Q3DImporter() { // Returns whether the class can handle the format of the given file. bool Q3DImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const { static const char *tokens[] = { "quick3Do", "quick3Ds" }; - return SearchFileHeaderForToken(pIOHandler, pFile, tokens, std::size(tokens)); + return SearchFileHeaderForToken(pIOHandler, pFile, tokens, AI_COUNT_OF(tokens)); } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/STL/STLLoader.cpp b/code/AssetLib/STL/STLLoader.cpp index 65cfc7830..68a2244f4 100644 --- a/code/AssetLib/STL/STLLoader.cpp +++ b/code/AssetLib/STL/STLLoader.cpp @@ -143,7 +143,7 @@ STLImporter::~STLImporter() { // Returns whether the class can handle the format of the given file. bool STLImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const { static const char *tokens[] = { "STL", "solid" }; - return SearchFileHeaderForToken(pIOHandler, pFile, tokens, std::size(tokens)); + return SearchFileHeaderForToken(pIOHandler, pFile, tokens, AI_COUNT_OF(tokens)); } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/Terragen/TerragenLoader.cpp b/code/AssetLib/Terragen/TerragenLoader.cpp index 16e5ac470..6308727f8 100644 --- a/code/AssetLib/Terragen/TerragenLoader.cpp +++ b/code/AssetLib/Terragen/TerragenLoader.cpp @@ -79,7 +79,7 @@ TerragenImporter::~TerragenImporter() {} // Returns whether the class can handle the format of the given file. bool TerragenImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const { static const char *tokens[] = { "terragen" }; - return SearchFileHeaderForToken(pIOHandler, pFile, tokens, std::size(tokens)); + return SearchFileHeaderForToken(pIOHandler, pFile, tokens, AI_COUNT_OF(tokens)); } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/X/XFileImporter.cpp b/code/AssetLib/X/XFileImporter.cpp index 73cb1acda..a2a26824a 100644 --- a/code/AssetLib/X/XFileImporter.cpp +++ b/code/AssetLib/X/XFileImporter.cpp @@ -91,7 +91,7 @@ XFileImporter::~XFileImporter() { // Returns whether the class can handle the format of the given file. bool XFileImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool /*checkSig*/) const { static const uint32_t token[] = { AI_MAKE_MAGIC("xof ") }; - return CheckMagicToken(pIOHandler,pFile,token,std::size(token)); + return CheckMagicToken(pIOHandler,pFile,token,AI_COUNT_OF(token)); } // ------------------------------------------------------------------------------------------------ diff --git a/code/AssetLib/XGL/XGLLoader.cpp b/code/AssetLib/XGL/XGLLoader.cpp index 7e134a054..943791ef0 100644 --- a/code/AssetLib/XGL/XGLLoader.cpp +++ b/code/AssetLib/XGL/XGLLoader.cpp @@ -107,7 +107,7 @@ XGLImporter::~XGLImporter() { // Returns whether the class can handle the format of the given file. bool XGLImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const { static const char *tokens[] = { "", "", "" }; - return SearchFileHeaderForToken(pIOHandler, pFile, tokens, std::size(tokens)); + return SearchFileHeaderForToken(pIOHandler, pFile, tokens, AI_COUNT_OF(tokens)); } // ------------------------------------------------------------------------------------------------ diff --git a/include/assimp/defs.h b/include/assimp/defs.h index d61fd7901..dbaff24c1 100644 --- a/include/assimp/defs.h +++ b/include/assimp/defs.h @@ -322,4 +322,6 @@ static const ai_real ai_epsilon = (ai_real)0.00001; #define AI_DEBUG_INVALIDATE_PTR(x) #endif +#define AI_COUNT_OF(X) (sizeof(X) / sizeof((X)[0])) + #endif // !! AI_DEFINES_H_INC From ae93f53b514750015003a118282805ae09c737cb Mon Sep 17 00:00:00 2001 From: Krishty Date: Wed, 5 May 2021 00:57:43 +0200 Subject: [PATCH 06/30] fixed signature-based detection on unknown extensions --- code/Common/Importer.cpp | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/code/Common/Importer.cpp b/code/Common/Importer.cpp index 9c877288c..9eabe73b3 100644 --- a/code/Common/Importer.cpp +++ b/code/Common/Importer.cpp @@ -676,15 +676,12 @@ const aiScene* Importer::ReadFile( const char* _pFile, unsigned int pFlags) { if (!imp) { // not so bad yet ... try format auto detection. - const std::string::size_type s = pFile.find_last_of('.'); - if (s != std::string::npos) { - ASSIMP_LOG_INFO("File extension not known, trying signature-based detection"); - for( unsigned int a = 0; a < pimpl->mImporter.size(); a++) { - if( pimpl->mImporter[a]->CanRead( pFile, pimpl->mIOHandler, true)) { - imp = pimpl->mImporter[a]; - SetPropertyInteger("importerIndex", a); - break; - } + ASSIMP_LOG_INFO("File extension not known, trying signature-based detection"); + for( unsigned int a = 0; a < pimpl->mImporter.size(); a++) { + if( pimpl->mImporter[a]->CanRead( pFile, pimpl->mIOHandler, true)) { + imp = pimpl->mImporter[a]; + SetPropertyInteger("importerIndex", a); + break; } } // Put a proper error message if no suitable importer was found From b546dfccc7a10b9daed580c7fd6b8a194a8de399 Mon Sep 17 00:00:00 2001 From: Krishty Date: Wed, 5 May 2021 13:14:25 +0200 Subject: [PATCH 07/30] fix for 0f3e6e2941f754014d5f9f12656a9e326fca61ed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Visual Studio’s Search All Files ignores ObjFileImporter.cpp and now I’m sure that’s a bug --- code/AssetLib/Obj/ObjFileImporter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/AssetLib/Obj/ObjFileImporter.cpp b/code/AssetLib/Obj/ObjFileImporter.cpp index fa5a77ea6..b850b1a6a 100644 --- a/code/AssetLib/Obj/ObjFileImporter.cpp +++ b/code/AssetLib/Obj/ObjFileImporter.cpp @@ -90,7 +90,7 @@ ObjFileImporter::~ObjFileImporter() { // Returns true if file is an obj file. bool ObjFileImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const { static const char *tokens[] = { "mtllib", "usemtl", "v ", "vt ", "vn ", "o ", "g ", "s ", "f " }; - return BaseImporter::SearchFileHeaderForToken(pIOHandler, pFile, tokens, std::size(tokens), 200, false, true); + return BaseImporter::SearchFileHeaderForToken(pIOHandler, pFile, tokens, AI_COUNT_OF(tokens), 200, false, true); } // ------------------------------------------------------------------------------------------------ From acd70b35a04cee6e433d7208847a2dd5a8436953 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Sun, 16 Jan 2022 20:57:56 +0100 Subject: [PATCH 08/30] Fix merge issues --- code/AssetLib/Blender/BlenderLoader.cpp | 4 +++- code/AssetLib/Ogre/OgreImporter.cpp | 3 --- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/code/AssetLib/Blender/BlenderLoader.cpp b/code/AssetLib/Blender/BlenderLoader.cpp index 8fad056b6..42d25ebac 100644 --- a/code/AssetLib/Blender/BlenderLoader.cpp +++ b/code/AssetLib/Blender/BlenderLoader.cpp @@ -120,7 +120,9 @@ static const char * const TokensForSearch[] = { "blender" }; // Returns whether the class can handle the format of the given file. bool BlenderImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const { // note: this won't catch compressed files - return SearchFileHeaderForToken(pIOHandler, pFile, TokensForSearch, AI_COUNT_OF(TokensForSearch)); + static const char *tokens[] = { "" }; return SearchFileHeaderForToken(pIOHandler, pFile, tokens, AI_COUNT_OF(tokens)); - } else { - /// @todo Read and validate first header chunk? - return false; } /// @todo Read and validate first header chunk? From 5af79cac1da8c78ad03c3d099a3aea84d0902492 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 18 Jan 2022 21:23:43 +0100 Subject: [PATCH 09/30] Fix unittests + small refactorings --- code/AssetLib/glTF2/glTF2Importer.cpp | 67 ++++++++++++++++----------- code/AssetLib/glTF2/glTF2Importer.h | 2 +- 2 files changed, 40 insertions(+), 29 deletions(-) diff --git a/code/AssetLib/glTF2/glTF2Importer.cpp b/code/AssetLib/glTF2/glTF2Importer.cpp index 963752519..4e78d0e17 100644 --- a/code/AssetLib/glTF2/glTF2Importer.cpp +++ b/code/AssetLib/glTF2/glTF2Importer.cpp @@ -98,7 +98,7 @@ static const aiImporterDesc desc = { glTF2Importer::glTF2Importer() : BaseImporter(), meshOffsets(), - embeddedTexIdxs(), + mEmbeddedTexIdxs(), mScene(nullptr) { // empty } @@ -111,18 +111,21 @@ const aiImporterDesc *glTF2Importer::GetInfo() const { return &desc; } -bool glTF2Importer::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /* checkSig */) const { - try { - glTF2::Asset asset(pIOHandler); - asset.Load(pFile, GetExtension(pFile) == "glb"); - std::string version = asset.asset.version; - return !version.empty() && version[0] == '2'; - } catch(...) { +bool glTF2Importer::CanRead(const std::string &filename, IOSystem *pIOHandler, bool checkSig ) const { + const std::string extension = GetExtension(filename); + if (!checkSig && (extension != "gltf") && (extension != "glb")) { return false; } + + if (pIOHandler) { + glTF2::Asset asset(pIOHandler); + return asset.CanRead(filename, extension == "glb"); + } + + return false; } -static aiTextureMapMode ConvertWrappingMode(SamplerWrap gltfWrapMode) { +static inline aiTextureMapMode ConvertWrappingMode(SamplerWrap gltfWrapMode) { switch (gltfWrapMode) { case SamplerWrap::Mirrored_Repeat: return aiTextureMapMode_Mirror; @@ -137,21 +140,21 @@ static aiTextureMapMode ConvertWrappingMode(SamplerWrap gltfWrapMode) { } } -inline void SetMaterialColorProperty(Asset & /*r*/, vec4 &prop, aiMaterial *mat, +static inline void SetMaterialColorProperty(Asset & /*r*/, vec4 &prop, aiMaterial *mat, const char *pKey, unsigned int type, unsigned int idx) { aiColor4D col; CopyValue(prop, col); mat->AddProperty(&col, 1, pKey, type, idx); } -inline void SetMaterialColorProperty(Asset & /*r*/, vec3 &prop, aiMaterial *mat, +static inline void SetMaterialColorProperty(Asset & /*r*/, vec3 &prop, aiMaterial *mat, const char *pKey, unsigned int type, unsigned int idx) { aiColor4D col; glTFCommon::CopyValue(prop, col); mat->AddProperty(&col, 1, pKey, type, idx); } -inline void SetMaterialTextureProperty(std::vector &embeddedTexIdxs, Asset & /*r*/, +static void SetMaterialTextureProperty(std::vector &embeddedTexIdxs, Asset & /*r*/, glTF2::TextureInfo prop, aiMaterial *mat, aiTextureType texType, unsigned int texSlot = 0) { if (prop.texture && prop.texture->source) { @@ -368,10 +371,10 @@ void glTF2Importer::ImportMaterials(Asset &r) { mScene->mNumMaterials = numImportedMaterials + 1; mScene->mMaterials = new aiMaterial *[mScene->mNumMaterials]; std::fill(mScene->mMaterials, mScene->mMaterials + mScene->mNumMaterials, nullptr); - mScene->mMaterials[numImportedMaterials] = ImportMaterial(embeddedTexIdxs, r, defaultMaterial); + mScene->mMaterials[numImportedMaterials] = ImportMaterial(mEmbeddedTexIdxs, r, defaultMaterial); for (unsigned int i = 0; i < numImportedMaterials; ++i) { - mScene->mMaterials[i] = ImportMaterial(embeddedTexIdxs, r, r.materials[i]); + mScene->mMaterials[i] = ImportMaterial(mEmbeddedTexIdxs, r, r.materials[i]); } } @@ -799,8 +802,7 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { if (actualNumFaces < nFaces) { ASSIMP_LOG_WARN("Some faces had out-of-range indices. Those faces were dropped."); } - if (actualNumFaces == 0) - { + if (actualNumFaces == 0) { throw DeadlyImportError("Mesh \"", aim->mName.C_Str(), "\" has no faces"); } aim->mNumFaces = actualNumFaces; @@ -840,7 +842,6 @@ void glTF2Importer::ImportCameras(glTF2::Asset &r) { aicam->mLookAt = aiVector3D(0.f, 0.f, -1.f); if (cam.type == Camera::Perspective) { - aicam->mAspect = cam.cameraProperties.perspective.aspectRatio; aicam->mHorizontalFOV = cam.cameraProperties.perspective.yfov * ((aicam->mAspect == 0.f) ? 1.f : aicam->mAspect); aicam->mClipPlaneFar = cam.cameraProperties.perspective.zfar; @@ -859,8 +860,9 @@ void glTF2Importer::ImportCameras(glTF2::Asset &r) { } void glTF2Importer::ImportLights(glTF2::Asset &r) { - if (!r.lights.Size()) + if (!r.lights.Size()) { return; + } const unsigned int numLights = r.lights.Size(); ASSIMP_LOG_DEBUG("Importing ", numLights, " lights"); @@ -1122,8 +1124,8 @@ aiNode *ImportNode(aiScene *pScene, glTF2::Asset &r, std::vector & bone->mNumWeights = static_cast(weights.size()); if (bone->mNumWeights > 0) { - bone->mWeights = new aiVertexWeight[bone->mNumWeights]; - memcpy(bone->mWeights, weights.data(), bone->mNumWeights * sizeof(aiVertexWeight)); + bone->mWeights = new aiVertexWeight[bone->mNumWeights]; + memcpy(bone->mWeights, weights.data(), bone->mNumWeights * sizeof(aiVertexWeight)); } else { // Assimp expects all bones to have at least 1 weight. bone->mWeights = new aiVertexWeight[1]; @@ -1164,8 +1166,7 @@ aiNode *ImportNode(aiScene *pScene, glTF2::Asset &r, std::vector & if (!ainode->mMetaData) { ainode->mMetaData = aiMetadata::Alloc(1); ainode->mMetaData->Set(0, "PBR_LightRange", node.light->range.value); - } - else { + } else { ainode->mMetaData->Add("PBR_LightRange", node.light->range.value); } } @@ -1506,16 +1507,26 @@ void glTF2Importer::ImportAnimations(glTF2::Asset &r) { } } -void glTF2Importer::ImportEmbeddedTextures(glTF2::Asset &r) { - embeddedTexIdxs.resize(r.images.Size(), -1); - - int numEmbeddedTexs = 0; +static unsigned int countEmbeddedTextures(glTF2::Asset &r) { + unsigned int numEmbeddedTexs = 0; for (size_t i = 0; i < r.images.Size(); ++i) { if (r.images[i].HasData()) { numEmbeddedTexs += 1; } } + return numEmbeddedTexs; +} + +void glTF2Importer::ImportEmbeddedTextures(glTF2::Asset &r) { + mEmbeddedTexIdxs.resize(r.images.Size(), -1); + const unsigned int numEmbeddedTexs = countEmbeddedTextures(r); + /* for (size_t i = 0; i < r.images.Size(); ++i) { + if (r.images[i].HasData()) { + numEmbeddedTexs += 1; + } + }*/ + if (numEmbeddedTexs == 0) { return; } @@ -1533,7 +1544,7 @@ void glTF2Importer::ImportEmbeddedTextures(glTF2::Asset &r) { } int idx = mScene->mNumTextures++; - embeddedTexIdxs[i] = idx; + mEmbeddedTexIdxs[i] = idx; aiTexture *tex = mScene->mTextures[idx] = new aiTexture(); @@ -1594,7 +1605,7 @@ void glTF2Importer::InternReadFile(const std::string &pFile, aiScene *pScene, IO // clean all member arrays meshOffsets.clear(); - embeddedTexIdxs.clear(); + mEmbeddedTexIdxs.clear(); this->mScene = pScene; diff --git a/code/AssetLib/glTF2/glTF2Importer.h b/code/AssetLib/glTF2/glTF2Importer.h index 9a05c4dba..831bcd7d2 100644 --- a/code/AssetLib/glTF2/glTF2Importer.h +++ b/code/AssetLib/glTF2/glTF2Importer.h @@ -79,7 +79,7 @@ private: private: std::vector meshOffsets; - std::vector embeddedTexIdxs; + std::vector mEmbeddedTexIdxs; aiScene *mScene; /// An instance of rapidjson::IRemoteSchemaDocumentProvider From 9d0006fecbb164e9cc596a0ea3b95f9b1b8355f1 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 18 Jan 2022 21:32:06 +0100 Subject: [PATCH 10/30] Revert change --- code/AssetLib/B3D/B3DImporter.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/code/AssetLib/B3D/B3DImporter.cpp b/code/AssetLib/B3D/B3DImporter.cpp index 78d26bdcc..6a0c09ee9 100644 --- a/code/AssetLib/B3D/B3DImporter.cpp +++ b/code/AssetLib/B3D/B3DImporter.cpp @@ -89,11 +89,22 @@ void DeleteAllBarePointers(std::vector &x) { } B3DImporter::~B3DImporter() { + // empty } // ------------------------------------------------------------------------------------------------ bool B3DImporter::CanRead(const std::string & /*pFile*/, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const { - return false; + size_t pos = pFile.find_last_of('.'); + if (pos == string::npos) { + return false; + } + + string ext = pFile.substr(pos + 1); + if (ext.size() != 3) { + return false; + } + + return (ext[0] == 'b' || ext[0] == 'B') && (ext[1] == '3') && (ext[2] == 'd' || ext[2] == 'D'); } // ------------------------------------------------------------------------------------------------ From cff213d4ddf2c77bad571e323f333a40085b6a99 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 18 Jan 2022 21:33:05 +0100 Subject: [PATCH 11/30] Revert change --- code/AssetLib/C4D/C4DImporter.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/code/AssetLib/C4D/C4DImporter.cpp b/code/AssetLib/C4D/C4DImporter.cpp index 5107717c3..b14df4300 100644 --- a/code/AssetLib/C4D/C4DImporter.cpp +++ b/code/AssetLib/C4D/C4DImporter.cpp @@ -118,8 +118,12 @@ C4DImporter::~C4DImporter() { // ------------------------------------------------------------------------------------------------ bool C4DImporter::CanRead( const std::string& /*pFile*/, IOSystem* /*pIOHandler*/, bool /*checkSig*/) const { - // TODO - return false; +const std::string& extension = GetExtension(pFile); + if (extension == "c4d") { + return true; + } else if ((!extension.length() || checkSig) && pIOHandler) { + // TODO + } } // ------------------------------------------------------------------------------------------------ From 037732c429f30dd4bfc17b17d1bdc72298b81f35 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 18 Jan 2022 21:34:18 +0100 Subject: [PATCH 12/30] Revert change --- code/AssetLib/Q3BSP/Q3BSPFileImporter.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/code/AssetLib/Q3BSP/Q3BSPFileImporter.cpp b/code/AssetLib/Q3BSP/Q3BSPFileImporter.cpp index f98d1264a..6c1498892 100644 --- a/code/AssetLib/Q3BSP/Q3BSPFileImporter.cpp +++ b/code/AssetLib/Q3BSP/Q3BSPFileImporter.cpp @@ -158,6 +158,9 @@ Q3BSPFileImporter::~Q3BSPFileImporter() { // ------------------------------------------------------------------------------------------------ // Returns true if the loader can read this. bool Q3BSPFileImporter::CanRead(const std::string &/*rFile*/, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const { + if (!checkSig) { + return SimpleExtensionCheck(rFile, "pk3", "bsp"); + } return false; } From 1e80630d5d124f60c59c9605844d3bd60c7fda53 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 18 Jan 2022 21:35:09 +0100 Subject: [PATCH 13/30] Revert change. --- code/AssetLib/Raw/RawLoader.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/AssetLib/Raw/RawLoader.cpp b/code/AssetLib/Raw/RawLoader.cpp index 26b6896b1..b2f56f9e4 100644 --- a/code/AssetLib/Raw/RawLoader.cpp +++ b/code/AssetLib/Raw/RawLoader.cpp @@ -85,7 +85,7 @@ RAWImporter::~RAWImporter() { // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. bool RAWImporter::CanRead(const std::string & /*pFile*/, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const { - return false; + return SimpleExtensionCheck(pFile, "raw"); } // ------------------------------------------------------------------------------------------------ From cf474117427bd2f5b37c433b0569ebfdd9a6343e Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 18 Jan 2022 21:36:02 +0100 Subject: [PATCH 14/30] Revert change --- code/AssetLib/SIB/SIBImporter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/AssetLib/SIB/SIBImporter.cpp b/code/AssetLib/SIB/SIBImporter.cpp index 904e2896a..569e3082a 100644 --- a/code/AssetLib/SIB/SIBImporter.cpp +++ b/code/AssetLib/SIB/SIBImporter.cpp @@ -218,7 +218,7 @@ SIBImporter::~SIBImporter() { // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. bool SIBImporter::CanRead(const std::string & /*pFile*/, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const { - return false; + return SimpleExtensionCheck(pFile, "sib"); } // ------------------------------------------------------------------------------------------------ From 7350baea9306ead00df9f65aa72f1e74af6caf65 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 18 Jan 2022 21:36:55 +0100 Subject: [PATCH 15/30] Revert change --- code/AssetLib/SMD/SMDLoader.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/code/AssetLib/SMD/SMDLoader.cpp b/code/AssetLib/SMD/SMDLoader.cpp index af5b49524..fc674caa0 100644 --- a/code/AssetLib/SMD/SMDLoader.cpp +++ b/code/AssetLib/SMD/SMDLoader.cpp @@ -102,8 +102,7 @@ SMDImporter::~SMDImporter() { // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. bool SMDImporter::CanRead( const std::string& /*pFile*/, IOSystem* /*pIOHandler*/, bool) const { - // fixme: auto format detection - return false; + return SimpleExtensionCheck(pFile,"smd","vta"); } // ------------------------------------------------------------------------------------------------ From 959af1ac4ddc06deeac44a93eeaaf4d20aa8ff3c Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 18 Jan 2022 21:37:49 +0100 Subject: [PATCH 16/30] Revert change --- code/AssetLib/Unreal/UnrealLoader.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/code/AssetLib/Unreal/UnrealLoader.cpp b/code/AssetLib/Unreal/UnrealLoader.cpp index a1e96a058..54ffb8c89 100644 --- a/code/AssetLib/Unreal/UnrealLoader.cpp +++ b/code/AssetLib/Unreal/UnrealLoader.cpp @@ -168,16 +168,20 @@ static const aiImporterDesc desc = { // ------------------------------------------------------------------------------------------------ // Constructor to be privately used by Importer UnrealImporter::UnrealImporter() : - mConfigFrameID(0), mConfigHandleFlags(true) {} + mConfigFrameID(0), mConfigHandleFlags(true) { + // empty +} // ------------------------------------------------------------------------------------------------ // Destructor, private as well -UnrealImporter::~UnrealImporter() {} +UnrealImporter::~UnrealImporter() { + // empty +} // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. bool UnrealImporter::CanRead(const std::string & /*pFile*/, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const { - return false; + return SimpleExtensionCheck(pFile, "3d", "uc"); } // ------------------------------------------------------------------------------------------------ From ea5996307a912668a0b272f0d0af018aef46f1ce Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 18 Jan 2022 22:20:37 +0100 Subject: [PATCH 17/30] Update B3DImporter.cpp --- code/AssetLib/B3D/B3DImporter.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/AssetLib/B3D/B3DImporter.cpp b/code/AssetLib/B3D/B3DImporter.cpp index 6a0c09ee9..c4ef75be3 100644 --- a/code/AssetLib/B3D/B3DImporter.cpp +++ b/code/AssetLib/B3D/B3DImporter.cpp @@ -81,7 +81,7 @@ static const aiImporterDesc desc = { //#define DEBUG_B3D -template +template void DeleteAllBarePointers(std::vector &x) { for (auto p : x) { delete p; @@ -93,7 +93,7 @@ B3DImporter::~B3DImporter() { } // ------------------------------------------------------------------------------------------------ -bool B3DImporter::CanRead(const std::string & /*pFile*/, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const { +bool B3DImporter::CanRead(const std::string &pFile, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const { size_t pos = pFile.find_last_of('.'); if (pos == string::npos) { return false; From c3b91dd766267119e06ee032124a0a4977897838 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 18 Jan 2022 22:21:58 +0100 Subject: [PATCH 18/30] C4D. Fix merge conflicts --- code/AssetLib/C4D/C4DImporter.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/code/AssetLib/C4D/C4DImporter.cpp b/code/AssetLib/C4D/C4DImporter.cpp index b14df4300..06d7a3412 100644 --- a/code/AssetLib/C4D/C4DImporter.cpp +++ b/code/AssetLib/C4D/C4DImporter.cpp @@ -117,13 +117,15 @@ C4DImporter::~C4DImporter() { } // ------------------------------------------------------------------------------------------------ -bool C4DImporter::CanRead( const std::string& /*pFile*/, IOSystem* /*pIOHandler*/, bool /*checkSig*/) const { -const std::string& extension = GetExtension(pFile); +bool C4DImporter::CanRead( const std::string& pFile, IOSystem* /*pIOHandler*/, bool /*checkSig*/) const { + const std::string& extension = GetExtension(pFile); if (extension == "c4d") { return true; } else if ((!extension.length() || checkSig) && pIOHandler) { // TODO } + + return false; } // ------------------------------------------------------------------------------------------------ From 331cb5ac7231d18cbc2a21a10e3725edbecbda32 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 18 Jan 2022 22:23:01 +0100 Subject: [PATCH 19/30] Update M3DImporter.cpp --- code/AssetLib/M3D/M3DImporter.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/code/AssetLib/M3D/M3DImporter.cpp b/code/AssetLib/M3D/M3DImporter.cpp index 02a11c840..895b2bf70 100644 --- a/code/AssetLib/M3D/M3DImporter.cpp +++ b/code/AssetLib/M3D/M3DImporter.cpp @@ -112,10 +112,8 @@ M3DImporter::M3DImporter() : // ------------------------------------------------------------------------------------------------ // Returns true, if file is a binary or ASCII Model 3D file. bool M3DImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const { - /* - * don't use CheckMagicToken because that checks with swapped bytes too, leading to false - * positives. This magic is not uint32_t, but char[4], so memcmp is the best way - */ + // don't use CheckMagicToken because that checks with swapped bytes too, leading to false + // positives. This magic is not uint32_t, but char[4], so memcmp is the best way std::unique_ptr pStream(pIOHandler->Open(pFile, "rb")); unsigned char data[4]; if (4 != pStream->Read(data, 1, 4)) { From ffc13f18ba320d1e2f1e0b249f1d8f468b35cbc5 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 18 Jan 2022 22:24:11 +0100 Subject: [PATCH 20/30] NFF: reenable canRead --- code/AssetLib/NFF/NFFLoader.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/code/AssetLib/NFF/NFFLoader.cpp b/code/AssetLib/NFF/NFFLoader.cpp index a7e8bd616..48271d137 100644 --- a/code/AssetLib/NFF/NFFLoader.cpp +++ b/code/AssetLib/NFF/NFFLoader.cpp @@ -5,8 +5,6 @@ Open Asset Import Library (assimp) Copyright (c) 2006-2022, assimp team - - All rights reserved. Redistribution and use of this software in source and binary forms, @@ -83,8 +81,8 @@ NFFImporter::~NFFImporter() {} // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. -bool NFFImporter::CanRead(const std::string & /*pFile*/, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const { - return false; +bool NFFImporter::CanRead(const std::string & pFile, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const { + return SimpleExtensionCheck(pFile, "nff", "enff"); } // ------------------------------------------------------------------------------------------------ From b4dee9220d1a55028b715e13514448cf00ae9dd4 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 18 Jan 2022 22:25:22 +0100 Subject: [PATCH 21/30] Update Q3BSPFileImporter.cpp --- code/AssetLib/Q3BSP/Q3BSPFileImporter.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/AssetLib/Q3BSP/Q3BSPFileImporter.cpp b/code/AssetLib/Q3BSP/Q3BSPFileImporter.cpp index 6c1498892..5a2f12366 100644 --- a/code/AssetLib/Q3BSP/Q3BSPFileImporter.cpp +++ b/code/AssetLib/Q3BSP/Q3BSPFileImporter.cpp @@ -157,9 +157,9 @@ Q3BSPFileImporter::~Q3BSPFileImporter() { // ------------------------------------------------------------------------------------------------ // Returns true if the loader can read this. -bool Q3BSPFileImporter::CanRead(const std::string &/*rFile*/, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const { +bool Q3BSPFileImporter::CanRead(const std::string &filename, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const { if (!checkSig) { - return SimpleExtensionCheck(rFile, "pk3", "bsp"); + return SimpleExtensionCheck(filename, "pk3", "bsp"); } return false; } From 088658b35947d1cd3fd15b54473dd47bf9468645 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 18 Jan 2022 22:25:43 +0100 Subject: [PATCH 22/30] Update Q3BSPFileImporter.cpp --- code/AssetLib/Q3BSP/Q3BSPFileImporter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/AssetLib/Q3BSP/Q3BSPFileImporter.cpp b/code/AssetLib/Q3BSP/Q3BSPFileImporter.cpp index 5a2f12366..db9a4a02f 100644 --- a/code/AssetLib/Q3BSP/Q3BSPFileImporter.cpp +++ b/code/AssetLib/Q3BSP/Q3BSPFileImporter.cpp @@ -157,7 +157,7 @@ Q3BSPFileImporter::~Q3BSPFileImporter() { // ------------------------------------------------------------------------------------------------ // Returns true if the loader can read this. -bool Q3BSPFileImporter::CanRead(const std::string &filename, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const { +bool Q3BSPFileImporter::CanRead(const std::string &filename, IOSystem * /*pIOHandler*/, bool checkSig) const { if (!checkSig) { return SimpleExtensionCheck(filename, "pk3", "bsp"); } From ec63fe74ee85ef44f2b145813f324c9afc5a264c Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 18 Jan 2022 22:26:16 +0100 Subject: [PATCH 23/30] Update RawLoader.cpp --- code/AssetLib/Raw/RawLoader.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/AssetLib/Raw/RawLoader.cpp b/code/AssetLib/Raw/RawLoader.cpp index b2f56f9e4..bfd2e6d6e 100644 --- a/code/AssetLib/Raw/RawLoader.cpp +++ b/code/AssetLib/Raw/RawLoader.cpp @@ -84,8 +84,8 @@ RAWImporter::~RAWImporter() { // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. -bool RAWImporter::CanRead(const std::string & /*pFile*/, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const { - return SimpleExtensionCheck(pFile, "raw"); +bool RAWImporter::CanRead(const std::string &filename, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const { + return SimpleExtensionCheck(filename, "raw"); } // ------------------------------------------------------------------------------------------------ From 7b24437f0635668186f677512e7ff4c0934dfc8a Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 18 Jan 2022 22:27:19 +0100 Subject: [PATCH 24/30] Update SIBImporter.cpp --- code/AssetLib/SIB/SIBImporter.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/code/AssetLib/SIB/SIBImporter.cpp b/code/AssetLib/SIB/SIBImporter.cpp index 569e3082a..7b66afa44 100644 --- a/code/AssetLib/SIB/SIBImporter.cpp +++ b/code/AssetLib/SIB/SIBImporter.cpp @@ -5,8 +5,6 @@ Open Asset Import Library (assimp) Copyright (c) 2006-2022, assimp team - - All rights reserved. Redistribution and use of this software in source and binary forms, @@ -61,7 +59,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifdef ASSIMP_USE_HUNTER #include #else -//# include "../contrib/ConvertUTF/ConvertUTF.h" #include "../contrib/utf8cpp/source/utf8.h" #endif #include @@ -217,8 +214,8 @@ SIBImporter::~SIBImporter() { // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. -bool SIBImporter::CanRead(const std::string & /*pFile*/, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const { - return SimpleExtensionCheck(pFile, "sib"); +bool SIBImporter::CanRead(const std::string &filename, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const { + return SimpleExtensionCheck(filename, "sib"); } // ------------------------------------------------------------------------------------------------ From 12b01dd5753386984d41566ac1f3cf528f717a94 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 18 Jan 2022 22:28:54 +0100 Subject: [PATCH 25/30] Update SMDLoader.cpp --- code/AssetLib/SMD/SMDLoader.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/code/AssetLib/SMD/SMDLoader.cpp b/code/AssetLib/SMD/SMDLoader.cpp index fc674caa0..46fd968c8 100644 --- a/code/AssetLib/SMD/SMDLoader.cpp +++ b/code/AssetLib/SMD/SMDLoader.cpp @@ -81,15 +81,15 @@ static const aiImporterDesc desc = { // ------------------------------------------------------------------------------------------------ // Constructor to be privately used by Importer -SMDImporter::SMDImporter() -: configFrameID() -, mBuffer() -, pScene( nullptr ) -, iFileSize( 0 ) -, iSmallestFrame( INT_MAX ) -, dLengthOfAnim( 0.0 ) -, bHasUVs(false ) -, iLineNumber((unsigned int)-1) { +SMDImporter::SMDImporter() : + configFrameID(), + mBuffer(), + pScene( nullptr ), + iFileSize( 0 ), + iSmallestFrame( INT_MAX ), + dLengthOfAnim( 0.0 ), + bHasUVs(false ), + iLineNumber((unsigned int)-1) { // empty } @@ -101,8 +101,8 @@ SMDImporter::~SMDImporter() { // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. -bool SMDImporter::CanRead( const std::string& /*pFile*/, IOSystem* /*pIOHandler*/, bool) const { - return SimpleExtensionCheck(pFile,"smd","vta"); +bool SMDImporter::CanRead( const std::string& filename, IOSystem* /*pIOHandler*/, bool) const { + return SimpleExtensionCheck(filename, "smd", "vta"); } // ------------------------------------------------------------------------------------------------ From 4cb76e6a18f9d05c8769e44bb6b1ca8283b4fd9d Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 18 Jan 2022 22:29:55 +0100 Subject: [PATCH 26/30] Update UnrealLoader.cpp --- code/AssetLib/Unreal/UnrealLoader.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/code/AssetLib/Unreal/UnrealLoader.cpp b/code/AssetLib/Unreal/UnrealLoader.cpp index 54ffb8c89..661952b3d 100644 --- a/code/AssetLib/Unreal/UnrealLoader.cpp +++ b/code/AssetLib/Unreal/UnrealLoader.cpp @@ -74,7 +74,7 @@ namespace Unreal { 3 = Masked two-sided 4 = Modulation blended two-sided 8 = Placeholder triangle for weapon positioning (invisible) - */ +*/ enum MeshFlags { MF_NORMAL_OS = 0, MF_NORMAL_TS = 1, @@ -180,8 +180,8 @@ UnrealImporter::~UnrealImporter() { // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. -bool UnrealImporter::CanRead(const std::string & /*pFile*/, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const { - return SimpleExtensionCheck(pFile, "3d", "uc"); +bool UnrealImporter::CanRead(const std::string & filename, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const { + return SimpleExtensionCheck(filename, "3d", "uc"); } // ------------------------------------------------------------------------------------------------ From 17d5633a5b2688681fb9b83fbf47e228264b6924 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Wed, 19 Jan 2022 21:42:14 +0100 Subject: [PATCH 27/30] Add missing SImpleExtensionCheck --- code/AssetLib/glTF2/glTF2Importer.cpp | 6 ------ code/Common/BaseImporter.cpp | 27 +++++++++++++++++++++++++++ include/assimp/BaseImporter.h | 14 ++++++++++++++ 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/code/AssetLib/glTF2/glTF2Importer.cpp b/code/AssetLib/glTF2/glTF2Importer.cpp index 4e78d0e17..1f4cafdda 100644 --- a/code/AssetLib/glTF2/glTF2Importer.cpp +++ b/code/AssetLib/glTF2/glTF2Importer.cpp @@ -1521,12 +1521,6 @@ static unsigned int countEmbeddedTextures(glTF2::Asset &r) { void glTF2Importer::ImportEmbeddedTextures(glTF2::Asset &r) { mEmbeddedTexIdxs.resize(r.images.Size(), -1); const unsigned int numEmbeddedTexs = countEmbeddedTextures(r); - /* for (size_t i = 0; i < r.images.Size(); ++i) { - if (r.images[i].HasData()) { - numEmbeddedTexs += 1; - } - }*/ - if (numEmbeddedTexs == 0) { return; } diff --git a/code/Common/BaseImporter.cpp b/code/Common/BaseImporter.cpp index e400f5c29..383300ef1 100644 --- a/code/Common/BaseImporter.cpp +++ b/code/Common/BaseImporter.cpp @@ -225,6 +225,32 @@ void BaseImporter::GetExtensionList(std::set &extensions) { return false; } +// ------------------------------------------------------------------------------------------------ +// Simple check for file extension +/*static*/ bool BaseImporter::SimpleExtensionCheck(const std::string &pFile, + const char *ext0, + const char *ext1, + const char *ext2) { + std::string::size_type pos = pFile.find_last_of('.'); + + // no file extension - can't read + if (pos == std::string::npos) + return false; + + const char *ext_real = &pFile[pos + 1]; + if (!ASSIMP_stricmp(ext_real, ext0)) + return true; + + // check for other, optional, file extensions + if (ext1 && !ASSIMP_stricmp(ext_real, ext1)) + return true; + + if (ext2 && !ASSIMP_stricmp(ext_real, ext2)) + return true; + + return false; +} + // ------------------------------------------------------------------------------------------------ // Get file extension from path std::string BaseImporter::GetExtension(const std::string &file) { @@ -242,6 +268,7 @@ std::string BaseImporter::GetExtension(const std::string &file) { return ret; } + // ------------------------------------------------------------------------------------------------ // Check for magic bytes at the beginning of the file. /* static */ bool BaseImporter::CheckMagicToken(IOSystem *pIOHandler, const std::string &pFile, diff --git a/include/assimp/BaseImporter.h b/include/assimp/BaseImporter.h index e7aafef23..5a3c764d4 100644 --- a/include/assimp/BaseImporter.h +++ b/include/assimp/BaseImporter.h @@ -260,6 +260,20 @@ public: // static utilities bool tokensSol = false, bool noAlphaBeforeTokens = false); + // ------------------------------------------------------------------- + /** @brief Check whether a file has a specific file extension + * @param pFile Input file + * @param ext0 Extension to check for. Lowercase characters only, no dot! + * @param ext1 Optional second extension + * @param ext2 Optional third extension + * @note Case-insensitive + */ + static bool SimpleExtensionCheck( + const std::string &pFile, + const char *ext0, + const char *ext1 = nullptr, + const char *ext2 = nullptr); + // ------------------------------------------------------------------- /** @brief Extract file extension from a string * @param pFile Input file From 15b08f643aaa72267a590ce5b98a64b2b15e2089 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Wed, 19 Jan 2022 21:55:24 +0100 Subject: [PATCH 28/30] Fix warning: unused variable --- code/AssetLib/Blender/BlenderLoader.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/code/AssetLib/Blender/BlenderLoader.cpp b/code/AssetLib/Blender/BlenderLoader.cpp index 42d25ebac..f1450168b 100644 --- a/code/AssetLib/Blender/BlenderLoader.cpp +++ b/code/AssetLib/Blender/BlenderLoader.cpp @@ -120,8 +120,6 @@ static const char * const TokensForSearch[] = { "blender" }; // Returns whether the class can handle the format of the given file. bool BlenderImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const { // note: this won't catch compressed files - static const char *tokens[] = { " Date: Wed, 19 Jan 2022 22:00:53 +0100 Subject: [PATCH 29/30] Fix compile error: missing var --- code/AssetLib/Blender/BlenderLoader.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/code/AssetLib/Blender/BlenderLoader.cpp b/code/AssetLib/Blender/BlenderLoader.cpp index f1450168b..bfc44aea2 100644 --- a/code/AssetLib/Blender/BlenderLoader.cpp +++ b/code/AssetLib/Blender/BlenderLoader.cpp @@ -114,12 +114,14 @@ BlenderImporter::~BlenderImporter() { } static const char * const Tokens[] = { "BLENDER" }; -static const char * const TokensForSearch[] = { "blender" }; +//static const char * const TokensForSearch[] = { "blender" }; // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. bool BlenderImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const { // note: this won't catch compressed files + static const char *tokens[] = { " Date: Wed, 19 Jan 2022 22:03:11 +0100 Subject: [PATCH 30/30] Fix compile error: missing var --- code/AssetLib/Blender/BlenderLoader.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/code/AssetLib/Blender/BlenderLoader.cpp b/code/AssetLib/Blender/BlenderLoader.cpp index bfc44aea2..3d9e8017b 100644 --- a/code/AssetLib/Blender/BlenderLoader.cpp +++ b/code/AssetLib/Blender/BlenderLoader.cpp @@ -114,7 +114,6 @@ BlenderImporter::~BlenderImporter() { } static const char * const Tokens[] = { "BLENDER" }; -//static const char * const TokensForSearch[] = { "blender" }; // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file.