forked from WeaselGames/godot_luaAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlua.cpp
More file actions
321 lines (282 loc) · 10.5 KB
/
Copy pathlua.cpp
File metadata and controls
321 lines (282 loc) · 10.5 KB
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
#include "lua.h"
Lua::Lua(){
// Createing lua state instance
state = luaL_newstate();
threaded = true;
luaopen_base(state);
luaopen_math(state);
luaopen_string(state);
luaopen_table(state);
lua_sethook(state, &LineHook, LUA_MASKLINE, 0);
lua_register(state, "print", luaPrint);
}
Lua::~Lua(){
// Destroying lua state instance
lua_close(state);
}
static bool shouldKill = false;
// Bind C++ functions to GDScript
void Lua::_bind_methods(){
ClassDB::bind_method(D_METHOD("killAll"),&Lua::killAll);
ClassDB::bind_method(D_METHOD("setThreaded", "bool"),&Lua::setThreaded);
ClassDB::bind_method(D_METHOD("doFile", "NodeObject", "File", "Callback=String()"), &Lua::doFile);
ClassDB::bind_method(D_METHOD("doString", "NodeObject", "Code", "Callback=String()"), &Lua::doString);
ClassDB::bind_method(D_METHOD("pushVariant", "var"),&Lua::pushGlobalVariant);
ClassDB::bind_method(D_METHOD("exposeFunction", "NodeObject", "GDFunction", "LuaFunctionName"),&Lua::exposeFunction);
ClassDB::bind_method(D_METHOD("callFunction", "NodeObject", "LuaFunctionName", "Args"), &Lua::callFunction);
}
// expose a GDScript function to lua
void Lua::exposeFunction(Object *instance, String function, String name){
// Createing lamda function so we can capture the object instanse and call the GDScript method. Or in theory other scripting languages?
auto f = [](lua_State* L) -> int{
const Object *instance2 = (const Object*) lua_topointer(L, lua_upvalueindex(1));
class Lua *obj = (class Lua*) lua_topointer(L, lua_upvalueindex(2));
const char *function2 = lua_tostring(L, lua_upvalueindex(3));
Variant arg1 = obj->getVariant(1);
Variant arg2 = obj->getVariant(2);
Variant arg3 = obj->getVariant(3);
Variant arg4 = obj->getVariant(4);
Variant arg5 = obj->getVariant(5);
ScriptInstance *scriptInstance = instance2->get_script_instance();
Variant returned = scriptInstance->call(function2, arg1, arg2, arg3, arg4, arg5);
if (returned.get_type() != Variant::Type::NIL){
obj->pushVariant(returned);
return 1;
}
return 0;
};
// Pushing the object instnace to the stack to be retrived when the function is called
lua_pushlightuserdata(state, instance);
// Pushing the referance of the class
lua_pushlightuserdata(state, this);
// Convert lua and gdscript function names from wstring to string for lua's usage
std::wstring temp = function.c_str();
std::string func(temp.begin(), temp.end());
temp = name.c_str();
std::string fname(temp.begin(), temp.end());
// Pushing the script function name string to the stack to br retrived when called
lua_pushstring(state, func.c_str());
// Pushing the actual lambda function to the stack
lua_pushcclosure(state, f, 3);
// Setting the global name for the function in lua
lua_setglobal(state, fname.c_str());
}
// call a Lua function from GDScript
void Lua::callFunction(Object *instance, String name, Array args) {
std::wstring temp = name.c_str();
std::string fname(temp.begin(), temp.end());
// put global function name on stack
lua_getglobal(state, fname.c_str());
// push args
for (int i = 0; i < args.size(); ++i) {
pushVariant(args[i]);
}
// call function (for now, lua functions cannot return values to gdscript)
lua_call(state, args.size(), 0);
}
void Lua::setThreaded(bool thread){
threaded = thread;
}
// doFile() will just load the file's text and call doString()
void Lua::doFile(Object *instance, String fileName, String callback){
_File file;
file.open(fileName,_File::ModeFlags::READ);
String code = file.get_as_text();
file.close();
doString(instance, code, callback);
}
// kill all active threads
void Lua::killAll(){
//TODO: Create a method to kill individual threads
shouldKill = true;
}
// Called every line of lua that is ran
void Lua::LineHook(lua_State *L, lua_Debug *ar){
if(shouldKill){
luaL_error(L, "Execution terminated");
shouldKill = false;
}
}
// Run lua string in a thread if threading is enabled
void Lua::doString(Object *instance, String code, String callback){
if(threaded){
std::thread(runLua, instance, code, callback, state).detach();
}else{
runLua(instance, code, callback, state);
}
}
// Execute a lua script string and call the passed callBack function with the error as the aurgument if an error occures
void Lua::runLua(Object *instance, String code, String callback, lua_State *L){
std::wstring luaCode = code.c_str();
int result = luaL_dostring(L, std::string( luaCode.begin(), luaCode.end() ).c_str());
if(result != LUA_OK){
if (callback != String()){
ScriptInstance *scriptInstance = instance->get_script_instance();
scriptInstance->call(callback, lua_tostring(L, -1));
}
}
}
// Push a GD Variant to the lua stack and return false if type is not supported.
bool Lua::pushVariant(Variant var) {
std::wstring str;
switch (var.get_type())
{
case Variant::Type::STRING:
str = (var.operator String().c_str());
lua_pushstring(state, std::string( str.begin(), str.end() ).c_str());
break;
case Variant::Type::INT:
lua_pushinteger(state, (int64_t)var);
break;
case Variant::Type::REAL:
lua_pushnumber(state,var.operator double());
break;
case Variant::Type::BOOL:
lua_pushboolean(state, (bool)var);
break;
case Variant::Type::POOL_BYTE_ARRAY:
case Variant::Type::POOL_INT_ARRAY:
case Variant::Type::POOL_STRING_ARRAY:
case Variant::Type::POOL_REAL_ARRAY:
case Variant::Type::POOL_VECTOR2_ARRAY:
case Variant::Type::POOL_VECTOR3_ARRAY:
case Variant::Type::POOL_COLOR_ARRAY:
case Variant::Type::ARRAY: {
Array array = var.operator Array();
lua_newtable(state);
for(int i = 0; i < array.size(); i++) {
Variant key = i+1;
Variant value = array[i];
pushVariant(key);
pushVariant(value);
lua_settable(state,-3);
}
break;
}
case Variant::Type::DICTIONARY:
lua_newtable(state);
for(int i = 0; i < ((Dictionary)var).size(); i++) {
Variant key = ((Dictionary)var).keys()[i];
Variant value = ((Dictionary)var)[key];
pushVariant(key);
pushVariant(value);
lua_settable(state, -3);
}
break;
case Variant::Type::VECTOR2: {
Vector2 vector2 = var.operator Vector2();
lua_newtable(state);
lua_pushstring(state, "X");
lua_pushnumber(state, vector2.x);
lua_settable(state, -3);
lua_pushstring(state, "Y");
lua_pushnumber(state, vector2.y);
lua_settable(state, -3);
break;
}
case Variant::Type::VECTOR3: {
Vector3 vector3 = var.operator Vector3();
lua_newtable(state);
lua_pushstring(state,"X");
lua_pushnumber(state,vector3.x);
lua_settable(state,-3);
lua_pushstring(state,"Y");
lua_pushnumber(state,vector3.y);
lua_settable(state,-3);
lua_pushstring(state,"Z");
lua_pushnumber(state,vector3.z);
lua_settable(state,-3);
break;
}
case Variant::Type::COLOR: {
Color color = var.operator Color();
lua_newtable(state);
lua_pushstring(state,"R");
lua_pushnumber(state,color.r);
lua_settable(state,-3);
lua_pushstring(state,"G");
lua_pushnumber(state,color.g);
lua_settable(state,-3);
lua_pushstring(state,"B");
lua_pushnumber(state,color.b);
lua_settable(state,-3);
lua_pushstring(state,"A");
lua_pushnumber(state,color.a);
lua_settable(state,-3);
break;
}
default:
print_line(var);
return false;
}
return true;
}
// Call pushVarient() and set it to a global name
bool Lua::pushGlobalVariant(Variant var, String name) {
if (pushVariant(var)) {
std::wstring str = name.c_str();
lua_setglobal(state,std::string( str.begin(), str.end() ).c_str());
return true;
}
return false;
}
// Pop the value at the top of the stack and return getVarient()
Variant Lua::popVariant() {
Variant result = getVariant();
lua_pop(state, 1);
return result;
}
// get a value at the given index and return as a variant
Variant Lua::getVariant(int index) {
Variant result;
int type = lua_type(state, index);
switch (type) {
case LUA_TSTRING:
result = lua_tostring(state, index);
break;
case LUA_TNUMBER:
result = lua_tonumber(state, index);
break;
case LUA_TBOOLEAN:
result = (bool)lua_toboolean(state, index);
break;
case LUA_TTABLE:
{
Dictionary dict;
for (lua_pushnil(state); lua_next(state, index-1); lua_pop(state, 1)) {
Variant key = getVariant(-2);
Variant value = getVariant(-1);
dict[key] = value;
}
if (dict.size() == 2) {
if (dict.keys().count("X") && dict.keys().count("Y")) {
result = Vector2(dict["X"],dict["Y"]);
}
} else if (dict.size() == 3) {
if (dict.keys().count("X") && dict.keys().count("Y") && dict.keys().count("Z")) {
result = Vector3(dict["X"],dict["Y"],dict["Z"]);
}
} else if (dict.size() == 4) {
if (dict.keys().count("R") && dict.keys().count("G") && dict.keys().count("B") && dict.keys().count("B")) {
result = Color(dict["R"],dict["G"],dict["B"],dict["A"]);
}
} else {
result = dict;
}
break;
}
default:
result = Variant();
}
return result;
}
// Lua functions
// Change lua's print function to print to the Godot console by default
int Lua::luaPrint(lua_State* state)
{
int args = lua_gettop(state);
for ( int n=1; n<=args; ++n) {
print_line(lua_tostring(state, n));
}
return 0;
}