-
Notifications
You must be signed in to change notification settings - Fork 361
/
ElunaInstanceAI.cpp
231 lines (185 loc) · 5.67 KB
/
ElunaInstanceAI.cpp
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* 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
*/
#include "ElunaInstanceAI.h"
#include "ElunaUtility.h"
#include "lmarshal.h"
#if !defined ELUNA_TRINITY
void ElunaInstanceAI::Initialize()
{
ASSERT(!instance->GetEluna()->HasInstanceData(instance));
// Create a new table for instance data.
lua_State* L = instance->GetEluna()->L;
lua_newtable(L);
instance->GetEluna()->CreateInstanceData(instance);
instance->GetEluna()->OnInitialize(this);
}
#endif
void ElunaInstanceAI::Load(const char* data)
{
// If we get passed NULL (i.e. `Reload` was called) then use
// the last known save data (or maybe just an empty string).
if (!data)
{
data = lastSaveData.c_str();
}
else // Otherwise, copy the new data into our buffer.
{
lastSaveData.assign(data);
}
if (data[0] == '\0')
{
ASSERT(!instance->GetEluna()->HasInstanceData(instance));
// Create a new table for instance data.
lua_State* L = instance->GetEluna()->L;
lua_newtable(L);
instance->GetEluna()->CreateInstanceData(instance);
instance->GetEluna()->OnLoad(this);
// Stack: (empty)
return;
}
size_t decodedLength;
const unsigned char* decodedData = ElunaUtil::DecodeData(data, &decodedLength);
lua_State* L = instance->GetEluna()->L;
if (decodedData)
{
// Stack: (empty)
lua_pushcfunction(L, mar_decode);
lua_pushlstring(L, (const char*)decodedData, decodedLength);
// Stack: mar_decode, decoded_data
// Call `mar_decode` and check for success.
if (lua_pcall(L, 1, 1, 0) == 0)
{
// Stack: data
// Only use the data if it's a table.
if (lua_istable(L, -1))
{
instance->GetEluna()->CreateInstanceData(instance);
// Stack: (empty)
instance->GetEluna()->OnLoad(this);
// WARNING! lastSaveData might be different after `OnLoad` if the Lua code saved data.
}
else
{
ELUNA_LOG_ERROR("Error while loading instance data: Expected data to be a table (type 5), got type %d instead", lua_type(L, -1));
lua_pop(L, 1);
// Stack: (empty)
#if !defined ELUNA_TRINITY
Initialize();
#endif
}
}
else
{
// Stack: error_message
ELUNA_LOG_ERROR("Error while parsing instance data with lua-marshal: %s", lua_tostring(L, -1));
lua_pop(L, 1);
// Stack: (empty)
#if !defined ELUNA_TRINITY
Initialize();
#endif
}
delete[] decodedData;
}
else
{
ELUNA_LOG_ERROR("Error while decoding instance data: Data is not valid base-64");
#if !defined ELUNA_TRINITY
Initialize();
#endif
}
}
const char* ElunaInstanceAI::Save() const
{
lua_State* L = instance->GetEluna()->L;
// Stack: (empty)
/*
* Need to cheat because this method actually does modify this instance,
* even though it's declared as `const`.
*
* Declaring virtual methods as `const` is BAD!
* Don't dictate to children that their methods must be pure.
*/
ElunaInstanceAI* self = const_cast<ElunaInstanceAI*>(this);
lua_pushcfunction(L, mar_encode);
instance->GetEluna()->PushInstanceData(self, false);
// Stack: mar_encode, instance_data
if (lua_pcall(L, 1, 1, 0) != 0)
{
// Stack: error_message
ELUNA_LOG_ERROR("Error while saving: %s", lua_tostring(L, -1));
lua_pop(L, 1);
return NULL;
}
// Stack: data
size_t dataLength;
const unsigned char* data = (const unsigned char*)lua_tolstring(L, -1, &dataLength);
ElunaUtil::EncodeData(data, dataLength, self->lastSaveData);
lua_pop(L, 1);
// Stack: (empty)
return lastSaveData.c_str();
}
uint32 ElunaInstanceAI::GetData(uint32 key) const
{
Eluna* E = instance->GetEluna();
lua_State* L = E->L;
// Stack: (empty)
E->PushInstanceData(const_cast<ElunaInstanceAI*>(this), false);
// Stack: instance_data
E->Push(key);
// Stack: instance_data, key
lua_gettable(L, -2);
// Stack: instance_data, value
uint32 value = E->CHECKVAL<uint32>(-1, 0);
lua_pop(L, 2);
// Stack: (empty)
return value;
}
void ElunaInstanceAI::SetData(uint32 key, uint32 value)
{
Eluna* E = instance->GetEluna();
lua_State* L = E->L;
// Stack: (empty)
E->PushInstanceData(this, false);
// Stack: instance_data
E->Push(key);
E->Push(value);
// Stack: instance_data, key, value
lua_settable(L, -3);
// Stack: instance_data
lua_pop(L, 1);
// Stack: (empty)
}
uint64 ElunaInstanceAI::GetData64(uint32 key) const
{
Eluna* E = instance->GetEluna();
lua_State* L = E->L;
// Stack: (empty)
E->PushInstanceData(const_cast<ElunaInstanceAI*>(this), false);
// Stack: instance_data
E->Push(key);
// Stack: instance_data, key
lua_gettable(L, -2);
// Stack: instance_data, value
uint64 value = E->CHECKVAL<uint64>(-1, 0);
lua_pop(L, 2);
// Stack: (empty)
return value;
}
void ElunaInstanceAI::SetData64(uint32 key, uint64 value)
{
Eluna* E = instance->GetEluna();
lua_State* L = E->L;
// Stack: (empty)
E->PushInstanceData(this, false);
// Stack: instance_data
E->Push(key);
E->Push(value);
// Stack: instance_data, key, value
lua_settable(L, -3);
// Stack: instance_data
lua_pop(L, 1);
// Stack: (empty)
}