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/ScriptMgr.cpp

389 lines
11 KiB
C++
Raw Normal View History

#include <boost/lexical_cast.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/format.hpp>
#include <boost/foreach.hpp>
2018-03-06 22:22:19 +01:00
#include <Logging/Logger.h>
#include <Exd/ExdDataGenerated.h>
#include <Config/XMLConfig.h>
#include "watchdog/Watchdog.h"
2017-08-18 17:29:36 +02:00
#include "Zone/Zone.h"
#include "Zone/InstanceContent.h"
#include "Actor/Player.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"
2017-08-18 17:29:36 +02:00
#include "Script/ScriptMgr.h"
2017-08-18 17:29:36 +02:00
#include "NativeScriptMgr.h"
#include "ServerZone.h"
#include "Framework.h"
// enable the ambiguity fix for every platform to avoid #define nonsense
#define WIN_AMBIGUITY_FIX
2017-12-14 22:30:06 +11:00
extern Core::Framework g_fw;
2017-08-18 17:29:36 +02:00
Core::Scripting::ScriptMgr::ScriptMgr() :
2017-12-14 22:30:06 +11:00
m_firstScriptChangeNotificiation( false )
2017-08-18 17:29:36 +02:00
{
m_nativeScriptMgr = createNativeScriptMgr();
2017-08-18 17:29:36 +02:00
}
Core::Scripting::ScriptMgr::~ScriptMgr()
2017-08-18 17:29:36 +02:00
{
2017-12-14 22:30:06 +11:00
Watchdog::unwatchAll();
}
2017-08-18 17:29:36 +02:00
void Core::Scripting::ScriptMgr::update()
2017-12-14 22:30:06 +11:00
{
m_nativeScriptMgr->processLoadQueue();
2017-08-18 17:29:36 +02:00
}
bool Core::Scripting::ScriptMgr::init()
{
std::set< std::string > files;
auto pConfig = g_fw.get< XMLConfig >();
auto pLog = g_fw.get< Logger >();
loadDir( pConfig->getValue< std::string >( "Settings.General.Scripts.Path", "./compiledscripts/" ),
files, m_nativeScriptMgr->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_nativeScriptMgr->loadScript( path ) )
2017-12-14 22:30:06 +11:00
scriptsLoaded++;
}
pLog->info( "ScriptMgr: Loaded " + std::to_string( scriptsLoaded ) + "/" + std::to_string( scriptsFound ) + " scripts successfully" );
2017-12-14 22:30:06 +11:00
watchDirectories();
return true;
}
void Core::Scripting::ScriptMgr::watchDirectories()
2017-12-14 22:30:06 +11:00
{
auto pConfig = g_fw.get< XMLConfig >();
auto shouldWatch = pConfig->getValue< bool >( "Settings.General.Scripts.HotSwap.Enabled", true );
2018-01-18 13:56:55 +11:00
if( !shouldWatch )
return;
Watchdog::watchMany( pConfig->getValue< std::string >( "Settings.General.Scripts.Path", "./compiledscripts/" ) + "*" + m_nativeScriptMgr->getModuleExtension(),
2017-12-14 22:30:06 +11:00
[ 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;
}
auto pLog = g_fw.get< Logger >();
2017-12-14 22:30:06 +11:00
for( auto path : paths )
2017-12-14 22:30:06 +11:00
{
if( m_nativeScriptMgr->isModuleLoaded( path.stem().string() ) )
2017-12-14 22:30:06 +11:00
{
pLog->debug( "Reloading changed script: " + path.stem().string() );
2017-12-14 22:30:06 +11:00
m_nativeScriptMgr->queueScriptReload( path.stem( ).string( ));
2017-12-14 22:30:06 +11:00
}
else
{
pLog->debug( "Loading new script: " + path.stem().string() );
2017-12-14 22:30:06 +11:00
m_nativeScriptMgr->loadScript( path.string() );
2017-12-14 22:30:06 +11:00
}
}
});
}
void Core::Scripting::ScriptMgr::loadDir( const std::string& dirname, std::set<std::string> &files, const std::string& ext )
2017-08-18 17:29:36 +02:00
{
auto pLog = g_fw.get< Logger >();
pLog->info( "ScriptEngine: loading scripts from " + dirname );
2017-11-26 00:38:33 +11:00
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::ScriptMgr::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::ScriptMgr::onTalk( Entity::Player& player, uint64_t actorId, uint32_t eventId )
2017-08-18 17:29:36 +02:00
{
auto pExdData = g_fw.get< Data::ExdDataGenerated >();
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
{
auto aetherInfo = pExdData->get< Core::Data::Aetheryte >( 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_nativeScriptMgr->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::ScriptMgr::onEnterTerritory( Entity::Player& player, uint32_t eventId,
2017-08-18 17:29:36 +02:00
uint16_t param1, uint16_t param2 )
{
auto script = m_nativeScriptMgr->getScript< EventScript >( eventId );
if( !script )
return false;
script->onEnterTerritory( player, eventId, param1, param2 );
return true;
2017-08-18 17:29:36 +02:00
}
bool Core::Scripting::ScriptMgr::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_nativeScriptMgr->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::ScriptMgr::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_nativeScriptMgr->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::ScriptMgr::onEmote( Entity::Player& player, uint64_t actorId,
2017-08-18 17:29:36 +02:00
uint32_t eventId, uint8_t emoteId )
{
auto script = m_nativeScriptMgr->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::ScriptMgr::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
{
2018-01-18 21:49:12 +01:00
return false;
2017-08-18 17:29:36 +02:00
}
bool Core::Scripting::ScriptMgr::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_nativeScriptMgr->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::ScriptMgr::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_nativeScriptMgr->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::ScriptMgr::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_nativeScriptMgr->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::ScriptMgr::onCastFinish( Entity::Player& player, Entity::CharaPtr pTarget, uint32_t actionId )
2017-08-18 17:29:36 +02:00
{
auto script = m_nativeScriptMgr->getScript< ActionScript >( actionId );
if( script )
script->onCastFinish( player, *pTarget );
return true;
2017-08-18 17:29:36 +02:00
}
bool Core::Scripting::ScriptMgr::onStatusReceive( Entity::CharaPtr pActor, uint32_t effectId )
2017-08-18 17:29:36 +02:00
{
auto script = m_nativeScriptMgr->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::ScriptMgr::onStatusTick( Entity::CharaPtr pChara, Core::StatusEffect::StatusEffect& effect )
2017-08-18 17:29:36 +02:00
{
auto script = m_nativeScriptMgr->getScript< StatusEffectScript >( effect.getId() );
if( script )
2017-08-18 17:29:36 +02:00
{
if( pChara->isPlayer() )
pChara->getAsPlayer()->sendDebug( "Calling status tick for statusid: " + std::to_string( effect.getId() ) );
2017-08-18 17:29:36 +02:00
script->onTick( *pChara );
return true;
2017-08-18 17:29:36 +02:00
}
return false;
2017-08-18 17:29:36 +02:00
}
bool Core::Scripting::ScriptMgr::onStatusTimeOut( Entity::CharaPtr pChara, uint32_t effectId )
2017-08-18 17:29:36 +02:00
{
auto script = m_nativeScriptMgr->getScript< StatusEffectScript >( effectId );
if( script )
2017-08-18 17:29:36 +02:00
{
if( pChara->isPlayer() )
pChara->getAsPlayer()->sendDebug( "Calling status timeout for statusid: " + std::to_string( effectId ) );
2017-08-18 17:29:36 +02:00
script->onExpire( *pChara );
return true;
2017-08-18 17:29:36 +02:00
}
return false;
2017-08-18 17:29:36 +02:00
}
bool Core::Scripting::ScriptMgr::onZoneInit( ZonePtr pZone )
2017-08-18 17:29:36 +02:00
{
auto script = m_nativeScriptMgr->getScript< ZoneScript >( pZone->getTerritoryId() );
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
}
bool Core::Scripting::ScriptMgr::onInstanceInit( InstanceContentPtr instance )
2018-02-09 02:34:43 +11:00
{
auto script = m_nativeScriptMgr->getScript< InstanceContentScript >( instance->getDirectorId() );
2018-02-09 02:34:43 +11:00
if( script )
{
script->onInit( instance );
return true;
}
return false;
}
bool Core::Scripting::ScriptMgr::onInstanceUpdate( InstanceContentPtr instance, uint32_t currTime )
2018-02-09 02:34:43 +11:00
{
auto script = m_nativeScriptMgr->getScript< InstanceContentScript >( instance->getDirectorId() );
2018-02-09 02:34:43 +11:00
if( script )
{
script->onUpdate( instance, currTime );
return true;
}
return false;
}
bool Core::Scripting::ScriptMgr::onInstanceEnterTerritory( InstanceContentPtr instance, Entity::Player& player, uint32_t eventId, uint16_t param1, uint16_t param2 )
2018-03-05 22:10:14 +11:00
{
auto script = m_nativeScriptMgr->getScript< InstanceContentScript >( instance->getDirectorId() );
2018-03-05 22:10:14 +11:00
if( script )
{
script->onEnterTerritory( player, eventId, param1, param2 );
return true;
}
return false;
}
Scripting::NativeScriptMgr& Core::Scripting::ScriptMgr::getNativeScriptHandler()
{
return *m_nativeScriptMgr;
}