2017-12-10 15:31:48 +11:00
|
|
|
#include "ScriptLoader.h"
|
|
|
|
|
2017-12-18 12:36:52 +01:00
|
|
|
#include <common/Logging/Logger.h>
|
|
|
|
#include <common/Config/XMLConfig.h>
|
2017-12-13 22:19:00 +11:00
|
|
|
#include "ServerZone.h"
|
|
|
|
|
2017-12-10 15:31:48 +11:00
|
|
|
#include <boost/format.hpp>
|
2017-12-12 13:17:43 +11:00
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
2017-12-14 17:02:30 +11:00
|
|
|
#include <boost/filesystem.hpp>
|
2017-12-10 15:31:48 +11:00
|
|
|
|
|
|
|
extern Core::Logger g_log;
|
2017-12-13 22:19:00 +11:00
|
|
|
extern Core::ServerZone g_serverZone;
|
2017-12-10 15:31:48 +11:00
|
|
|
|
2017-12-14 17:02:30 +11:00
|
|
|
namespace fs = boost::filesystem;
|
2017-12-14 13:23:59 +11:00
|
|
|
|
2017-12-12 01:45:24 +11:00
|
|
|
Core::Scripting::ScriptLoader::ScriptLoader()
|
|
|
|
{}
|
|
|
|
|
2017-12-10 15:31:48 +11:00
|
|
|
const std::string Core::Scripting::ScriptLoader::getModuleExtension()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
return ".dll";
|
|
|
|
#elif __APPLE__
|
|
|
|
return ".dylib";
|
|
|
|
#else
|
|
|
|
return ".so";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Core::Scripting::ScriptLoader::unloadModule( ModuleHandle handle )
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
bool success = FreeLibrary( handle ) != 0;
|
|
|
|
#else
|
|
|
|
bool success = dlclose( handle ) == 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if( !success )
|
|
|
|
{
|
2017-12-13 13:06:02 +11:00
|
|
|
g_log.error( "Failed to unload module @ 0x" + boost::str( boost::format( "%|08X|" ) % handle ) );
|
2017-12-10 15:31:48 +11:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_log.debug( "Unloaded module @ 0x" + boost::str( boost::format( "%|08X|" ) % handle ) );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-12 14:57:13 +11:00
|
|
|
Core::Scripting::ScriptInfo* Core::Scripting::ScriptLoader::loadModule( const std::string& path )
|
2017-12-10 15:31:48 +11:00
|
|
|
{
|
2017-12-14 15:03:23 +11:00
|
|
|
fs::path f( path );
|
2017-12-12 13:17:43 +11:00
|
|
|
|
|
|
|
if ( isModuleLoaded( f.stem().string() ) )
|
|
|
|
{
|
|
|
|
g_log.error( "Unable to load module '" + f.stem().string() + "' as it is already loaded" );
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-12-14 13:23:59 +11:00
|
|
|
// copy to temp dir
|
|
|
|
fs::path cacheDir( f.parent_path() /= g_serverZone.getConfig()->getValue< std::string >( "Settings.General.Scripts.CachePath", "./cache/" ) );
|
|
|
|
fs::create_directories( cacheDir );
|
|
|
|
fs::path dest( cacheDir /= f.filename().string() );
|
|
|
|
|
2017-12-14 22:30:06 +11:00
|
|
|
try
|
2017-12-14 13:23:59 +11:00
|
|
|
{
|
2017-12-14 22:30:06 +11:00
|
|
|
fs::copy_file( f, dest, fs::copy_option::overwrite_if_exists );
|
|
|
|
}
|
|
|
|
catch( const boost::filesystem::filesystem_error& err )
|
|
|
|
{
|
|
|
|
g_log.error( "Error copying file to cache: " + err.code().message() );
|
|
|
|
|
|
|
|
return nullptr;
|
2017-12-14 13:23:59 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-10 15:31:48 +11:00
|
|
|
#ifdef _WIN32
|
2017-12-14 13:23:59 +11:00
|
|
|
ModuleHandle handle = LoadLibrary( dest.string().c_str() );
|
2017-12-10 15:31:48 +11:00
|
|
|
#else
|
2017-12-14 13:23:59 +11:00
|
|
|
ModuleHandle handle = dlopen( dest.string().c_str(), RTLD_LAZY );
|
2017-12-10 15:31:48 +11:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if( !handle )
|
|
|
|
{
|
|
|
|
g_log.error( "Failed to load module from: " + path );
|
|
|
|
|
2017-12-12 01:45:24 +11:00
|
|
|
return nullptr;
|
2017-12-10 15:31:48 +11:00
|
|
|
}
|
|
|
|
|
2017-12-14 22:30:06 +11:00
|
|
|
g_log.debug( "Loaded module '" + f.filename().string() + "' @ 0x" + boost::str( boost::format( "%|08X|" ) % handle ) );
|
2017-12-10 15:31:48 +11:00
|
|
|
|
2017-12-12 01:45:24 +11:00
|
|
|
auto info = new ScriptInfo;
|
|
|
|
info->handle = handle;
|
|
|
|
info->library_name = f.stem().string();
|
2017-12-14 13:23:59 +11:00
|
|
|
info->cache_path = dest.string();
|
|
|
|
info->library_path = f.string();
|
2017-12-12 01:45:24 +11:00
|
|
|
|
|
|
|
m_scriptMap.insert( std::make_pair( f.stem().string(), info ) );
|
|
|
|
|
|
|
|
return info;
|
2017-12-10 15:31:48 +11:00
|
|
|
}
|
|
|
|
|
2017-12-27 18:47:51 +11:00
|
|
|
ScriptObject** Core::Scripting::ScriptLoader::getScripts( ModuleHandle handle )
|
2017-12-10 15:31:48 +11:00
|
|
|
{
|
2017-12-27 18:47:51 +11:00
|
|
|
using getScripts = ScriptObject**(*)();
|
2017-12-10 15:31:48 +11:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2017-12-27 18:47:51 +11:00
|
|
|
getScripts func = reinterpret_cast< getScripts >( GetProcAddress( handle, "getScripts" ) );
|
2017-12-10 15:31:48 +11:00
|
|
|
#else
|
2017-12-27 18:47:51 +11:00
|
|
|
getScripts func = reinterpret_cast< getScripts >( dlsym( handle, "getScripts" ) );
|
2017-12-10 15:31:48 +11:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if( func )
|
2017-12-11 21:10:54 +11:00
|
|
|
{
|
|
|
|
auto ptr = func();
|
|
|
|
|
2017-12-27 18:47:51 +11:00
|
|
|
g_log.debug( "got ScriptObject array @ 0x" + boost::str( boost::format( "%|08X|" ) % ptr ) );
|
2017-12-11 21:10:54 +11:00
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
2017-12-10 15:31:48 +11:00
|
|
|
else
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-12-12 13:17:43 +11:00
|
|
|
bool Core::Scripting::ScriptLoader::unloadScript( Core::Scripting::ScriptInfo* info )
|
2017-12-10 15:31:48 +11:00
|
|
|
{
|
2017-12-12 13:17:43 +11:00
|
|
|
return unloadScript( info->handle );
|
2017-12-10 15:31:48 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Core::Scripting::ScriptLoader::unloadScript( ModuleHandle handle )
|
|
|
|
{
|
2017-12-12 01:45:24 +11:00
|
|
|
for( auto it = m_scriptMap.begin(); it != m_scriptMap.end(); ++it )
|
2017-12-10 15:31:48 +11:00
|
|
|
{
|
2017-12-12 01:45:24 +11:00
|
|
|
if( it->second->handle == handle )
|
2017-12-10 15:31:48 +11:00
|
|
|
{
|
2017-12-14 13:23:59 +11:00
|
|
|
auto info = it->second;
|
2017-12-12 01:45:24 +11:00
|
|
|
m_scriptMap.erase( it );
|
|
|
|
|
2017-12-14 13:23:59 +11:00
|
|
|
if( unloadModule( handle ) )
|
|
|
|
{
|
|
|
|
// remove cached file
|
|
|
|
fs::remove( info->cache_path );
|
|
|
|
|
|
|
|
delete info;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_log.error( "failed to unload module: " + info->library_name );
|
|
|
|
|
|
|
|
return false;
|
2017-12-10 15:31:48 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2017-12-12 01:45:24 +11:00
|
|
|
}
|
|
|
|
|
2017-12-12 13:17:43 +11:00
|
|
|
bool Core::Scripting::ScriptLoader::isModuleLoaded( std::string name )
|
2017-12-12 01:45:24 +11:00
|
|
|
{
|
|
|
|
for( auto it = m_scriptMap.begin(); it != m_scriptMap.end(); ++it )
|
|
|
|
{
|
2017-12-12 13:17:43 +11:00
|
|
|
if( boost::iequals( it->second->library_name, name ) )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Core::Scripting::ScriptInfo* Core::Scripting::ScriptLoader::getScriptInfo( std::string name )
|
|
|
|
{
|
|
|
|
for( auto it = m_scriptMap.begin(); it != m_scriptMap.end(); ++it )
|
|
|
|
{
|
2017-12-27 18:47:51 +11:00
|
|
|
if( it->second->library_name == name )
|
2017-12-12 01:45:24 +11:00
|
|
|
{
|
2017-12-12 13:17:43 +11:00
|
|
|
return it->second;
|
2017-12-12 01:45:24 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-12 13:17:43 +11:00
|
|
|
return nullptr;
|
2017-12-12 17:29:31 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
void Core::Scripting::ScriptLoader::findScripts( std::set< Core::Scripting::ScriptInfo* >& scripts, const std::string& search )
|
|
|
|
{
|
|
|
|
for( auto it = m_scriptMap.begin(); it != m_scriptMap.end(); ++it )
|
|
|
|
{
|
2017-12-27 18:47:51 +11:00
|
|
|
if( it->second->library_name.find( search ) != std::string::npos )
|
2017-12-12 17:29:31 +11:00
|
|
|
{
|
|
|
|
scripts.insert( it->second );
|
|
|
|
}
|
|
|
|
}
|
2017-12-18 12:36:52 +01:00
|
|
|
}
|