-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlua_script.cc
174 lines (165 loc) · 5.15 KB
/
lua_script.cc
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
// Copyright 2018 [email protected]
// College of Information and Computer Sciences,
// University of Massachusetts Amherst
// Based on source by Elias Daler
//
// This software is free: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License Version 3,
// as published by the Free Software Foundation.
//
// This software 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// Version 3 in the file COPYING that came with this distribution.
// If not, see <http://www.gnu.org/licenses/>.
// ========================================================================
#include "lua_script.h"
#include <string.h>
// Constructor
LuaScript::LuaScript(const std::vector<std::string>& files) {
L = luaL_newstate();
luaL_openlibs(L);
for (std::string filename : files) {
if (luaL_loadfile(L, filename.c_str()) || lua_pcall(L, 0, 0, 0)) {
std::cout << "Error: failed to load (" << filename << ")" << std::endl;
std::cout << "Error Message: " << lua_tostring(L, -1) << std::endl;
L = 0;
}
}
if (L) luaL_openlibs(L);
}
// Destructor for the LuaScript Object
LuaScript::~LuaScript() {
if (L) lua_close(L);
}
void LuaScript::printError(const std::string& variableName,
const std::string& reason) {
std::cout << "Error: can't get [" << variableName << "]. " << reason
<< std::endl;
}
// Wrapper to return an array of ints from Lua
std::vector<int> LuaScript::getIntVector(const std::string& name) {
std::vector<int> v;
lua_gettostack(name.c_str());
if (lua_isnil(L, -1)) { // array is not found
return std::vector<int>();
}
lua_pushnil(L);
while (lua_next(L, -2)) {
v.push_back((int)lua_tonumber(L, -1));
lua_pop(L, 1);
}
clean();
return v;
}
// Wrapper to return an Eigen::Vector2f from Lua
Eigen::Vector2f LuaScript::getVector2f(const std::string& name) {
Eigen::Vector2f v(0, 0);
lua_gettostack(name.c_str());
if (lua_isnil(L, -1)) { // array is not found
std::cout << "Vector2f not found in Lua file" << std::endl;
return v;
}
lua_pushnil(L);
int i = 0;
while (lua_next(L, -2) && i < 2) {
if (strncmp(lua_typename(L, lua_type(L, -2)), "string", 6) == 0) {
if (strncmp(lua_tostring(L, -2), "x", 1) == 0) {
v(0) = ((float)lua_tonumber(L, -1));
} else {
v(1) = ((float)lua_tonumber(L, -1));
}
lua_pop(L, 1);
i++;
} else {
std::cout << "ERROR: Key is not a string." << std::endl;
}
}
clean();
return v;
}
Eigen::Vector2d LuaScript::getVector2d(const std::string& name) {
Eigen::Vector2d v(0, 0);
lua_gettostack(name.c_str());
if (lua_isnil(L, -1)) { // array is not found
std::cout << "Vector2d not found in Lua file" << std::endl;
return v;
}
lua_pushnil(L);
int i = 0;
while (lua_next(L, -2) && i < 2) {
if (strncmp(lua_typename(L, lua_type(L, -2)), "string", 6) == 0) {
if (strncmp(lua_tostring(L, -2), "x", 1) == 0) {
v(0) = ((double)lua_tonumber(L, -1));
} else {
v(1) = ((double)lua_tonumber(L, -1));
}
lua_pop(L, 1);
i++;
} else {
std::cout << "ERROR: Key is not a string." << std::endl;
}
}
clean();
return v;
}
Eigen::Vector3d LuaScript::getVector3d(const std::string& name) {
Eigen::Vector3d v(0, 0, 0);
lua_gettostack(name.c_str());
if (lua_isnil(L, -1)) { // array is not found
std::cout << "Vector3d not found in Lua file" << std::endl;
return v;
}
lua_pushnil(L);
int i = 0;
while (lua_next(L, -2) && i < 3) {
if (strncmp(lua_typename(L, lua_type(L, -2)), "string", 6) == 0) {
if (strncmp(lua_tostring(L, -2), "x", 1) == 0) {
v(0) = ((double)lua_tonumber(L, -1));
} else if (strncmp(lua_tostring(L, -2), "y", 1) == 0) {
v(1) = ((double)lua_tonumber(L, -1));
} else {
v(2) = ((double)lua_tonumber(L, -1));
}
lua_pop(L, 1);
i++;
} else {
std::cout << "ERROR: Key is not a string." << std::endl;
}
}
clean();
return v;
}
// Wrapper to return all the keys from a Lua table
std::vector<std::string> LuaScript::getTableKeys(const std::string& name) {
std::string code =
"function getKeys(name) "
"s = \"\""
"for k, v in pairs(_G[name]) do "
" s = s..k..\",\" "
" end "
"return s "
"end"; // function for getting table keys
luaL_loadstring(L, code.c_str()); // execute code
lua_pcall(L, 0, 0, 0);
lua_getglobal(L, "getKeys"); // get function
lua_pushstring(L, name.c_str());
lua_pcall(L, 1, 1, 0); // execute function
std::string test = lua_tostring(L, -1);
std::vector<std::string> strings;
std::string temp = "";
std::cout << "TEMP:" << test << std::endl;
for (unsigned int i = 0; i < test.size(); i++) {
if (test.at(i) != ',') {
temp += test.at(i);
} else {
strings.push_back(temp);
temp = "";
}
}
clean();
return strings;
}