ScriptController.cpp 42 KB

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