1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-26 06:27:45 +00:00
sapphire/src/servers/Server_Zone/Script/ScriptLoader.cpp

160 lines
3.8 KiB
C++
Raw Normal View History

#include "ScriptLoader.h"
#include <Server_Common/Logging/Logger.h>
#include <Server_Common/Config/XMLConfig.h>
#include "ServerZone.h"
#include <boost/format.hpp>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string/predicate.hpp>
extern Core::Logger g_log;
extern Core::ServerZone g_serverZone;
Core::Scripting::ScriptLoader::ScriptLoader()
{}
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 )
{
g_log.error( "Failed to unload module @ 0x" + boost::str( boost::format( "%|08X|" ) % handle ) );
return false;
}
g_log.debug( "Unloaded module @ 0x" + boost::str( boost::format( "%|08X|" ) % handle ) );
return true;
}
Core::Scripting::ScriptInfo* Core::Scripting::ScriptLoader::loadModule( const std::string& path )
{
boost::filesystem::path f( path );
if ( isModuleLoaded( f.stem().string() ) )
{
g_log.error( "Unable to load module '" + f.stem().string() + "' as it is already loaded" );
return nullptr;
}
#ifdef _WIN32
2017-12-13 23:08:16 +11:00
ModuleHandle handle = LoadLibrary( path.c_str() );
#else
2017-12-13 23:08:16 +11:00
ModuleHandle handle = dlopen( path.c_str(), RTLD_LAZY );
#endif
if( !handle )
{
g_log.error( "Failed to load module from: " + path );
return nullptr;
}
g_log.info( "Loaded module from '" + path + "' @ 0x" + boost::str( boost::format( "%|08X|" ) % handle ) );
auto info = new ScriptInfo;
info->handle = handle;
info->library_name = f.stem().string();
2017-12-13 23:08:16 +11:00
info->library_path = path;
m_scriptMap.insert( std::make_pair( f.stem().string(), info ) );
return info;
}
ScriptObject* Core::Scripting::ScriptLoader::getScriptObject( ModuleHandle handle )
{
using getScript = ScriptObject*(*)();
#ifdef _WIN32
getScript func = reinterpret_cast< getScript >( GetProcAddress( handle, "getScript" ) );
#else
getScript func = reinterpret_cast< getScript >( dlsym( handle, "getScript" ) );
#endif
if( func )
2017-12-11 21:10:54 +11:00
{
auto ptr = func();
g_log.debug( "got ScriptObject @ 0x" + boost::str( boost::format( "%|08X|" ) % ptr ) );
g_log.debug( "script info -> name: " + std::string( ptr->getName() ) + ", id: " + std::to_string( ptr->getId() ) );
return ptr;
}
else
return nullptr;
}
bool Core::Scripting::ScriptLoader::unloadScript( Core::Scripting::ScriptInfo* info )
{
return unloadScript( info->handle );
}
bool Core::Scripting::ScriptLoader::unloadScript( ModuleHandle handle )
{
for( auto it = m_scriptMap.begin(); it != m_scriptMap.end(); ++it )
{
if( it->second->handle == handle )
{
2017-12-13 23:08:16 +11:00
delete it->second;
m_scriptMap.erase( it );
2017-12-13 23:08:16 +11:00
return unloadModule( handle );
}
}
return false;
}
bool Core::Scripting::ScriptLoader::isModuleLoaded( std::string name )
{
for( auto it = m_scriptMap.begin(); it != m_scriptMap.end(); ++it )
{
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 )
{
if( it->second->script_name == name )
{
return it->second;
}
}
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 )
{
if( it->second->script_name.find( search ) != std::string::npos )
{
scripts.insert( it->second );
}
}
}