1/*
2Open Asset Import Library (assimp)
3----------------------------------------------------------------------
4
5Copyright (c) 2006-2017, assimp team
6
7All rights reserved.
8
9Redistribution and use of this software in source and binary forms,
10with or without modification, are permitted provided that the
11following conditions are met:
12
13* Redistributions of source code must retain the above
14 copyright notice, this list of conditions and the
15 following disclaimer.
16
17* Redistributions in binary form must reproduce the above
18 copyright notice, this list of conditions and the
19 following disclaimer in the documentation and/or other
20 materials provided with the distribution.
21
22* Neither the name of the assimp team, nor the names of its
23 contributors may be used to endorse or promote products
24 derived from this software without specific prior
25 written permission of the assimp team.
26
27THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39----------------------------------------------------------------------
40*/
41
42#ifndef ASSIMP_BUILD_NO_OGRE_IMPORTER
43
44#include "OgreImporter.h"
45#include "OgreBinarySerializer.h"
46#include "OgreXmlSerializer.h"
47#include <assimp/Importer.hpp>
48#include <assimp/importerdesc.h>
49#include <memory>
50
51static const aiImporterDesc desc = {
52 "Ogre3D Mesh Importer",
53 "",
54 "",
55 "",
56 aiImporterFlags_SupportTextFlavour | aiImporterFlags_SupportBinaryFlavour,
57 0,
58 0,
59 0,
60 0,
61 "mesh mesh.xml"
62};
63
64namespace Assimp
65{
66namespace Ogre
67{
68
69const aiImporterDesc* OgreImporter::GetInfo() const
70{
71 return &desc;
72}
73
74void OgreImporter::SetupProperties(const Importer* pImp)
75{
76 m_userDefinedMaterialLibFile = pImp->GetPropertyString(AI_CONFIG_IMPORT_OGRE_MATERIAL_FILE, "Scene.material");
77 m_detectTextureTypeFromFilename = pImp->GetPropertyBool(AI_CONFIG_IMPORT_OGRE_TEXTURETYPE_FROM_FILENAME, false);
78}
79
80bool OgreImporter::CanRead(const std::string &pFile, Assimp::IOSystem *pIOHandler, bool checkSig) const
81{
82 if (!checkSig) {
83 return EndsWith(pFile, ".mesh.xml", false) || EndsWith(pFile, ".mesh", false);
84 }
85
86 if (EndsWith(pFile, ".mesh.xml", false))
87 {
88 const char* tokens[] = { "<mesh>" };
89 return SearchFileHeaderForToken(pIOHandler, pFile, tokens, 1);
90 }
91 else
92 {
93 /// @todo Read and validate first header chunk?
94 return EndsWith(pFile, ".mesh", false);
95 }
96}
97
98void OgreImporter::InternReadFile(const std::string &pFile, aiScene *pScene, Assimp::IOSystem *pIOHandler)
99{
100 // Open source file
101 IOStream *f = pIOHandler->Open(pFile, "rb");
102 if (!f) {
103 throw DeadlyImportError("Failed to open file " + pFile);
104 }
105
106 // Binary .mesh import
107 if (EndsWith(pFile, ".mesh", false))
108 {
109 /// @note MemoryStreamReader takes ownership of f.
110 MemoryStreamReader reader(f);
111
112 // Import mesh
113 std::unique_ptr<Mesh> mesh(OgreBinarySerializer::ImportMesh(&reader));
114
115 // Import skeleton
116 OgreBinarySerializer::ImportSkeleton(pIOHandler, mesh.get());
117
118 // Import mesh referenced materials
119 ReadMaterials(pFile, pIOHandler, pScene, mesh.get());
120
121 // Convert to Assimp
122 mesh->ConvertToAssimpScene(pScene);
123 }
124 // XML .mesh.xml import
125 else
126 {
127 /// @note XmlReader does not take ownership of f, hence the scoped ptr.
128 std::unique_ptr<IOStream> scopedFile(f);
129 std::unique_ptr<CIrrXML_IOStreamReader> xmlStream(new CIrrXML_IOStreamReader(scopedFile.get()));
130 std::unique_ptr<XmlReader> reader(irr::io::createIrrXMLReader(xmlStream.get()));
131
132 // Import mesh
133 std::unique_ptr<MeshXml> mesh(OgreXmlSerializer::ImportMesh(reader.get()));
134
135 // Import skeleton
136 OgreXmlSerializer::ImportSkeleton(pIOHandler, mesh.get());
137
138 // Import mesh referenced materials
139 ReadMaterials(pFile, pIOHandler, pScene, mesh.get());
140
141 // Convert to Assimp
142 mesh->ConvertToAssimpScene(pScene);
143 }
144}
145
146} // Ogre
147} // Assimp
148
149#endif // ASSIMP_BUILD_NO_OGRE_IMPORTER
150