From 15d6ae7e0d21799e49cbfb3a9cc03854905bab9d Mon Sep 17 00:00:00 2001 From: Adam Date: Wed, 17 Jan 2018 20:09:16 +1100 Subject: [PATCH] use typeinfo for script types instead of an enum --- .../sapphire_zone/Script/NativeScriptApi.h | 29 +++++++------------ .../Script/NativeScriptManager.h | 6 ++-- src/servers/sapphire_zone/Script/ScriptInfo.h | 2 +- .../sapphire_zone/Script/ScriptManager.cpp | 26 ++++++++--------- 4 files changed, 29 insertions(+), 34 deletions(-) diff --git a/src/servers/sapphire_zone/Script/NativeScriptApi.h b/src/servers/sapphire_zone/Script/NativeScriptApi.h index 65d317d4..4835027d 100644 --- a/src/servers/sapphire_zone/Script/NativeScriptApi.h +++ b/src/servers/sapphire_zone/Script/NativeScriptApi.h @@ -2,6 +2,9 @@ #define NATIVE_SCRIPT_API #include +#include +#include + #include #include #include @@ -18,25 +21,15 @@ using namespace Core; #define EVENTSCRIPT_AETHERYTE_ID 0x50000 #define EVENTSCRIPT_AETHERNET_ID 0x50001 -enum ScriptType -{ - None, - ScriptedStatusEffect, - ScriptedAction, - ScriptedEvent, - ScriptedBattleNpc, - ScriptedZone -}; - class ScriptObject { protected: std::string m_scriptName; uint32_t m_id; - ScriptType m_type; + std::size_t m_type; public: - ScriptObject( std::string name, uint32_t id, ScriptType type ) : + ScriptObject( std::string name, uint32_t id, std::size_t type ) : m_scriptName( name ), m_id( id ), m_type( type ) @@ -52,7 +45,7 @@ public: return m_id; } - virtual ScriptType getType() const + virtual std::size_t getType() const { return m_type; } @@ -63,7 +56,7 @@ class StatusEffectScript : public ScriptObject { public: StatusEffectScript( std::string name, uint32_t effectId ) : - ScriptObject( name, effectId, ScriptType::ScriptedStatusEffect ) + ScriptObject( name, effectId, typeid( StatusEffectScript ).hash_code() ) { } virtual void onTick( Entity::Actor& actor ) { } @@ -81,7 +74,7 @@ class ActionScript : public ScriptObject { public: ActionScript( std::string name, uint32_t abilityId ) : - ScriptObject( name, abilityId, ScriptType::ScriptedAction ) + ScriptObject( name, abilityId, typeid( ActionScript ).hash_code() ) { } virtual void onStart( Entity::Actor& sourceActor, Entity::Actor& targetActor ) { } @@ -94,7 +87,7 @@ class EventScript : public ScriptObject { public: EventScript( std::string name, uint32_t questId ) : - ScriptObject( name, questId, ScriptType::ScriptedEvent ) + ScriptObject( name, questId, typeid( EventScript ).hash_code() ) { } virtual void onTalk( uint32_t eventId, Entity::Player& player, uint64_t actorId ) { } @@ -112,7 +105,7 @@ class BattleNpcScript : public ScriptObject { public: BattleNpcScript( std::string name, uint32_t npcId ) : - ScriptObject( name, npcId, ScriptType::ScriptedBattleNpc ) + ScriptObject( name, npcId, typeid( BattleNpcScript ).hash_code() ) { } }; @@ -120,7 +113,7 @@ class ZoneScript : public ScriptObject { public: ZoneScript( std::string name, uint32_t zoneId ) : - ScriptObject( name, zoneId, ScriptType::ScriptedZone ) + ScriptObject( name, zoneId, typeid( ZoneScript ).hash_code() ) { } virtual void onZoneInit() { } diff --git a/src/servers/sapphire_zone/Script/NativeScriptManager.h b/src/servers/sapphire_zone/Script/NativeScriptManager.h index af3e256c..38588ffd 100644 --- a/src/servers/sapphire_zone/Script/NativeScriptManager.h +++ b/src/servers/sapphire_zone/Script/NativeScriptManager.h @@ -19,7 +19,7 @@ namespace Scripting { class NativeScriptManager { protected: - std::unordered_map< ScriptType, std::unordered_map< uint32_t, ScriptObject* > > m_scripts; + std::unordered_map< std::size_t, std::unordered_map< uint32_t, ScriptObject* > > m_scripts; ScriptLoader m_loader; @@ -43,8 +43,10 @@ namespace Scripting { // todo: use some template magic (type_traits is_same?) to avoid ScriptType param // not sure if worthwhile given that it adds an extra place where script types need to be managed template< typename T > - T* getScript( ScriptType type, uint32_t scriptId ) + T* getScript( uint32_t scriptId ) { + auto type = typeid( T ).hash_code(); + auto script = m_scripts[type].find( scriptId ); if( script == m_scripts[type].end() ) return nullptr; diff --git a/src/servers/sapphire_zone/Script/ScriptInfo.h b/src/servers/sapphire_zone/Script/ScriptInfo.h index 7ddabc18..ae9fafb0 100644 --- a/src/servers/sapphire_zone/Script/ScriptInfo.h +++ b/src/servers/sapphire_zone/Script/ScriptInfo.h @@ -26,7 +26,7 @@ namespace Scripting { ModuleHandle handle; std::vector< ScriptObject* > scripts; - ScriptType type; + std::size_t type; }; } } diff --git a/src/servers/sapphire_zone/Script/ScriptManager.cpp b/src/servers/sapphire_zone/Script/ScriptManager.cpp index f949c249..40ec2bdc 100644 --- a/src/servers/sapphire_zone/Script/ScriptManager.cpp +++ b/src/servers/sapphire_zone/Script/ScriptManager.cpp @@ -156,7 +156,7 @@ bool Core::Scripting::ScriptManager::onTalk( Entity::Player& player, uint64_t ac scriptId = EVENTSCRIPT_AETHERNET_ID; } - auto script = m_nativeScriptManager->getScript< EventScript >( ScriptType::ScriptedEvent, scriptId ); + auto script = m_nativeScriptManager->getScript< EventScript >( scriptId ); if( !script ) return false; script->onTalk( eventId, player, actorId ); @@ -166,7 +166,7 @@ bool Core::Scripting::ScriptManager::onTalk( Entity::Player& player, uint64_t ac bool Core::Scripting::ScriptManager::onEnterTerritory( Entity::Player& player, uint32_t eventId, uint16_t param1, uint16_t param2 ) { - auto script = m_nativeScriptManager->getScript< EventScript >( ScriptType::ScriptedEvent, eventId ); + auto script = m_nativeScriptManager->getScript< EventScript >( eventId ); if( !script ) return false; script->onEnterZone( player, eventId, param1, param2 ); @@ -176,7 +176,7 @@ bool Core::Scripting::ScriptManager::onEnterTerritory( Entity::Player& player, u bool Core::Scripting::ScriptManager::onWithinRange( Entity::Player& player, uint32_t eventId, uint32_t param1, float x, float y, float z ) { - auto script = m_nativeScriptManager->getScript< EventScript >( ScriptType::ScriptedEvent, eventId ); + auto script = m_nativeScriptManager->getScript< EventScript >( eventId ); if( !script ) return false; script->onWithinRange( player, eventId, param1, x, y, z ); @@ -186,7 +186,7 @@ bool Core::Scripting::ScriptManager::onWithinRange( Entity::Player& player, uint bool Core::Scripting::ScriptManager::onOutsideRange( Entity::Player& player, uint32_t eventId, uint32_t param1, float x, float y, float z ) { - auto script = m_nativeScriptManager->getScript< EventScript >( ScriptType::ScriptedEvent, eventId ); + auto script = m_nativeScriptManager->getScript< EventScript >( eventId ); if( !script ) return false; script->onOutsideRange( player, eventId, param1, x, y, z ); @@ -196,7 +196,7 @@ bool Core::Scripting::ScriptManager::onOutsideRange( Entity::Player& player, uin bool Core::Scripting::ScriptManager::onEmote( Entity::Player& player, uint64_t actorId, uint32_t eventId, uint8_t emoteId ) { - auto script = m_nativeScriptManager->getScript< EventScript >( ScriptType::ScriptedEvent, eventId ); + auto script = m_nativeScriptManager->getScript< EventScript >( eventId ); if( !script ) return false; script->onEmote( actorId, eventId, emoteId, player ); @@ -235,7 +235,7 @@ bool Core::Scripting::ScriptManager::onEventHandlerReturn( Entity::Player& playe bool Core::Scripting::ScriptManager::onEventHandlerTradeReturn( Entity::Player& player, uint32_t eventId, uint16_t subEvent, uint16_t param, uint32_t catalogId ) { - auto script = m_nativeScriptManager->getScript< EventScript >( ScriptType::ScriptedEvent, eventId ); + auto script = m_nativeScriptManager->getScript< EventScript >( eventId ); if( script ) { script->onEventHandlerTradeReturn( player, eventId, subEvent, param, catalogId ); @@ -252,7 +252,7 @@ bool Core::Scripting::ScriptManager::onEventItem( Entity::Player& player, uint32 std::string objName = Event::getEventName( eventId ); player.sendDebug( "Calling: " + objName + "." + eventName + " - " + std::to_string( eventId ) ); - auto script = m_nativeScriptManager->getScript< EventScript >( ScriptType::ScriptedEvent, eventId ); + auto script = m_nativeScriptManager->getScript< EventScript >( eventId ); if( script ) { player.eventStart( targetId, eventId, Event::EventHandler::Item, 0, 0 ); @@ -278,7 +278,7 @@ bool Core::Scripting::ScriptManager::onMobKill( Entity::Player& player, uint16_t uint16_t questId = activeQuests->c.questId; - auto script = m_nativeScriptManager->getScript< EventScript >( ScriptType::ScriptedEvent, questId ); + auto script = m_nativeScriptManager->getScript< EventScript >( questId ); if( script ) { std::string objName = Event::getEventName( 0x00010000 | questId ); @@ -294,7 +294,7 @@ bool Core::Scripting::ScriptManager::onMobKill( Entity::Player& player, uint16_t bool Core::Scripting::ScriptManager::onCastFinish( Entity::Player& player, Entity::ActorPtr pTarget, uint32_t actionId ) { - auto script = m_nativeScriptManager->getScript< ActionScript >( ScriptType::ScriptedAction, actionId ); + auto script = m_nativeScriptManager->getScript< ActionScript >( actionId ); if( script ) script->onCastFinish( player, *pTarget ); @@ -303,7 +303,7 @@ bool Core::Scripting::ScriptManager::onCastFinish( Entity::Player& player, Entit bool Core::Scripting::ScriptManager::onStatusReceive( Entity::ActorPtr pActor, uint32_t effectId ) { - auto script = m_nativeScriptManager->getScript< StatusEffectScript >( ScriptType::ScriptedStatusEffect, effectId ); + auto script = m_nativeScriptManager->getScript< StatusEffectScript >( effectId ); if( script ) { @@ -319,7 +319,7 @@ bool Core::Scripting::ScriptManager::onStatusReceive( Entity::ActorPtr pActor, u bool Core::Scripting::ScriptManager::onStatusTick( Entity::ActorPtr pActor, Core::StatusEffect::StatusEffect& effect ) { - auto script = m_nativeScriptManager->getScript< StatusEffectScript >( ScriptType::ScriptedStatusEffect, effect.getId() ); + auto script = m_nativeScriptManager->getScript< StatusEffectScript >( effect.getId() ); if( script ) { if( pActor->isPlayer() ) @@ -334,7 +334,7 @@ bool Core::Scripting::ScriptManager::onStatusTick( Entity::ActorPtr pActor, Core bool Core::Scripting::ScriptManager::onStatusTimeOut( Entity::ActorPtr pActor, uint32_t effectId ) { - auto script = m_nativeScriptManager->getScript< StatusEffectScript >( ScriptType::ScriptedStatusEffect, effectId ); + auto script = m_nativeScriptManager->getScript< StatusEffectScript >( effectId ); if( script ) { if( pActor->isPlayer() ) @@ -349,7 +349,7 @@ bool Core::Scripting::ScriptManager::onStatusTimeOut( Entity::ActorPtr pActor, u bool Core::Scripting::ScriptManager::onZoneInit( ZonePtr pZone ) { - auto script = m_nativeScriptManager->getScript< ZoneScript >( ScriptType::ScriptedZone, pZone->getId() ); + auto script = m_nativeScriptManager->getScript< ZoneScript >( pZone->getId() ); if( script ) { script->onZoneInit();