2018-03-02 07:22:25 -03:00
|
|
|
#include "NativeScriptMgr.h"
|
2018-02-28 10:26:03 +01:00
|
|
|
|
2018-03-06 22:22:19 +01:00
|
|
|
#include <Crypt/md5.h>
|
2019-01-07 23:00:09 +11:00
|
|
|
#include "ServerMgr.h"
|
2017-12-13 15:07:03 +11:00
|
|
|
|
2018-11-13 20:43:29 +11:00
|
|
|
#include "Framework.h"
|
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
namespace Sapphire::Scripting
|
2018-08-29 21:40:59 +02:00
|
|
|
{
|
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
bool NativeScriptMgr::loadScript( const std::string& path )
|
2018-08-29 21:40:59 +02:00
|
|
|
{
|
2018-11-29 16:55:48 +01:00
|
|
|
auto module = m_loader.loadModule( path );
|
|
|
|
if( !module )
|
|
|
|
return false;
|
2018-08-29 21:40:59 +02:00
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
auto scripts = m_loader.getScripts( module->handle );
|
|
|
|
if( !scripts )
|
|
|
|
{
|
|
|
|
m_loader.unloadScript( module );
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-29 21:40:59 +02:00
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
//
|
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
for( int i = 0;; i++ )
|
|
|
|
{
|
|
|
|
if( scripts[ i ] == nullptr )
|
|
|
|
break;
|
|
|
|
|
|
|
|
auto script = scripts[ i ];
|
|
|
|
module->scripts.push_back( script );
|
2018-08-29 21:40:59 +02:00
|
|
|
|
2018-12-23 03:53:08 +01:00
|
|
|
script->setFramework( framework().get() );
|
2018-08-29 21:40:59 +02:00
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
m_scripts[ script->getType() ][ script->getId() ] = script;
|
2018-11-13 20:43:29 +11:00
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
success = true;
|
|
|
|
}
|
2018-08-29 21:40:59 +02:00
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
if( !success )
|
|
|
|
{
|
|
|
|
m_loader.unloadScript( module->handle );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2018-08-29 21:40:59 +02:00
|
|
|
}
|
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
const std::string NativeScriptMgr::getModuleExtension()
|
2018-08-29 21:40:59 +02:00
|
|
|
{
|
2018-11-29 16:55:48 +01:00
|
|
|
return m_loader.getModuleExtension();
|
2018-08-29 21:40:59 +02:00
|
|
|
}
|
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
bool NativeScriptMgr::unloadScript( const std::string& name )
|
|
|
|
{
|
|
|
|
auto info = m_loader.getScriptInfo( name );
|
|
|
|
if( !info )
|
|
|
|
return false;
|
2018-08-29 21:40:59 +02:00
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
return unloadScript( info );
|
|
|
|
}
|
2017-12-13 15:07:03 +11:00
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
bool NativeScriptMgr::unloadScript( ScriptInfo* info )
|
2018-08-29 21:40:59 +02:00
|
|
|
{
|
2018-11-29 16:55:48 +01:00
|
|
|
for( auto& script : info->scripts )
|
|
|
|
{
|
|
|
|
m_scripts[ script->getType() ].erase( script->getId() );
|
|
|
|
|
|
|
|
delete script;
|
|
|
|
}
|
2017-12-13 15:07:03 +11:00
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
return m_loader.unloadScript( info );
|
2018-08-29 21:40:59 +02:00
|
|
|
}
|
2017-12-14 22:30:06 +11:00
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
void NativeScriptMgr::queueScriptReload( const std::string& name )
|
|
|
|
{
|
|
|
|
auto info = m_loader.getScriptInfo( name );
|
|
|
|
if( !info )
|
|
|
|
return;
|
2018-08-29 21:40:59 +02:00
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
// backup actual lib path
|
|
|
|
std::string libPath( info->library_path );
|
2018-08-29 21:40:59 +02:00
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
if( !unloadScript( info ) )
|
|
|
|
return;
|
2018-08-29 21:40:59 +02:00
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
m_scriptLoadQueue.push( libPath );
|
|
|
|
}
|
2018-08-29 21:40:59 +02:00
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
void NativeScriptMgr::processLoadQueue()
|
|
|
|
{
|
|
|
|
std::vector< std::string > deferredLoads;
|
2017-12-13 15:07:03 +11:00
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
while( !m_scriptLoadQueue.empty() )
|
|
|
|
{
|
|
|
|
auto item = m_scriptLoadQueue.front();
|
2017-12-13 15:07:03 +11:00
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
// if it fails, we defer the loading to the next tick
|
|
|
|
if( !loadScript( item ) )
|
|
|
|
deferredLoads.push_back( item );
|
2018-08-29 21:40:59 +02:00
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
m_scriptLoadQueue.pop();
|
|
|
|
}
|
2018-08-29 21:40:59 +02:00
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
if( !deferredLoads.empty() )
|
|
|
|
{
|
|
|
|
for( auto& item : deferredLoads )
|
|
|
|
m_scriptLoadQueue.push( item );
|
|
|
|
}
|
2018-08-29 21:40:59 +02:00
|
|
|
}
|
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
void NativeScriptMgr::findScripts( std::set< Sapphire::Scripting::ScriptInfo* >& scripts, const std::string& search )
|
2018-08-29 21:40:59 +02:00
|
|
|
{
|
2018-11-29 16:55:48 +01:00
|
|
|
return m_loader.findScripts( scripts, search );
|
2018-08-29 21:40:59 +02:00
|
|
|
}
|
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
bool NativeScriptMgr::isModuleLoaded( const std::string& name )
|
|
|
|
{
|
|
|
|
return m_loader.isModuleLoaded( name );
|
|
|
|
}
|
2018-08-29 21:40:59 +02:00
|
|
|
|
2018-12-23 03:53:08 +01:00
|
|
|
NativeScriptMgr::NativeScriptMgr( FrameworkPtr pFw ) :
|
|
|
|
World::Manager::BaseManager( pFw )
|
|
|
|
{
|
2019-01-07 23:00:09 +11:00
|
|
|
auto pServerMgr = framework()->get< Sapphire::World::ServerMgr >();
|
|
|
|
m_loader.setCachePath( pServerMgr->getConfig().scripts.cachePath );
|
2018-12-23 03:53:08 +01:00
|
|
|
}
|
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
|
2018-12-23 03:53:08 +01:00
|
|
|
std::shared_ptr< NativeScriptMgr > createNativeScriptMgr( FrameworkPtr pFw )
|
2018-11-29 16:55:48 +01:00
|
|
|
{
|
2018-12-23 03:53:08 +01:00
|
|
|
return std::make_shared< NativeScriptMgr >( pFw );
|
2018-11-29 16:55:48 +01:00
|
|
|
}
|
2017-12-13 15:07:03 +11:00
|
|
|
}
|
2018-11-29 16:55:48 +01:00
|
|
|
|