-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogrammableblock.cpp
384 lines (346 loc) · 11.3 KB
/
programmableblock.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
#include "programmableblock.h"
ProgrammableBlock::ProgrammableBlock(SpriteManager* sm, olc::vi2d pos) : Block(sm, pos, {1, 1}), running(false), code(defaultCode)
{
this->toYield = this->warmupTime;
this->SetSprite("code_block");
}
ProgrammableBlock::ProgrammableBlock(SpriteManager* sm, olc::vi2d pos, int inputPorts, int outputPorts) : Block(sm, pos, {1, 1}, inputPorts, outputPorts), running(false), code(defaultCode)
{
this->toYield = this->warmupTime;
this->SetSprite("code_block");
}
ProgrammableBlock::ProgrammableBlock(SpriteManager* sm, olc::vi2d pos, std::vector<std::string> inputPorts, std::vector<std::string> outputPorts) : Block(sm, pos, {1, 1}, inputPorts, outputPorts), running(false), code(defaultCode)
{
this->toYield = this->warmupTime;
this->SetSprite("code_block");
}
ProgrammableBlock::ProgrammableBlock(const ProgrammableBlock& other) : Block(other)
{
this->toYield = this->warmupTime;
this->code = other.code;
this->running = false;
}
ProgrammableBlock::~ProgrammableBlock()
{}
bool ProgrammableBlock::IsProgrammable()
{
return true;
}
std::string ProgrammableBlock::GetDescription()
{
return "Programmable block. Can be programmed in Lua to receive and respond to inputs "
"from other components (e.g. buttons) and produce outputs which can be sent for "
"further processing to another programmable blocks or connect to other components "
"to control them. Please see examples folder to learn how to use it (especially "
"examples/basic.lua, as it contains many essential information).";
}
std::shared_ptr<olc::Sprite> ProgrammableBlock::GetDefaultSprite()
{
return this->sm->GetSprite("code_block");
}
ProgrammableBlock* ProgrammableBlock::Clone()
{
return new ProgrammableBlock(*this);
}
//static int luaPrint(lua_State* L)
//{
// if (lua_type(L, 1) == LUA_TSTRING)
// {
// const char* str = luaL_checkstring(L, 1);
// ImGui::Begin("Console");
// ImGui::Text(str);
// ImGui::End();
// }
// return 0;
//}
static void luaHook(lua_State *L, lua_Debug *ar)
{
ProgrammableBlock* pb = *(ProgrammableBlock**)lua_getextraspace(L);
if (!pb->UpdateYieldTimer())
{
pb->SetRunning(false);
luaL_error(L, "Execution time exceeded time limit");
}
}
void ProgrammableBlock::InitLua()
{
this->L = luaL_newstate();
// load libraries
luaL_requiref(this->L, "basic", luaopen_base, false);
// turn off dofile and loadfile to prevent any filesystem access
lua_pushnil(this->L);
lua_pushnil(this->L);
lua_setglobal(this->L, "dofile");
lua_setglobal(this->L, "loadfile");
luaL_requiref(this->L, "coroutine", luaopen_coroutine, true);
luaL_requiref(this->L, "string", luaopen_string, true);
luaL_requiref(this->L, "utf8", luaopen_utf8, true);
luaL_requiref(this->L, "table", luaopen_table, true);
luaL_requiref(this->L, "math", luaopen_math, true);
// setup custom functions
//lua_pushcfunction(this->L, luaPrint);
//lua_setglobal(this->L, "print");
// setup colors global
lua_newtable(this->L);
for (int i = 0; i < this->colors.size(); ++i)
{
lua_pushstring(this->L, this->colors[i].c_str());
lua_pushinteger(this->L, i);
lua_settable(this->L, -3);
}
lua_setglobal(this->L, "color");
lua_pop(this->L, 1);
// setup 'ports' global
this->SetupLuaPorts();
// setup hook function
*(ProgrammableBlock**)lua_getextraspace(this->L) = this;
lua_sethook(this->L, luaHook, LUA_MASKCOUNT, this->hookInstrCount);
}
void ProgrammableBlock::SetupLuaPorts()
{
lua_newtable(this->L);
for (auto port : ports)
{
// t[portname] = portvalue
lua_pushstring(this->L, port.first.c_str());
this->PushDataValue(port.second->GetData());
lua_settable(this->L, -3);
}
lua_setglobal(this->L, "ports");
}
std::string ProgrammableBlock::PopString()
{
const char* str = luaL_checkstring(L, -1);
if (str)
{
std::string res(str);
lua_pop(L, 1);
return res;
}
return std::string();
}
bool ProgrammableBlock::RunSetup()
{
if (luaL_dostring(this->L, this->code.c_str()) == LUA_OK)
return true;
else
{
this->lastError = this->PopString();
return false;
}
}
void ProgrammableBlock::ResetYieldTimer(std::chrono::milliseconds t)
{
this->prevTime = std::chrono::steady_clock::now();
this->toYield = t;
}
bool ProgrammableBlock::RunUpdate()
{
this->ResetYieldTimer(this->warmupTime);
if (lua_getglobal(this->L, "update") != LUA_TFUNCTION)
{
lua_pop(this->L, 1);
this->lastError = "Missing 'update' method";
return false;
}
if (lua_pcall(this->L, 0, 0, 0) == LUA_OK)
return true;
else
{
this->lastError = this->PopString();
return false;
}
}
bool ProgrammableBlock::VerifyCode()
{
lua_State* L = luaL_newstate();
int res = luaL_loadstring(L, this->code.c_str());
if (res == LUA_OK)
{
lua_close(L);
return true;
}
else
{
const char* str = luaL_checkstring(L, -1);
if (str)
this->lastError = std::string(str);
lua_close(L);
return false;
}
}
std::string ProgrammableBlock::GetError()
{
return this->lastError;
}
void ProgrammableBlock::Start()
{
this->InitLua();
if (!this->VerifyCode())
return;
this->running = true;
this->ResetYieldTimer(this->warmupTime);
if (!this->RunSetup())
this->running = false;
this->ResetYieldTimer(this->yieldTime);
}
void ProgrammableBlock::Stop()
{
this->running = false;
lua_close(this->L);
}
void ProgrammableBlock::SetRunning(bool val)
{
this->running = val;
}
void ProgrammableBlock::PushDataValue(DataValueEx data)
{
if (std::holds_alternative<nil>(data))
lua_pushnil(this->L);
else if (std::holds_alternative<bool>(data))
lua_pushboolean(this->L, std::get<bool>(data));
else if (std::holds_alternative<int64_t>(data))
lua_pushinteger(this->L, std::get<int64_t>(data));
else if (std::holds_alternative<double>(data))
lua_pushnumber(this->L, std::get<double>(data));
else if (std::holds_alternative<std::string>(data))
lua_pushstring(this->L, std::get<std::string>(data).c_str());
else if (std::holds_alternative<std::vector<DataValue>>(data))
{
std::vector<DataValue> table = std::get<std::vector<DataValue>>(data);
lua_createtable(this->L, table.size(), 0);
for (int i = 0; i < table.size(); ++i)
{
DataValue item = table[i];
if (std::holds_alternative<nil>(item))
lua_pushnil(this->L);
else if (std::holds_alternative<bool>(item))
lua_pushboolean(this->L, std::get<bool>(item));
else if (std::holds_alternative<int64_t>(item))
lua_pushinteger(this->L, std::get<int64_t>(item));
else if (std::holds_alternative<double>(item))
lua_pushnumber(this->L, std::get<double>(item));
else if (std::holds_alternative<std::string>(item))
lua_pushstring(this->L, std::get<std::string>(item).c_str());
else
assert(false);
lua_seti(this->L, -2, i+1);
}
}
else
assert(false);
}
DataValueEx ProgrammableBlock::GetDataValue(int index)
{
DataValueEx data;
size_t len;
int tableLen;
int type = lua_type(this->L, index);
switch (type)
{
case LUA_TBOOLEAN:
data = static_cast<int64_t>(lua_toboolean(this->L, index));
break;
case LUA_TNUMBER:
if (lua_isinteger(this->L, index))
data = static_cast<int64_t>(lua_tointeger(this->L, index));
else
data = static_cast<double>(lua_tonumber(this->L, index));
break;
case LUA_TSTRING:
data = std::string(lua_tolstring(this->L, index, &len), len);
break;
case LUA_TTABLE:
data = std::vector<DataValue>();
lua_len(this->L, index);
tableLen = lua_tointeger(this->L, -1);
lua_pop(this->L, 1);
for (int i = 0; i < tableLen; ++i)
{
lua_geti(this->L, index, i+1);
int type = lua_type(this->L, index);
switch (type)
{
case LUA_TBOOLEAN:
std::get<std::vector<DataValue>>(data).emplace_back(static_cast<bool>(lua_toboolean(this->L, index)));
break;
case LUA_TNUMBER:
if (lua_isinteger(this->L, index))
std::get<std::vector<DataValue>>(data).emplace_back(static_cast<int64_t>(lua_tointeger(this->L, index)));
else
std::get<std::vector<DataValue>>(data).emplace_back(static_cast<double>(lua_tonumber(this->L, index)));
break;
case LUA_TSTRING:
std::get<std::vector<DataValue>>(data).emplace_back(std::string(lua_tolstring(this->L, index, &len), len));
break;
case LUA_TNIL:
default:
std::get<std::vector<DataValue>>(data).emplace_back(nil());
break;
}
lua_pop(this->L, 1);
}
break;
case LUA_TNIL:
default:
data = nil();
break;
}
return data;
}
bool ProgrammableBlock::UpdateYieldTimer()
{
auto now = std::chrono::steady_clock::now();
this->toYield -= now - this->prevTime;
this->prevTime = now;
if (this->toYield <= std::chrono::duration<int64_t, std::nano>::zero())
{
this->toYield = this->yieldTime;
return false;
}
return true;
}
void ProgrammableBlock::DisplayError()
{
ImGui::SetNextWindowSizeConstraints({200, 60}, {300, 150});
ImGui::SetNextWindowPos({(float)(this->pos.x * 64), (float)(this->pos.y * 64)});
ImGui::Begin(("Error##" + std::to_string((intptr_t)this)).c_str(), NULL, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_Tooltip);
ImGui::TextWrapped(this->GetError().c_str());
ImGui::End();
}
void ProgrammableBlock::Update(float timedelta)
{
Block::Update(timedelta);
if (this->running)
{
if (!this->RunUpdate())
this->DisplayError();
else
{
if (lua_getglobal(this->L, "ports") != LUA_TTABLE)
{
lua_pop(this->L, 1);
this->SetupLuaPorts();
}
for (auto port : ports)
{
lua_pushstring(this->L, port.first.c_str());
if (port.second->IsInput())
{
auto data = port.second->GetData();
this->PushDataValue(data);
lua_settable(this->L, -3);
}
else if (port.second->IsOutput())
{
lua_gettable(this->L, -2);
port.second->Update(this->GetDataValue(-1));
lua_pop(this->L, 1);
}
}
lua_pop(this->L, 1);
}
}
else
this->DisplayError();
}