ScriptController.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. #include "Base.h"
  2. #include "FileSystem.h"
  3. #include "ScriptController.h"
  4. #include "lua/lua_all_bindings.h"
  5. #define GENERATE_LUA_GET_POINTER(type, checkFunc) \
  6. /* Check that the parameter is the correct type. */ \
  7. if (!lua_istable(ScriptController::__instance->_lua, index)) \
  8. { \
  9. if (lua_islightuserdata(ScriptController::__instance->_lua, index)) \
  10. return (type*)lua_touserdata(ScriptController::__instance->_lua, index); \
  11. lua_pushfstring(ScriptController::__instance->_lua, "Expected a " #type " pointer (an array represented as a Lua table), got '%s' instead.", \
  12. luaL_typename(ScriptController::__instance->_lua, index)); \
  13. lua_error(ScriptController::__instance->_lua); \
  14. return NULL; \
  15. } \
  16. \
  17. /* Get the size of the array. */ \
  18. lua_len(ScriptController::__instance->_lua, index); \
  19. int size = luaL_checkint(ScriptController::__instance->_lua, -1); \
  20. if (size <= 0) \
  21. return NULL; \
  22. \
  23. /* Create an array to store the values. */ \
  24. type* values = (type*)malloc(sizeof(type)*size); \
  25. \
  26. /* Push the first key. */ \
  27. lua_pushnil(ScriptController::__instance->_lua); \
  28. int i = 0; \
  29. for (; lua_next(ScriptController::__instance->_lua, index) != 0 && i < size; i++) \
  30. { \
  31. values[i] = (checkFunc(ScriptController::__instance->_lua, -1)); \
  32. \
  33. /* Remove the value we just retrieved, but leave the key for the next iteration. */ \
  34. lua_pop(ScriptController::__instance->_lua, 1); \
  35. } \
  36. \
  37. return values
  38. namespace gameplay
  39. {
  40. void ScriptUtil::registerLibrary(const char* name, const luaL_Reg* functions)
  41. {
  42. lua_newtable(ScriptController::__instance->_lua);
  43. // Go through the list of functions and add them to the table.
  44. const luaL_Reg* iter = functions;
  45. for (; iter && iter->name; iter++)
  46. {
  47. lua_pushcfunction(ScriptController::__instance->_lua, iter->func);
  48. lua_setfield(ScriptController::__instance->_lua, -2, iter->name);
  49. }
  50. lua_setglobal(ScriptController::__instance->_lua, name);
  51. }
  52. void ScriptUtil::registerConstantBool(std::string name, bool value, std::vector<std::string> scopePath)
  53. {
  54. // If the constant is within a scope, get the correct parent
  55. // table on the stack before setting its value.
  56. if (scopePath.size() > 0)
  57. {
  58. lua_getglobal(ScriptController::__instance->_lua, scopePath[0].c_str());
  59. for (unsigned int i = 1; i < scopePath.size(); i++)
  60. {
  61. lua_pushstring(ScriptController::__instance->_lua, scopePath[i].c_str());
  62. lua_gettable(ScriptController::__instance->_lua, -2);
  63. }
  64. // Add the constant to the parent table.
  65. lua_pushboolean(ScriptController::__instance->_lua, value);
  66. lua_setfield(ScriptController::__instance->_lua, -2, name.c_str());
  67. // Pop all the parent tables off the stack.
  68. int size = scopePath.size();
  69. lua_pop(ScriptController::__instance->_lua, size);
  70. }
  71. else
  72. {
  73. // TODO: Currently unsupported (we don't parse for this yet).
  74. // If the constant is global, add it to the global table.
  75. lua_pushboolean(ScriptController::__instance->_lua, value);
  76. lua_pushvalue(ScriptController::__instance->_lua, -1);
  77. lua_setglobal(ScriptController::__instance->_lua, name.c_str());
  78. }
  79. }
  80. void ScriptUtil::registerConstantNumber(std::string name, double value, std::vector<std::string> scopePath)
  81. {
  82. // If the constant is within a scope, get the correct parent
  83. // table on the stack before setting its value.
  84. if (scopePath.size() > 0)
  85. {
  86. lua_getglobal(ScriptController::__instance->_lua, scopePath[0].c_str());
  87. for (unsigned int i = 1; i < scopePath.size(); i++)
  88. {
  89. lua_pushstring(ScriptController::__instance->_lua, scopePath[i].c_str());
  90. lua_gettable(ScriptController::__instance->_lua, -2);
  91. }
  92. // Add the constant to the parent table.
  93. lua_pushnumber(ScriptController::__instance->_lua, value);
  94. lua_setfield(ScriptController::__instance->_lua, -2, name.c_str());
  95. // Pop all the parent tables off the stack.
  96. int size = scopePath.size();
  97. lua_pop(ScriptController::__instance->_lua, size);
  98. }
  99. else
  100. {
  101. // TODO: Currently unsupported (we don't parse for this yet).
  102. // If the constant is global, add it to the global table.
  103. lua_pushnumber(ScriptController::__instance->_lua, value);
  104. lua_pushvalue(ScriptController::__instance->_lua, -1);
  105. lua_setglobal(ScriptController::__instance->_lua, name.c_str());
  106. }
  107. }
  108. void ScriptUtil::registerConstantString(std::string name, std::string value, std::vector<std::string> scopePath)
  109. {
  110. // If the constant is within a scope, get the correct parent
  111. // table on the stack before setting its value.
  112. if (scopePath.size() > 0)
  113. {
  114. lua_getglobal(ScriptController::__instance->_lua, scopePath[0].c_str());
  115. for (unsigned int i = 1; i < scopePath.size(); i++)
  116. {
  117. lua_pushstring(ScriptController::__instance->_lua, scopePath[i].c_str());
  118. lua_gettable(ScriptController::__instance->_lua, -2);
  119. }
  120. // Add the constant to the parent table.
  121. lua_pushstring(ScriptController::__instance->_lua, value.c_str());
  122. lua_setfield(ScriptController::__instance->_lua, -2, name.c_str());
  123. // Pop all the parent tables off the stack.
  124. int size = scopePath.size();
  125. lua_pop(ScriptController::__instance->_lua, size);
  126. }
  127. else
  128. {
  129. // TODO: Currently unsupported (we don't parse for this yet).
  130. // If the constant is global, add it to the global table.
  131. lua_pushstring(ScriptController::__instance->_lua, value.c_str());
  132. lua_pushvalue(ScriptController::__instance->_lua, -1);
  133. lua_setglobal(ScriptController::__instance->_lua, name.c_str());
  134. }
  135. }
  136. void ScriptUtil::registerClass(const char* name, const luaL_Reg* members, lua_CFunction newFunction,
  137. lua_CFunction deleteFunction, const luaL_Reg* statics, std::vector<std::string> scopePath)
  138. {
  139. // If the type is an inner type, get the correct parent
  140. // table on the stack before creating the table for the class.
  141. if (scopePath.size() > 0)
  142. {
  143. std::string tablename = name;
  144. // Strip off the scope path part of the name.
  145. lua_getglobal(ScriptController::__instance->_lua, scopePath[0].c_str());
  146. std::size_t index = tablename.find(scopePath[0]);
  147. if (index != tablename.npos)
  148. tablename = tablename.substr(index + scopePath[0].size());
  149. for (unsigned int i = 1; i < scopePath.size(); i++)
  150. {
  151. lua_pushstring(ScriptController::__instance->_lua, scopePath[i].c_str());
  152. lua_gettable(ScriptController::__instance->_lua, -2);
  153. index = tablename.find(scopePath[i]);
  154. if (index != tablename.npos)
  155. tablename = tablename.substr(index + scopePath[i].size());
  156. }
  157. lua_pushstring(ScriptController::__instance->_lua, tablename.c_str());
  158. lua_newtable(ScriptController::__instance->_lua);
  159. }
  160. else
  161. {
  162. // If the type is not an inner type, set it as a global table.
  163. lua_newtable(ScriptController::__instance->_lua);
  164. lua_pushvalue(ScriptController::__instance->_lua, -1);
  165. lua_setglobal(ScriptController::__instance->_lua, name);
  166. }
  167. // Create the metatable and populate it with the member functions.
  168. lua_pushliteral(ScriptController::__instance->_lua, "__metatable");
  169. luaL_newmetatable(ScriptController::__instance->_lua, name);
  170. if (members)
  171. luaL_setfuncs(ScriptController::__instance->_lua, members, 0);
  172. lua_pushstring(ScriptController::__instance->_lua, "__index");
  173. lua_pushvalue(ScriptController::__instance->_lua, -2);
  174. lua_settable(ScriptController::__instance->_lua, -3);
  175. // Add the delete function if it was specified.
  176. if (deleteFunction)
  177. {
  178. lua_pushstring(ScriptController::__instance->_lua, "__gc");
  179. lua_pushcfunction(ScriptController::__instance->_lua, deleteFunction);
  180. lua_settable(ScriptController::__instance->_lua, -3);
  181. }
  182. // Set the metatable on the main table.
  183. lua_settable(ScriptController::__instance->_lua, -3);
  184. // Populate the main table with the static functions.
  185. if (statics)
  186. luaL_setfuncs(ScriptController::__instance->_lua, statics, 0);
  187. // Set the new function(s) for the class.
  188. if (newFunction)
  189. {
  190. lua_pushliteral(ScriptController::__instance->_lua, "new");
  191. lua_pushcfunction(ScriptController::__instance->_lua, newFunction);
  192. lua_settable(ScriptController::__instance->_lua, -3);
  193. }
  194. // Set the table we just created within the correct parent table.
  195. if (scopePath.size() > 0)
  196. {
  197. lua_settable(ScriptController::__instance->_lua, -3);
  198. // Pop all the parent tables off the stack.
  199. int size = scopePath.size();
  200. lua_pop(ScriptController::__instance->_lua, size);
  201. }
  202. else
  203. {
  204. // Pop the main table off the stack.
  205. lua_pop(ScriptController::__instance->_lua, 1);
  206. }
  207. }
  208. void ScriptUtil::registerFunction(const char* luaFunction, lua_CFunction cppFunction)
  209. {
  210. lua_pushcfunction(ScriptController::__instance->_lua, cppFunction);
  211. lua_setglobal(ScriptController::__instance->_lua, luaFunction);
  212. }
  213. void ScriptUtil::setGlobalHierarchy(std::map<std::string, std::vector<std::string> > hierarchy)
  214. {
  215. ScriptController::__instance->_hierarchy = hierarchy;
  216. }
  217. bool* ScriptUtil::getBoolPointer(int index)
  218. {
  219. GENERATE_LUA_GET_POINTER(bool, luaCheckBool);
  220. }
  221. short* ScriptUtil::getShortPointer(int index)
  222. {
  223. GENERATE_LUA_GET_POINTER(short, (short)luaL_checkint);
  224. }
  225. int* ScriptUtil::getIntPointer(int index)
  226. {
  227. GENERATE_LUA_GET_POINTER(int, (int)luaL_checkint);
  228. }
  229. long* ScriptUtil::getLongPointer(int index)
  230. {
  231. GENERATE_LUA_GET_POINTER(long, (long)luaL_checkint);
  232. }
  233. unsigned char* ScriptUtil::getUnsignedCharPointer(int index)
  234. {
  235. GENERATE_LUA_GET_POINTER(unsigned char, (unsigned char)luaL_checkunsigned);
  236. }
  237. unsigned short* ScriptUtil::getUnsignedShortPointer(int index)
  238. {
  239. GENERATE_LUA_GET_POINTER(unsigned short, (unsigned short)luaL_checkunsigned);
  240. }
  241. unsigned int* ScriptUtil::getUnsignedIntPointer(int index)
  242. {
  243. GENERATE_LUA_GET_POINTER(unsigned int, (unsigned int)luaL_checkunsigned);
  244. }
  245. unsigned long* ScriptUtil::getUnsignedLongPointer(int index)
  246. {
  247. GENERATE_LUA_GET_POINTER(unsigned long, (unsigned long)luaL_checkunsigned);
  248. }
  249. float* ScriptUtil::getFloatPointer(int index)
  250. {
  251. GENERATE_LUA_GET_POINTER(float, (float)luaL_checknumber);
  252. }
  253. double* ScriptUtil::getDoublePointer(int index)
  254. {
  255. GENERATE_LUA_GET_POINTER(double, (double)luaL_checknumber);
  256. }
  257. const char* ScriptUtil::getString(int index, bool isStdString)
  258. {
  259. if (lua_type(ScriptController::__instance->_lua, index) == LUA_TSTRING)
  260. return luaL_checkstring(ScriptController::__instance->_lua, index);
  261. else if (lua_type(ScriptController::__instance->_lua, index) == LUA_TNIL && !isStdString)
  262. return NULL;
  263. else
  264. {
  265. GP_ERROR("Invalid string parameter (index = %d).", index);
  266. return NULL;
  267. }
  268. }
  269. bool ScriptUtil::luaCheckBool(lua_State* state, int n)
  270. {
  271. if (!lua_isboolean(state, n))
  272. {
  273. const char* msg = lua_pushfstring(state, "%s expected, got %s", lua_typename(state, LUA_TBOOLEAN), luaL_typename(state, n));
  274. luaL_argerror(state, n, msg);
  275. return false;
  276. }
  277. return (lua_toboolean(state, n) != 0);
  278. }
  279. ScriptController* ScriptController::__instance = NULL;
  280. ScriptController* ScriptController::getInstance()
  281. {
  282. return __instance;
  283. }
  284. void ScriptController::loadScript(const char* path)
  285. {
  286. const char* scriptContents = FileSystem::readAll(path);
  287. if (luaL_dostring(_lua, scriptContents))
  288. GP_ERROR("Failed to run Lua script with error: '%s'.", lua_tostring(_lua, -1));
  289. SAFE_DELETE_ARRAY(scriptContents);
  290. }
  291. bool ScriptController::getBool(const char* name)
  292. {
  293. lua_getglobal(_lua, name);
  294. return ScriptUtil::luaCheckBool(_lua, -1);
  295. }
  296. char ScriptController::getChar(const char* name)
  297. {
  298. lua_getglobal(_lua, name);
  299. return (char)luaL_checkint(_lua, -1);
  300. }
  301. short ScriptController::getShort(const char* name)
  302. {
  303. lua_getglobal(_lua, name);
  304. return (short)luaL_checkint(_lua, -1);
  305. }
  306. int ScriptController::getInt(const char* name)
  307. {
  308. lua_getglobal(_lua, name);
  309. return luaL_checkint(_lua, -1);
  310. }
  311. long ScriptController::getLong(const char* name)
  312. {
  313. lua_getglobal(_lua, name);
  314. return luaL_checklong(_lua, -1);
  315. }
  316. unsigned char ScriptController::getUnsignedChar(const char* name)
  317. {
  318. lua_getglobal(_lua, name);
  319. return (unsigned char)luaL_checkunsigned(_lua, -1);
  320. }
  321. unsigned short ScriptController::getUnsignedShort(const char* name)
  322. {
  323. lua_getglobal(_lua, name);
  324. return (unsigned short)luaL_checkunsigned(_lua, -1);
  325. }
  326. unsigned int ScriptController::getUnsignedInt(const char* name)
  327. {
  328. lua_getglobal(_lua, name);
  329. return (unsigned int)luaL_checkunsigned(_lua, -1);
  330. }
  331. unsigned long ScriptController::getUnsignedLong(const char* name)
  332. {
  333. lua_getglobal(_lua, name);
  334. return (unsigned long)luaL_checkunsigned(_lua, -1);
  335. }
  336. float ScriptController::getFloat(const char* name)
  337. {
  338. lua_getglobal(_lua, name);
  339. return (float)luaL_checknumber(_lua, -1);
  340. }
  341. double ScriptController::getDouble(const char* name)
  342. {
  343. lua_getglobal(_lua, name);
  344. return (double)luaL_checknumber(_lua, -1);
  345. }
  346. const char* ScriptController::getString(const char* name)
  347. {
  348. lua_getglobal(_lua, name);
  349. return luaL_checkstring(_lua, -1);
  350. }
  351. void ScriptController::setBool(const char* name, bool v)
  352. {
  353. lua_pushboolean(_lua, v);
  354. lua_setglobal(_lua, name);
  355. }
  356. void ScriptController::setChar(const char* name, char v)
  357. {
  358. lua_pushinteger(_lua, v);
  359. lua_setglobal(_lua, name);
  360. }
  361. void ScriptController::setShort(const char* name, short v)
  362. {
  363. lua_pushinteger(_lua, v);
  364. lua_setglobal(_lua, name);
  365. }
  366. void ScriptController::setInt(const char* name, int v)
  367. {
  368. lua_pushinteger(_lua, v);
  369. lua_setglobal(_lua, name);
  370. }
  371. void ScriptController::setLong(const char* name, long v)
  372. {
  373. lua_pushinteger(_lua, v);
  374. lua_setglobal(_lua, name);
  375. }
  376. void ScriptController::setUnsignedChar(const char* name, unsigned char v)
  377. {
  378. lua_pushunsigned(_lua, v);
  379. lua_setglobal(_lua, name);
  380. }
  381. void ScriptController::setUnsignedShort(const char* name, unsigned short v)
  382. {
  383. lua_pushunsigned(_lua, v);
  384. lua_setglobal(_lua, name);
  385. }
  386. void ScriptController::setUnsignedInt(const char* name, unsigned int v)
  387. {
  388. lua_pushunsigned(_lua, v);
  389. lua_setglobal(_lua, name);
  390. }
  391. void ScriptController::setUnsignedLong(const char* name, unsigned long v)
  392. {
  393. lua_pushunsigned(_lua, v);
  394. lua_setglobal(_lua, name);
  395. }
  396. void ScriptController::setFloat(const char* name, float v)
  397. {
  398. lua_pushnumber(_lua, v);
  399. lua_setglobal(_lua, name);
  400. }
  401. void ScriptController::setDouble(const char* name, double v)
  402. {
  403. lua_pushnumber(_lua, v);
  404. lua_setglobal(_lua, name);
  405. }
  406. void ScriptController::setString(const char* name, const char* v)
  407. {
  408. lua_pushstring(_lua, v);
  409. lua_setglobal(_lua, name);
  410. }
  411. ScriptController::ScriptController() : _lua(NULL)
  412. {
  413. memset(_callbacks, 0, sizeof(std::string*) * CALLBACK_COUNT);
  414. __instance = this;
  415. }
  416. ScriptController::~ScriptController()
  417. {
  418. __instance = NULL;
  419. }
  420. void ScriptController::initialize()
  421. {
  422. _lua = luaL_newstate();
  423. if (!_lua)
  424. GP_ERROR("Failed to initialize Lua scripting engine.");
  425. luaL_openlibs(_lua);
  426. lua_RegisterAllBindings();
  427. }
  428. void ScriptController::initializeGame()
  429. {
  430. if (_callbacks[INITIALIZE])
  431. {
  432. executeFunction<void>(_callbacks[INITIALIZE]->c_str());
  433. }
  434. }
  435. void ScriptController::finalize()
  436. {
  437. if (_lua)
  438. lua_close(_lua);
  439. }
  440. void ScriptController::finalizeGame()
  441. {
  442. if (_callbacks[FINALIZE])
  443. {
  444. executeFunction<void>(_callbacks[FINALIZE]->c_str());
  445. }
  446. }
  447. void ScriptController::update(float elapsedTime)
  448. {
  449. if (_callbacks[UPDATE])
  450. {
  451. executeFunction<void>(_callbacks[UPDATE]->c_str(), "f", elapsedTime);
  452. }
  453. }
  454. void ScriptController::render(float elapsedTime)
  455. {
  456. if (_callbacks[RENDER])
  457. {
  458. executeFunction<void>(_callbacks[RENDER]->c_str(), "f", elapsedTime);
  459. }
  460. }
  461. void ScriptController::keyEvent(Keyboard::KeyEvent evt, int key)
  462. {
  463. if (_callbacks[KEY_EVENT])
  464. {
  465. executeFunction<void>(_callbacks[KEY_EVENT]->c_str(), "[Keyboard::KeyEvent][Keyboard::Key]", evt, key);
  466. }
  467. }
  468. void ScriptController::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  469. {
  470. if (_callbacks[TOUCH_EVENT])
  471. {
  472. executeFunction<void>(_callbacks[TOUCH_EVENT]->c_str(), "[Touch::TouchEvent]iiui", evt, x, y, contactIndex);
  473. }
  474. }
  475. bool ScriptController::mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  476. {
  477. if (_callbacks[MOUSE_EVENT])
  478. {
  479. return executeFunction<bool>(_callbacks[MOUSE_EVENT]->c_str(), "[Mouse::MouseEvent]iiii", evt, x, y, wheelDelta);
  480. }
  481. return false;
  482. }
  483. void ScriptController::gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad)
  484. {
  485. if (_callbacks[GAMEPAD_EVENT])
  486. {
  487. executeFunction<void>(_callbacks[GAMEPAD_EVENT]->c_str(), "[Gamepad::GamepadEvent]<Gamepad>", evt, gamepad);
  488. }
  489. }
  490. void ScriptController::executeFunctionHelper(int resultCount, const char* func, const char* args, va_list* list)
  491. {
  492. if (func == NULL)
  493. {
  494. GP_ERROR("Lua function name must be non-null.");
  495. return;
  496. }
  497. const char* sig = args;
  498. int argumentCount = 0;
  499. lua_getglobal(_lua, func);
  500. // Push the arguments to the Lua stack if there are any.
  501. if (sig)
  502. {
  503. while (true)
  504. {
  505. if (!(*sig))
  506. break;
  507. switch(*sig++)
  508. {
  509. // Signed integers.
  510. case 'c':
  511. case 'h':
  512. case 'i':
  513. case 'l':
  514. lua_pushinteger(_lua, va_arg(*list, int));
  515. break;
  516. // Unsigned integers.
  517. case 'u':
  518. // Skip past the actual type (long, int, short, char).
  519. sig++;
  520. lua_pushunsigned(_lua, va_arg(*list, int));
  521. break;
  522. // Booleans.
  523. case 'b':
  524. lua_pushboolean(_lua, va_arg(*list, int));
  525. break;
  526. // Floating point numbers.
  527. case 'f':
  528. case 'd':
  529. lua_pushnumber(_lua, va_arg(*list, double));
  530. break;
  531. // Strings.
  532. case 's':
  533. lua_pushstring(_lua, va_arg(*list, char*));
  534. break;
  535. // Pointers.
  536. case 'p':
  537. lua_pushlightuserdata(_lua, va_arg(*list, void*));
  538. break;
  539. // Enums.
  540. case '[':
  541. {
  542. std::string type = sig;
  543. type = type.substr(0, type.find("]"));
  544. // Skip past the closing ']' (the semi-colon here is intentional-do not remove).
  545. while (*sig++ != ']');
  546. lua_pushstring(_lua, lua_stringFromEnumGlobal(type, va_arg(*list, int)));
  547. break;
  548. }
  549. // Object references/pointers (Lua userdata).
  550. case '<':
  551. {
  552. std::string type = sig;
  553. type = type.substr(0, type.find(">"));
  554. // Skip past the closing '>' (the semi-colon here is intentional-do not remove).
  555. while (*sig++ != '>');
  556. // Calculate the unique Lua type name.
  557. size_t i = type.find("::");
  558. while (i != type.npos)
  559. {
  560. // We use "" as the replacement here-this must match the preprocessor
  561. // define SCOPE_REPLACEMENT from the gameplay-luagen project.
  562. type.replace(i, 2, "");
  563. i = type.find("::");
  564. }
  565. void* ptr = va_arg(*list, void*);
  566. if (ptr == NULL)
  567. {
  568. lua_pushnil(_lua);
  569. }
  570. else
  571. {
  572. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(_lua, sizeof(ScriptUtil::LuaObject));
  573. object->instance = ptr;
  574. object->owns = false;
  575. luaL_getmetatable(_lua, type.c_str());
  576. lua_setmetatable(_lua, -2);
  577. }
  578. break;
  579. }
  580. default:
  581. GP_ERROR("Invalid argument type '%d'.", *(sig - 1));
  582. }
  583. argumentCount++;
  584. luaL_checkstack(_lua, 1, "Too many arguments.");
  585. }
  586. }
  587. // Perform the function call.
  588. if (lua_pcall(_lua, argumentCount, resultCount, 0) != 0)
  589. GP_ERROR("Failed to call function '%s' with error '%s'.", func, lua_tostring(_lua, -1));
  590. }
  591. void ScriptController::registerCallback(ScriptCallback callback, std::string function)
  592. {
  593. SAFE_DELETE(_callbacks[callback]);
  594. _callbacks[callback] = new std::string(function);
  595. }
  596. // Helper macros.
  597. #define SCRIPT_EXECUTE_FUNCTION_NO_PARAM(type, checkfunc) \
  598. executeFunctionHelper(1, func, NULL, NULL); \
  599. type value = (type)checkfunc(_lua, -1); \
  600. lua_pop(_lua, -1); \
  601. return value;
  602. #define SCRIPT_EXECUTE_FUNCTION_PARAM(type, checkfunc) \
  603. va_list list; \
  604. va_start(list, args); \
  605. executeFunctionHelper(1, func, args, &list); \
  606. type value = (type)checkfunc(_lua, -1); \
  607. lua_pop(_lua, -1); \
  608. va_end(list); \
  609. return value;
  610. template<> void ScriptController::executeFunction<void>(const char* func)
  611. {
  612. executeFunctionHelper(0, func, NULL, NULL);
  613. }
  614. template<> bool ScriptController::executeFunction<bool>(const char* func)
  615. {
  616. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(bool, ScriptUtil::luaCheckBool);
  617. }
  618. template<> char ScriptController::executeFunction<char>(const char* func)
  619. {
  620. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(char, luaL_checkint);
  621. }
  622. template<> short ScriptController::executeFunction<short>(const char* func)
  623. {
  624. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(short, luaL_checkint);
  625. }
  626. template<> int ScriptController::executeFunction<int>(const char* func)
  627. {
  628. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(int, luaL_checkint);
  629. }
  630. template<> long ScriptController::executeFunction<long>(const char* func)
  631. {
  632. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(long, luaL_checklong);
  633. }
  634. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func)
  635. {
  636. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(unsigned char, luaL_checkunsigned);
  637. }
  638. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func)
  639. {
  640. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(unsigned short, luaL_checkunsigned);
  641. }
  642. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func)
  643. {
  644. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(unsigned int, luaL_checkunsigned);
  645. }
  646. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func)
  647. {
  648. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(unsigned long, luaL_checkunsigned);
  649. }
  650. template<> float ScriptController::executeFunction<float>(const char* func)
  651. {
  652. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(float, luaL_checknumber);
  653. }
  654. template<> double ScriptController::executeFunction<double>(const char* func)
  655. {
  656. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(double, luaL_checknumber);
  657. }
  658. template<> std::string ScriptController::executeFunction<std::string>(const char* func)
  659. {
  660. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(std::string, luaL_checkstring);
  661. }
  662. template<> void ScriptController::executeFunction<void>(const char* func, const char* args, ...)
  663. {
  664. va_list list;
  665. va_start(list, args);
  666. executeFunctionHelper(0, func, args, &list);
  667. va_end(list);
  668. }
  669. template<> bool ScriptController::executeFunction<bool>(const char* func, const char* args, ...)
  670. {
  671. SCRIPT_EXECUTE_FUNCTION_PARAM(bool, ScriptUtil::luaCheckBool);
  672. }
  673. template<> char ScriptController::executeFunction<char>(const char* func, const char* args, ...)
  674. {
  675. SCRIPT_EXECUTE_FUNCTION_PARAM(char, luaL_checkint);
  676. }
  677. template<> short ScriptController::executeFunction<short>(const char* func, const char* args, ...)
  678. {
  679. SCRIPT_EXECUTE_FUNCTION_PARAM(short, luaL_checkint);
  680. }
  681. template<> int ScriptController::executeFunction<int>(const char* func, const char* args, ...)
  682. {
  683. SCRIPT_EXECUTE_FUNCTION_PARAM(int, luaL_checkint);
  684. }
  685. template<> long ScriptController::executeFunction<long>(const char* func, const char* args, ...)
  686. {
  687. SCRIPT_EXECUTE_FUNCTION_PARAM(long, luaL_checklong);
  688. }
  689. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func, const char* args, ...)
  690. {
  691. SCRIPT_EXECUTE_FUNCTION_PARAM(unsigned char, luaL_checkunsigned);
  692. }
  693. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func, const char* args, ...)
  694. {
  695. SCRIPT_EXECUTE_FUNCTION_PARAM(unsigned short, luaL_checkunsigned);
  696. }
  697. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func, const char* args, ...)
  698. {
  699. SCRIPT_EXECUTE_FUNCTION_PARAM(unsigned int, luaL_checkunsigned);
  700. }
  701. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func, const char* args, ...)
  702. {
  703. SCRIPT_EXECUTE_FUNCTION_PARAM(unsigned long, luaL_checkunsigned);
  704. }
  705. template<> float ScriptController::executeFunction<float>(const char* func, const char* args, ...)
  706. {
  707. SCRIPT_EXECUTE_FUNCTION_PARAM(float, luaL_checknumber);
  708. }
  709. template<> double ScriptController::executeFunction<double>(const char* func, const char* args, ...)
  710. {
  711. SCRIPT_EXECUTE_FUNCTION_PARAM(double, luaL_checknumber);
  712. }
  713. template<> std::string ScriptController::executeFunction<std::string>(const char* func, const char* args, ...)
  714. {
  715. SCRIPT_EXECUTE_FUNCTION_PARAM(std::string, luaL_checkstring);
  716. }
  717. }