2016-05-07 12:16:33 +00:00
|
|
|
/*
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2019-01-30 08:41:39 +00:00
|
|
|
Copyright (c) 2006-2019, assimp team
|
2018-01-28 18:42:05 +00:00
|
|
|
|
2017-05-09 17:57:36 +00:00
|
|
|
|
2016-05-07 12:16:33 +00:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the
|
|
|
|
following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of the assimp team, nor the names of its
|
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
|
|
|
written permission of the assimp team.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2017-01-10 14:53:11 +00:00
|
|
|
#ifndef ASSIMP_BUILD_NO_3MF_IMPORTER
|
2016-05-07 12:16:33 +00:00
|
|
|
|
|
|
|
#include "D3MFOpcPackage.h"
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/Exceptional.h>
|
2016-06-06 20:04:29 +00:00
|
|
|
|
|
|
|
#include <assimp/IOStream.hpp>
|
|
|
|
#include <assimp/IOSystem.hpp>
|
|
|
|
#include <assimp/DefaultLogger.hpp>
|
|
|
|
#include <assimp/ai_assert.h>
|
2019-07-11 11:22:43 +00:00
|
|
|
#include <assimp/ZipArchiveIOSystem.h>
|
2016-05-07 12:16:33 +00:00
|
|
|
|
2017-08-02 11:15:07 +00:00
|
|
|
#include <cstdlib>
|
2016-05-07 12:16:33 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
2017-11-21 20:34:25 +00:00
|
|
|
#include "3MFXmlTags.h"
|
2016-10-10 15:58:06 +00:00
|
|
|
|
2016-05-07 12:16:33 +00:00
|
|
|
namespace Assimp {
|
|
|
|
|
|
|
|
namespace D3MF {
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
typedef std::shared_ptr<OpcPackageRelationship> OpcPackageRelationshipPtr;
|
|
|
|
|
2017-11-21 17:41:16 +00:00
|
|
|
class OpcPackageRelationshipReader {
|
2016-05-07 12:16:33 +00:00
|
|
|
public:
|
2017-11-21 17:41:16 +00:00
|
|
|
OpcPackageRelationshipReader(XmlReader* xmlReader) {
|
|
|
|
while(xmlReader->read()) {
|
2016-05-07 12:16:33 +00:00
|
|
|
if(xmlReader->getNodeType() == irr::io::EXN_ELEMENT &&
|
|
|
|
xmlReader->getNodeName() == XmlTag::RELS_RELATIONSHIP_CONTAINER)
|
|
|
|
{
|
|
|
|
ParseRootNode(xmlReader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-10 07:27:29 +00:00
|
|
|
|
2016-05-07 12:16:33 +00:00
|
|
|
void ParseRootNode(XmlReader* xmlReader)
|
|
|
|
{
|
|
|
|
ParseAttributes(xmlReader);
|
|
|
|
|
|
|
|
while(xmlReader->read())
|
|
|
|
{
|
|
|
|
if(xmlReader->getNodeType() == irr::io::EXN_ELEMENT &&
|
|
|
|
xmlReader->getNodeName() == XmlTag::RELS_RELATIONSHIP_NODE)
|
|
|
|
{
|
|
|
|
ParseChildNode(xmlReader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 20:34:25 +00:00
|
|
|
void ParseAttributes(XmlReader*) {
|
2017-11-23 21:47:18 +00:00
|
|
|
// empty
|
2016-05-07 12:16:33 +00:00
|
|
|
}
|
2016-09-10 07:27:29 +00:00
|
|
|
|
2017-12-06 21:15:34 +00:00
|
|
|
bool validateRels( OpcPackageRelationshipPtr &relPtr ) {
|
|
|
|
if ( relPtr->id.empty() || relPtr->type.empty() || relPtr->target.empty() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-21 20:34:25 +00:00
|
|
|
void ParseChildNode(XmlReader* xmlReader) {
|
2016-05-07 13:36:05 +00:00
|
|
|
OpcPackageRelationshipPtr relPtr(new OpcPackageRelationship());
|
|
|
|
|
2017-12-06 20:41:48 +00:00
|
|
|
relPtr->id = xmlReader->getAttributeValueSafe(XmlTag::RELS_ATTRIB_ID.c_str());
|
|
|
|
relPtr->type = xmlReader->getAttributeValueSafe(XmlTag::RELS_ATTRIB_TYPE.c_str());
|
|
|
|
relPtr->target = xmlReader->getAttributeValueSafe(XmlTag::RELS_ATTRIB_TARGET.c_str());
|
2017-12-06 21:15:34 +00:00
|
|
|
if ( validateRels( relPtr ) ) {
|
|
|
|
m_relationShips.push_back( relPtr );
|
|
|
|
}
|
2016-05-07 12:16:33 +00:00
|
|
|
}
|
2017-11-21 20:34:25 +00:00
|
|
|
|
2016-05-07 12:16:33 +00:00
|
|
|
std::vector<OpcPackageRelationshipPtr> m_relationShips;
|
|
|
|
};
|
|
|
|
|
2018-03-09 10:40:45 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-07 12:16:33 +00:00
|
|
|
D3MFOpcPackage::D3MFOpcPackage(IOSystem* pIOHandler, const std::string& rFile)
|
2017-11-21 20:34:25 +00:00
|
|
|
: mRootStream(nullptr)
|
|
|
|
, mZipArchive() {
|
2019-07-11 11:22:43 +00:00
|
|
|
mZipArchive.reset( new ZipArchiveIOSystem( pIOHandler, rFile ) );
|
2017-11-21 20:34:25 +00:00
|
|
|
if(!mZipArchive->isOpen()) {
|
2016-05-07 12:16:33 +00:00
|
|
|
throw DeadlyImportError("Failed to open file " + rFile+ ".");
|
2016-09-10 07:27:29 +00:00
|
|
|
}
|
2016-05-07 12:16:33 +00:00
|
|
|
|
|
|
|
std::vector<std::string> fileList;
|
2017-11-21 20:34:25 +00:00
|
|
|
mZipArchive->getFileList(fileList);
|
2016-05-07 12:16:33 +00:00
|
|
|
|
2017-11-21 20:34:25 +00:00
|
|
|
for (auto& file: fileList) {
|
2016-09-10 07:27:29 +00:00
|
|
|
if(file == D3MF::XmlTag::ROOT_RELATIONSHIPS_ARCHIVE) {
|
2016-05-07 12:16:33 +00:00
|
|
|
//PkgRelationshipReader pkgRelReader(file, archive);
|
2017-11-21 20:34:25 +00:00
|
|
|
ai_assert(mZipArchive->Exists(file.c_str()));
|
2016-05-07 12:16:33 +00:00
|
|
|
|
2017-11-21 20:34:25 +00:00
|
|
|
IOStream *fileStream = mZipArchive->Open(file.c_str());
|
2016-05-07 12:16:33 +00:00
|
|
|
|
|
|
|
ai_assert(fileStream != nullptr);
|
|
|
|
|
|
|
|
std::string rootFile = ReadPackageRootRelationship(fileStream);
|
2017-11-21 20:34:25 +00:00
|
|
|
if ( rootFile.size() > 0 && rootFile[ 0 ] == '/' ) {
|
|
|
|
rootFile = rootFile.substr( 1 );
|
2017-12-06 21:15:34 +00:00
|
|
|
if ( rootFile[ 0 ] == '/' ) {
|
2018-03-09 10:40:45 +00:00
|
|
|
// deal with zip-bug
|
2017-12-06 21:15:34 +00:00
|
|
|
rootFile = rootFile.substr( 1 );
|
|
|
|
}
|
2017-11-21 20:34:25 +00:00
|
|
|
}
|
2016-05-07 12:16:33 +00:00
|
|
|
|
2018-04-19 14:48:43 +00:00
|
|
|
ASSIMP_LOG_DEBUG(rootFile);
|
2016-05-07 12:16:33 +00:00
|
|
|
|
2019-07-25 08:54:39 +00:00
|
|
|
mZipArchive->Close(fileStream);
|
|
|
|
|
2017-11-21 20:34:25 +00:00
|
|
|
mRootStream = mZipArchive->Open(rootFile.c_str());
|
2017-12-06 21:15:34 +00:00
|
|
|
ai_assert( mRootStream != nullptr );
|
|
|
|
if ( nullptr == mRootStream ) {
|
2018-03-09 10:40:45 +00:00
|
|
|
throw DeadlyExportError( "Cannot open root-file in archive : " + rootFile );
|
2017-12-06 21:15:34 +00:00
|
|
|
}
|
2016-05-07 12:16:33 +00:00
|
|
|
|
2017-11-21 20:34:25 +00:00
|
|
|
} else if( file == D3MF::XmlTag::CONTENT_TYPES_ARCHIVE) {
|
2019-03-30 18:12:13 +00:00
|
|
|
ASSIMP_LOG_WARN_F("Ignored file of unsupported type CONTENT_TYPES_ARCHIVES",file);
|
|
|
|
} else {
|
|
|
|
ASSIMP_LOG_WARN_F("Ignored file of unknown type: ",file);
|
2016-05-07 12:16:33 +00:00
|
|
|
}
|
2019-03-30 18:12:13 +00:00
|
|
|
|
2016-05-07 12:16:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 20:34:25 +00:00
|
|
|
D3MFOpcPackage::~D3MFOpcPackage() {
|
2019-07-25 08:54:39 +00:00
|
|
|
mZipArchive->Close(mRootStream);
|
2016-05-07 12:16:33 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 20:34:25 +00:00
|
|
|
IOStream* D3MFOpcPackage::RootStream() const {
|
|
|
|
return mRootStream;
|
2016-05-07 12:16:33 +00:00
|
|
|
}
|
|
|
|
|
2018-03-09 10:40:45 +00:00
|
|
|
static const std::string ModelRef = "3D/3dmodel.model";
|
|
|
|
|
|
|
|
bool D3MFOpcPackage::validate() {
|
|
|
|
if ( nullptr == mRootStream || nullptr == mZipArchive ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mZipArchive->Exists( ModelRef.c_str() );
|
|
|
|
}
|
|
|
|
|
2017-11-21 20:34:25 +00:00
|
|
|
std::string D3MFOpcPackage::ReadPackageRootRelationship(IOStream* stream) {
|
2016-05-07 12:16:33 +00:00
|
|
|
std::unique_ptr<CIrrXML_IOStreamReader> xmlStream(new CIrrXML_IOStreamReader(stream));
|
|
|
|
std::unique_ptr<XmlReader> xml(irr::io::createIrrXMLReader(xmlStream.get()));
|
|
|
|
|
|
|
|
OpcPackageRelationshipReader reader(xml.get());
|
|
|
|
|
|
|
|
auto itr = std::find_if(reader.m_relationShips.begin(), reader.m_relationShips.end(), [](const OpcPackageRelationshipPtr& rel){
|
|
|
|
return rel->type == XmlTag::PACKAGE_START_PART_RELATIONSHIP_TYPE;
|
|
|
|
});
|
|
|
|
|
2018-03-09 10:40:45 +00:00
|
|
|
if ( itr == reader.m_relationShips.end() ) {
|
|
|
|
throw DeadlyImportError( "Cannot find " + XmlTag::PACKAGE_START_PART_RELATIONSHIP_TYPE );
|
|
|
|
}
|
2016-05-07 12:16:33 +00:00
|
|
|
|
|
|
|
return (*itr)->target;
|
|
|
|
}
|
|
|
|
|
2017-11-21 20:34:25 +00:00
|
|
|
} // Namespace D3MF
|
|
|
|
} // Namespace Assimp
|
2016-05-07 12:16:33 +00:00
|
|
|
|
|
|
|
#endif //ASSIMP_BUILD_NO_3MF_IMPORTER
|