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

370 lines
11 KiB
C++
Raw Normal View History

#include <common/Logging/Logger.h>
#include <common/Exd/ExdData.h>
#include <common/Config/XMLConfig.h>
2017-08-18 17:29:36 +02:00
#include "NativeScriptManager.h"
2017-08-18 17:29:36 +02:00
#include "Zone/Zone.h"
#include "Actor/Player.h"
#include "Actor/BattleNpc.h"
#include "ServerZone.h"
2018-01-09 23:50:54 +01:00
#include "Event/EventHandler.h"
#include "Event/EventHelper.h"
#include "StatusEffect/StatusEffect.h"
#include "Network/PacketWrappers/ServerNoticePacket.h"
#include "Script/ScriptManager.h"
2017-08-18 17:29:36 +02:00
#include <boost/lexical_cast.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/format.hpp>
#include <boost/foreach.hpp>
2017-12-14 22:30:06 +11:00
// enable the ambiguity fix for every platform to avoid #define nonsense
#define WIN_AMBIGUITY_FIX
#include <libraries/external/watchdog/Watchdog.h>
2017-08-18 17:29:36 +02:00
extern Core::Logger g_log;
extern Core::Data::ExdData g_exdData;
extern Core::ServerZone g_serverZone;
2017-12-14 22:30:06 +11:00
Core::Scripting::ScriptManager::ScriptManager() :
m_firstScriptChangeNotificiation( false )
2017-08-18 17:29:36 +02:00
{
m_nativeScriptManager = createNativeScriptMgr();
2017-08-18 17:29:36 +02:00
}
Core::Scripting::ScriptManager::~ScriptManager()
{
2017-12-14 22:30:06 +11:00
Watchdog::unwatchAll();
}
2017-08-18 17:29:36 +02:00
2017-12-14 22:30:06 +11:00
void Core::Scripting::ScriptManager::update()
{
m_nativeScriptManager->processLoadQueue();
2017-08-18 17:29:36 +02:00
}
bool Core::Scripting::ScriptManager::init()
{
std::set< std::string > files;
loadDir( g_serverZone.getConfig()->getValue< std::string >( "Settings.General.Scripts.Path", "./compiledscripts/" ),
files, m_nativeScriptManager->getModuleExtension() );
2017-12-14 22:30:06 +11:00
uint32_t scriptsFound = 0;
uint32_t scriptsLoaded = 0;
for( auto itr = files.begin(); itr != files.end(); ++itr )
{
auto& path = *itr;
2017-12-14 22:30:06 +11:00
scriptsFound++;
if( m_nativeScriptManager->loadScript( path ) )
scriptsLoaded++;
}
2017-12-14 22:30:06 +11:00
g_log.info( "ScriptManager: Loaded " + std::to_string( scriptsLoaded ) + "/" + std::to_string( scriptsFound ) + " scripts successfully" );
watchDirectories();
return true;
}
2017-12-14 22:30:06 +11:00
void Core::Scripting::ScriptManager::watchDirectories()
{
2018-01-18 13:56:55 +11:00
auto shouldWatch = g_serverZone.getConfig()->getValue< bool >( "Settings.General.Scripts.HotSwap.Enabled", true );
if( !shouldWatch )
return;
2017-12-14 22:30:06 +11:00
Watchdog::watchMany( g_serverZone.getConfig()->getValue< std::string >( "Settings.General.Scripts.Path", "./compiledscripts/" ) + "*" + m_nativeScriptManager->getModuleExtension(),
[ this ]( const std::vector< ci::fs::path >& paths )
{
if( !m_firstScriptChangeNotificiation )
{
// for whatever reason, the first time this runs, it detects every file as changed
// so we're always going to ignore the first notification
m_firstScriptChangeNotificiation = true;
return;
}
for( auto path : paths )
{
if( m_nativeScriptManager->isModuleLoaded( path.stem().string() ) )
{
g_log.debug( "Reloading changed script: " + path.stem().string() );
m_nativeScriptManager->queueScriptReload( path.stem( ).string( ));
2017-12-14 22:30:06 +11:00
}
else
{
g_log.debug( "Loading new script: " + path.stem().string() );
m_nativeScriptManager->loadScript( path.string() );
}
}
});
}
void Core::Scripting::ScriptManager::loadDir( const std::string& dirname, std::set<std::string> &files, const std::string& ext )
2017-08-18 17:29:36 +02:00
{
2017-11-26 00:38:33 +11:00
g_log.info( "ScriptEngine: loading scripts from " + dirname );
2017-08-18 17:29:36 +02:00
boost::filesystem::path targetDir( dirname );
boost::filesystem::directory_iterator iter( targetDir );
boost::filesystem::directory_iterator eod;
2017-08-18 17:29:36 +02:00
BOOST_FOREACH( boost::filesystem::path const& i, make_pair( iter, eod ) )
{
if( is_regular_file( i ) && boost::filesystem::extension( i.string() ) == ext )
{
files.insert( i.string() );
}
}
2017-08-18 17:29:36 +02:00
}
void Core::Scripting::ScriptManager::onPlayerFirstEnterWorld( Entity::Player& player )
2017-08-18 17:29:36 +02:00
{
// try
// {
// std::string test = m_onFirstEnterWorld( player );
// }
// catch( const std::exception &e )
// {
// std::string what = e.what();
// g_log.Log( LoggingSeverity::error, what );
// }
2017-08-18 17:29:36 +02:00
}
bool Core::Scripting::ScriptManager::registerBnpcTemplate( std::string templateName, uint32_t bnpcBaseId,
uint32_t bnpcNameId, uint32_t modelId, std::string aiName )
2017-08-18 17:29:36 +02:00
{
return g_serverZone.registerBnpcTemplate( templateName, bnpcBaseId, bnpcNameId, modelId, aiName );
}
bool Core::Scripting::ScriptManager::onTalk( Entity::Player& player, uint64_t actorId, uint32_t eventId )
2017-08-18 17:29:36 +02:00
{
uint16_t eventType = eventId >> 16;
uint32_t scriptId = eventId;
2017-08-18 17:29:36 +02:00
2017-12-12 17:29:31 +11:00
// aethernet/aetherytes need to be handled separately
if( eventType == Event::EventHandler::EventHandlerType::Aetheryte )
2017-08-18 17:29:36 +02:00
{
2017-12-12 17:29:31 +11:00
auto aetherInfo = g_exdData.getAetheryteInfo( eventId & 0xFFFF );
2017-12-13 14:05:50 +11:00
scriptId = EVENTSCRIPT_AETHERYTE_ID;
2017-12-12 17:29:31 +11:00
if( !aetherInfo->isAetheryte )
2017-12-13 14:05:50 +11:00
scriptId = EVENTSCRIPT_AETHERNET_ID;
}
2017-12-12 17:29:31 +11:00
auto script = m_nativeScriptManager->getScript< EventScript >( scriptId );
if( !script )
return false;
script->onTalk( eventId, player, actorId );
2017-08-18 17:29:36 +02:00
return true;
}
bool Core::Scripting::ScriptManager::onEnterTerritory( Entity::Player& player, uint32_t eventId,
2017-08-18 17:29:36 +02:00
uint16_t param1, uint16_t param2 )
{
auto script = m_nativeScriptManager->getScript< EventScript >( eventId );
if( !script )
return false;
script->onEnterZone( player, eventId, param1, param2 );
return true;
2017-08-18 17:29:36 +02:00
}
bool Core::Scripting::ScriptManager::onWithinRange( Entity::Player& player, uint32_t eventId, uint32_t param1,
float x, float y, float z )
2017-08-18 17:29:36 +02:00
{
auto script = m_nativeScriptManager->getScript< EventScript >( eventId );
if( !script )
return false;
script->onWithinRange( player, eventId, param1, x, y, z );
return true;
2017-08-18 17:29:36 +02:00
}
bool Core::Scripting::ScriptManager::onOutsideRange( Entity::Player& player, uint32_t eventId, uint32_t param1,
float x, float y, float z )
2017-08-18 17:29:36 +02:00
{
auto script = m_nativeScriptManager->getScript< EventScript >( eventId );
if( !script )
return false;
script->onOutsideRange( player, eventId, param1, x, y, z );
return true;
2017-08-18 17:29:36 +02:00
}
bool Core::Scripting::ScriptManager::onEmote( Entity::Player& player, uint64_t actorId,
2017-08-18 17:29:36 +02:00
uint32_t eventId, uint8_t emoteId )
{
auto script = m_nativeScriptManager->getScript< EventScript >( eventId );
if( !script )
2017-08-18 17:29:36 +02:00
return false;
script->onEmote( actorId, eventId, emoteId, player );
2017-08-18 17:29:36 +02:00
return true;
}
bool Core::Scripting::ScriptManager::onEventHandlerReturn( Entity::Player& player, uint32_t eventId,
uint16_t subEvent, uint16_t param1, uint16_t param2,
uint16_t param3 )
2017-08-18 17:29:36 +02:00
{
player.sendDebug( "eventId: " +
std::to_string( eventId ) +
" ( 0x" + boost::str( boost::format( "%|08X|" ) % ( uint64_t ) ( eventId & 0xFFFFFFF ) ) + " ) " +
" scene: " + std::to_string( subEvent ) +
" p1: " + std::to_string( param1 ) +
" p2: " + std::to_string( param2 ) +
" p3: " + std::to_string( param3 ) );
2017-08-18 17:29:36 +02:00
2018-01-16 14:01:58 +01:00
auto pEvent = player.getEvent( eventId );
if( pEvent )
2017-08-18 17:29:36 +02:00
{
2018-01-16 14:01:58 +01:00
pEvent->setPlayedScene( false );
// try to retrieve a stored callback
auto eventCallback = pEvent->getEventReturnCallback();
// if there is one, proceed to call it
if( eventCallback )
eventCallback( player, eventId, param1, param2, param3 );
// check the event, finishing it if needed
player.checkEvent( eventId );
2017-08-18 17:29:36 +02:00
}
return true;
}
bool Core::Scripting::ScriptManager::onEventHandlerTradeReturn( Entity::Player& player, uint32_t eventId,
2017-08-18 17:29:36 +02:00
uint16_t subEvent, uint16_t param, uint32_t catalogId )
{
auto script = m_nativeScriptManager->getScript< EventScript >( eventId );
if( script )
{
script->onEventHandlerTradeReturn( player, eventId, subEvent, param, catalogId );
return true;
}
return false;
2017-08-18 17:29:36 +02:00
}
bool Core::Scripting::ScriptManager::onEventItem( Entity::Player& player, uint32_t eventItemId,
uint32_t eventId, uint32_t castTime, uint64_t targetId )
2017-08-18 17:29:36 +02:00
{
std::string eventName = "onEventItem";
std::string objName = Event::getEventName( eventId );
player.sendDebug( "Calling: " + objName + "." + eventName + " - " + std::to_string( eventId ) );
auto script = m_nativeScriptManager->getScript< EventScript >( eventId );
if( script )
{
2018-01-09 23:50:54 +01:00
player.eventStart( targetId, eventId, Event::EventHandler::Item, 0, 0 );
script->onEventItem( player, eventItemId, eventId, castTime, targetId );
return true;
}
return false;
2017-08-18 17:29:36 +02:00
}
bool Core::Scripting::ScriptManager::onMobKill( Entity::Player& player, uint16_t nameId )
2017-08-18 17:29:36 +02:00
{
std::string eventName = "onBnpcKill_" + std::to_string( nameId );
2017-08-18 17:29:36 +02:00
// loop through all active quests and try to call available onMobKill callbacks
for( size_t i = 0; i < 30; i++ )
{
auto activeQuests = player.getQuestActive( static_cast< uint16_t >( i ) );
2017-08-18 17:29:36 +02:00
if( !activeQuests )
continue;
uint16_t questId = activeQuests->c.questId;
auto script = m_nativeScriptManager->getScript< EventScript >( questId );
if( script )
2017-08-18 17:29:36 +02:00
{
std::string objName = Event::getEventName( 0x00010000 | questId );
player.sendDebug("Calling: " + objName + "." + eventName);
2017-08-18 17:29:36 +02:00
script->onNpcKill( nameId, player );
2017-08-18 17:29:36 +02:00
}
}
return true;
}
bool Core::Scripting::ScriptManager::onCastFinish( Entity::Player& player, Entity::ActorPtr pTarget, uint32_t actionId )
2017-08-18 17:29:36 +02:00
{
auto script = m_nativeScriptManager->getScript< ActionScript >( actionId );
if( script )
script->onCastFinish( player, *pTarget );
return true;
2017-08-18 17:29:36 +02:00
}
bool Core::Scripting::ScriptManager::onStatusReceive( Entity::ActorPtr pActor, uint32_t effectId )
{
auto script = m_nativeScriptManager->getScript< StatusEffectScript >( effectId );
if( script )
2017-08-18 17:29:36 +02:00
{
if( pActor->isPlayer() )
pActor->getAsPlayer()->sendDebug( "Calling status receive for statusid: " + std::to_string( effectId ) );
2017-08-18 17:29:36 +02:00
script->onApply( *pActor );
return true;
2017-08-18 17:29:36 +02:00
}
return false;
2017-08-18 17:29:36 +02:00
}
bool Core::Scripting::ScriptManager::onStatusTick( Entity::ActorPtr pActor, Core::StatusEffect::StatusEffect& effect )
{
auto script = m_nativeScriptManager->getScript< StatusEffectScript >( effect.getId() );
if( script )
2017-08-18 17:29:36 +02:00
{
if( pActor->isPlayer() )
pActor->getAsPlayer()->sendDebug( "Calling status tick for statusid: " + std::to_string( effect.getId() ) );
2017-08-18 17:29:36 +02:00
script->onTick( *pActor );
return true;
2017-08-18 17:29:36 +02:00
}
return false;
2017-08-18 17:29:36 +02:00
}
bool Core::Scripting::ScriptManager::onStatusTimeOut( Entity::ActorPtr pActor, uint32_t effectId )
{
auto script = m_nativeScriptManager->getScript< StatusEffectScript >( effectId );
if( script )
2017-08-18 17:29:36 +02:00
{
if( pActor->isPlayer() )
pActor->getAsPlayer()->sendDebug( "Calling status timeout for statusid: " + std::to_string( effectId ) );
2017-08-18 17:29:36 +02:00
script->onExpire( *pActor );
return true;
2017-08-18 17:29:36 +02:00
}
return false;
2017-08-18 17:29:36 +02:00
}
bool Core::Scripting::ScriptManager::onZoneInit( ZonePtr pZone )
{
auto script = m_nativeScriptManager->getScript< ZoneScript >( pZone->getId() );
if( script )
2017-08-18 17:29:36 +02:00
{
script->onZoneInit();
return true;
}
2017-08-18 17:29:36 +02:00
return false;
2017-08-18 17:29:36 +02:00
}
Scripting::NativeScriptManager& Core::Scripting::ScriptManager::getNativeScriptHandler()
{
return *m_nativeScriptManager;
}