summaryrefslogtreecommitdiff
path: root/src/server/game/Scripting/ScriptDefines/ServerScript.h
blob: 89c29b1f0d1b5fdba1ebf1b3f5e81e19b782a159 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
 * This file is part of the AzerothCore Project. See AUTHORS file for Copyright information
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by the
 * Free Software Foundation; either version 3 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef SCRIPT_OBJECT_SERVER_SCRIPT_H_
#define SCRIPT_OBJECT_SERVER_SCRIPT_H_

#include "ScriptObject.h"
#include <vector>
#include <memory> // NOTE: this import is NEEDED (even though some IDEs report it as unused)

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, std::vector<uint16> enabledHooks = std::vector<uint16>());

public:
    // Called when reactive socket I/O is started (WorldSocketMgr).
    virtual void OnNetworkStart() { }

    // Called when reactive I/O is stopped.
    virtual void OnNetworkStop() { }

    // Called when a remote socket establishes a connection to the server. Do not store the socket object.
    virtual void OnSocketOpen(std::shared_ptr<WorldSocket> /*socket*/) { }

    // Called when a socket is closed. Do not store the socket object, and do not rely on the connection
    // being open; it is not.
    virtual void OnSocketClose(std::shared_ptr<WorldSocket> /*socket*/) { }

    /**
     * @brief This hook called when a packet is sent to a client. The packet object is a copy of the original packet, so reading and modifying it is safe.
     *
     * @param session Contains information about the WorldSession
     * @param packet Contains information about the WorldPacket
     * @return True if you want to continue sending the packet, false if you want to disallow sending the packet
     */
    [[nodiscard]] virtual bool CanPacketSend(WorldSession* /*session*/, WorldPacket& /*packet*/) { return true; }

    /**
     * @brief Called when a (valid) packet is received by a client. The packet object is a copy of the original packet, so
     * reading and modifying it is safe. Make sure to check WorldSession pointer before usage, it might be null in case of auth packets
     *
     * @param session Contains information about the WorldSession
     * @param packet Contains information about the WorldPacket
     * @return True if you want to continue receive the packet, false if you want to disallow receive the packet
     */
    [[nodiscard]] virtual bool CanPacketReceive(WorldSession* /*session*/, WorldPacket& /*packet*/) { return true; }
};

#endif