1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-25 14:07:46 +00:00

simplify script exporting, fix invalid indenting

This commit is contained in:
GokuWeedLord 2017-12-12 14:57:13 +11:00
parent 010d09239d
commit ed8958fb43
8 changed files with 193 additions and 182 deletions

View file

@ -12,4 +12,4 @@ public:
} }
}; };
EXPORT_ACTIONSCRIPT( ActionSprint ) EXPORT_SCRIPTOBJECT( ActionSprint )

View file

@ -26,4 +26,4 @@ public:
}; };
EXPORT_STATUSEFFECTSCRIPT( StatusSprint ) EXPORT_SCRIPTOBJECT( StatusSprint )

View file

@ -15,9 +15,9 @@ namespace Core {
return script->second; return script->second;
} }
ActionScript* NativeScript::getActionScript(uint32_t abilityId) ActionScript* NativeScript::getActionScript( uint32_t actionId )
{ {
auto script = m_actionScripts.find( abilityId ); auto script = m_actionScripts.find( actionId );
if( script == m_actionScripts.end() ) if( script == m_actionScripts.end() )
return nullptr; return nullptr;
@ -51,50 +51,47 @@ namespace Core {
return script->second; return script->second;
} }
bool NativeScript::loadScript( std::string path ) bool NativeScript::loadScript( const std::string& path )
{ {
auto info = m_loader.loadModule( path ); auto module = m_loader.loadModule( path );
if( info ) if( module )
{ {
// todo: this is shit auto script = m_loader.getScriptObject( module->handle );
if( auto script = m_loader.getScriptObject< StatusEffectScript >( info->handle, "StatusEffectScript" ) ) if( script )
{ {
// todo: make this a define or something module->script = script;
info->script = script; module->script_name = script->getName();
info->script_name = script->getName(); module->type = script->getType();
m_statusEffectScripts[ script->getId() ] = script;
switch( script->getType() )
{
case ScriptType::StatusEffect:
m_statusEffectScripts[ script->getId() ] = dynamic_cast< StatusEffectScript* >( script );
break;
case ScriptType::Action:
m_actionScripts[ script->getId() ] = dynamic_cast< ActionScript* >( script );
break;
case ScriptType::Quest:
m_questScripts[ script->getId() ] = dynamic_cast< QuestScript* >( script );
break;
case ScriptType::BattleNpc:
m_battleNpcScripts[ script->getId() ] = dynamic_cast< BattleNpcScript* >( script );
break;
case ScriptType::Zone:
m_zoneScripts[ script->getId() ] = dynamic_cast< ZoneScript* >( script );
break;
default:
m_loader.unloadScript( module );
return false;
} }
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 else
{ {
// unload anything which doesn't have a suitable export m_loader.unloadScript( module );
m_loader.unloadScript( info->handle );
return false; return false;
} }
} }
else else
return false; return false;
@ -107,24 +104,36 @@ namespace Core {
return m_loader.getModuleExtension(); return m_loader.getModuleExtension();
} }
bool NativeScript::unloadScript( std::string name ) bool NativeScript::unloadScript( const std::string& name )
{ {
auto info = m_loader.getScriptInfo( name ); auto info = m_loader.getScriptInfo( name );
if( info ) if( info )
{ {
auto ptr = info->script; auto ptr = info->script;
if( removeValueFromMap< uint32_t, StatusEffectScript* >( ptr, m_statusEffectScripts ) ) switch( info->type )
return m_loader.unloadScript( info ); {
else if( removeValueFromMap< uint32_t, ActionScript* >( ptr, m_actionScripts ) ) case ScriptType::StatusEffect:
return m_loader.unloadScript( info ); removeValueFromMap< uint32_t, StatusEffectScript* >( ptr, m_statusEffectScripts );
else if( removeValueFromMap< uint32_t, QuestScript* >( ptr, m_questScripts ) ) break;
return m_loader.unloadScript( info ); case ScriptType::Action:
else if( removeValueFromMap< uint32_t, BattleNpcScript* >( ptr, m_battleNpcScripts ) ) removeValueFromMap< uint32_t, ActionScript* >( ptr, m_actionScripts );
return m_loader.unloadScript( info ); break;
else if( removeValueFromMap< uint32_t, ZoneScript* >( ptr, m_zoneScripts ) ) case ScriptType::Quest:
return m_loader.unloadScript( info ); removeValueFromMap< uint32_t, QuestScript* >( ptr, m_questScripts );
break;
case ScriptType::BattleNpc:
removeValueFromMap< uint32_t, BattleNpcScript* >( ptr, m_battleNpcScripts );
break;
case ScriptType::Zone:
removeValueFromMap< uint32_t, ZoneScript* >( ptr, m_zoneScripts );
break;
default:
return false;
}
return m_loader.unloadScript( info );
} }
return false; return false;

View file

@ -29,13 +29,13 @@ namespace Core {
NativeScript( ); NativeScript( );
StatusEffectScript* getStatusEffectScript( uint32_t statusId ); StatusEffectScript* getStatusEffectScript( uint32_t statusId );
ActionScript* getActionScript(uint32_t abilityId); ActionScript* getActionScript( uint32_t actionId );
QuestScript* getQuestScript( uint32_t questId ); QuestScript* getQuestScript( uint32_t questId );
BattleNpcScript* getBattleNpcScript( uint32_t npcId ); BattleNpcScript* getBattleNpcScript( uint32_t npcId );
ZoneScript* getZoneScript( uint32_t zoneId ); ZoneScript* getZoneScript( uint32_t zoneId );
bool loadScript( std::string ); bool loadScript( const std::string& path );
bool unloadScript( std::string ); bool unloadScript( const std::string& name );
const std::string getModuleExtension(); const std::string getModuleExtension();

View file

@ -12,28 +12,33 @@
#define EXPORT __attribute__((visibility("default"))) #define EXPORT __attribute__((visibility("default")))
#endif #endif
#define EXPORT_SCRIPTOBJECT( type, base ) \ #define EXPORT_SCRIPTOBJECT( type ) \
extern "C" EXPORT base* get##base() \ extern "C" EXPORT ScriptObject* getScript() \
{ return static_cast< base* >( new type ); } { return static_cast< ScriptObject* >( new type ); }
#define EXPORT_STATUSEFFECTSCRIPT( type ) EXPORT_SCRIPTOBJECT( type, StatusEffectScript )
#define EXPORT_ACTIONSCRIPT( type ) EXPORT_SCRIPTOBJECT( type, ActionScript )
#define EXPORT_QUESTSCRIPT( type ) EXPORT_SCRIPTOBJECT( type, QuestScript )
#define EXPORT_BATTLENPCSCRIPT( type ) EXPORT_SCRIPTOBJECT( type, BattleNpcScript )
#define EXPORT_ZONESCRIPT( type ) EXPORT_SCRIPTOBJECT( type, ZoneScript )
using namespace Core; using namespace Core;
enum ScriptType
{
StatusEffect,
Action,
Quest,
BattleNpc,
Zone
};
class ScriptObject class ScriptObject
{ {
protected: protected:
std::string m_scriptName; std::string m_scriptName;
uint32_t m_id; uint32_t m_id;
ScriptType m_type;
public: public:
ScriptObject( std::string name, uint32_t id ) : ScriptObject( std::string name, uint32_t id, ScriptType type ) :
m_scriptName( name ), m_scriptName( name ),
m_id( id ) m_id( id ),
m_type( type )
{ } { }
virtual const std::string& getName() const virtual const std::string& getName() const
@ -45,6 +50,11 @@ public:
{ {
return m_id; return m_id;
} }
virtual ScriptType getType() const
{
return m_type;
}
}; };
@ -52,7 +62,7 @@ class StatusEffectScript : public ScriptObject
{ {
public: public:
StatusEffectScript( std::string name, uint32_t effectId ) : StatusEffectScript( std::string name, uint32_t effectId ) :
ScriptObject( name, effectId ) ScriptObject( name, effectId, ScriptType::StatusEffect )
{ } { }
virtual void onTick( Entity::Actor& actor ) { } virtual void onTick( Entity::Actor& actor ) { }
@ -70,7 +80,7 @@ class ActionScript : public ScriptObject
{ {
public: public:
ActionScript( std::string name, uint32_t abilityId ) : ActionScript( std::string name, uint32_t abilityId ) :
ScriptObject( name, abilityId ) ScriptObject( name, abilityId, ScriptType::Action )
{ } { }
virtual void onStart( Entity::Actor& sourceActor, Entity::Actor& targetActor ) { } virtual void onStart( Entity::Actor& sourceActor, Entity::Actor& targetActor ) { }
@ -83,7 +93,7 @@ class QuestScript : public ScriptObject
{ {
public: public:
QuestScript( std::string name, uint32_t questId ) : QuestScript( std::string name, uint32_t questId ) :
ScriptObject( name, questId ) ScriptObject( name, questId, ScriptType::Quest )
{ } { }
virtual void onTalk( uint32_t eventId, Entity::Player& player, uint64_t actorId ) { } virtual void onTalk( uint32_t eventId, Entity::Player& player, uint64_t actorId ) { }
@ -96,7 +106,7 @@ class BattleNpcScript : public ScriptObject
{ {
public: public:
BattleNpcScript( std::string name, uint32_t npcId ) : BattleNpcScript( std::string name, uint32_t npcId ) :
ScriptObject( name, npcId ) ScriptObject( name, npcId, ScriptType::BattleNpc )
{ } { }
}; };
@ -104,7 +114,7 @@ class ZoneScript : public ScriptObject
{ {
public: public:
ZoneScript( std::string name, uint32_t zoneId ) : ZoneScript( std::string name, uint32_t zoneId ) :
ScriptObject( name, zoneId ) ScriptObject( name, zoneId, ScriptType::Zone )
{ } { }
virtual void onZoneInit() { } virtual void onZoneInit() { }

View file

@ -22,6 +22,7 @@ namespace Scripting {
std::string script_name; std::string script_name;
ModuleHandle handle; ModuleHandle handle;
ScriptObject* script; ScriptObject* script;
ScriptType type;
}; };
} }
} }

View file

@ -41,7 +41,7 @@ bool Core::Scripting::ScriptLoader::unloadModule( ModuleHandle handle )
return true; return true;
} }
Core::Scripting::ScriptInfo* Core::Scripting::ScriptLoader::loadModule( std::string path ) Core::Scripting::ScriptInfo* Core::Scripting::ScriptLoader::loadModule( const std::string& path )
{ {
boost::filesystem::path f( path ); boost::filesystem::path f( path );
@ -76,17 +76,14 @@ Core::Scripting::ScriptInfo* Core::Scripting::ScriptLoader::loadModule( std::str
return info; return info;
} }
ScriptObject* Core::Scripting::ScriptLoader::getScriptObjectExport( ModuleHandle handle, std::string name ) ScriptObject* Core::Scripting::ScriptLoader::getScriptObject( ModuleHandle handle )
{ {
typedef ScriptObject* (*getScriptObjectType)(); typedef ScriptObject* (*getScriptObjectType)();
auto fn = boost::str( boost::format( "get%1%" ) % name );
g_log.debug( "getting symbol: " + fn );
#ifdef _WIN32 #ifdef _WIN32
getScriptObjectType func = reinterpret_cast< getScriptObjectType >( GetProcAddress( handle, fn.c_str() ) ); getScriptObjectType func = reinterpret_cast< getScriptObjectType >( GetProcAddress( handle, "getScript" ) );
#else #else
getScriptObjectType func = reinterpret_cast< getScriptObjectType >( dlsym( handle, fn.c_str() ) ); getScriptObjectType func = reinterpret_cast< getScriptObjectType >( dlsym( handle, "getScript" ) );
#endif #endif
if( func ) if( func )

View file

@ -27,18 +27,12 @@ namespace Scripting {
ScriptLoader(); ScriptLoader();
const std::string getModuleExtension(); const std::string getModuleExtension();
ScriptInfo* loadModule( std::string ); ScriptInfo* loadModule( const std::string& );
bool unloadScript( ScriptInfo* ); bool unloadScript( ScriptInfo* );
bool unloadScript( ModuleHandle ); bool unloadScript( ModuleHandle );
ScriptInfo* getScriptInfo( std::string name ); ScriptInfo* getScriptInfo( std::string name );
ScriptObject* getScriptObjectExport( ModuleHandle handle, std::string name ); ScriptObject* getScriptObject(ModuleHandle handle);
bool isModuleLoaded( std::string name ); bool isModuleLoaded( std::string name );
template< typename T >
T* getScriptObject( ModuleHandle handle, std::string name )
{
return static_cast< T* >( getScriptObjectExport( handle, name ) );
}
}; };
} }