mirror of
https://github.com/mod-playerbots/azerothcore-wotlk.git
synced 2026-01-14 09:39:11 +00:00
feat(Core/Scripts): Optimize ServerScript (#18721)
* Add files via upload * Add files via upload * Add files via upload * Add files via upload * Add files via upload * Update ServerScript.h
This commit is contained in:
@@ -21,58 +21,26 @@
|
||||
|
||||
void ScriptMgr::OnNetworkStart()
|
||||
{
|
||||
ExecuteScript<ServerScript>([&](ServerScript* script)
|
||||
{
|
||||
script->OnNetworkStart();
|
||||
});
|
||||
CALL_ENABLED_HOOKS(ServerScript, SERVERHOOK_ON_NETWORK_START, script->OnNetworkStart());
|
||||
}
|
||||
|
||||
void ScriptMgr::OnNetworkStop()
|
||||
{
|
||||
ExecuteScript<ServerScript>([&](ServerScript* script)
|
||||
{
|
||||
script->OnNetworkStop();
|
||||
});
|
||||
CALL_ENABLED_HOOKS(ServerScript, SERVERHOOK_ON_NETWORK_STOP, script->OnNetworkStop());
|
||||
}
|
||||
|
||||
void ScriptMgr::OnSocketOpen(std::shared_ptr<WorldSocket> socket)
|
||||
{
|
||||
ASSERT(socket);
|
||||
|
||||
ExecuteScript<ServerScript>([&](ServerScript* script)
|
||||
{
|
||||
script->OnSocketOpen(socket);
|
||||
});
|
||||
CALL_ENABLED_HOOKS(ServerScript, SERVERHOOK_ON_SOCKET_OPEN, script->OnSocketOpen(socket));
|
||||
}
|
||||
|
||||
void ScriptMgr::OnSocketClose(std::shared_ptr<WorldSocket> socket)
|
||||
{
|
||||
ASSERT(socket);
|
||||
|
||||
ExecuteScript<ServerScript>([&](ServerScript* script)
|
||||
{
|
||||
script->OnSocketClose(socket);
|
||||
});
|
||||
}
|
||||
|
||||
bool ScriptMgr::CanPacketReceive(WorldSession* session, WorldPacket const& packet)
|
||||
{
|
||||
if (ScriptRegistry<ServerScript>::ScriptPointerList.empty())
|
||||
return true;
|
||||
|
||||
WorldPacket copy(packet);
|
||||
|
||||
auto ret = IsValidBoolScript<ServerScript>([&](ServerScript* script)
|
||||
{
|
||||
return !script->CanPacketReceive(session, copy);
|
||||
});
|
||||
|
||||
if (ret && *ret)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
CALL_ENABLED_HOOKS(ServerScript, SERVERHOOK_ON_SOCKET_CLOSE, script->OnSocketClose(socket));
|
||||
}
|
||||
|
||||
bool ScriptMgr::CanPacketSend(WorldSession* session, WorldPacket const& packet)
|
||||
@@ -84,23 +52,28 @@ bool ScriptMgr::CanPacketSend(WorldSession* session, WorldPacket const& packet)
|
||||
|
||||
WorldPacket copy(packet);
|
||||
|
||||
auto ret = IsValidBoolScript<ServerScript>([&](ServerScript* script)
|
||||
{
|
||||
return !script->CanPacketSend(session, copy);
|
||||
});
|
||||
|
||||
if (ret && *ret)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
CALL_ENABLED_BOOLEAN_HOOKS(ServerScript, SERVERHOOK_CAN_PACKET_SEND, !script->CanPacketSend(session, copy));
|
||||
}
|
||||
|
||||
ServerScript::ServerScript(const char* name)
|
||||
: ScriptObject(name)
|
||||
bool ScriptMgr::CanPacketReceive(WorldSession* session, WorldPacket const& packet)
|
||||
{
|
||||
ScriptRegistry<ServerScript>::AddScript(this);
|
||||
if (ScriptRegistry<ServerScript>::ScriptPointerList.empty())
|
||||
return true;
|
||||
|
||||
WorldPacket copy(packet);
|
||||
|
||||
CALL_ENABLED_BOOLEAN_HOOKS(ServerScript, SERVERHOOK_CAN_PACKET_RECEIVE, !script->CanPacketReceive(session, copy));
|
||||
}
|
||||
|
||||
ServerScript::ServerScript(const char* name, std::vector<uint16> enabledHooks)
|
||||
: ScriptObject(name, SERVERHOOK_END)
|
||||
{
|
||||
// If empty - enable all available hooks.
|
||||
if (enabledHooks.empty())
|
||||
for (uint16 i = 0; i < SERVERHOOK_END; ++i)
|
||||
enabledHooks.emplace_back(i);
|
||||
|
||||
ScriptRegistry<ServerScript>::AddScript(this, std::move(enabledHooks));
|
||||
}
|
||||
|
||||
template class AC_GAME_API ScriptRegistry<ServerScript>;
|
||||
|
||||
@@ -19,11 +19,23 @@
|
||||
#define SCRIPT_OBJECT_SERVER_SCRIPT_H_
|
||||
|
||||
#include "ScriptObject.h"
|
||||
#include <vector>
|
||||
|
||||
enum ServerHook
|
||||
{
|
||||
SERVERHOOK_ON_NETWORK_START,
|
||||
SERVERHOOK_ON_NETWORK_STOP,
|
||||
SERVERHOOK_ON_SOCKET_OPEN,
|
||||
SERVERHOOK_ON_SOCKET_CLOSE,
|
||||
SERVERHOOK_CAN_PACKET_SEND,
|
||||
SERVERHOOK_CAN_PACKET_RECEIVE,
|
||||
SERVERHOOK_END
|
||||
};
|
||||
|
||||
class ServerScript : public ScriptObject
|
||||
{
|
||||
protected:
|
||||
ServerScript(const char* name);
|
||||
ServerScript(const char* name, std::vector<uint16> enabledHooks = std::vector<uint16>());
|
||||
|
||||
public:
|
||||
// Called when reactive socket I/O is started (WorldSocketMgr).
|
||||
|
||||
Reference in New Issue
Block a user