123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- /*
- ---------------------------------------------------------------------------
- Open Asset Import Library (ASSIMP)
- ---------------------------------------------------------------------------
- Copyright (c) 2006-2008, ASSIMP Development Team
- 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 Development 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.
- ---------------------------------------------------------------------------
- */
- /** @file Implementation of the Plain-C API */
- #include "AssimpPCH.h"
- #include "../include/assimp.h"
- // public ASSIMP headers
- #include "../include/aiFileIO.h"
- #include "GenericProperty.h"
- #if (defined AI_C_THREADSAFE)
- # include <boost/thread/thread.hpp>
- # include <boost/thread/mutex.hpp>
- #endif
- using namespace Assimp;
- /** Stores the importer objects for all active import processes */
- typedef std::map< const aiScene*, Assimp::Importer* > ImporterMap;
- /** Local storage of all active import processes */
- static ImporterMap gActiveImports;
- /** Error message of the last failed import process */
- static std::string gLastErrorString;
- /** Configuration properties */
- static ImporterPimpl::IntPropertyMap gIntProperties;
- static ImporterPimpl::FloatPropertyMap gFloatProperties;
- static ImporterPimpl::StringPropertyMap gStringProperties;
- #if (defined AI_C_THREADSAFE)
- /** Global mutex to manage the access to the importer map */
- static boost::mutex gMutex;
- #endif
- class CIOSystemWrapper;
- class CIOStreamWrapper;
- // ------------------------------------------------------------------------------------------------
- // Custom IOStream implementation for the C-API
- class CIOStreamWrapper : public IOStream
- {
- friend class CIOSystemWrapper;
- public:
- CIOStreamWrapper(aiFile* pFile)
- : mFile(pFile)
- {}
- // -------------------------------------------------------------------
- size_t Read(void* pvBuffer,
- size_t pSize,
- size_t pCount)
- {
- // need to typecast here as C has no void*
- return mFile->ReadProc(mFile,(char*)pvBuffer,pSize,pCount);
- }
- // -------------------------------------------------------------------
- size_t Write(const void* pvBuffer,
- size_t pSize,
- size_t pCount)
- {
- // need to typecast here as C has no void*
- return mFile->WriteProc(mFile,(const char*)pvBuffer,pSize,pCount);
- }
- // -------------------------------------------------------------------
- aiReturn Seek(size_t pOffset,
- aiOrigin pOrigin)
- {
- return mFile->SeekProc(mFile,pOffset,pOrigin);
- }
- // -------------------------------------------------------------------
- size_t Tell(void) const
- {
- return mFile->TellProc(mFile);
- }
- // -------------------------------------------------------------------
- size_t FileSize() const
- {
- return mFile->FileSizeProc(mFile);
- }
- // -------------------------------------------------------------------
- void Flush ()
- {
- return mFile->FlushProc(mFile);
- }
- private:
- aiFile* mFile;
- };
- // ------------------------------------------------------------------------------------------------
- // Custom IOStream implementation for the C-API
- class CIOSystemWrapper : public IOSystem
- {
- public:
- CIOSystemWrapper(aiFileIO* pFile)
- : mFileSystem(pFile)
- {}
- // -------------------------------------------------------------------
- bool Exists( const char* pFile) const
- {
- CIOSystemWrapper* pip = const_cast<CIOSystemWrapper*>(this);
- IOStream* p = pip->Open(pFile);
- if (p){
- pip->Close(p);
- return true;
- }
- return false;
- }
- // -------------------------------------------------------------------
- char getOsSeparator() const
- {
- // FIXME
- return '/';
- }
- // -------------------------------------------------------------------
- IOStream* Open(const char* pFile,const char* pMode = "rb")
- {
- aiFile* p = mFileSystem->OpenProc(mFileSystem,pFile,pMode);
- if (!p)return NULL;
- return new CIOStreamWrapper(p);
- }
- // -------------------------------------------------------------------
- void Close( IOStream* pFile)
- {
- if (!pFile)return;
- mFileSystem->CloseProc(mFileSystem,((CIOStreamWrapper*) pFile)->mFile);
- delete pFile;
- }
- private:
- aiFileIO* mFileSystem;
- };
- // ------------------------------------------------------------------------------------------------
- void ReportSceneNotFoundError()
- {
- DefaultLogger::get()->error("Unable to find the Importer instance for this scene. "
- "Are you sure it has been created by aiImportFile(ex)(...)?");
- }
- // ------------------------------------------------------------------------------------------------
- // Reads the given file and returns its content.
- const aiScene* aiImportFile( const char* pFile, unsigned int pFlags)
- {
- return aiImportFileEx(pFile,pFlags,NULL);
- }
- // ------------------------------------------------------------------------------------------------
- const aiScene* aiImportFileEx( const char* pFile, unsigned int pFlags,
- aiFileIO* pFS)
- {
- ai_assert(NULL != pFile);
- // create an Importer for this file
- Assimp::Importer* imp = new Assimp::Importer;
- // copy the global property lists to the Importer instance
- // (we are a friend of Importer)
- imp->pimpl->mIntProperties = gIntProperties;
- imp->pimpl->mFloatProperties = gFloatProperties;
- imp->pimpl->mStringProperties = gStringProperties;
- // setup a custom IO system if necessary
- if (pFS)
- {
- imp->SetIOHandler( new CIOSystemWrapper (pFS) );
- }
- // and have it read the file
- const aiScene* scene = imp->ReadFile( pFile, pFlags);
- // if succeeded, place it in the collection of active processes
- if( scene)
- {
- #if (defined AI_C_THREADSAFE)
- boost::mutex::scoped_lock lock(gMutex);
- #endif
- gActiveImports[scene] = imp;
- }
- else
- {
- // if failed, extract error code and destroy the import
- gLastErrorString = imp->GetErrorString();
- delete imp;
- }
- // return imported data. If the import failed the pointer is NULL anyways
- return scene;
- }
- // ------------------------------------------------------------------------------------------------
- // Releases all resources associated with the given import process.
- void aiReleaseImport( const aiScene* pScene)
- {
- if (!pScene)return;
- // lock the mutex
- #if (defined AI_C_THREADSAFE)
- boost::mutex::scoped_lock lock(gMutex);
- #endif
- // find the importer associated with this data
- ImporterMap::iterator it = gActiveImports.find( pScene);
- // it should be there... else the user is playing fools with us
- if( it == gActiveImports.end())
- {
- ReportSceneNotFoundError();
- return;
- }
- // kill the importer, the data dies with it
- delete it->second;
- gActiveImports.erase( it);
- }
- // ------------------------------------------------------------------------------------------------
- // Returns the error text of the last failed import process.
- const char* aiGetErrorString()
- {
- return gLastErrorString.c_str();
- }
- // ------------------------------------------------------------------------------------------------
- // Returns the error text of the last failed import process.
- int aiIsExtensionSupported(const char* szExtension)
- {
- ai_assert(NULL != szExtension);
- // lock the mutex
- #if (defined AI_C_THREADSAFE)
- boost::mutex::scoped_lock lock(gMutex);
- #endif
- if (!gActiveImports.empty()) {
- return (int)((*(gActiveImports.begin())).second->IsExtensionSupported( szExtension ));
- }
- // need to create a temporary Importer instance.
- // TODO: Find a better solution ...
- Assimp::Importer* pcTemp = new Assimp::Importer();
- int i = (int)pcTemp->IsExtensionSupported(std::string(szExtension));
- delete pcTemp;
- return i;
- }
- // ------------------------------------------------------------------------------------------------
- // Get a list of all file extensions supported by ASSIMP
- void aiGetExtensionList(aiString* szOut)
- {
- ai_assert(NULL != szOut);
- // lock the mutex
- #if (defined AI_C_THREADSAFE)
- boost::mutex::scoped_lock lock(gMutex);
- #endif
- if (!gActiveImports.empty())
- {
- (*(gActiveImports.begin())).second->GetExtensionList(*szOut);
- return;
- }
- // need to create a temporary Importer instance.
- // TODO: Find a better solution ...
- Assimp::Importer* pcTemp = new Assimp::Importer();
- pcTemp->GetExtensionList(*szOut);
- delete pcTemp;
- }
- // ------------------------------------------------------------------------------------------------
- void aiGetMemoryRequirements(const C_STRUCT aiScene* pIn,
- C_STRUCT aiMemoryInfo* in)
- {
- // lock the mutex
- #if (defined AI_C_THREADSAFE)
- boost::mutex::scoped_lock lock(gMutex);
- #endif
- // find the importer associated with this data
- ImporterMap::iterator it = gActiveImports.find( pIn);
- // it should be there... else the user is playing fools with us
- if( it == gActiveImports.end())
- {
- ReportSceneNotFoundError();
- return;
- }
- // get memory statistics
- it->second->GetMemoryRequirements(*in);
- }
- // ------------------------------------------------------------------------------------------------
- ASSIMP_API void aiSetImportPropertyInteger(const char* szName, int value)
- {
- SetGenericProperty<int>(gIntProperties,szName,value,NULL);
- }
- // ------------------------------------------------------------------------------------------------
- ASSIMP_API void aiSetImportPropertyFloat(const char* szName, float value)
- {
- SetGenericProperty<float>(gFloatProperties,szName,value,NULL);
- }
- // ------------------------------------------------------------------------------------------------
- ASSIMP_API void aiSetImportPropertyString(const char* szName,
- const C_STRUCT aiString* st)
- {
- if (!st)return;
- SetGenericProperty<std::string>(gStringProperties,szName,
- std::string( st->data ),NULL);
- }
|