lua.d 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. // quantumde1 developed software, licensed under BSD-0-Clause license.
  2. module scripts.lua;
  3. import bindbc.lua;
  4. import raylib;
  5. import variables;
  6. import graphics.effects;
  7. import std.conv;
  8. import system.abstraction;
  9. import system.config;
  10. import std.string;
  11. import graphics.engine;
  12. import graphics.playback;
  13. import std.file;
  14. import std.array;
  15. import std.algorithm;
  16. /*
  17. * This module provides Lua bindings for various engine functionalities.
  18. * Functions are built on top of engine built-in functions for execution from scripts.
  19. * Not all engine functions usable for scripting are yet implemented.
  20. */
  21. string hint;
  22. extern (C) nothrow int luaL_showHint(lua_State* L)
  23. {
  24. hint = "" ~ to!string(luaL_checkstring(L, 1));
  25. hintNeeded = true;
  26. return 0;
  27. }
  28. /* text window */
  29. extern (C) nothrow int luaL_dialogBox(lua_State* L)
  30. {
  31. showDialog = true;
  32. // parse table with text pages
  33. luaL_checktype(L, 1, LUA_TTABLE);
  34. int textTableLength = cast(int) lua_objlen(L, 1);
  35. messageGlobal = new string[](textTableLength);
  36. for (int i = 0; i < textTableLength; i++) {
  37. lua_rawgeti(L, 1, i + 1);
  38. messageGlobal[i] = luaL_checkstring(L, -1).to!string;
  39. backlogText ~= messageGlobal[i];
  40. lua_pop(L, 1);
  41. }
  42. //parse table with choices
  43. luaL_checktype(L, 2, LUA_TTABLE);
  44. int choicesLength = cast(int) lua_objlen(L, 2);
  45. choices = new string[choicesLength];
  46. for (int i = 0; i < choicesLength; i++)
  47. {
  48. lua_rawgeti(L, 2, i + 1);
  49. choices[i] = luaL_checkstring(L, -1).to!string;
  50. lua_pop(L, 1);
  51. }
  52. //if provided, get page on which choices must be shown
  53. if (lua_gettop(L) == 3) {
  54. choicePage = cast(int)luaL_checkinteger(L, 3);
  55. }
  56. //if provided, change speed of showing text
  57. if (lua_gettop(L) == 4) {
  58. typingSpeed = cast(float) luaL_checknumber(L, 7);
  59. }
  60. return 0;
  61. }
  62. extern (C) nothrow int luaL_getAnswerValue(lua_State* L)
  63. {
  64. lua_pushinteger(L, selectedChoice);
  65. return 1;
  66. }
  67. extern (C) nothrow int luaL_isDialogExecuted(lua_State *L) {
  68. lua_pushboolean(L, showDialog);
  69. return 1;
  70. }
  71. extern (C) nothrow int luaL_dialogAnswerValue(lua_State* L)
  72. {
  73. lua_pushinteger(L, selectedChoice);
  74. return 1;
  75. }
  76. /* background drawing and loading */
  77. extern (C) nothrow int luaL_load2Dbackground(lua_State* L)
  78. {
  79. try
  80. {
  81. int count = cast(int)luaL_checkinteger(L, 2);
  82. if (count >= backgroundTextures.length) {
  83. backgroundTextures.length = count + 1;
  84. }
  85. char* filename = cast(char*)luaL_checkstring(L, 1);
  86. backgroundTextures[count].texture = LoadTexture(filename);
  87. }
  88. catch (Exception e)
  89. {
  90. debugWriteln(e.msg);
  91. }
  92. return 0;
  93. }
  94. extern (C) nothrow int luaL_draw2Dbackground(lua_State* L)
  95. {
  96. try
  97. {
  98. int count = cast(int)luaL_checkinteger(L, 4);
  99. debugWriteln(backgroundTextures[count]);
  100. backgroundTextures[count].height = backgroundTextures[count].texture.height;
  101. backgroundTextures[count].width = backgroundTextures[count].texture.width;
  102. backgroundTextures[count].x = luaL_checknumber(L, 1);
  103. backgroundTextures[count].y = luaL_checknumber(L, 2);
  104. backgroundTextures[count].scale = cast(int)luaL_checknumber(L, 3);
  105. backgroundTextures[count].drawTexture = true;
  106. debugWriteln(backgroundTextures[count]);
  107. }
  108. catch (Exception e)
  109. {
  110. debugWriteln(e.msg);
  111. }
  112. return 0;
  113. }
  114. extern (C) nothrow int luaL_stopDraw2Dbackground(lua_State* L)
  115. {
  116. try
  117. {
  118. int count = cast(int)luaL_checkinteger(L, 1);
  119. backgroundTextures[count].drawTexture = false;
  120. }
  121. catch (Exception e)
  122. {
  123. debugWriteln(e.msg);
  124. }
  125. return 0;
  126. }
  127. extern (C) nothrow int luaL_unload2Dbackground(lua_State* L)
  128. {
  129. int count = cast(int)luaL_checkinteger(L, 1);
  130. UnloadTexture(backgroundTextures[count].texture);
  131. return 0;
  132. }
  133. /* character textures */
  134. extern (C) nothrow int luaL_load2Dcharacter(lua_State *L) {
  135. try
  136. {
  137. int count = cast(int) luaL_checkinteger(L, 2);
  138. if (count >= characterTextures.length) {
  139. characterTextures.length = count + 1;
  140. }
  141. characterTextures[count].texture = LoadTexture(luaL_checkstring(L, 1));
  142. characterTextures[count].width = characterTextures[count].texture.width;
  143. characterTextures[count].height = characterTextures[count].texture.height;
  144. characterTextures[count].drawTexture = false;
  145. }
  146. catch (Exception e) {
  147. debugWriteln(e.msg);
  148. }
  149. return 0;
  150. }
  151. extern (C) nothrow int luaL_draw2Dcharacter(lua_State* L)
  152. {
  153. try {
  154. //configuring needed parameters in characterTextures like coordinates, scale and drawTexture(its a boolean value which checks need this texture to be drawn or not)
  155. int count = to!int(luaL_checkinteger(L, 4));
  156. characterTextures[count].scale = luaL_checknumber(L, 3);
  157. characterTextures[count].y = cast(int) luaL_checkinteger(L, 2);
  158. characterTextures[count].x = cast(int) luaL_checkinteger(L, 1);
  159. characterTextures[count].drawTexture = true;
  160. debugWriteln("Count: ", count, " drawTexture cond: ", characterTextures[count].drawTexture);
  161. } catch (Exception e) {
  162. debugWriteln(e.msg);
  163. }
  164. return 0;
  165. }
  166. extern (C) nothrow int luaL_stopDraw2Dcharacter(lua_State* L)
  167. {
  168. int count = cast(int) luaL_checkinteger(L, 1);
  169. characterTextures[count].drawTexture = false;
  170. return 0;
  171. }
  172. extern (C) nothrow int luaL_unload2Dcharacter(lua_State *L) {
  173. int count = cast(int) luaL_checkinteger(L, 1);
  174. UnloadTexture(characterTextures[count].texture);
  175. return 0;
  176. }
  177. /* music and video */
  178. extern (C) nothrow int luaL_loadMusic(lua_State* L)
  179. {
  180. try
  181. {
  182. musicPath = cast(char*) luaL_checkstring(L, 1);
  183. music = LoadMusicStream(musicPath);
  184. }
  185. catch (Exception e)
  186. {
  187. debugWriteln(e.msg);
  188. }
  189. return 0;
  190. }
  191. extern (C) nothrow int luaL_playMusic(lua_State* L)
  192. {
  193. PlayMusicStream(music);
  194. return 0;
  195. }
  196. extern (C) nothrow int luaL_stopMusic(lua_State* L)
  197. {
  198. StopMusicStream(music);
  199. return 0;
  200. }
  201. extern (C) nothrow int luaL_unloadMusic(lua_State* L)
  202. {
  203. UnloadMusicStream(music);
  204. return 0;
  205. }
  206. extern (C) nothrow int luaL_playSfx(lua_State *L) {
  207. try {
  208. playSfx(to!string(luaL_checkstring(L, 1)));
  209. } catch (Exception e) {
  210. debugWriteln(e.msg);
  211. }
  212. return 0;
  213. }
  214. extern (C) nothrow int luaL_stopSfx(lua_State *L) {
  215. StopSound(sfx);
  216. return 0;
  217. }
  218. extern (C) nothrow int luaL_playVideo(lua_State* L)
  219. {
  220. try
  221. {
  222. videoFinished = false;
  223. playVideo(luaL_checkstring(L, 1).to!string);
  224. }
  225. catch (Exception e)
  226. {
  227. debugWriteln(e.msg);
  228. }
  229. return 0;
  230. }
  231. /* ui animations */
  232. extern (C) nothrow int luaL_moveCamera(lua_State *L) {
  233. try {
  234. float targetX = cast(float) luaL_checknumber(L, 1);
  235. float targetY = cast(float) luaL_checknumber(L, 2);
  236. float zoom = cast(float) luaL_optnumber(L, 3, 1.0f);
  237. float speed = cast(float) luaL_optnumber(L, 4, 5.0f);
  238. cameraTargetX = targetX;
  239. cameraTargetY = targetY;
  240. cameraTargetZoom = zoom;
  241. cameraMoveSpeed = speed;
  242. isCameraMoving = true;
  243. } catch (Exception e) {
  244. debugWriteln(e.msg);
  245. }
  246. return 0;
  247. }
  248. extern (C) nothrow int luaL_isCameraMoving(lua_State *L) {
  249. lua_pushboolean(L, isCameraMoving);
  250. return 1;
  251. }
  252. extern (C) nothrow int luaL_loadUIAnimation(lua_State *L) {
  253. try {
  254. //loads from uifx folder HPFF files, in which png textures are stored
  255. framesUI = loadAnimationFramesUI(to!string(luaL_checkstring(L, 1)), to!string(luaL_checkstring(L, 2)));
  256. if (lua_gettop(L) == 3) {
  257. frameDuration = luaL_checknumber(L, 3);
  258. debug debugWriteln("frameDuration: ", frameDuration);
  259. }
  260. } catch (Exception e) {
  261. debugWriteln(e.msg);
  262. }
  263. return 0;
  264. }
  265. extern (C) nothrow int luaL_playUIAnimation(lua_State *L) {
  266. debug debugWriteln("Animation UI start");
  267. try {
  268. playAnimation = true;
  269. } catch (Exception e) {
  270. debugWriteln(e.msg);
  271. }
  272. return 0;
  273. }
  274. extern (C) nothrow int luaL_stopUIAnimation(lua_State *L) {
  275. playAnimation = false;
  276. debug debugWriteln("Animation UI stop");
  277. frameDuration = 0.016f;
  278. currentFrame = 0;
  279. return 0;
  280. }
  281. extern (C) nothrow int luaL_unloadUIAnimation(lua_State *L) {
  282. try {
  283. for (int i = 0; i < framesUI.length; i++) {
  284. UnloadTexture(framesUI[i]);
  285. }
  286. } catch (Exception e) {
  287. debugWriteln(e.msg);
  288. }
  289. return 0;
  290. }
  291. /* system */
  292. extern (C) nothrow int luaL_getScreenWidth(lua_State* L)
  293. {
  294. lua_pushinteger(L, GetScreenWidth());
  295. return 1;
  296. }
  297. extern (C) nothrow int luaL_getScreenHeight(lua_State* L)
  298. {
  299. lua_pushinteger(L, GetScreenHeight());
  300. return 1;
  301. }
  302. extern (C) nothrow int luaL_getUsedLanguage(lua_State* L)
  303. {
  304. lua_pushstring(L, usedLang.toStringz());
  305. return 1;
  306. }
  307. extern (C) nothrow int luaL_2dModeEnable(lua_State* L)
  308. {
  309. neededDraw2D = true;
  310. return 0;
  311. }
  312. extern (C) nothrow int luaL_2dModeDisable(lua_State* L)
  313. {
  314. neededDraw2D = false;
  315. return 0;
  316. }
  317. extern (C) nothrow int luaL_setGameFont(lua_State* L)
  318. {
  319. const char* x = luaL_checkstring(L, 1);
  320. debugWriteln("Setting custom font: ", x.to!string);
  321. int[512] codepoints = 0;
  322. //configuring both cyrillic and latin fonts if available
  323. foreach (i; 0 .. 95)
  324. {
  325. codepoints[i] = 32 + i;
  326. }
  327. foreach (i; 0 .. 255)
  328. {
  329. codepoints[96 + i] = 0x400 + i;
  330. }
  331. textFont = LoadFontEx(x, 40, codepoints.ptr, codepoints.length);
  332. return 0;
  333. }
  334. extern (C) nothrow int luaL_getTime(lua_State* L)
  335. {
  336. //getTime() returns current time.
  337. lua_pushnumber(L, GetTime());
  338. return 1;
  339. }
  340. extern (C) nothrow int luaL_isKeyPressed(lua_State* L)
  341. {
  342. try
  343. {
  344. int keyCode = cast(int)luaL_checkinteger(L, 1);
  345. lua_pushboolean(L, IsKeyPressed(keyCode).to!bool);
  346. return 1;
  347. }
  348. catch (Exception e)
  349. {
  350. debugWriteln(e.msg);
  351. return 1;
  352. }
  353. }
  354. extern (C) nothrow int luaL_loadScript(lua_State* L)
  355. {
  356. for (int i = cast(int)characterTextures.length; i < characterTextures.length; i++)
  357. {
  358. UnloadTexture(characterTextures[i].texture);
  359. }
  360. for (int i = cast(int)backgroundTextures.length; i < backgroundTextures.length; i++)
  361. {
  362. UnloadTexture(backgroundTextures[i].texture);
  363. }
  364. try
  365. {
  366. luaExec = luaL_checkstring(L, 1).to!string;
  367. resetAllScriptValues();
  368. }
  369. catch (Exception e)
  370. {
  371. debugWriteln(e.msg);
  372. }
  373. luaReload = true;
  374. return 0;
  375. }
  376. extern (C) nothrow int luaL_setGameState(lua_State *L) {
  377. currentGameState = cast(int)luaL_checkinteger(L, 1);
  378. return 0;
  379. }
  380. /* raylib direct bindings for graphics */
  381. /* basic */
  382. extern (C) nothrow int luaL_loadTexture(lua_State *L) {
  383. const char* fileName = luaL_checkstring(L, 1);
  384. Texture2D texture = LoadTexture(fileName);
  385. Texture2D* texturePtr = cast(Texture2D*)lua_newuserdata(L, Texture2D.sizeof);
  386. *texturePtr = texture;
  387. if (luaL_newmetatable(L, "Texture")) {
  388. lua_pushcfunction(L, &luaL_textureGC);
  389. lua_setfield(L, -2, "__gc");
  390. }
  391. lua_setmetatable(L, -2);
  392. return 1;
  393. }
  394. extern (C) nothrow int luaL_textureGC(lua_State *L) {
  395. Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
  396. UnloadTexture(*texture);
  397. return 0;
  398. }
  399. extern (C) nothrow int luaL_drawTexture(lua_State *L) {
  400. Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
  401. int x = cast(int)luaL_checkinteger(L, 2);
  402. int y = cast(int)luaL_checkinteger(L, 3);
  403. Color color = Colors.WHITE;
  404. if (lua_gettop(L) >= 4 && lua_istable(L, 4)) {
  405. lua_getfield(L, 4, "r");
  406. color.r = cast(ubyte)lua_tointeger(L, -1);
  407. lua_pop(L, 1);
  408. lua_getfield(L, 4, "g");
  409. color.g = cast(ubyte)lua_tointeger(L, -1);
  410. lua_pop(L, 1);
  411. lua_getfield(L, 4, "b");
  412. color.b = cast(ubyte)lua_tointeger(L, -1);
  413. lua_pop(L, 1);
  414. lua_getfield(L, 4, "a");
  415. color.a = cast(ubyte)lua_tointeger(L, -1);
  416. lua_pop(L, 1);
  417. }
  418. DrawTexture(*texture, x, y, color);
  419. return 0;
  420. }
  421. extern (C) nothrow int luaL_getTextureWidth(lua_State *L) {
  422. Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
  423. lua_pushinteger(L, texture.width);
  424. return 1;
  425. }
  426. extern (C) nothrow int luaL_getTextureHeight(lua_State *L) {
  427. Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
  428. lua_pushinteger(L, texture.height);
  429. return 1;
  430. }
  431. extern (C) nothrow int luaL_drawText(lua_State *L) {
  432. const char* text = luaL_checkstring(L, 1);
  433. int x = cast(int)luaL_checkinteger(L, 2);
  434. int y = cast(int)luaL_checkinteger(L, 3);
  435. int fontSize = cast(int)luaL_optinteger(L, 4, 20);
  436. Color color = Colors.WHITE;
  437. if (lua_istable(L, 5)) {
  438. lua_getfield(L, 5, "r");
  439. color.r = cast(ubyte)lua_tointeger(L, -1);
  440. lua_pop(L, 1);
  441. lua_getfield(L, 5, "g");
  442. color.g = cast(ubyte)lua_tointeger(L, -1);
  443. lua_pop(L, 1);
  444. lua_getfield(L, 5, "b");
  445. color.b = cast(ubyte)lua_tointeger(L, -1);
  446. lua_pop(L, 1);
  447. lua_getfield(L, 5, "a");
  448. color.a = cast(ubyte)lua_tointeger(L, -1);
  449. lua_pop(L, 1);
  450. }
  451. DrawTextEx(textFont, text, Vector2(x, y), fontSize, 1.0f, color);
  452. return 0;
  453. }
  454. extern (C) nothrow int luaL_measureTextX(lua_State *L) {
  455. lua_pushinteger(L, cast(int)MeasureTextEx(textFont, luaL_checkstring(L, 1), cast(int)luaL_checkinteger(L, 2), 1.0f).x);
  456. return 1;
  457. }
  458. extern (C) nothrow int luaL_measureTextY(lua_State *L) {
  459. lua_pushinteger(L, cast(int)MeasureTextEx(textFont, luaL_checkstring(L, 1), cast(int)luaL_checkinteger(L, 2), 1.0f).y);
  460. return 1;
  461. }
  462. /* extended */
  463. extern (C) nothrow int luaL_drawTextureEx(lua_State *L) {
  464. Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
  465. float x = luaL_checknumber(L, 2);
  466. float y = luaL_checknumber(L, 3);
  467. float rotation = luaL_optnumber(L, 4, 0);
  468. float scale = luaL_optnumber(L, 5, 1);
  469. Color color = Colors.WHITE;
  470. if (lua_istable(L, 6)) {
  471. lua_getfield(L, 6, "r");
  472. color.r = cast(ubyte)lua_tointeger(L, -1);
  473. lua_pop(L, 1);
  474. lua_getfield(L, 6, "g");
  475. color.g = cast(ubyte)lua_tointeger(L, -1);
  476. lua_pop(L, 1);
  477. lua_getfield(L, 6, "b");
  478. color.b = cast(ubyte)lua_tointeger(L, -1);
  479. lua_pop(L, 1);
  480. lua_getfield(L, 6, "a");
  481. color.a = cast(ubyte)lua_tointeger(L, -1);
  482. lua_pop(L, 1);
  483. }
  484. DrawTextureEx(*texture, Vector2(x, y), rotation, scale, color);
  485. return 0;
  486. }
  487. /* Register functions */
  488. extern (C) nothrow void luaL_loader(lua_State* L)
  489. {
  490. lua_register(L, "dialogBox", &luaL_dialogBox);
  491. lua_register(L, "dialogAnswerValue", &luaL_dialogAnswerValue);
  492. lua_register(L, "isDialogExecuted", &luaL_isDialogExecuted);
  493. lua_register(L, "getAnswerValue", &luaL_getAnswerValue);
  494. lua_register(L, "loadAnimationUI", &luaL_loadUIAnimation);
  495. lua_register(L, "playAnimationUI", &luaL_playUIAnimation);
  496. lua_register(L, "stopAnimationUI", &luaL_stopUIAnimation);
  497. lua_register(L, "unloadAnimationUI", &luaL_unloadUIAnimation);
  498. lua_register(L, "moveCamera", &luaL_moveCamera);
  499. lua_register(L, "isCameraMoving", &luaL_isCameraMoving);
  500. lua_register(L, "playVideo", &luaL_playVideo);
  501. lua_register(L, "loadMusic", &luaL_loadMusic);
  502. lua_register(L, "playMusic", &luaL_playMusic);
  503. lua_register(L, "stopMusic", &luaL_stopMusic);
  504. lua_register(L, "unloadMusic", &luaL_unloadMusic);
  505. lua_register(L, "playSfx", &luaL_playSfx);
  506. lua_register(L, "stopSfx", &luaL_stopSfx);
  507. lua_register(L, "Begin2D", &luaL_2dModeEnable);
  508. lua_register(L, "End2D", &luaL_2dModeDisable);
  509. lua_register(L, "loadCharacter", &luaL_load2Dcharacter);
  510. lua_register(L, "drawCharacter", &luaL_draw2Dcharacter);
  511. lua_register(L, "stopDrawCharacter", &luaL_stopDraw2Dcharacter);
  512. lua_register(L, "unloadCharacter", &luaL_unload2Dcharacter);
  513. lua_register(L, "loadBackground", &luaL_load2Dbackground);
  514. lua_register(L, "drawBackground", &luaL_draw2Dbackground);
  515. lua_register(L, "stopDrawBackground", &luaL_stopDraw2Dbackground);
  516. lua_register(L, "unloadBackground", &luaL_unload2Dbackground);
  517. lua_register(L, "getTime", &luaL_getTime);
  518. lua_register(L, "loadScript", &luaL_loadScript);
  519. lua_register(L, "setFont", &luaL_setGameFont);
  520. lua_register(L, "getScreenHeight", &luaL_getScreenHeight);
  521. lua_register(L, "getScreenWidth", &luaL_getScreenWidth);
  522. lua_register(L, "isKeyPressed", &luaL_isKeyPressed);
  523. lua_register(L, "getLanguage", &luaL_getUsedLanguage);
  524. lua_register(L, "setGameState", &luaL_setGameState);
  525. //raylib direct bindings
  526. lua_register(L, "loadTexture", &luaL_loadTexture);
  527. lua_register(L, "drawTexture", &luaL_drawTexture);
  528. lua_register(L, "drawTextureEx", &luaL_drawTextureEx);
  529. lua_register(L, "drawText", &luaL_drawText);
  530. lua_register(L, "measureTextX", &luaL_measureTextX);
  531. lua_register(L, "measureTextY", &luaL_measureTextY);
  532. lua_register(L, "getTextureWidth", &luaL_getTextureWidth);
  533. lua_register(L, "getTextureHeight", &luaL_getTextureHeight);
  534. }
  535. int luaInit(string luaExec)
  536. {
  537. debugWriteln("loading Lua");
  538. L = luaL_newstate();
  539. luaL_openlibs(L);
  540. luaL_loader(L);
  541. debugWriteln("Executing next Lua file: ", luaExec);
  542. if (std.file.exists(luaExec) == false) {
  543. debugWriteln("Script file not found! Exiting.");
  544. return EngineExitCodes.EXIT_FILE_NOT_FOUND;
  545. }
  546. if (luaL_dofile(L, toStringz(luaExec)) != LUA_OK) {
  547. debugWriteln("Lua error: ", to!string(lua_tostring(L, -1)));
  548. return EngineExitCodes.EXIT_SCRIPT_ERROR;
  549. }
  550. return EngineExitCodes.EXIT_OK;
  551. }
  552. void luaEventLoop()
  553. {
  554. lua_getglobal(L, "EventLoop");
  555. if (lua_pcall(L, 0, 0, 0) != LUA_OK)
  556. {
  557. debug debugWriteln("Error in EventLoop: ", to!string(lua_tostring(L, -1)));
  558. }
  559. lua_pop(L, 0);
  560. }