TuttleOFX
1
|
00001 #include "Core.hpp" 00002 00003 #include <tuttle/host/ofx/OfxhImageEffectPlugin.hpp> 00004 #include <tuttle/host/memory/MemoryPool.hpp> 00005 #include <tuttle/host/memory/MemoryCache.hpp> 00006 00007 #include <tuttle/common/system/system.hpp> 00008 00009 #include <boost/archive/xml_oarchive.hpp> 00010 #include <boost/archive/binary_oarchive.hpp> 00011 #include <boost/archive/text_oarchive.hpp> 00012 #include <boost/archive/xml_iarchive.hpp> 00013 #include <boost/archive/binary_iarchive.hpp> 00014 #include <boost/archive/text_iarchive.hpp> 00015 00016 #include <boost/serialization/serialization.hpp> 00017 #include <boost/serialization/nvp.hpp> 00018 00019 #include <boost/uuid/uuid.hpp> 00020 #include <boost/uuid/uuid_io.hpp> 00021 #include <boost/uuid/uuid_generators.hpp> 00022 00023 #include <boost/filesystem/operations.hpp> 00024 #include <boost/filesystem/convenience.hpp> 00025 00026 #ifdef TUTTLE_HOST_WITH_PYTHON_EXPRESSION 00027 #include <boost/python.hpp> 00028 #endif 00029 00030 #include <stdexcept> 00031 #include <iostream> 00032 #include <fstream> 00033 #include <vector> 00034 #include <cstring> // memset 00035 00036 namespace tuttle { 00037 namespace host { 00038 00039 namespace { 00040 memory::MemoryPool pool; 00041 memory::MemoryCache cache; 00042 } 00043 00044 Core::Core() 00045 : _imageEffectPluginCache( _host ) 00046 , _memoryPool( pool ) 00047 , _memoryCache( cache ) 00048 , _isPreloaded( false ) 00049 { 00050 #ifdef TUTTLE_HOST_WITH_PYTHON_EXPRESSION 00051 Py_Initialize( ); 00052 #endif 00053 _pluginCache.setCacheVersion( "tuttleV1" ); 00054 00055 // register the image effect cache with the global plugin cache 00056 _pluginCache.registerAPICache( _imageEffectPluginCache ); 00057 00058 _memoryPool.updateMemoryAuthorizedWithRAM(); 00059 // preload(); 00060 } 00061 00062 Core::~Core() 00063 {} 00064 00065 void Core::preload( const bool useCache ) 00066 { 00067 if( _isPreloaded ) 00068 return; 00069 00070 _isPreloaded = true; 00071 00072 #ifndef __WINDOWS__ 00073 // typedef boost::archive::binary_oarchive OArchive; 00074 // typedef boost::archive::binary_iarchive IArchive; 00075 // typedef boost::archive::text_oarchive OArchive; 00076 // typedef boost::archive::text_iarchive IArchive; 00077 typedef boost::archive::xml_oarchive OArchive; 00078 typedef boost::archive::xml_iarchive IArchive; 00079 00080 std::string cacheFile; 00081 if( useCache ) 00082 { 00083 cacheFile = (getPreferences().getTuttleHomePath() / "tuttlePluginCacheSerialize.xml").string(); 00084 00085 TUTTLE_LOG_DEBUG( TUTTLE_INFO, "plugin cache file = " << cacheFile ); 00086 00087 try 00088 { 00089 std::ifstream ifsb( cacheFile.c_str(), std::ios::in ); 00090 if( ifsb.is_open() ) 00091 { 00092 TUTTLE_LOG_DEBUG( TUTTLE_INFO, "Read plugins cache." ); 00093 IArchive iArchive( ifsb ); 00094 iArchive >> BOOST_SERIALIZATION_NVP( _pluginCache ); 00095 ifsb.close(); 00096 } 00097 } 00098 catch( std::exception& e ) 00099 { 00100 TUTTLE_LOG_ERROR( "Exception when reading cache file (" << e.what() << ")." ); 00101 } 00102 } 00103 #endif 00104 _pluginCache.scanPluginFiles(); 00105 #ifndef __WINDOWS__ 00106 if( useCache && _pluginCache.isDirty() ) 00107 { 00108 // generate unique name for writing 00109 boost::uuids::random_generator gen; 00110 boost::uuids::uuid u = gen(); 00111 const std::string tmpCacheFile( cacheFile + ".writing." + boost::uuids::to_string(u) + ".xml" ); 00112 00113 TUTTLE_LOG_DEBUG( TUTTLE_INFO, "Write plugins cache " << tmpCacheFile ); 00114 // serialize into a temporary file 00115 std::ofstream ofsb( tmpCacheFile.c_str(), std::ios::out ); 00116 if( ofsb.is_open() ) 00117 { 00118 OArchive oArchive( ofsb ); 00119 oArchive << BOOST_SERIALIZATION_NVP( _pluginCache ); 00120 ofsb.close(); 00121 // replace the cache file 00122 boost::filesystem::rename( tmpCacheFile, cacheFile ); 00123 } 00124 } 00125 #endif 00126 } 00127 00128 std::ostream& operator<<( std::ostream& os, const Core& v ) 00129 { 00130 os << "Core {" << std::endl; 00131 os << v.getImageEffectPluginCache(); 00132 os << "}" << std::endl; 00133 return os; 00134 } 00135 00136 } 00137 } 00138