From 77bf4662f64c0d92b4a532bffb7159ff67b9cda9 Mon Sep 17 00:00:00 2001 From: GokuWeedLord Date: Tue, 12 Dec 2017 13:17:43 +1100 Subject: [PATCH] unloading works now, scriptinfo is properly stored --- .../DebugCommand/DebugCommandHandler.cpp | 5 +- .../DebugCommand/DebugCommandHandler.h | 2 - .../Server_Zone/Script/NativeScript.cpp | 48 ++++++++++--------- src/servers/Server_Zone/Script/NativeScript.h | 23 +++++---- src/servers/Server_Zone/Script/ScriptInfo.h | 1 + .../Server_Zone/Script/ScriptLoader.cpp | 40 +++++++++++----- src/servers/Server_Zone/Script/ScriptLoader.h | 7 ++- 7 files changed, 76 insertions(+), 50 deletions(-) diff --git a/src/servers/Server_Zone/DebugCommand/DebugCommandHandler.cpp b/src/servers/Server_Zone/DebugCommand/DebugCommandHandler.cpp index 90eab316..471ca10f 100644 --- a/src/servers/Server_Zone/DebugCommand/DebugCommandHandler.cpp +++ b/src/servers/Server_Zone/DebugCommand/DebugCommandHandler.cpp @@ -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" ) { diff --git a/src/servers/Server_Zone/DebugCommand/DebugCommandHandler.h b/src/servers/Server_Zone/DebugCommand/DebugCommandHandler.h index 4ed13442..4da90f29 100644 --- a/src/servers/Server_Zone/DebugCommand/DebugCommandHandler.h +++ b/src/servers/Server_Zone/DebugCommand/DebugCommandHandler.h @@ -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 ); diff --git a/src/servers/Server_Zone/Script/NativeScript.cpp b/src/servers/Server_Zone/Script/NativeScript.cpp index 0cadd108..50adb4c3 100644 --- a/src/servers/Server_Zone/Script/NativeScript.cpp +++ b/src/servers/Server_Zone/Script/NativeScript.cpp @@ -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; } diff --git a/src/servers/Server_Zone/Script/NativeScript.h b/src/servers/Server_Zone/Script/NativeScript.h index 0e724ec7..3bb79167 100644 --- a/src/servers/Server_Zone/Script/NativeScript.h +++ b/src/servers/Server_Zone/Script/NativeScript.h @@ -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; + } }; diff --git a/src/servers/Server_Zone/Script/ScriptInfo.h b/src/servers/Server_Zone/Script/ScriptInfo.h index aabca7f1..49a49de2 100644 --- a/src/servers/Server_Zone/Script/ScriptInfo.h +++ b/src/servers/Server_Zone/Script/ScriptInfo.h @@ -19,6 +19,7 @@ namespace Scripting { ScriptInfo() = default; std::string library_name; + std::string script_name; ModuleHandle handle; ScriptObject* script; }; diff --git a/src/servers/Server_Zone/Script/ScriptLoader.cpp b/src/servers/Server_Zone/Script/ScriptLoader.cpp index ccb973b6..a22b397d 100644 --- a/src/servers/Server_Zone/Script/ScriptLoader.cpp +++ b/src/servers/Server_Zone/Script/ScriptLoader.cpp @@ -3,6 +3,7 @@ #include #include #include +#include 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; } \ No newline at end of file diff --git a/src/servers/Server_Zone/Script/ScriptLoader.h b/src/servers/Server_Zone/Script/ScriptLoader.h index ac17fd27..cbbabf3b 100644 --- a/src/servers/Server_Zone/Script/ScriptLoader.h +++ b/src/servers/Server_Zone/Script/ScriptLoader.h @@ -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 )