2017-12-13 15:07:03 +11:00
|
|
|
#include "NativeScriptManager.h"
|
|
|
|
|
|
|
|
namespace Core {
|
2018-01-04 16:14:14 +11:00
|
|
|
namespace Scripting {
|
2017-12-13 15:07:03 +11:00
|
|
|
|
|
|
|
bool NativeScriptManager::loadScript( const std::string& path )
|
|
|
|
{
|
|
|
|
auto module = m_loader.loadModule( path );
|
|
|
|
if( !module )
|
|
|
|
return false;
|
|
|
|
|
2017-12-27 18:47:51 +11:00
|
|
|
auto scripts = m_loader.getScripts( module->handle );
|
|
|
|
if( !scripts )
|
2017-12-13 22:19:00 +11:00
|
|
|
{
|
|
|
|
m_loader.unloadScript( module );
|
2017-12-13 15:07:03 +11:00
|
|
|
return false;
|
2017-12-13 22:19:00 +11:00
|
|
|
}
|
2017-12-13 15:07:03 +11:00
|
|
|
|
2017-12-27 18:47:51 +11:00
|
|
|
//
|
|
|
|
bool success = false;
|
2017-12-13 15:07:03 +11:00
|
|
|
|
2017-12-27 18:47:51 +11:00
|
|
|
for( int i = 0; ; i++ )
|
2017-12-13 15:07:03 +11:00
|
|
|
{
|
2017-12-27 18:47:51 +11:00
|
|
|
if( scripts[i] == nullptr )
|
2017-12-13 15:07:03 +11:00
|
|
|
break;
|
|
|
|
|
2017-12-27 18:47:51 +11:00
|
|
|
auto script = scripts[i];
|
|
|
|
module->scripts.push_back( script );
|
|
|
|
|
2018-01-04 22:40:48 +11:00
|
|
|
m_scripts[script->getType()][script->getId()] = script;
|
2017-12-27 18:47:51 +11:00
|
|
|
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !success )
|
|
|
|
{
|
|
|
|
m_loader.unloadScript( module->handle );
|
|
|
|
return false;
|
2017-12-13 15:07:03 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string NativeScriptManager::getModuleExtension()
|
|
|
|
{
|
|
|
|
return m_loader.getModuleExtension();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NativeScriptManager::unloadScript( const std::string& name )
|
|
|
|
{
|
|
|
|
auto info = m_loader.getScriptInfo( name );
|
|
|
|
if( !info )
|
|
|
|
return false;
|
|
|
|
|
2017-12-14 13:23:59 +11:00
|
|
|
return unloadScript( info );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NativeScriptManager::unloadScript( ScriptInfo* info )
|
|
|
|
{
|
2017-12-27 18:47:51 +11:00
|
|
|
for( auto& script : info->scripts )
|
2017-12-13 15:07:03 +11:00
|
|
|
{
|
2018-01-04 22:40:48 +11:00
|
|
|
m_scripts[script->getType()].erase( script->getId() );
|
2017-12-27 18:53:45 +11:00
|
|
|
|
|
|
|
delete script;
|
2017-12-13 15:07:03 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
return m_loader.unloadScript( info );
|
|
|
|
}
|
|
|
|
|
2017-12-14 23:05:53 +11:00
|
|
|
void NativeScriptManager::queueScriptReload( const std::string &name )
|
2017-12-14 13:23:59 +11:00
|
|
|
{
|
|
|
|
auto info = m_loader.getScriptInfo( name );
|
|
|
|
if( !info )
|
2017-12-14 22:30:06 +11:00
|
|
|
return;
|
2017-12-14 13:23:59 +11:00
|
|
|
|
|
|
|
// backup actual lib path
|
|
|
|
std::string libPath( info->library_path );
|
|
|
|
|
|
|
|
if( !unloadScript( info ) )
|
2017-12-14 22:30:06 +11:00
|
|
|
return;
|
|
|
|
|
|
|
|
m_scriptLoadQueue.push( libPath );
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeScriptManager::processLoadQueue()
|
|
|
|
{
|
2017-12-15 00:19:30 +11:00
|
|
|
std::vector< std::string > deferredLoads;
|
|
|
|
|
2017-12-14 22:30:06 +11:00
|
|
|
while( !m_scriptLoadQueue.empty() )
|
|
|
|
{
|
|
|
|
auto item = m_scriptLoadQueue.front();
|
|
|
|
|
2017-12-15 00:19:30 +11:00
|
|
|
// if it fails, we defer the loading to the next tick
|
|
|
|
if( !loadScript( item ) )
|
|
|
|
deferredLoads.push_back( item );
|
2017-12-14 13:23:59 +11:00
|
|
|
|
2017-12-14 22:30:06 +11:00
|
|
|
m_scriptLoadQueue.pop();
|
|
|
|
}
|
2017-12-15 00:19:30 +11:00
|
|
|
|
|
|
|
if( !deferredLoads.empty() )
|
|
|
|
{
|
|
|
|
for( auto& item : deferredLoads )
|
|
|
|
m_scriptLoadQueue.push( item );
|
|
|
|
}
|
2017-12-14 13:23:59 +11:00
|
|
|
}
|
|
|
|
|
2017-12-13 15:07:03 +11:00
|
|
|
void NativeScriptManager::findScripts( std::set< Core::Scripting::ScriptInfo* >& scripts, const std::string& search )
|
|
|
|
{
|
|
|
|
return m_loader.findScripts( scripts, search );
|
|
|
|
}
|
|
|
|
|
2017-12-14 22:30:06 +11:00
|
|
|
bool NativeScriptManager::isModuleLoaded( const std::string &name )
|
|
|
|
{
|
|
|
|
return m_loader.isModuleLoaded( name );
|
|
|
|
}
|
|
|
|
|
2017-12-13 15:07:03 +11:00
|
|
|
|
|
|
|
|
|
|
|
boost::shared_ptr< NativeScriptManager > createNativeScriptMgr()
|
|
|
|
{
|
|
|
|
return boost::make_shared< NativeScriptManager >();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|