forked from ElunaLuaEngine/Eluna
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ElunaInstanceAI.h
166 lines (149 loc) · 4.52 KB
/
ElunaInstanceAI.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* Copyright (C) 2010 - 2024 Eluna Lua Engine <https://elunaluaengine.github.io/>
* This program is free software licensed under GPL version 3
* Please see the included DOCS/LICENSE.md for more information
*/
#ifndef _ELUNA_INSTANCE_DATA_H
#define _ELUNA_INSTANCE_DATA_H
#include "LuaEngine.h"
#if defined ELUNA_TRINITY
#include "InstanceScript.h"
#include "Map.h"
#elif defined ELUNA_CMANGOS
#include "Maps/InstanceData.h"
#else
#include "InstanceData.h"
#endif
/*
* This class is a small wrapper around `InstanceData`,
* allowing instances to be scripted with Eluna.
*
*
* Note 1
* ======
*
* Instances of `ElunaInstanceAI` are owned by the core, so they
* are not deleted when Eluna is reloaded. Thus `Load` is only called
* by the core once, no matter how many times Eluna is reloaded.
*
* However, when Eluna reloads, all instance data in Eluna is lost.
* So the solution is as follows:
*
* 1. Store the last save data in the member var `lastSaveData`.
*
* At first this is just the data given to us by the core when it calls `Load`,
* but later on once we start saving new data this is from Eluna.
*
* 2. When retrieving instance data from Eluna, check if it's missing.
*
* The data will be missing if Eluna is reloaded, since a new Lua state is created.
*
* 3. If it *is* missing, call `Reload`.
*
* This reloads the last known instance save data into Eluna, and calls the appropriate hooks.
*
*
* Note 2
* ======
*
* CMaNGOS expects some of these methods to be `const`. However, any of these
* methods are free to call `Save`, resulting in mutation of `lastSaveData`.
*
* Therefore, none of the hooks are `const`-safe, and `const_cast` is used
* to escape from these restrictions.
*/
class ElunaInstanceAI : public InstanceData
{
private:
// The last save data to pass through this class,
// either through `Load` or `Save`.
std::string lastSaveData;
public:
#if defined ELUNA_TRINITY
ElunaInstanceAI(Map* map) : InstanceData(map->ToInstanceMap())
{
}
#else
ElunaInstanceAI(Map* map) : InstanceData(map)
{
}
#endif
#if !defined ELUNA_TRINITY
void Initialize() override;
#endif
/*
* These are responsible for serializing/deserializing the instance's
* data table to/from the core.
*/
void Load(const char* data) override;
#if defined ELUNA_TRINITY
// Simply calls Save, since the functions are a bit different in name and data types on different cores
std::string GetSaveData() override
{
return Save();
}
const char* Save() const;
#elif defined ELUNA_VMANGOS
const char* Save() const;
#else
const char* Save() const override;
#endif
/*
* Calls `Load` with the last save data that was passed to
* or from Eluna.
*
* See: big documentation blurb at the top of this class.
*/
void Reload()
{
Load(NULL);
}
/*
* These methods allow non-Lua scripts (e.g. DB, C++) to get/set instance data.
*/
#if !defined ELUNA_VMANGOS
uint32 GetData(uint32 key) const override;
#else
uint32 GetData(uint32 key) const;
#endif
void SetData(uint32 key, uint32 value) override;
#if !defined ELUNA_VMANGOS
uint64 GetData64(uint32 key) const override;
#else
uint64 GetData64(uint32 key) const;
#endif
void SetData64(uint32 key, uint64 value) override;
/*
* These methods are just thin wrappers around Eluna.
*/
void Update(uint32 diff) override
{
// If Eluna is reloaded, it will be missing our instance data.
// Reload here instead of waiting for the next hook call (possibly never).
// This avoids having to have an empty Update hook handler just to trigger the reload.
if (!instance->GetEluna()->HasInstanceData(instance))
Reload();
instance->GetEluna()->OnUpdateInstance(this, diff);
}
bool IsEncounterInProgress() const override
{
return instance->GetEluna()->OnCheckEncounterInProgress(const_cast<ElunaInstanceAI*>(this));
}
void OnPlayerEnter(Player* player) override
{
instance->GetEluna()->OnPlayerEnterInstance(this, player);
}
#if defined ELUNA_TRINITY
void OnGameObjectCreate(GameObject* gameobject) override
#else
void OnObjectCreate(GameObject* gameobject) override
#endif
{
instance->GetEluna()->OnGameObjectCreate(this, gameobject);
}
void OnCreatureCreate(Creature* creature) override
{
instance->GetEluna()->OnCreatureCreate(this, creature);
}
};
#endif // _ELUNA_INSTANCE_DATA_H