ScriptController.cpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  1. #include "Base.h"
  2. #include "FileSystem.h"
  3. #include "ScriptController.h"
  4. #ifndef NO_LUA_BINDINGS
  5. #include "lua/lua_all_bindings.h"
  6. #endif
  7. #define GENERATE_LUA_GET_POINTER(type, checkFunc) \
  8. ScriptController* sc = Game::getInstance()->getScriptController(); \
  9. /* Check that the parameter is the correct type. */ \
  10. if (!lua_istable(sc->_lua, index)) \
  11. { \
  12. if (lua_islightuserdata(sc->_lua, index)) \
  13. return LuaArray<type>((type*)lua_touserdata(sc->_lua, index)); \
  14. lua_pushfstring(sc->_lua, "Expected a " #type " pointer (an array represented as a Lua table), got '%s' instead.", \
  15. luaL_typename(sc->_lua, index)); \
  16. lua_error(sc->_lua); \
  17. return LuaArray<type>((type*)NULL); \
  18. } \
  19. \
  20. /* Get the size of the array. */ \
  21. lua_len(sc->_lua, index); \
  22. int size = luaL_checkint(sc->_lua, -1); \
  23. if (size <= 0) \
  24. return LuaArray<type>((type*)NULL); \
  25. \
  26. /* Declare a LuaArray to store the values. */ \
  27. LuaArray<type> arr(size); \
  28. \
  29. /* Push the first key. */ \
  30. lua_pushnil(sc->_lua); \
  31. int i = 0; \
  32. for (; lua_next(sc->_lua, index) != 0 && i < size; i++) \
  33. { \
  34. arr[i] = (checkFunc(sc->_lua, -1)); \
  35. \
  36. /* Remove the value we just retrieved, but leave the key for the next iteration. */ \
  37. lua_pop(sc->_lua, 1); \
  38. } \
  39. \
  40. return arr
  41. #define PUSH_NESTED_VARIABLE(name, defaultValue) \
  42. int top = lua_gettop(_lua); \
  43. if (!getNestedVariable(_lua, (name))) \
  44. { \
  45. lua_settop(_lua, top); \
  46. return (defaultValue); \
  47. }
  48. #define POP_NESTED_VARIABLE() \
  49. lua_settop(_lua, top)
  50. /**
  51. * Pushes onto the stack, the value of the global 'name' or the nested table value if 'name' is a '.' separated
  52. * list of tables of the form "A.B.C.D", where A, B and C are tables and D is a variable name in the table C.
  53. *
  54. * If 'name' does not contain any '.' then it is assumed to be the name of a global variable.
  55. *
  56. * This function will not restore the stack if there is an error.
  57. *
  58. * @param lua The Lua state.
  59. * @param name The name of a global variable or a '.' separated list of nested tables ending with a variable name.
  60. * The name value may be in the format "A.B.C.D" where A is a table and B, C are child tables.
  61. * D is any type, which will be accessed by the calling function.
  62. *
  63. * @return True if the tables were pushed on the stack or the global variable was pushed. Returns false on error.
  64. */
  65. static bool getNestedVariable(lua_State* lua, const char* name)
  66. {
  67. if (strchr(name, '.') == NULL)
  68. {
  69. lua_getglobal(lua, name);
  70. return true;
  71. }
  72. static std::string str;
  73. // Copy the input string to a std::string so we can modify it because
  74. // some of the Lua functions require NULL terminated c-strings.
  75. str.assign(name);
  76. // Find the first table, which will be a global variable.
  77. char* start = const_cast<char*>(str.c_str());
  78. char* end = strchr(start, '.');
  79. if (end == NULL)
  80. {
  81. return false;
  82. }
  83. ++end;
  84. *(end - 1) = '\0';
  85. lua_getglobal(lua, start);
  86. *(end - 1) = '.';
  87. if (!lua_istable(lua, -1))
  88. {
  89. return false;
  90. }
  91. // Push the nested tables
  92. for (;;)
  93. {
  94. start = end;
  95. end = strchr(start, '.');
  96. if (end == '\0' || end == NULL)
  97. {
  98. // push the last variable
  99. lua_pushstring(lua, start);
  100. lua_gettable(lua, -2);
  101. return true;
  102. }
  103. else
  104. {
  105. // Push the next table
  106. *end = '\0';
  107. lua_pushstring(lua, start);
  108. *end = '.';
  109. lua_gettable(lua, -2);
  110. if (!lua_istable(lua, -1))
  111. {
  112. return false;
  113. }
  114. ++end;
  115. if (*end == '.')
  116. {
  117. return false;
  118. }
  119. }
  120. }
  121. return false;
  122. }
  123. namespace gameplay
  124. {
  125. extern void splitURL(const std::string& url, std::string* file, std::string* id);
  126. void ScriptUtil::registerLibrary(const char* name, const luaL_Reg* functions)
  127. {
  128. ScriptController* sc = Game::getInstance()->getScriptController();
  129. lua_newtable(sc->_lua);
  130. // Go through the list of functions and add them to the table.
  131. const luaL_Reg* iter = functions;
  132. for (; iter && iter->name; iter++)
  133. {
  134. lua_pushcfunction(sc->_lua, iter->func);
  135. lua_setfield(sc->_lua, -2, iter->name);
  136. }
  137. lua_setglobal(sc->_lua, name);
  138. }
  139. void ScriptUtil::registerConstantBool(const std::string& name, bool value, const std::vector<std::string>& scopePath)
  140. {
  141. ScriptController* sc = Game::getInstance()->getScriptController();
  142. // If the constant is within a scope, get the correct parent
  143. // table on the stack before setting its value.
  144. if (!scopePath.empty())
  145. {
  146. lua_getglobal(sc->_lua, scopePath[0].c_str());
  147. for (unsigned int i = 1; i < scopePath.size(); i++)
  148. {
  149. lua_pushstring(sc->_lua, scopePath[i].c_str());
  150. lua_gettable(sc->_lua, -2);
  151. }
  152. // Add the constant to the parent table.
  153. lua_pushboolean(sc->_lua, value);
  154. lua_setfield(sc->_lua, -2, name.c_str());
  155. // Pop all the parent tables off the stack.
  156. int size = (int)scopePath.size();
  157. lua_pop(sc->_lua, size);
  158. }
  159. else
  160. {
  161. // TODO: Currently unsupported (we don't parse for this yet).
  162. // If the constant is global, add it to the global table.
  163. lua_pushboolean(sc->_lua, value);
  164. lua_pushvalue(sc->_lua, -1);
  165. lua_setglobal(sc->_lua, name.c_str());
  166. }
  167. }
  168. void ScriptUtil::registerConstantNumber(const std::string& name, double value, const std::vector<std::string>& scopePath)
  169. {
  170. ScriptController* sc = Game::getInstance()->getScriptController();
  171. // If the constant is within a scope, get the correct parent
  172. // table on the stack before setting its value.
  173. if (!scopePath.empty())
  174. {
  175. lua_getglobal(sc->_lua, scopePath[0].c_str());
  176. for (unsigned int i = 1; i < scopePath.size(); i++)
  177. {
  178. lua_pushstring(sc->_lua, scopePath[i].c_str());
  179. lua_gettable(sc->_lua, -2);
  180. }
  181. // Add the constant to the parent table.
  182. lua_pushnumber(sc->_lua, value);
  183. lua_setfield(sc->_lua, -2, name.c_str());
  184. // Pop all the parent tables off the stack.
  185. int size = (int)scopePath.size();
  186. lua_pop(sc->_lua, size);
  187. }
  188. else
  189. {
  190. // TODO: Currently unsupported (we don't parse for this yet).
  191. // If the constant is global, add it to the global table.
  192. lua_pushnumber(sc->_lua, value);
  193. lua_pushvalue(sc->_lua, -1);
  194. lua_setglobal(sc->_lua, name.c_str());
  195. }
  196. }
  197. void ScriptUtil::registerConstantString(const std::string& name, const std::string& value, const std::vector<std::string>& scopePath)
  198. {
  199. ScriptController* sc = Game::getInstance()->getScriptController();
  200. // If the constant is within a scope, get the correct parent
  201. // table on the stack before setting its value.
  202. if (!scopePath.empty())
  203. {
  204. lua_getglobal(sc->_lua, scopePath[0].c_str());
  205. for (unsigned int i = 1; i < scopePath.size(); i++)
  206. {
  207. lua_pushstring(sc->_lua, scopePath[i].c_str());
  208. lua_gettable(sc->_lua, -2);
  209. }
  210. // Add the constant to the parent table.
  211. lua_pushstring(sc->_lua, value.c_str());
  212. lua_setfield(sc->_lua, -2, name.c_str());
  213. // Pop all the parent tables off the stack.
  214. int size = (int)scopePath.size();
  215. lua_pop(sc->_lua, size);
  216. }
  217. else
  218. {
  219. // TODO: Currently unsupported (we don't parse for this yet).
  220. // If the constant is global, add it to the global table.
  221. lua_pushstring(sc->_lua, value.c_str());
  222. lua_pushvalue(sc->_lua, -1);
  223. lua_setglobal(sc->_lua, name.c_str());
  224. }
  225. }
  226. void ScriptUtil::registerClass(const char* name, const luaL_Reg* members, lua_CFunction newFunction,
  227. lua_CFunction deleteFunction, const luaL_Reg* statics, const std::vector<std::string>& scopePath)
  228. {
  229. ScriptController* sc = Game::getInstance()->getScriptController();
  230. // If the type is an inner type, get the correct parent
  231. // table on the stack before creating the table for the class.
  232. if (!scopePath.empty())
  233. {
  234. std::string tablename = name;
  235. // Strip off the scope path part of the name.
  236. lua_getglobal(sc->_lua, scopePath[0].c_str());
  237. std::size_t index = tablename.find(scopePath[0]);
  238. if (index != std::string::npos)
  239. tablename = tablename.substr(index + scopePath[0].size());
  240. for (unsigned int i = 1; i < scopePath.size(); i++)
  241. {
  242. lua_pushstring(sc->_lua, scopePath[i].c_str());
  243. lua_gettable(sc->_lua, -2);
  244. index = tablename.find(scopePath[i]);
  245. if (index != std::string::npos)
  246. tablename = tablename.substr(index + scopePath[i].size());
  247. }
  248. lua_pushstring(sc->_lua, tablename.c_str());
  249. lua_newtable(sc->_lua);
  250. }
  251. else
  252. {
  253. // If the type is not an inner type, set it as a global table.
  254. lua_newtable(sc->_lua);
  255. lua_pushvalue(sc->_lua, -1);
  256. lua_setglobal(sc->_lua, name);
  257. }
  258. // Create the metatable and populate it with the member functions.
  259. lua_pushliteral(sc->_lua, "__metatable");
  260. luaL_newmetatable(sc->_lua, name);
  261. if (members)
  262. luaL_setfuncs(sc->_lua, members, 0);
  263. lua_pushstring(sc->_lua, "__index");
  264. lua_pushvalue(sc->_lua, -2);
  265. lua_settable(sc->_lua, -3);
  266. // Add the delete function if it was specified.
  267. if (deleteFunction)
  268. {
  269. lua_pushstring(sc->_lua, "__gc");
  270. lua_pushcfunction(sc->_lua, deleteFunction);
  271. lua_settable(sc->_lua, -3);
  272. }
  273. // Set the metatable on the main table.
  274. lua_settable(sc->_lua, -3);
  275. // Populate the main table with the static functions.
  276. if (statics)
  277. luaL_setfuncs(sc->_lua, statics, 0);
  278. // Set the new function(s) for the class.
  279. if (newFunction)
  280. {
  281. lua_pushliteral(sc->_lua, "new");
  282. lua_pushcfunction(sc->_lua, newFunction);
  283. lua_settable(sc->_lua, -3);
  284. }
  285. // Set the table we just created within the correct parent table.
  286. if (!scopePath.empty())
  287. {
  288. lua_settable(sc->_lua, -3);
  289. // Pop all the parent tables off the stack.
  290. int size = (int)scopePath.size();
  291. lua_pop(sc->_lua, size);
  292. }
  293. else
  294. {
  295. // Pop the main table off the stack.
  296. lua_pop(sc->_lua, 1);
  297. }
  298. }
  299. void ScriptUtil::registerFunction(const char* luaFunction, lua_CFunction cppFunction)
  300. {
  301. lua_pushcfunction(Game::getInstance()->getScriptController()->_lua, cppFunction);
  302. lua_setglobal(Game::getInstance()->getScriptController()->_lua, luaFunction);
  303. }
  304. void ScriptUtil::setGlobalHierarchyPair(const std::string& base, const std::string& derived)
  305. {
  306. Game::getInstance()->getScriptController()->_hierarchy[base].push_back(derived);
  307. }
  308. void ScriptUtil::addStringFromEnumConversionFunction(luaStringEnumConversionFunction stringFromEnum)
  309. {
  310. Game::getInstance()->getScriptController()->_stringFromEnum.push_back(stringFromEnum);
  311. }
  312. ScriptUtil::LuaArray<bool> ScriptUtil::getBoolPointer(int index)
  313. {
  314. GENERATE_LUA_GET_POINTER(bool, luaCheckBool);
  315. }
  316. ScriptUtil::LuaArray<short> ScriptUtil::getShortPointer(int index)
  317. {
  318. GENERATE_LUA_GET_POINTER(short, (short)luaL_checkint);
  319. }
  320. ScriptUtil::LuaArray<int> ScriptUtil::getIntPointer(int index)
  321. {
  322. GENERATE_LUA_GET_POINTER(int, (int)luaL_checkint);
  323. }
  324. ScriptUtil::LuaArray<long> ScriptUtil::getLongPointer(int index)
  325. {
  326. GENERATE_LUA_GET_POINTER(long, (long)luaL_checkint);
  327. }
  328. ScriptUtil::LuaArray<unsigned char> ScriptUtil::getUnsignedCharPointer(int index)
  329. {
  330. GENERATE_LUA_GET_POINTER(unsigned char, (unsigned char)luaL_checkunsigned);
  331. }
  332. ScriptUtil::LuaArray<unsigned short> ScriptUtil::getUnsignedShortPointer(int index)
  333. {
  334. GENERATE_LUA_GET_POINTER(unsigned short, (unsigned short)luaL_checkunsigned);
  335. }
  336. ScriptUtil::LuaArray<unsigned int> ScriptUtil::getUnsignedIntPointer(int index)
  337. {
  338. GENERATE_LUA_GET_POINTER(unsigned int, (unsigned int)luaL_checkunsigned);
  339. }
  340. ScriptUtil::LuaArray<unsigned long> ScriptUtil::getUnsignedLongPointer(int index)
  341. {
  342. GENERATE_LUA_GET_POINTER(unsigned long, (unsigned long)luaL_checkunsigned);
  343. }
  344. ScriptUtil::LuaArray<float> ScriptUtil::getFloatPointer(int index)
  345. {
  346. GENERATE_LUA_GET_POINTER(float, (float)luaL_checknumber);
  347. }
  348. ScriptUtil::LuaArray<double> ScriptUtil::getDoublePointer(int index)
  349. {
  350. GENERATE_LUA_GET_POINTER(double, (double)luaL_checknumber);
  351. }
  352. const char* ScriptUtil::getString(int index, bool isStdString)
  353. {
  354. if (lua_type(Game::getInstance()->getScriptController()->_lua, index) == LUA_TSTRING)
  355. return luaL_checkstring(Game::getInstance()->getScriptController()->_lua, index);
  356. else if (lua_type(Game::getInstance()->getScriptController()->_lua, index) == LUA_TNIL && !isStdString)
  357. return NULL;
  358. else
  359. {
  360. GP_ERROR("Invalid string parameter (index = %d).", index);
  361. return NULL;
  362. }
  363. }
  364. bool ScriptUtil::luaCheckBool(lua_State* state, int n)
  365. {
  366. if (!lua_isboolean(state, n))
  367. {
  368. const char* msg = lua_pushfstring(state, "%s expected, got %s", lua_typename(state, LUA_TBOOLEAN), luaL_typename(state, n));
  369. luaL_argerror(state, n, msg);
  370. return false;
  371. }
  372. return (lua_toboolean(state, n) != 0);
  373. }
  374. void ScriptController::loadScript(const char* path, bool forceReload)
  375. {
  376. std::set<std::string>::iterator iter = _loadedScripts.find(path);
  377. if (iter == _loadedScripts.end() || forceReload)
  378. {
  379. const char* scriptContents = FileSystem::readAll(path);
  380. if (luaL_dostring(_lua, scriptContents))
  381. GP_WARN("Failed to run Lua script with error: '%s'.", lua_tostring(_lua, -1));
  382. SAFE_DELETE_ARRAY(scriptContents);
  383. if (iter == _loadedScripts.end())
  384. _loadedScripts.insert(path);
  385. }
  386. }
  387. std::string ScriptController::loadUrl(const char* url)
  388. {
  389. std::string file;
  390. std::string id;
  391. splitURL(url, &file, &id);
  392. // Make sure the function isn't empty.
  393. if (id.size() <= 0)
  394. {
  395. GP_ERROR("Got an empty function name when parsing function url '%s'.", url);
  396. return std::string();
  397. }
  398. // Ensure the script is loaded.
  399. if (file.size() > 0)
  400. Game::getInstance()->getScriptController()->loadScript(file.c_str());
  401. // Return the function name.
  402. return id;
  403. }
  404. bool ScriptController::getBool(const char* name, bool defaultValue)
  405. {
  406. PUSH_NESTED_VARIABLE(name, defaultValue);
  407. bool b = lua_isboolean(_lua, -1) ? ScriptUtil::luaCheckBool(_lua, -1) : defaultValue;
  408. POP_NESTED_VARIABLE();
  409. return b;
  410. }
  411. char ScriptController::getChar(const char* name, char defaultValue)
  412. {
  413. PUSH_NESTED_VARIABLE(name, defaultValue);
  414. char c = lua_isnumber(_lua, -1) ? (char)luaL_checkint(_lua, -1) : defaultValue;
  415. POP_NESTED_VARIABLE();
  416. return c;
  417. }
  418. short ScriptController::getShort(const char* name, short defaultValue)
  419. {
  420. PUSH_NESTED_VARIABLE(name, defaultValue);
  421. short n = lua_isnumber(_lua, -1) ? (short)luaL_checkint(_lua, -1) : defaultValue;
  422. POP_NESTED_VARIABLE();
  423. return n;
  424. }
  425. int ScriptController::getInt(const char* name, int defaultValue)
  426. {
  427. PUSH_NESTED_VARIABLE(name, defaultValue);
  428. int n = lua_isnumber(_lua, -1) ? luaL_checkint(_lua, -1) : defaultValue;
  429. POP_NESTED_VARIABLE();
  430. return n;
  431. }
  432. long ScriptController::getLong(const char* name, long defaultValue)
  433. {
  434. PUSH_NESTED_VARIABLE(name, defaultValue);
  435. long n = lua_isnumber(_lua, -1) ? luaL_checklong(_lua, -1) : defaultValue;
  436. POP_NESTED_VARIABLE();
  437. return n;
  438. }
  439. unsigned char ScriptController::getUnsignedChar(const char* name, unsigned char defaultValue)
  440. {
  441. PUSH_NESTED_VARIABLE(name, defaultValue);
  442. unsigned char c = lua_isnumber(_lua, -1) ? (unsigned char)luaL_checkunsigned(_lua, -1) : defaultValue;
  443. POP_NESTED_VARIABLE();
  444. return c;
  445. }
  446. unsigned short ScriptController::getUnsignedShort(const char* name, unsigned short defaultValue)
  447. {
  448. PUSH_NESTED_VARIABLE(name, defaultValue);
  449. unsigned short n = lua_isnumber(_lua, -1) ? (unsigned short)luaL_checkunsigned(_lua, -1) : defaultValue;
  450. POP_NESTED_VARIABLE();
  451. return n;
  452. }
  453. unsigned int ScriptController::getUnsignedInt(const char* name, unsigned int defaultValue)
  454. {
  455. PUSH_NESTED_VARIABLE(name, defaultValue);
  456. unsigned int n = lua_isnumber(_lua, -1) ? (unsigned int)luaL_checkunsigned(_lua, -1) : defaultValue;
  457. POP_NESTED_VARIABLE();
  458. return n;
  459. }
  460. unsigned long ScriptController::getUnsignedLong(const char* name, unsigned long defaultValue)
  461. {
  462. PUSH_NESTED_VARIABLE(name, defaultValue);
  463. unsigned long n = lua_isnumber(_lua, -1) ? (unsigned long)luaL_checkunsigned(_lua, -1) : defaultValue;
  464. POP_NESTED_VARIABLE();
  465. return n;
  466. }
  467. float ScriptController::getFloat(const char* name, float defaultValue)
  468. {
  469. PUSH_NESTED_VARIABLE(name, defaultValue);
  470. float f = lua_isnumber(_lua, -1) ? (float)luaL_checknumber(_lua, -1) : defaultValue;
  471. POP_NESTED_VARIABLE();
  472. return f;
  473. }
  474. double ScriptController::getDouble(const char* name, double defaultValue)
  475. {
  476. PUSH_NESTED_VARIABLE(name, defaultValue);
  477. double n = lua_isnumber(_lua, -1) ? (double)luaL_checknumber(_lua, -1) : defaultValue;
  478. POP_NESTED_VARIABLE();
  479. return n;
  480. }
  481. const char* ScriptController::getString(const char* name)
  482. {
  483. PUSH_NESTED_VARIABLE(name, NULL);
  484. const char* s = lua_isstring(_lua, -1) ? luaL_checkstring(_lua, -1) : NULL;
  485. POP_NESTED_VARIABLE();
  486. return s;
  487. }
  488. void ScriptController::setBool(const char* name, bool v)
  489. {
  490. lua_pushboolean(_lua, v);
  491. lua_setglobal(_lua, name);
  492. }
  493. void ScriptController::setChar(const char* name, char v)
  494. {
  495. lua_pushinteger(_lua, v);
  496. lua_setglobal(_lua, name);
  497. }
  498. void ScriptController::setShort(const char* name, short v)
  499. {
  500. lua_pushinteger(_lua, v);
  501. lua_setglobal(_lua, name);
  502. }
  503. void ScriptController::setInt(const char* name, int v)
  504. {
  505. lua_pushinteger(_lua, v);
  506. lua_setglobal(_lua, name);
  507. }
  508. void ScriptController::setLong(const char* name, long v)
  509. {
  510. lua_pushinteger(_lua, v);
  511. lua_setglobal(_lua, name);
  512. }
  513. void ScriptController::setUnsignedChar(const char* name, unsigned char v)
  514. {
  515. lua_pushunsigned(_lua, v);
  516. lua_setglobal(_lua, name);
  517. }
  518. void ScriptController::setUnsignedShort(const char* name, unsigned short v)
  519. {
  520. lua_pushunsigned(_lua, v);
  521. lua_setglobal(_lua, name);
  522. }
  523. void ScriptController::setUnsignedInt(const char* name, unsigned int v)
  524. {
  525. lua_pushunsigned(_lua, v);
  526. lua_setglobal(_lua, name);
  527. }
  528. void ScriptController::setUnsignedLong(const char* name, unsigned long v)
  529. {
  530. lua_pushunsigned(_lua, v);
  531. lua_setglobal(_lua, name);
  532. }
  533. void ScriptController::setFloat(const char* name, float v)
  534. {
  535. lua_pushnumber(_lua, v);
  536. lua_setglobal(_lua, name);
  537. }
  538. void ScriptController::setDouble(const char* name, double v)
  539. {
  540. lua_pushnumber(_lua, v);
  541. lua_setglobal(_lua, name);
  542. }
  543. void ScriptController::setString(const char* name, const char* v)
  544. {
  545. lua_pushstring(_lua, v);
  546. lua_setglobal(_lua, name);
  547. }
  548. void ScriptController::print(const char* str)
  549. {
  550. gameplay::print("%s", str);
  551. }
  552. void ScriptController::print(const char* str1, const char* str2)
  553. {
  554. gameplay::print("%s%s", str1, str2);
  555. }
  556. ScriptController::ScriptController() : _lua(NULL)
  557. {
  558. memset(_callbacks, 0, sizeof(std::string*) * CALLBACK_COUNT);
  559. }
  560. ScriptController::~ScriptController()
  561. {
  562. for (unsigned int i = 0; i < CALLBACK_COUNT; i++)
  563. {
  564. SAFE_DELETE(_callbacks[i]);
  565. }
  566. }
  567. static const char* lua_print_function =
  568. "function print(...)\n"
  569. " ScriptController.print(table.concat({...},\"\\t\"), \"\\n\")\n"
  570. "end\n";
  571. #ifndef WIN32
  572. static const char* lua_loadfile_function =
  573. "do\n"
  574. " local oldLoadfile = loadfile\n"
  575. " loadfile = function(filename)\n"
  576. " if filename ~= nil and not FileSystem.isAbsolutePath(filename) then\n"
  577. " FileSystem.createFileFromAsset(filename)\n"
  578. " filename = FileSystem.getResourcePath() .. filename\n"
  579. " end\n"
  580. " return oldLoadfile(filename)\n"
  581. " end\n"
  582. "end\n";
  583. static const char* lua_dofile_function =
  584. "do\n"
  585. " local oldDofile = dofile\n"
  586. " dofile = function(filename)\n"
  587. " if filename ~= nil and not FileSystem.isAbsolutePath(filename) then\n"
  588. " FileSystem.createFileFromAsset(filename)\n"
  589. " filename = FileSystem.getResourcePath() .. filename\n"
  590. " end\n"
  591. " return oldDofile(filename)\n"
  592. " end\n"
  593. "end\n";
  594. #endif
  595. void ScriptController::initialize()
  596. {
  597. _lua = luaL_newstate();
  598. if (!_lua)
  599. GP_ERROR("Failed to initialize Lua scripting engine.");
  600. luaL_openlibs(_lua);
  601. #ifndef NO_LUA_BINDINGS
  602. lua_RegisterAllBindings();
  603. ScriptUtil::registerFunction("convert", ScriptController::convert);
  604. #endif
  605. // Create our own print() function that uses gameplay::print.
  606. if (luaL_dostring(_lua, lua_print_function))
  607. GP_ERROR("Failed to load custom print() function with error: '%s'.", lua_tostring(_lua, -1));
  608. #ifndef WIN32
  609. // Change the functions that read a file to use FileSystem.getResourcePath as their base path.
  610. if (luaL_dostring(_lua, lua_loadfile_function))
  611. GP_ERROR("Failed to load custom loadfile() function with error: '%s'.", lua_tostring(_lua, -1));
  612. if (luaL_dostring(_lua, lua_dofile_function))
  613. GP_ERROR("Failed to load custom dofile() function with error: '%s'.", lua_tostring(_lua, -1));
  614. #endif
  615. }
  616. void ScriptController::initializeGame()
  617. {
  618. if (_callbacks[INITIALIZE])
  619. {
  620. executeFunction<void>(_callbacks[INITIALIZE]->c_str());
  621. }
  622. }
  623. void ScriptController::finalize()
  624. {
  625. if (_lua)
  626. {
  627. lua_close(_lua);
  628. _lua = NULL;
  629. }
  630. }
  631. void ScriptController::finalizeGame()
  632. {
  633. std::string finalizeCallback;
  634. if (_callbacks[FINALIZE])
  635. finalizeCallback = _callbacks[FINALIZE]->c_str();
  636. // Remove any registered callbacks so they don't get called after shutdown
  637. for (unsigned int i = 0; i < CALLBACK_COUNT; i++)
  638. {
  639. SAFE_DELETE(_callbacks[i]);
  640. }
  641. // Fire script finalize callback
  642. if (!finalizeCallback.empty())
  643. {
  644. executeFunction<void>(finalizeCallback.c_str());
  645. }
  646. // Perform a full garbage collection cycle.
  647. // Note that this does NOT free any global variables declared in scripts, since
  648. // they are stored in the global state and are still referenced. Only after
  649. // closing the state (lua_close) will those variables be released.
  650. lua_gc(_lua, LUA_GCCOLLECT, 0);
  651. }
  652. void ScriptController::update(float elapsedTime)
  653. {
  654. if (_callbacks[UPDATE])
  655. {
  656. executeFunction<void>(_callbacks[UPDATE]->c_str(), "f", elapsedTime);
  657. }
  658. }
  659. void ScriptController::render(float elapsedTime)
  660. {
  661. if (_callbacks[RENDER])
  662. {
  663. executeFunction<void>(_callbacks[RENDER]->c_str(), "f", elapsedTime);
  664. }
  665. }
  666. void ScriptController::resizeEvent(unsigned int width, unsigned int height)
  667. {
  668. if (_callbacks[RESIZE_EVENT])
  669. {
  670. executeFunction<void>(_callbacks[RESIZE_EVENT]->c_str(), "uiui", width, height);
  671. }
  672. }
  673. void ScriptController::keyEvent(Keyboard::KeyEvent evt, int key)
  674. {
  675. if (_callbacks[KEY_EVENT])
  676. {
  677. executeFunction<void>(_callbacks[KEY_EVENT]->c_str(), "[Keyboard::KeyEvent][Keyboard::Key]", evt, key);
  678. }
  679. }
  680. void ScriptController::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  681. {
  682. if (_callbacks[TOUCH_EVENT])
  683. {
  684. executeFunction<void>(_callbacks[TOUCH_EVENT]->c_str(), "[Touch::TouchEvent]iiui", evt, x, y, contactIndex);
  685. }
  686. }
  687. bool ScriptController::mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  688. {
  689. if (_callbacks[MOUSE_EVENT])
  690. {
  691. return executeFunction<bool>(_callbacks[MOUSE_EVENT]->c_str(), "[Mouse::MouseEvent]iiii", evt, x, y, wheelDelta);
  692. }
  693. return false;
  694. }
  695. void ScriptController::gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad)
  696. {
  697. if (_callbacks[GAMEPAD_EVENT])
  698. {
  699. executeFunction<void>(_callbacks[GAMEPAD_EVENT]->c_str(), "[Gamepad::GamepadEvent]<Gamepad>", evt, gamepad);
  700. }
  701. }
  702. void ScriptController::executeFunctionHelper(int resultCount, const char* func, const char* args, va_list* list)
  703. {
  704. if (!_lua)
  705. return; // handles calling this method after script is finalized
  706. if (func == NULL)
  707. {
  708. GP_ERROR("Lua function name must be non-null.");
  709. return;
  710. }
  711. if (!getNestedVariable(_lua, func))
  712. {
  713. GP_WARN("Failed to call function '%s'", func);
  714. return;
  715. }
  716. const char* sig = args;
  717. int argumentCount = 0;
  718. // Push the arguments to the Lua stack if there are any.
  719. if (sig)
  720. {
  721. while (true)
  722. {
  723. if (!(*sig))
  724. break;
  725. switch(*sig++)
  726. {
  727. // Signed integers.
  728. case 'c':
  729. case 'h':
  730. case 'i':
  731. case 'l':
  732. lua_pushinteger(_lua, va_arg(*list, int));
  733. break;
  734. // Unsigned integers.
  735. case 'u':
  736. // Skip past the actual type (long, int, short, char).
  737. sig++;
  738. lua_pushunsigned(_lua, va_arg(*list, int));
  739. break;
  740. // Booleans.
  741. case 'b':
  742. lua_pushboolean(_lua, va_arg(*list, int));
  743. break;
  744. // Floating point numbers.
  745. case 'f':
  746. case 'd':
  747. lua_pushnumber(_lua, va_arg(*list, double));
  748. break;
  749. // Strings.
  750. case 's':
  751. lua_pushstring(_lua, va_arg(*list, char*));
  752. break;
  753. // Pointers.
  754. case 'p':
  755. lua_pushlightuserdata(_lua, va_arg(*list, void*));
  756. break;
  757. // Enums.
  758. case '[':
  759. {
  760. std::string type = sig;
  761. type = type.substr(0, type.find("]"));
  762. // Skip past the closing ']' (the semi-colon here is intentional-do not remove).
  763. while (*sig++ != ']');
  764. unsigned int value = va_arg(*list, int);
  765. std::string enumStr = "";
  766. for (unsigned int i = 0; enumStr.size() == 0 && i < _stringFromEnum.size(); i++)
  767. {
  768. enumStr = (*_stringFromEnum[i])(type, value);
  769. }
  770. lua_pushstring(_lua, enumStr.c_str());
  771. break;
  772. }
  773. // Object references/pointers (Lua userdata).
  774. case '<':
  775. {
  776. std::string type = sig;
  777. type = type.substr(0, type.find(">"));
  778. // Skip past the closing '>' (the semi-colon here is intentional-do not remove).
  779. while (*sig++ != '>');
  780. // Calculate the unique Lua type name.
  781. size_t i = type.find("::");
  782. while (i != std::string::npos)
  783. {
  784. // We use "" as the replacement here-this must match the preprocessor
  785. // define SCOPE_REPLACEMENT from the gameplay-luagen project.
  786. type.replace(i, 2, "");
  787. i = type.find("::");
  788. }
  789. void* ptr = va_arg(*list, void*);
  790. if (ptr == NULL)
  791. {
  792. lua_pushnil(_lua);
  793. }
  794. else
  795. {
  796. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(_lua, sizeof(ScriptUtil::LuaObject));
  797. object->instance = ptr;
  798. object->owns = false;
  799. luaL_getmetatable(_lua, type.c_str());
  800. lua_setmetatable(_lua, -2);
  801. }
  802. break;
  803. }
  804. default:
  805. GP_ERROR("Invalid argument type '%d'.", *(sig - 1));
  806. break;
  807. }
  808. argumentCount++;
  809. luaL_checkstack(_lua, 1, "Too many arguments.");
  810. }
  811. }
  812. // Perform the function call.
  813. if (lua_pcall(_lua, argumentCount, resultCount, 0) != 0)
  814. GP_WARN("Failed to call function '%s' with error '%s'.", func, lua_tostring(_lua, -1));
  815. }
  816. void ScriptController::registerCallback(ScriptCallback callback, const std::string& function)
  817. {
  818. SAFE_DELETE(_callbacks[callback]);
  819. _callbacks[callback] = new std::string(function);
  820. }
  821. ScriptController::ScriptCallback ScriptController::toCallback(const char* name)
  822. {
  823. if (strcmp(name, "initialize") == 0)
  824. return ScriptController::INITIALIZE;
  825. else if (strcmp(name, "update") == 0)
  826. return ScriptController::UPDATE;
  827. else if (strcmp(name, "render") == 0)
  828. return ScriptController::RENDER;
  829. else if (strcmp(name, "finalize") == 0)
  830. return ScriptController::FINALIZE;
  831. else if (strcmp(name, "resizeEvent") == 0)
  832. return ScriptController::RESIZE_EVENT;
  833. else if (strcmp(name, "keyEvent") == 0)
  834. return ScriptController::KEY_EVENT;
  835. else if (strcmp(name, "touchEvent") == 0)
  836. return ScriptController::TOUCH_EVENT;
  837. else if (strcmp(name, "mouseEvent") == 0)
  838. return ScriptController::MOUSE_EVENT;
  839. else if (strcmp(name, "gamepadEvent") == 0)
  840. return ScriptController::GAMEPAD_EVENT;
  841. else
  842. return ScriptController::INVALID_CALLBACK;
  843. }
  844. int ScriptController::convert(lua_State* state)
  845. {
  846. // Get the number of parameters.
  847. int paramCount = lua_gettop(state);
  848. // Attempt to match the parameters to a valid binding.
  849. switch (paramCount)
  850. {
  851. case 2:
  852. {
  853. if (lua_type(state, 1) == LUA_TUSERDATA && lua_type(state, 2) == LUA_TSTRING )
  854. {
  855. // Get parameter 2
  856. const char* param2 = ScriptUtil::getString(2, false);
  857. if (param2 != NULL)
  858. {
  859. luaL_getmetatable(state, param2);
  860. lua_setmetatable(state, -3);
  861. }
  862. return 0;
  863. }
  864. lua_pushstring(state, "lua_convert - Failed to match the given parameters to a valid function signature.");
  865. lua_error(state);
  866. break;
  867. }
  868. default:
  869. {
  870. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  871. lua_error(state);
  872. break;
  873. }
  874. }
  875. return 0;
  876. }
  877. // Helper macros.
  878. #define SCRIPT_EXECUTE_FUNCTION_NO_PARAM(type, checkfunc) \
  879. int top = lua_gettop(_lua); \
  880. executeFunctionHelper(1, func, NULL, NULL); \
  881. type value = (type)checkfunc(_lua, -1); \
  882. lua_pop(_lua, -1); \
  883. lua_settop(_lua, top); \
  884. return value;
  885. #define SCRIPT_EXECUTE_FUNCTION_PARAM(type, checkfunc) \
  886. int top = lua_gettop(_lua); \
  887. va_list list; \
  888. va_start(list, args); \
  889. executeFunctionHelper(1, func, args, &list); \
  890. type value = (type)checkfunc(_lua, -1); \
  891. lua_pop(_lua, -1); \
  892. va_end(list); \
  893. lua_settop(_lua, top); \
  894. return value;
  895. #define SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(type, checkfunc) \
  896. int top = lua_gettop(_lua); \
  897. executeFunctionHelper(1, func, args, list); \
  898. type value = (type)checkfunc(_lua, -1); \
  899. lua_pop(_lua, -1); \
  900. lua_settop(_lua, top); \
  901. return value;
  902. template<> void ScriptController::executeFunction<void>(const char* func)
  903. {
  904. int top = lua_gettop(_lua);
  905. executeFunctionHelper(0, func, NULL, NULL);
  906. lua_settop(_lua, top);
  907. }
  908. template<> bool ScriptController::executeFunction<bool>(const char* func)
  909. {
  910. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(bool, ScriptUtil::luaCheckBool);
  911. }
  912. template<> char ScriptController::executeFunction<char>(const char* func)
  913. {
  914. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(char, luaL_checkint);
  915. }
  916. template<> short ScriptController::executeFunction<short>(const char* func)
  917. {
  918. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(short, luaL_checkint);
  919. }
  920. template<> int ScriptController::executeFunction<int>(const char* func)
  921. {
  922. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(int, luaL_checkint);
  923. }
  924. template<> long ScriptController::executeFunction<long>(const char* func)
  925. {
  926. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(long, luaL_checklong);
  927. }
  928. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func)
  929. {
  930. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(unsigned char, luaL_checkunsigned);
  931. }
  932. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func)
  933. {
  934. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(unsigned short, luaL_checkunsigned);
  935. }
  936. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func)
  937. {
  938. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(unsigned int, luaL_checkunsigned);
  939. }
  940. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func)
  941. {
  942. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(unsigned long, luaL_checkunsigned);
  943. }
  944. template<> float ScriptController::executeFunction<float>(const char* func)
  945. {
  946. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(float, luaL_checknumber);
  947. }
  948. template<> double ScriptController::executeFunction<double>(const char* func)
  949. {
  950. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(double, luaL_checknumber);
  951. }
  952. template<> std::string ScriptController::executeFunction<std::string>(const char* func)
  953. {
  954. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(std::string, luaL_checkstring);
  955. }
  956. /** Template specialization. */
  957. template<> void ScriptController::executeFunction<void>(const char* func, const char* args, ...)
  958. {
  959. int top = lua_gettop(_lua);
  960. va_list list;
  961. va_start(list, args);
  962. executeFunctionHelper(0, func, args, &list);
  963. va_end(list);
  964. lua_settop(_lua, top);
  965. }
  966. /** Template specialization. */
  967. template<> bool ScriptController::executeFunction<bool>(const char* func, const char* args, ...)
  968. {
  969. SCRIPT_EXECUTE_FUNCTION_PARAM(bool, ScriptUtil::luaCheckBool);
  970. }
  971. /** Template specialization. */
  972. template<> char ScriptController::executeFunction<char>(const char* func, const char* args, ...)
  973. {
  974. SCRIPT_EXECUTE_FUNCTION_PARAM(char, luaL_checkint);
  975. }
  976. /** Template specialization. */
  977. template<> short ScriptController::executeFunction<short>(const char* func, const char* args, ...)
  978. {
  979. SCRIPT_EXECUTE_FUNCTION_PARAM(short, luaL_checkint);
  980. }
  981. /** Template specialization. */
  982. template<> int ScriptController::executeFunction<int>(const char* func, const char* args, ...)
  983. {
  984. SCRIPT_EXECUTE_FUNCTION_PARAM(int, luaL_checkint);
  985. }
  986. /** Template specialization. */
  987. template<> long ScriptController::executeFunction<long>(const char* func, const char* args, ...)
  988. {
  989. SCRIPT_EXECUTE_FUNCTION_PARAM(long, luaL_checklong);
  990. }
  991. /** Template specialization. */
  992. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func, const char* args, ...)
  993. {
  994. SCRIPT_EXECUTE_FUNCTION_PARAM(unsigned char, luaL_checkunsigned);
  995. }
  996. /** Template specialization. */
  997. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func, const char* args, ...)
  998. {
  999. SCRIPT_EXECUTE_FUNCTION_PARAM(unsigned short, luaL_checkunsigned);
  1000. }
  1001. /** Template specialization. */
  1002. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func, const char* args, ...)
  1003. {
  1004. SCRIPT_EXECUTE_FUNCTION_PARAM(unsigned int, luaL_checkunsigned);
  1005. }
  1006. /** Template specialization. */
  1007. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func, const char* args, ...)
  1008. {
  1009. SCRIPT_EXECUTE_FUNCTION_PARAM(unsigned long, luaL_checkunsigned);
  1010. }
  1011. /** Template specialization. */
  1012. template<> float ScriptController::executeFunction<float>(const char* func, const char* args, ...)
  1013. {
  1014. SCRIPT_EXECUTE_FUNCTION_PARAM(float, luaL_checknumber);
  1015. }
  1016. /** Template specialization. */
  1017. template<> double ScriptController::executeFunction<double>(const char* func, const char* args, ...)
  1018. {
  1019. SCRIPT_EXECUTE_FUNCTION_PARAM(double, luaL_checknumber);
  1020. }
  1021. /** Template specialization. */
  1022. template<> std::string ScriptController::executeFunction<std::string>(const char* func, const char* args, ...)
  1023. {
  1024. SCRIPT_EXECUTE_FUNCTION_PARAM(std::string, luaL_checkstring);
  1025. }
  1026. /** Template specialization. */
  1027. template<> void ScriptController::executeFunction<void>(const char* func, const char* args, va_list* list)
  1028. {
  1029. executeFunctionHelper(0, func, args, list);
  1030. }
  1031. /** Template specialization. */
  1032. template<> bool ScriptController::executeFunction<bool>(const char* func, const char* args, va_list* list)
  1033. {
  1034. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(bool, ScriptUtil::luaCheckBool);
  1035. }
  1036. /** Template specialization. */
  1037. template<> char ScriptController::executeFunction<char>(const char* func, const char* args, va_list* list)
  1038. {
  1039. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(char, luaL_checkint);
  1040. }
  1041. /** Template specialization. */
  1042. template<> short ScriptController::executeFunction<short>(const char* func, const char* args, va_list* list)
  1043. {
  1044. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(short, luaL_checkint);
  1045. }
  1046. /** Template specialization. */
  1047. template<> int ScriptController::executeFunction<int>(const char* func, const char* args, va_list* list)
  1048. {
  1049. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(int, luaL_checkint);
  1050. }
  1051. /** Template specialization. */
  1052. template<> long ScriptController::executeFunction<long>(const char* func, const char* args, va_list* list)
  1053. {
  1054. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(long, luaL_checklong);
  1055. }
  1056. /** Template specialization. */
  1057. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func, const char* args, va_list* list)
  1058. {
  1059. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(unsigned char, luaL_checkunsigned);
  1060. }
  1061. /** Template specialization. */
  1062. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func, const char* args, va_list* list)
  1063. {
  1064. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(unsigned short, luaL_checkunsigned);
  1065. }
  1066. /** Template specialization. */
  1067. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func, const char* args, va_list* list)
  1068. {
  1069. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(unsigned int, luaL_checkunsigned);
  1070. }
  1071. /** Template specialization. */
  1072. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func, const char* args, va_list* list)
  1073. {
  1074. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(unsigned long, luaL_checkunsigned);
  1075. }
  1076. /** Template specialization. */
  1077. template<> float ScriptController::executeFunction<float>(const char* func, const char* args, va_list* list)
  1078. {
  1079. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(float, luaL_checknumber);
  1080. }
  1081. /** Template specialization. */
  1082. template<> double ScriptController::executeFunction<double>(const char* func, const char* args, va_list* list)
  1083. {
  1084. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(double, luaL_checknumber);
  1085. }
  1086. /** Template specialization. */
  1087. template<> std::string ScriptController::executeFunction<std::string>(const char* func, const char* args, va_list* list)
  1088. {
  1089. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(std::string, luaL_checkstring);
  1090. }
  1091. }