-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLuaEngine.cpp
More file actions
180 lines (151 loc) · 5.72 KB
/
LuaEngine.cpp
File metadata and controls
180 lines (151 loc) · 5.72 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
#include "LuaEngine.h"
#include <QFile>
#include <QTextStream>
#include <QDir>
#include <regex>
#include <sstream>
void LuaEngine::run() {
initLua();
std::string scriptToExecute = m_script;
if (m_script.size() > 4 && m_script.substr(m_script.size() - 4) == ".lua") {
QString path = QDir::current().absoluteFilePath("Save/" + QString::fromStdString(m_script));
QFile file(path);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
scriptToExecute = in.readAll().toStdString();
std::cout << "Loading script from file: " << path.toStdString() << std::endl;
} else {
std::cerr << "Could not open script file: " << path.toStdString() << std::endl;
// Optionally: execute m_script as raw lua if file not found,
// but usually a .lua filename isn't valid lua code alone.
}
}
executeLuaScript(preprocess(scriptToExecute));
closeLua();
}
void LuaEngine::initLua() {
std::cout << "Creating Lua" << std::endl;
L = luaL_newstate();
luaL_openlibs(L);
lua_pushlightuserdata(L, this);
lua_setfield(L, LUA_REGISTRYINDEX, "LuaEngine_instance");
LuaBindings::registerAll(L);
// Globalize proto and engine functions
const char* globalizeSnippet =
"setmetatable(_G, {"
" __index = function(_, key)"
" local val = proto[key]"
" if val ~= nil then"
" if type(val) == 'function' then"
" return function(...) return val(proto, ...) end"
" end"
" return val"
" end"
" val = engine[key]"
" if val ~= nil then"
" if type(val) == 'function' then"
" return function(...) return val(engine, ...) end"
" end"
" return val"
" end"
" end"
"})";
if (luaL_dostring(L, globalizeSnippet) != LUA_OK) {
std::cerr << "Globalize Error: " << lua_tostring(L, -1) << std::endl;
lua_pop(L, 1);
}
// Set hook to check for interruption every 100 instructions
lua_sethook(L, luaHookCallback, LUA_MASKCOUNT, 100);
}
void LuaEngine::closeLua() {
if (L) {
lua_close(L);
L = nullptr;
}
std::cout << "Closing Lua" << std::endl;
}
void LuaEngine::executeLuaScript(const std::string &scriptText) {
if (!L) return;
m_returnedString.clear(); // Reset before execution
if (luaL_dostring(L, scriptText.c_str()) != LUA_OK) {
const char* error = lua_tostring(L, -1);
// Don't print error if it was intentional interruption
if (std::string(error) != "Script interrupted") {
std::cerr << "Lua Error: " << error << std::endl;
}
lua_pop(L, 1);
} else {
// Check if script returned a value
if (lua_gettop(L) > 0) {
// Check if the returned value is a string
if (lua_isstring(L, -1)) {
m_returnedString = lua_tostring(L, -1);
std::cout << "Lua script returned: '" << m_returnedString << "'" << std::endl;
}
lua_pop(L, 1); // Pop the return value
}
}
}
void LuaEngine::requestStop() {
m_shouldStop.store(true);
}
void LuaEngine::luaHookCallback(lua_State* L, lua_Debug* ar) {
// Retrieve the LuaEngine instance from registry
lua_getfield(L, LUA_REGISTRYINDEX, "LuaEngine_instance");
LuaEngine* luaEngine = static_cast<LuaEngine*>(lua_touserdata(L, -1));
lua_pop(L, 1);
if (luaEngine && luaEngine->m_shouldStop.load()) {
// Throw a Lua error to stop execution
luaL_error(L, "Script interrupted");
}
}
std::string LuaEngine::preprocess(const std::string& script) {
std::istringstream stream(script);
std::string line;
std::string result;
int blockLevel = 0;
// Regex to match if/elseif/while/for/function/repeat
std::regex blockStartRegex(R"(^\s*(if|elseif|while|for|function|repeat)\b)", std::regex_constants::icase);
// Regex to match then/do/until/end
std::regex blockEndRegex(R"(\b(then|do|until|end)\s*$)", std::regex_constants::icase);
// Regex to match end specifically
std::regex endRegex(R"(^\s*end\b)", std::regex_constants::icase);
// Regex to match repeat specifically
std::regex repeatRegex(R"(^\s*repeat\b)", std::regex_constants::icase);
while (std::getline(stream, line)) {
std::smatch match;
std::string trimmedLine = line;
trimmedLine.erase(0, trimmedLine.find_first_not_of(" \t\r\n"));
trimmedLine.erase(trimmedLine.find_last_not_of(" \t\r\n") + 1);
if (trimmedLine.empty()) {
result += line + "\n";
continue;
}
// Check for end
if (std::regex_search(trimmedLine, match, endRegex)) {
blockLevel--;
} else if (std::regex_search(trimmedLine, match, blockStartRegex)) {
std::string keyword = match[1];
bool hasBlockEnd = std::regex_search(trimmedLine, match, blockEndRegex);
if (keyword == "if" || keyword == "elseif") {
if (!hasBlockEnd && trimmedLine.find(" then") == std::string::npos && trimmedLine.back() != ':') {
line += " then";
}
} else if (keyword == "while" || keyword == "for") {
if (!hasBlockEnd && trimmedLine.find(" do") == std::string::npos) {
line += " do";
}
}
if (keyword != "elseif") {
blockLevel++;
}
}
result += line + "\n";
}
// Close remaining blocks
while (blockLevel > 0) {
result += "end\n";
blockLevel--;
}
return result;
}