-
Notifications
You must be signed in to change notification settings - Fork 34
/
IRuntimeEventHandler.h
86 lines (74 loc) · 2.34 KB
/
IRuntimeEventHandler.h
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
76
77
78
79
80
81
82
83
84
85
86
#pragma once
#include "cpp-sdk/SDK.h"
#include <array>
class IRuntimeEventHandler
{
static IRuntimeEventHandler*& _instance()
{
static IRuntimeEventHandler* instance = nullptr;
return instance;
}
using EventType = alt::CEvent::Type;
// All events the module uses, which need to be enabled and never disabled
static constexpr std::array internalEvents = {
EventType::CONNECTION_COMPLETE, EventType::DISCONNECT_EVENT, EventType::GAME_ENTITY_CREATE, EventType::GAME_ENTITY_DESTROY, EventType::RESOURCE_STOP,
EventType::DISCONNECT_EVENT,
// RPC events
EventType::SCRIPT_RPC_EVENT, EventType::SCRIPT_RPC_ANSWER_EVENT
#ifdef ALT_SERVER_API
// required for vehicle seat stuff
, EventType::PLAYER_ENTER_VEHICLE, EventType::PLAYER_LEAVE_VEHICLE, EventType::PLAYER_CHANGE_VEHICLE_SEAT,
#endif
};
// Keeps track of the event handlers registered for all events
std::unordered_map<alt::CEvent::Type, uint32_t> eventHandlersCount;
static void SetInstance(IRuntimeEventHandler* handler)
{
_instance() = handler;
}
public:
constexpr static bool IsNeededEvent(alt::CEvent::Type type)
{
for(alt::CEvent::Type evType : internalEvents)
{
if(evType == type) return true;
}
return false;
}
void Start()
{
SetInstance(this);
Reset();
}
void Reset()
{
// Enable all events the module needs
for(EventType type : internalEvents)
{
alt::ICore::Instance().ToggleEvent(type, true);
}
eventHandlersCount.clear();
}
void EventHandlerAdded(alt::CEvent::Type type)
{
// If the event is needed, we don't need to keep track of the amount of handlers for it
if(IsNeededEvent(type)) return;
if(eventHandlersCount[type]++ == 0)
{
alt::ICore::Instance().ToggleEvent(type, true);
}
}
void EventHandlerRemoved(alt::CEvent::Type type)
{
if(IsNeededEvent(type)) return;
if(--eventHandlersCount[type] == 0)
{
// If there are no more handlers for this event, we don't need it anymore
alt::ICore::Instance().ToggleEvent(type, false);
}
}
static IRuntimeEventHandler& Instance()
{
return *_instance();
}
};