/
scriptman.cpp
453 lines (339 loc) · 11.4 KB
/
scriptman.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/* xoreos - A reimplementation of BioWare's Aurora engine
*
* xoreos is the legal property of its developers, whose names
* can be found in the AUTHORS file distributed with this source
* distribution.
*
* xoreos is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* xoreos 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with xoreos. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file
* Lua script manager.
*/
#include <cassert>
#include <memory>
#include "external/lua/lualib.h"
#include "external/toluapp/tolua++.h"
#include "src/common/error.h"
#include "src/common/util.h"
#include "src/aurora/resman.h"
#include "src/aurora/util.h"
#include "src/aurora/lua/scriptman.h"
#include "src/aurora/lua/stack.h"
#include "src/aurora/lua/stackguard.h"
#include "src/aurora/lua/variable.h"
#include "src/aurora/lua/table.h"
#include "src/aurora/lua/function.h"
DECLARE_SINGLETON(Aurora::Lua::ScriptManager)
namespace Aurora {
namespace Lua {
ScriptManager::ScriptManager() : _luaState(0), _regNestingLevel(0) {
}
ScriptManager::~ScriptManager() {
try {
deinit();
} catch (...) {
}
}
void ScriptManager::init() {
assert(!ready());
openLuaState();
registerDefaultBindings();
executeDefaultCode();
}
void ScriptManager::deinit() {
if (!_objectLuaInstances.empty()) {
warning("Lua instances were not freed properly");
_objectLuaInstances.clear();
}
closeLuaState();
}
bool ScriptManager::ready() const {
return _luaState != 0;
}
void ScriptManager::executeFile(const Common::UString &path) {
assert(_luaState && _regNestingLevel == 0);
if (isIgnoredFile(path)) {
return;
}
std::unique_ptr<Common::SeekableReadStream> stream(ResMan.getResource(path, kFileTypeLUC));
if (!stream) {
const Common::UString fileName = TypeMan.setFileType(path, kFileTypeLUC);
throw Common::Exception("No such LUC \"%s\"", fileName.c_str());
}
std::unique_ptr<Common::MemoryReadStream> memStream(stream->readStream(stream->size()));
const char *data = reinterpret_cast<const char *>(memStream->getData());
const int dataSize = memStream->size();
const int execResult = lua_dobuffer(_luaState, data, dataSize, path.c_str());
if (execResult != 0) {
const Common::UString fileName = TypeMan.setFileType(path, kFileTypeLUC);
throw Common::Exception("Failed to execute Lua file: %s", fileName.c_str());
}
}
void ScriptManager::executeString(const Common::UString &code) {
assert(_luaState && _regNestingLevel == 0);
const int execResult = lua_dostring(_luaState, code.c_str());
if (execResult != 0) {
throw Common::Exception("Failed to execute Lua code: %s", code.c_str());
}
}
Variables ScriptManager::callFunction(const Common::UString &name, const Variables ¶ms) {
assert(!name.empty());
assert(_luaState && _regNestingLevel == 0);
std::vector<Common::UString> parts;
Common::UString::split(name, '.', parts);
if (parts.empty()) {
error("Lua call \"%s\" failed: bad name", name.c_str());
return Variables();
}
const Common::UString funcName = parts.back();
parts.pop_back();
if (parts.empty()) {
return getGlobalFunction(funcName).call(params);
}
TableRef table = getGlobalTable(parts[0]);
for (uint32_t i = 1; i < parts.size(); ++i) {
table = table.getTableAt(parts[i]);
}
return table.getFunctionAt(funcName).call(params);
}
Variables ScriptManager::callFunction(const Common::UString &name) {
return callFunction(name, Variables());
}
Variable ScriptManager::getGlobalVariable(const Common::UString &name) const {
StackGuard guard(*_luaState);
lua_getglobal(_luaState, name.c_str());
Stack stack(*_luaState);
return stack.getVariableAt(-1);
}
TableRef ScriptManager::getGlobalTable(const Common::UString &name) const {
return getGlobalVariable(name).getTable();
}
FunctionRef ScriptManager::getGlobalFunction(const Common::UString &name) const {
return getGlobalVariable(name).getFunction();
}
void ScriptManager::addIgnoredFile(const Common::UString &path) {
_ignoredFiles.insert(path);
}
void ScriptManager::removeIgnoredFile(const Common::UString &path) {
_ignoredFiles.erase(path);
}
bool ScriptManager::isIgnoredFile(const Common::UString &path) const {
return _ignoredFiles.find(path) != _ignoredFiles.end();
}
void ScriptManager::declareClass(const Common::UString &name) {
assert(!name.empty());
assert(_luaState && _regNestingLevel == 0);
tolua_usertype(_luaState, name.c_str());
}
void ScriptManager::beginRegister() {
assert(_luaState && _regNestingLevel == 0);
++_regNestingLevel;
tolua_module(_luaState, 0, 1);
tolua_beginmodule(_luaState, 0);
}
void ScriptManager::endRegister() {
assert(_luaState && _regNestingLevel == 1);
--_regNestingLevel;
tolua_endmodule(_luaState);
}
void ScriptManager::beginRegisterNamespace(const Common::UString &name) {
assert(!name.empty());
assert(_luaState && _regNestingLevel > 0);
++_regNestingLevel;
tolua_module(_luaState, name.c_str(), 1);
tolua_beginmodule(_luaState, name.c_str());
}
void ScriptManager::endRegisterNamespace() {
assert(_luaState && _regNestingLevel > 1);
--_regNestingLevel;
tolua_endmodule(_luaState);
}
void ScriptManager::beginRegisterClass(const Common::UString &name, const Common::UString &baseName,
lua_CFunction deleter) {
assert(!name.empty());
assert(_luaState && _regNestingLevel > 0);
++_regNestingLevel;
requireDeclaredClass(name);
requireDeclaredClass(baseName);
tolua_cclass(_luaState, name.c_str(), name.c_str(), baseName.c_str(), deleter);
tolua_beginmodule(_luaState, name.c_str());
}
void ScriptManager::endRegisterClass() {
assert(_luaState && _regNestingLevel > 1);
--_regNestingLevel;
tolua_endmodule(_luaState);
}
void ScriptManager::registerConstant(const Common::UString &name, float value) {
assert(!name.empty());
assert(_luaState &&_regNestingLevel > 0);
tolua_constant(_luaState, name.c_str(), value);
}
void ScriptManager::registerVariable(const Common::UString &name, lua_CFunction getter, lua_CFunction setter) {
assert(!name.empty() && getter);
assert(_luaState && _regNestingLevel > 0);
tolua_variable(_luaState, name.c_str(), getter, setter);
}
void ScriptManager::registerFunction(const Common::UString &name, lua_CFunction func) {
assert(!name.empty() && func);
assert(_luaState && _regNestingLevel > 0);
tolua_function(_luaState, name.c_str(), func);
}
int ScriptManager::getUsedMemoryAmount() const {
return lua_getgccount(_luaState);
}
void ScriptManager::setLuaInstanceForObject(void *object, const TableRef &luaInstance) {
assert(object);
// TODO: Commented out to make stubs work
// assert(_objectLuaInstances.find(object) == _objectLuaInstances.end());
_objectLuaInstances[object] = luaInstance;
}
void ScriptManager::unsetLuaInstanceForObject(void *object) {
assert(object);
_objectLuaInstances.erase(object);
}
const TableRef &ScriptManager::getLuaInstanceForObject(void *object) const {
assert(object);
static const TableRef invalidInstance;
ObjectLuaInstanceMap::const_iterator found = _objectLuaInstances.find(object);
if (found == _objectLuaInstances.end())
{
return invalidInstance;
}
return found->second;
}
void ScriptManager::injectNewIndexMetaEventIntoTable(const TableRef &table) {
const FunctionRef fn = getGlobalFunction("mt_new_index");
TableRef metaTable = table.getMetaTable();
metaTable.setFunctionAt("__newindex", fn);
}
void ScriptManager::openLuaState() {
_luaState = lua_open();
if (!_luaState) {
throw Common::Exception("Failed to open Lua state");
}
luaopen_base(_luaState);
luaopen_io(_luaState);
luaopen_math(_luaState);
luaopen_string(_luaState);
luaopen_table(_luaState);
luaopen_loadlib(_luaState);
luaopen_debug(_luaState);
tolua_open(_luaState);
lua_atpanic(_luaState, &ScriptManager::atPanic);
}
void ScriptManager::closeLuaState() {
if (_luaState) {
lua_close(_luaState);
_luaState = 0;
}
_regNestingLevel = 0;
}
void ScriptManager::requireDeclaredClass(const Common::UString &name) const {
assert(_luaState);
if (name.empty()) {
return;
}
luaL_getmetatable(_luaState, name.c_str());
if (lua_isnil(_luaState, -1)) {
lua_pop(_luaState, 1);
throw Common::Exception("Class \"%s\" wasn't declared for Lua", name.c_str());
}
lua_pop(_luaState, 1);
}
void ScriptManager::registerDefaultBindings() {
declareClass("ScriptManager");
beginRegister();
registerFunction("getLua", &ScriptManager::luaGetLua);
beginRegisterClass("ScriptManager");
registerFunction("PlayFile", &ScriptManager::luaPlayFile);
registerFunction("SetGCInterval", &ScriptManager::luaSetGCInterval);
registerFunction("RegisterSubst", &ScriptManager::luaRegisterSubst);
registerFunction("UnregisterSubst", &ScriptManager::luaUnregisterSubst);
registerFunction("RegisterHandler", &ScriptManager::luaRegisterHandler);
endRegisterClass();
endRegister();
}
void ScriptManager::executeDefaultCode() {
static const Common::UString fnNewIndexSource =
"function mt_new_index(table, key, value)"
" local objClass = getmetatable(table).__objectClass"
" if objClass ~= nil then"
" local cppInstance = rawget(table, \"CPP_instance\")"
" if cppInstance[key] ~= nil then"
" cppInstance[key] = value"
" end"
" return"
" end"
" rawset(table, key, value)"
"end";
executeString(fnNewIndexSource);
}
int ScriptManager::atPanic(lua_State *state) {
const char *message = luaL_checkstring(state, -1);
error("Lua has panicked: %s", message);
lua_pop(state, 1);
return 0;
}
int ScriptManager::luaGetLua(lua_State *state) {
assert(state);
Stack stack(*state);
stack.pushUserType<ScriptManager>(LuaScriptMan, "ScriptManager");
return 1;
}
int ScriptManager::luaPlayFile(lua_State *state) {
assert(state);
Stack stack(*state);
assert(stack.getSize() > 1);
const Common::UString scriptFile = stack.getStringAt(2);
LuaScriptMan.executeFile(scriptFile);
return 0;
}
int ScriptManager::luaSetGCInterval(lua_State *UNUSED(state)) {
// TODO
return 0;
}
int ScriptManager::luaRegisterSubst(lua_State *state) {
assert(state);
Aurora::Lua::Stack stack(*state);
assert(stack.getSize() == 3);
ScriptManager* scriptMan = stack.getUserTypeAt<ScriptManager>(1);
assert(scriptMan == &LuaScriptMan);
void* object = stack.getRawUserTypeAt(2);
assert(object);
const TableRef instance = stack.getTableAt(3);
scriptMan->setLuaInstanceForObject(object, instance);
return 0;
}
int ScriptManager::luaUnregisterSubst(lua_State *state) {
assert(state);
Aurora::Lua::Stack stack(*state);
assert(stack.getSize() == 2);
ScriptManager* scriptMan = stack.getUserTypeAt<ScriptManager>(1);
assert(scriptMan == &LuaScriptMan);
void* object = stack.getRawUserTypeAt(2);
assert(object);
scriptMan->unsetLuaInstanceForObject(object);
return 0;
}
int ScriptManager::luaRegisterHandler(lua_State *state) {
assert(state);
Aurora::Lua::Stack stack(*state);
assert(stack.getTypeAt(1) == Aurora::Lua::kTypeUserType);
assert(stack.getTypeAt(2) == Aurora::Lua::kTypeUserType);
assert(stack.getTypeAt(3) == Aurora::Lua::kTypeString);
// TODO
return 0;
}
} // End of namespace Lua
} // End of namespace Aurora