1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-24 21:57:44 +00:00

unloading works now, scriptinfo is properly stored

This commit is contained in:
GokuWeedLord 2017-12-12 13:17:43 +11:00
parent d58994fe9b
commit 77bf4662f6
7 changed files with 76 additions and 50 deletions

View file

@ -546,7 +546,10 @@ void Core::DebugCommandHandler::script( char* data, Entity::Player &player, boos
if ( subCommand == params )
player.sendDebug( "Command failed: requires name of script" );
else
g_scriptMgr.getNativeScriptHandler().unloadScript( params );
if( g_scriptMgr.getNativeScriptHandler().unloadScript( params ) )
player.sendDebug( "Unloaded script successfully." );
else
player.sendDebug( "Failed to unload script: " + params );
}
else if( subCommand == "find" || subCommand == "f" )
{

View file

@ -35,7 +35,6 @@ public:
void get( char * data, Entity::Player& player, boost::shared_ptr< DebugCommand > command );
void add( char * data, Entity::Player& player, boost::shared_ptr< DebugCommand > command );
//void debug( char * data, Entity::Player& player, boost::shared_ptr< DebugCommand > command );
void scriptReload( char * data, Entity::Player& player, boost::shared_ptr< DebugCommand > command );
void injectPacket( char * data, Entity::Player& player, boost::shared_ptr< DebugCommand > command );
void injectChatPacket( char * data, Entity::Player& player, boost::shared_ptr< DebugCommand > command );
@ -43,7 +42,6 @@ public:
void serverInfo( char * data, Entity::Player& player, boost::shared_ptr< DebugCommand > command );
void unlockCharacter( char* data, Entity::Player& player, boost::shared_ptr< DebugCommand > command );
void targetInfo( char* data, Entity::Player& player, boost::shared_ptr< DebugCommand > command );
void script( char* data, Entity::Player& player, boost::shared_ptr< DebugCommand > command );

View file

@ -15,7 +15,7 @@ namespace Core {
return script->second;
}
ActionScript* NativeScript::getAbilityScript( uint32_t abilityId )
ActionScript* NativeScript::getActionScript(uint32_t abilityId)
{
auto script = m_actionScripts.find( abilityId );
if( script == m_actionScripts.end() )
@ -51,26 +51,6 @@ namespace Core {
return script->second;
}
void NativeScript::removeStatusEffectScript( uint32_t statusId )
{
m_statusEffectScripts.erase( statusId );
}
void NativeScript::removeAbilityScript( uint32_t abilityId )
{
m_actionScripts.erase( abilityId );
}
void NativeScript::removeQuestScript( uint32_t questId )
{
m_questScripts.erase( questId );
}
void NativeScript::removeBattleNpcScript( uint32_t npcId )
{
m_battleNpcScripts.erase( npcId );
}
bool NativeScript::loadScript( std::string path )
{
auto info = m_loader.loadModule( path );
@ -79,27 +59,33 @@ namespace Core {
// todo: this is shit
if( auto script = m_loader.getScriptObject< StatusEffectScript >( info->handle, "StatusEffectScript" ) )
{
// todo: make this a define or something
info->script = script;
info->script_name = script->getName();
m_statusEffectScripts[ script->getId() ] = script;
}
else if( auto script = m_loader.getScriptObject< ActionScript >( info->handle, "ActionScript" ) )
{
info->script = script;
info->script_name = script->getName();
m_actionScripts[ script->getId() ] = script;
}
else if( auto script = m_loader.getScriptObject< QuestScript >( info->handle, "QuestScript" ) )
{
info->script = script;
info->script_name = script->getName();
m_questScripts[ script->getId() ] = script;
}
else if( auto script = m_loader.getScriptObject< BattleNpcScript >( info->handle, "BattleNpcScript" ) )
{
info->script = script;
info->script_name = script->getName();
m_battleNpcScripts[ script->getId() ] = script;
}
else if( auto script = m_loader.getScriptObject< ZoneScript >( info->handle, "ZoneScript" ) )
{
info->script = script;
info->script_name = script->getName();
m_zoneScripts[ script->getId() ] = script;
}
else
@ -123,7 +109,25 @@ namespace Core {
bool NativeScript::unloadScript( std::string name )
{
return m_loader.unloadScript( name );
auto info = m_loader.getScriptInfo( name );
if( info )
{
auto ptr = info->script;
if( removeValueFromMap< uint32_t, StatusEffectScript* >( ptr, m_statusEffectScripts ) )
return m_loader.unloadScript( info );
else if( removeValueFromMap< uint32_t, ActionScript* >( ptr, m_actionScripts ) )
return m_loader.unloadScript( info );
else if( removeValueFromMap< uint32_t, QuestScript* >( ptr, m_questScripts ) )
return m_loader.unloadScript( info );
else if( removeValueFromMap< uint32_t, BattleNpcScript* >( ptr, m_battleNpcScripts ) )
return m_loader.unloadScript( info );
else if( removeValueFromMap< uint32_t, ZoneScript* >( ptr, m_zoneScripts ) )
return m_loader.unloadScript( info );
}
return false;
}

View file

@ -29,23 +29,30 @@ namespace Core {
NativeScript( );
StatusEffectScript* getStatusEffectScript( uint32_t statusId );
ActionScript* getAbilityScript( uint32_t abilityId );
ActionScript* getActionScript(uint32_t abilityId);
QuestScript* getQuestScript( uint32_t questId );
BattleNpcScript* getBattleNpcScript( uint32_t npcId );
ZoneScript* getZoneScript( uint32_t zoneId );
void removeStatusEffectScript( uint32_t statusId );
void removeAbilityScript( uint32_t abilityId );
void removeQuestScript( uint32_t questId );
void removeBattleNpcScript( uint32_t npcId );
bool loadScript( std::string );
bool unloadScript( std::string );
void clearAllScripts();
const std::string getModuleExtension();
template< typename key, typename val >
bool removeValueFromMap( ScriptObject* ptr, std::unordered_map< key, val >& map )
{
for( typename std::unordered_map< key, val >::iterator it = map.begin(); it != map.end(); ++it )
{
if( ptr == static_cast< ScriptObject* >( it->second ) )
{
map.erase( it );
return true;
}
}
return false;
}
};

View file

@ -19,6 +19,7 @@ namespace Scripting {
ScriptInfo() = default;
std::string library_name;
std::string script_name;
ModuleHandle handle;
ScriptObject* script;
};

View file

@ -3,6 +3,7 @@
#include <Server_Common/Logging/Logger.h>
#include <boost/format.hpp>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string/predicate.hpp>
extern Core::Logger g_log;
@ -42,6 +43,15 @@ bool Core::Scripting::ScriptLoader::unloadModule( ModuleHandle handle )
Core::Scripting::ScriptInfo* Core::Scripting::ScriptLoader::loadModule( 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
ModuleHandle handle = LoadLibrary( path.c_str() );
#else
@ -57,8 +67,6 @@ Core::Scripting::ScriptInfo* Core::Scripting::ScriptLoader::loadModule( std::str
g_log.info( "Loaded module from '" + path + "' @ 0x" + boost::str( boost::format( "%|08X|" ) % handle ) );
boost::filesystem::path f( path );
auto info = new ScriptInfo;
info->handle = handle;
info->library_name = f.stem().string();
@ -94,13 +102,9 @@ ScriptObject* Core::Scripting::ScriptLoader::getScriptObjectExport( ModuleHandle
return nullptr;
}
bool Core::Scripting::ScriptLoader::unloadScript( std::string name )
bool Core::Scripting::ScriptLoader::unloadScript( Core::Scripting::ScriptInfo* info )
{
auto info = m_scriptMap.find( name );
if( info == m_scriptMap.end() )
return false;
return unloadScript( info->second->handle );
return unloadScript( info->handle );
}
bool Core::Scripting::ScriptLoader::unloadScript( ModuleHandle handle )
@ -119,16 +123,26 @@ bool Core::Scripting::ScriptLoader::unloadScript( ModuleHandle handle )
return false;
}
const std::string& Core::Scripting::ScriptLoader::getModuleNameFromHandle( ModuleHandle handle ) const
bool Core::Scripting::ScriptLoader::isModuleLoaded( std::string name )
{
for( auto it = m_scriptMap.begin(); it != m_scriptMap.end(); ++it )
{
if( it->second->handle == handle )
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->first;
return it->second;
}
}
// nb: i'm not sure how this would ever be reached but you know
return "";
return nullptr;
}

View file

@ -28,12 +28,11 @@ namespace Scripting {
const std::string getModuleExtension();
ScriptInfo* loadModule( std::string );
bool unloadScript( std::string );
bool unloadScript( ScriptInfo* );
bool unloadScript( ModuleHandle );
const std::string& getModuleNameFromHandle( ModuleHandle handle ) const;
ScriptInfo* getScriptInfo( std::string name );
ScriptObject* getScriptObjectExport( ModuleHandle handle, std::string name );
bool isModuleLoaded( std::string name );
template< typename T >
T* getScriptObject( ModuleHandle handle, std::string name )