ScriptController.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  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. #ifdef __ANDROID__
  382. const char* scriptContents = FileSystem::readAll(path);
  383. if (luaL_dostring(_lua, scriptContents))
  384. {
  385. GP_WARN("Failed to run Lua script with error: '%s'.", lua_tostring(_lua, -1));
  386. }
  387. SAFE_DELETE_ARRAY(scriptContents);
  388. #else
  389. std::string fullPath;
  390. if (!FileSystem::isAbsolutePath(path))
  391. {
  392. fullPath.append(FileSystem::getResourcePath());
  393. }
  394. fullPath.append(path);
  395. if (luaL_dofile(_lua, fullPath.c_str()))
  396. {
  397. GP_WARN("Failed to run Lua script with error: '%s'.", lua_tostring(_lua, -1));
  398. }
  399. #endif
  400. if (iter == _loadedScripts.end())
  401. {
  402. _loadedScripts.insert(path);
  403. }
  404. }
  405. }
  406. std::string ScriptController::loadUrl(const char* url)
  407. {
  408. std::string file;
  409. std::string id;
  410. splitURL(url, &file, &id);
  411. // Make sure the function isn't empty.
  412. if (id.size() <= 0)
  413. {
  414. GP_ERROR("Got an empty function name when parsing function url '%s'.", url);
  415. return std::string();
  416. }
  417. // Ensure the script is loaded.
  418. if (file.size() > 0)
  419. Game::getInstance()->getScriptController()->loadScript(file.c_str());
  420. // Return the function name.
  421. return id;
  422. }
  423. bool ScriptController::getBool(const char* name, bool defaultValue)
  424. {
  425. PUSH_NESTED_VARIABLE(name, defaultValue);
  426. bool b = lua_isboolean(_lua, -1) ? ScriptUtil::luaCheckBool(_lua, -1) : defaultValue;
  427. POP_NESTED_VARIABLE();
  428. return b;
  429. }
  430. char ScriptController::getChar(const char* name, char defaultValue)
  431. {
  432. PUSH_NESTED_VARIABLE(name, defaultValue);
  433. char c = lua_isnumber(_lua, -1) ? (char)luaL_checkint(_lua, -1) : defaultValue;
  434. POP_NESTED_VARIABLE();
  435. return c;
  436. }
  437. short ScriptController::getShort(const char* name, short defaultValue)
  438. {
  439. PUSH_NESTED_VARIABLE(name, defaultValue);
  440. short n = lua_isnumber(_lua, -1) ? (short)luaL_checkint(_lua, -1) : defaultValue;
  441. POP_NESTED_VARIABLE();
  442. return n;
  443. }
  444. int ScriptController::getInt(const char* name, int defaultValue)
  445. {
  446. PUSH_NESTED_VARIABLE(name, defaultValue);
  447. int n = lua_isnumber(_lua, -1) ? luaL_checkint(_lua, -1) : defaultValue;
  448. POP_NESTED_VARIABLE();
  449. return n;
  450. }
  451. long ScriptController::getLong(const char* name, long defaultValue)
  452. {
  453. PUSH_NESTED_VARIABLE(name, defaultValue);
  454. long n = lua_isnumber(_lua, -1) ? luaL_checklong(_lua, -1) : defaultValue;
  455. POP_NESTED_VARIABLE();
  456. return n;
  457. }
  458. unsigned char ScriptController::getUnsignedChar(const char* name, unsigned char defaultValue)
  459. {
  460. PUSH_NESTED_VARIABLE(name, defaultValue);
  461. unsigned char c = lua_isnumber(_lua, -1) ? (unsigned char)luaL_checkunsigned(_lua, -1) : defaultValue;
  462. POP_NESTED_VARIABLE();
  463. return c;
  464. }
  465. unsigned short ScriptController::getUnsignedShort(const char* name, unsigned short defaultValue)
  466. {
  467. PUSH_NESTED_VARIABLE(name, defaultValue);
  468. unsigned short n = lua_isnumber(_lua, -1) ? (unsigned short)luaL_checkunsigned(_lua, -1) : defaultValue;
  469. POP_NESTED_VARIABLE();
  470. return n;
  471. }
  472. unsigned int ScriptController::getUnsignedInt(const char* name, unsigned int defaultValue)
  473. {
  474. PUSH_NESTED_VARIABLE(name, defaultValue);
  475. unsigned int n = lua_isnumber(_lua, -1) ? (unsigned int)luaL_checkunsigned(_lua, -1) : defaultValue;
  476. POP_NESTED_VARIABLE();
  477. return n;
  478. }
  479. unsigned long ScriptController::getUnsignedLong(const char* name, unsigned long defaultValue)
  480. {
  481. PUSH_NESTED_VARIABLE(name, defaultValue);
  482. unsigned long n = lua_isnumber(_lua, -1) ? (unsigned long)luaL_checkunsigned(_lua, -1) : defaultValue;
  483. POP_NESTED_VARIABLE();
  484. return n;
  485. }
  486. float ScriptController::getFloat(const char* name, float defaultValue)
  487. {
  488. PUSH_NESTED_VARIABLE(name, defaultValue);
  489. float f = lua_isnumber(_lua, -1) ? (float)luaL_checknumber(_lua, -1) : defaultValue;
  490. POP_NESTED_VARIABLE();
  491. return f;
  492. }
  493. double ScriptController::getDouble(const char* name, double defaultValue)
  494. {
  495. PUSH_NESTED_VARIABLE(name, defaultValue);
  496. double n = lua_isnumber(_lua, -1) ? (double)luaL_checknumber(_lua, -1) : defaultValue;
  497. POP_NESTED_VARIABLE();
  498. return n;
  499. }
  500. const char* ScriptController::getString(const char* name)
  501. {
  502. PUSH_NESTED_VARIABLE(name, NULL);
  503. const char* s = lua_isstring(_lua, -1) ? luaL_checkstring(_lua, -1) : NULL;
  504. POP_NESTED_VARIABLE();
  505. return s;
  506. }
  507. void ScriptController::setBool(const char* name, bool v)
  508. {
  509. lua_pushboolean(_lua, v);
  510. lua_setglobal(_lua, name);
  511. }
  512. void ScriptController::setChar(const char* name, char v)
  513. {
  514. lua_pushinteger(_lua, v);
  515. lua_setglobal(_lua, name);
  516. }
  517. void ScriptController::setShort(const char* name, short v)
  518. {
  519. lua_pushinteger(_lua, v);
  520. lua_setglobal(_lua, name);
  521. }
  522. void ScriptController::setInt(const char* name, int v)
  523. {
  524. lua_pushinteger(_lua, v);
  525. lua_setglobal(_lua, name);
  526. }
  527. void ScriptController::setLong(const char* name, long v)
  528. {
  529. lua_pushinteger(_lua, v);
  530. lua_setglobal(_lua, name);
  531. }
  532. void ScriptController::setUnsignedChar(const char* name, unsigned char v)
  533. {
  534. lua_pushunsigned(_lua, v);
  535. lua_setglobal(_lua, name);
  536. }
  537. void ScriptController::setUnsignedShort(const char* name, unsigned short v)
  538. {
  539. lua_pushunsigned(_lua, v);
  540. lua_setglobal(_lua, name);
  541. }
  542. void ScriptController::setUnsignedInt(const char* name, unsigned int v)
  543. {
  544. lua_pushunsigned(_lua, v);
  545. lua_setglobal(_lua, name);
  546. }
  547. void ScriptController::setUnsignedLong(const char* name, unsigned long v)
  548. {
  549. lua_pushunsigned(_lua, v);
  550. lua_setglobal(_lua, name);
  551. }
  552. void ScriptController::setFloat(const char* name, float v)
  553. {
  554. lua_pushnumber(_lua, v);
  555. lua_setglobal(_lua, name);
  556. }
  557. void ScriptController::setDouble(const char* name, double v)
  558. {
  559. lua_pushnumber(_lua, v);
  560. lua_setglobal(_lua, name);
  561. }
  562. void ScriptController::setString(const char* name, const char* v)
  563. {
  564. lua_pushstring(_lua, v);
  565. lua_setglobal(_lua, name);
  566. }
  567. void ScriptController::print(const char* str)
  568. {
  569. gameplay::print("%s", str);
  570. }
  571. void ScriptController::print(const char* str1, const char* str2)
  572. {
  573. gameplay::print("%s%s", str1, str2);
  574. }
  575. ScriptController::ScriptController() : _lua(NULL)
  576. {
  577. }
  578. ScriptController::~ScriptController()
  579. {
  580. }
  581. static const char* lua_print_function =
  582. "function print(...)\n"
  583. " ScriptController.print(table.concat({...},\"\\t\"), \"\\n\")\n"
  584. "end\n";
  585. static const char* lua_loadfile_function =
  586. "do\n"
  587. " local oldLoadfile = loadfile\n"
  588. " loadfile = function(filename)\n"
  589. " if filename ~= nil and not FileSystem.isAbsolutePath(filename) then\n"
  590. " FileSystem.createFileFromAsset(filename)\n"
  591. " filename = FileSystem.getResourcePath() .. filename\n"
  592. " end\n"
  593. " return oldLoadfile(filename)\n"
  594. " end\n"
  595. "end\n";
  596. static const char* lua_dofile_function =
  597. "do\n"
  598. " local oldDofile = dofile\n"
  599. " dofile = 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 oldDofile(filename)\n"
  605. " end\n"
  606. "end\n";
  607. /**
  608. * @script{ignore}
  609. */
  610. void appendLuaPath(lua_State* state, const char* path)
  611. {
  612. lua_getglobal(state, "package");
  613. // Get the current path string from top of stack
  614. lua_getfield(state, -1, "path");
  615. std::string cur_path = lua_tostring(state, -1);
  616. lua_pop(state, 1);
  617. // Append our game resource path to the path
  618. cur_path += ';';
  619. cur_path += path;
  620. cur_path += "?.lua";
  621. // Push the new path
  622. lua_pushstring(state, cur_path.c_str());
  623. lua_setfield(state, -2, "path");
  624. lua_pop(state, 1);
  625. }
  626. void ScriptController::initialize()
  627. {
  628. _lua = luaL_newstate();
  629. if (!_lua)
  630. GP_ERROR("Failed to initialize Lua scripting engine.");
  631. luaL_openlibs(_lua);
  632. #ifndef NO_LUA_BINDINGS
  633. lua_RegisterAllBindings();
  634. ScriptUtil::registerFunction("convert", ScriptController::convert);
  635. #endif
  636. // Append to the LUA_PATH to allow scripts to be found in the resource folder on all platforms
  637. appendLuaPath(_lua, FileSystem::getResourcePath());
  638. // Create our own print() function that uses gameplay::print.
  639. if (luaL_dostring(_lua, lua_print_function))
  640. GP_ERROR("Failed to load custom print() function with error: '%s'.", lua_tostring(_lua, -1));
  641. // Change the functions that read a file to use FileSystem.getResourcePath as their base path.
  642. if (luaL_dostring(_lua, lua_loadfile_function))
  643. GP_ERROR("Failed to load custom loadfile() function with error: '%s'.", lua_tostring(_lua, -1));
  644. if (luaL_dostring(_lua, lua_dofile_function))
  645. GP_ERROR("Failed to load custom dofile() function with error: '%s'.", lua_tostring(_lua, -1));
  646. // Write game command-line arguments to a global lua "arg" table
  647. std::ostringstream args;
  648. int argc;
  649. char** argv;
  650. Game::getInstance()->getArguments(&argc, &argv);
  651. args << "arg = { }\n";
  652. for (int i = 0; i < argc; ++i)
  653. {
  654. args << "arg[" << (i) << "] = [[" << argv[i] << "]]\n";
  655. }
  656. std::string argsStr = args.str();
  657. if (argsStr.length() > 0)
  658. {
  659. if (luaL_dostring(_lua, argsStr.c_str()))
  660. GP_ERROR("Failed to pass command-line arguments with error: '%s'.", lua_tostring(_lua, -1));
  661. }
  662. }
  663. void ScriptController::initializeGame()
  664. {
  665. std::vector<std::string>& list = _callbacks[INITIALIZE];
  666. for (size_t i = 0; i < list.size(); ++i)
  667. executeFunction<void>(list[i].c_str());
  668. }
  669. void ScriptController::finalize()
  670. {
  671. if (_lua)
  672. {
  673. lua_close(_lua);
  674. _lua = NULL;
  675. }
  676. }
  677. void ScriptController::finalizeGame()
  678. {
  679. std::vector<std::string> finalizeCallbacks = _callbacks[FINALIZE]; // no & : makes a copy of the vector
  680. // Remove any registered callbacks so they don't get called after shutdown
  681. for (unsigned int i = 0; i < CALLBACK_COUNT; i++)
  682. _callbacks[i].clear();
  683. // Fire script finalize callbacks
  684. for (size_t i = 0; i < finalizeCallbacks.size(); ++i)
  685. executeFunction<void>(finalizeCallbacks[i].c_str());
  686. // Perform a full garbage collection cycle.
  687. // Note that this does NOT free any global variables declared in scripts, since
  688. // they are stored in the global state and are still referenced. Only after
  689. // closing the state (lua_close) will those variables be released.
  690. lua_gc(_lua, LUA_GCCOLLECT, 0);
  691. }
  692. void ScriptController::update(float elapsedTime)
  693. {
  694. std::vector<std::string>& list = _callbacks[UPDATE];
  695. for (size_t i = 0; i < list.size(); ++i)
  696. executeFunction<void>(list[i].c_str(), "f", elapsedTime);
  697. }
  698. void ScriptController::render(float elapsedTime)
  699. {
  700. std::vector<std::string>& list = _callbacks[RENDER];
  701. for (size_t i = 0; i < list.size(); ++i)
  702. executeFunction<void>(list[i].c_str(), "f", elapsedTime);
  703. }
  704. void ScriptController::resizeEvent(unsigned int width, unsigned int height)
  705. {
  706. std::vector<std::string>& list = _callbacks[RESIZE_EVENT];
  707. for (size_t i = 0; i < list.size(); ++i)
  708. executeFunction<void>(list[i].c_str(), "uiui", width, height);
  709. }
  710. void ScriptController::keyEvent(Keyboard::KeyEvent evt, int key)
  711. {
  712. std::vector<std::string>& list = _callbacks[KEY_EVENT];
  713. for (size_t i = 0; i < list.size(); ++i)
  714. executeFunction<void>(list[i].c_str(), "[Keyboard::KeyEvent][Keyboard::Key]", evt, key);
  715. }
  716. void ScriptController::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  717. {
  718. std::vector<std::string>& list = _callbacks[TOUCH_EVENT];
  719. for (size_t i = 0; i < list.size(); ++i)
  720. executeFunction<void>(list[i].c_str(), "[Touch::TouchEvent]iiui", evt, x, y, contactIndex);
  721. }
  722. bool ScriptController::mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  723. {
  724. std::vector<std::string>& list = _callbacks[MOUSE_EVENT];
  725. for (size_t i = 0; i < list.size(); ++i)
  726. {
  727. if (executeFunction<bool>(list[i].c_str(), "[Mouse::MouseEvent]iii", evt, x, y, wheelDelta))
  728. return true;
  729. }
  730. return false;
  731. }
  732. void ScriptController::gestureSwipeEvent(int x, int y, int direction)
  733. {
  734. std::vector<std::string>& list = _callbacks[GESTURE_SWIPE_EVENT];
  735. for (size_t i = 0; i < list.size(); ++i)
  736. executeFunction<void>(list[i].c_str(), "iii", x, y, direction);
  737. }
  738. void ScriptController::gesturePinchEvent(int x, int y, float scale)
  739. {
  740. std::vector<std::string>& list = _callbacks[GESTURE_PINCH_EVENT];
  741. for (size_t i = 0; i < list.size(); ++i)
  742. executeFunction<void>(list[i].c_str(), "iif", x, y, scale);
  743. }
  744. void ScriptController::gestureTapEvent(int x, int y)
  745. {
  746. std::vector<std::string>& list = _callbacks[GESTURE_TAP_EVENT];
  747. for (size_t i = 0; i < list.size(); ++i)
  748. executeFunction<void>(list[i].c_str(), "ii", x, y);
  749. }
  750. void ScriptController::gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex)
  751. {
  752. std::vector<std::string>& list = _callbacks[GAMEPAD_EVENT];
  753. for (size_t i = 0; i < list.size(); ++i)
  754. executeFunction<void>(list[i].c_str(), "[Gamepad::GamepadEvent]<Gamepad>", evt, gamepad);
  755. }
  756. void ScriptController::executeFunctionHelper(int resultCount, const char* func, const char* args, va_list* list)
  757. {
  758. if (!_lua)
  759. return; // handles calling this method after script is finalized
  760. if (func == NULL)
  761. {
  762. GP_ERROR("Lua function name must be non-null.");
  763. return;
  764. }
  765. if (!getNestedVariable(_lua, func))
  766. {
  767. GP_WARN("Failed to call function '%s'", func);
  768. return;
  769. }
  770. const char* sig = args;
  771. int argumentCount = 0;
  772. // Push the arguments to the Lua stack if there are any.
  773. if (sig)
  774. {
  775. while (true)
  776. {
  777. if (!(*sig))
  778. break;
  779. switch(*sig++)
  780. {
  781. // Signed integers.
  782. case 'c':
  783. case 'h':
  784. case 'i':
  785. case 'l':
  786. lua_pushinteger(_lua, va_arg(*list, int));
  787. break;
  788. // Unsigned integers.
  789. case 'u':
  790. // Skip past the actual type (long, int, short, char).
  791. sig++;
  792. lua_pushunsigned(_lua, va_arg(*list, int));
  793. break;
  794. // Booleans.
  795. case 'b':
  796. lua_pushboolean(_lua, va_arg(*list, int));
  797. break;
  798. // Floating point numbers.
  799. case 'f':
  800. case 'd':
  801. lua_pushnumber(_lua, va_arg(*list, double));
  802. break;
  803. // Strings.
  804. case 's':
  805. lua_pushstring(_lua, va_arg(*list, char*));
  806. break;
  807. // Pointers.
  808. case 'p':
  809. lua_pushlightuserdata(_lua, va_arg(*list, void*));
  810. break;
  811. // Enums.
  812. case '[':
  813. {
  814. std::string type = sig;
  815. type = type.substr(0, type.find("]"));
  816. // Skip past the closing ']' (the semi-colon here is intentional-do not remove).
  817. while (*sig++ != ']');
  818. unsigned int value = va_arg(*list, int);
  819. std::string enumStr = "";
  820. for (unsigned int i = 0; enumStr.size() == 0 && i < _stringFromEnum.size(); i++)
  821. {
  822. enumStr = (*_stringFromEnum[i])(type, value);
  823. }
  824. lua_pushstring(_lua, enumStr.c_str());
  825. break;
  826. }
  827. // Object references/pointers (Lua userdata).
  828. case '<':
  829. {
  830. std::string type = sig;
  831. type = type.substr(0, type.find(">"));
  832. // Skip past the closing '>' (the semi-colon here is intentional-do not remove).
  833. while (*sig++ != '>');
  834. // Calculate the unique Lua type name.
  835. size_t i = type.find("::");
  836. while (i != std::string::npos)
  837. {
  838. // We use "" as the replacement here-this must match the preprocessor
  839. // define SCOPE_REPLACEMENT from the gameplay-luagen project.
  840. type.replace(i, 2, "");
  841. i = type.find("::");
  842. }
  843. void* ptr = va_arg(*list, void*);
  844. if (ptr == NULL)
  845. {
  846. lua_pushnil(_lua);
  847. }
  848. else
  849. {
  850. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(_lua, sizeof(ScriptUtil::LuaObject));
  851. object->instance = ptr;
  852. object->owns = false;
  853. luaL_getmetatable(_lua, type.c_str());
  854. lua_setmetatable(_lua, -2);
  855. }
  856. break;
  857. }
  858. default:
  859. GP_ERROR("Invalid argument type '%d'.", *(sig - 1));
  860. break;
  861. }
  862. argumentCount++;
  863. luaL_checkstack(_lua, 1, "Too many arguments.");
  864. }
  865. }
  866. // Perform the function call.
  867. if (lua_pcall(_lua, argumentCount, resultCount, 0) != 0)
  868. GP_WARN("Failed to call function '%s' with error '%s'.", func, lua_tostring(_lua, -1));
  869. }
  870. void ScriptController::registerCallback(const char* callback, const char* function)
  871. {
  872. ScriptCallback scb = toCallback(callback);
  873. if (scb < INVALID_CALLBACK)
  874. {
  875. _callbacks[scb].push_back(function);
  876. }
  877. else
  878. {
  879. GP_WARN("Invalid script callback function specified: %s", callback);
  880. }
  881. }
  882. void ScriptController::unregisterCallback(const char* callback, const char* function)
  883. {
  884. ScriptCallback scb = toCallback(callback);
  885. if (scb < INVALID_CALLBACK)
  886. {
  887. std::vector<std::string>& list = _callbacks[scb];
  888. std::vector<std::string>::iterator itr = std::find(list.begin(), list.end(), std::string(function));
  889. if (itr != list.end())
  890. list.erase(itr);
  891. }
  892. else
  893. {
  894. GP_WARN("Invalid script callback function specified: %s", callback);
  895. }
  896. }
  897. ScriptController::ScriptCallback ScriptController::toCallback(const char* name)
  898. {
  899. if (strcmp(name, "initialize") == 0)
  900. return ScriptController::INITIALIZE;
  901. else if (strcmp(name, "update") == 0)
  902. return ScriptController::UPDATE;
  903. else if (strcmp(name, "render") == 0)
  904. return ScriptController::RENDER;
  905. else if (strcmp(name, "finalize") == 0)
  906. return ScriptController::FINALIZE;
  907. else if (strcmp(name, "resizeEvent") == 0)
  908. return ScriptController::RESIZE_EVENT;
  909. else if (strcmp(name, "keyEvent") == 0)
  910. return ScriptController::KEY_EVENT;
  911. else if (strcmp(name, "touchEvent") == 0)
  912. return ScriptController::TOUCH_EVENT;
  913. else if (strcmp(name, "mouseEvent") == 0)
  914. return ScriptController::MOUSE_EVENT;
  915. else if (strcmp(name, "gestureSwipeEvent") == 0)
  916. return ScriptController::GESTURE_SWIPE_EVENT;
  917. else if (strcmp(name, "gesturePinchEvent") == 0)
  918. return ScriptController::GESTURE_PINCH_EVENT;
  919. else if (strcmp(name, "gestureTapEvent") == 0)
  920. return ScriptController::GESTURE_TAP_EVENT;
  921. else if (strcmp(name, "gamepadEvent") == 0)
  922. return ScriptController::GAMEPAD_EVENT;
  923. else
  924. return ScriptController::INVALID_CALLBACK;
  925. }
  926. int ScriptController::convert(lua_State* state)
  927. {
  928. // Get the number of parameters.
  929. int paramCount = lua_gettop(state);
  930. // Attempt to match the parameters to a valid binding.
  931. switch (paramCount)
  932. {
  933. case 2:
  934. {
  935. if (lua_type(state, 1) == LUA_TUSERDATA && lua_type(state, 2) == LUA_TSTRING )
  936. {
  937. // Get parameter 2
  938. const char* param2 = ScriptUtil::getString(2, false);
  939. if (param2 != NULL)
  940. {
  941. luaL_getmetatable(state, param2);
  942. lua_setmetatable(state, -3);
  943. }
  944. return 0;
  945. }
  946. lua_pushstring(state, "lua_convert - Failed to match the given parameters to a valid function signature.");
  947. lua_error(state);
  948. break;
  949. }
  950. default:
  951. {
  952. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  953. lua_error(state);
  954. break;
  955. }
  956. }
  957. return 0;
  958. }
  959. // Helper macros.
  960. #define SCRIPT_EXECUTE_FUNCTION_NO_PARAM(type, checkfunc) \
  961. int top = lua_gettop(_lua); \
  962. executeFunctionHelper(1, func, NULL, NULL); \
  963. type value = (type)checkfunc(_lua, -1); \
  964. lua_pop(_lua, -1); \
  965. lua_settop(_lua, top); \
  966. return value;
  967. #define SCRIPT_EXECUTE_FUNCTION_PARAM(type, checkfunc) \
  968. int top = lua_gettop(_lua); \
  969. va_list list; \
  970. va_start(list, args); \
  971. executeFunctionHelper(1, func, args, &list); \
  972. type value = (type)checkfunc(_lua, -1); \
  973. lua_pop(_lua, -1); \
  974. va_end(list); \
  975. lua_settop(_lua, top); \
  976. return value;
  977. #define SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(type, checkfunc) \
  978. int top = lua_gettop(_lua); \
  979. executeFunctionHelper(1, func, args, list); \
  980. type value = (type)checkfunc(_lua, -1); \
  981. lua_pop(_lua, -1); \
  982. lua_settop(_lua, top); \
  983. return value;
  984. template<> void ScriptController::executeFunction<void>(const char* func)
  985. {
  986. int top = lua_gettop(_lua);
  987. executeFunctionHelper(0, func, NULL, NULL);
  988. lua_settop(_lua, top);
  989. }
  990. template<> bool ScriptController::executeFunction<bool>(const char* func)
  991. {
  992. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(bool, ScriptUtil::luaCheckBool);
  993. }
  994. template<> char ScriptController::executeFunction<char>(const char* func)
  995. {
  996. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(char, luaL_checkint);
  997. }
  998. template<> short ScriptController::executeFunction<short>(const char* func)
  999. {
  1000. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(short, luaL_checkint);
  1001. }
  1002. template<> int ScriptController::executeFunction<int>(const char* func)
  1003. {
  1004. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(int, luaL_checkint);
  1005. }
  1006. template<> long ScriptController::executeFunction<long>(const char* func)
  1007. {
  1008. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(long, luaL_checklong);
  1009. }
  1010. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func)
  1011. {
  1012. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(unsigned char, luaL_checkunsigned);
  1013. }
  1014. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func)
  1015. {
  1016. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(unsigned short, luaL_checkunsigned);
  1017. }
  1018. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func)
  1019. {
  1020. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(unsigned int, luaL_checkunsigned);
  1021. }
  1022. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func)
  1023. {
  1024. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(unsigned long, luaL_checkunsigned);
  1025. }
  1026. template<> float ScriptController::executeFunction<float>(const char* func)
  1027. {
  1028. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(float, luaL_checknumber);
  1029. }
  1030. template<> double ScriptController::executeFunction<double>(const char* func)
  1031. {
  1032. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(double, luaL_checknumber);
  1033. }
  1034. template<> std::string ScriptController::executeFunction<std::string>(const char* func)
  1035. {
  1036. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(std::string, luaL_checkstring);
  1037. }
  1038. /** Template specialization. */
  1039. template<> void ScriptController::executeFunction<void>(const char* func, const char* args, ...)
  1040. {
  1041. int top = lua_gettop(_lua);
  1042. va_list list;
  1043. va_start(list, args);
  1044. executeFunctionHelper(0, func, args, &list);
  1045. va_end(list);
  1046. lua_settop(_lua, top);
  1047. }
  1048. /** Template specialization. */
  1049. template<> bool ScriptController::executeFunction<bool>(const char* func, const char* args, ...)
  1050. {
  1051. SCRIPT_EXECUTE_FUNCTION_PARAM(bool, ScriptUtil::luaCheckBool);
  1052. }
  1053. /** Template specialization. */
  1054. template<> char ScriptController::executeFunction<char>(const char* func, const char* args, ...)
  1055. {
  1056. SCRIPT_EXECUTE_FUNCTION_PARAM(char, luaL_checkint);
  1057. }
  1058. /** Template specialization. */
  1059. template<> short ScriptController::executeFunction<short>(const char* func, const char* args, ...)
  1060. {
  1061. SCRIPT_EXECUTE_FUNCTION_PARAM(short, luaL_checkint);
  1062. }
  1063. /** Template specialization. */
  1064. template<> int ScriptController::executeFunction<int>(const char* func, const char* args, ...)
  1065. {
  1066. SCRIPT_EXECUTE_FUNCTION_PARAM(int, luaL_checkint);
  1067. }
  1068. /** Template specialization. */
  1069. template<> long ScriptController::executeFunction<long>(const char* func, const char* args, ...)
  1070. {
  1071. SCRIPT_EXECUTE_FUNCTION_PARAM(long, luaL_checklong);
  1072. }
  1073. /** Template specialization. */
  1074. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func, const char* args, ...)
  1075. {
  1076. SCRIPT_EXECUTE_FUNCTION_PARAM(unsigned char, luaL_checkunsigned);
  1077. }
  1078. /** Template specialization. */
  1079. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func, const char* args, ...)
  1080. {
  1081. SCRIPT_EXECUTE_FUNCTION_PARAM(unsigned short, luaL_checkunsigned);
  1082. }
  1083. /** Template specialization. */
  1084. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func, const char* args, ...)
  1085. {
  1086. SCRIPT_EXECUTE_FUNCTION_PARAM(unsigned int, luaL_checkunsigned);
  1087. }
  1088. /** Template specialization. */
  1089. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func, const char* args, ...)
  1090. {
  1091. SCRIPT_EXECUTE_FUNCTION_PARAM(unsigned long, luaL_checkunsigned);
  1092. }
  1093. /** Template specialization. */
  1094. template<> float ScriptController::executeFunction<float>(const char* func, const char* args, ...)
  1095. {
  1096. SCRIPT_EXECUTE_FUNCTION_PARAM(float, luaL_checknumber);
  1097. }
  1098. /** Template specialization. */
  1099. template<> double ScriptController::executeFunction<double>(const char* func, const char* args, ...)
  1100. {
  1101. SCRIPT_EXECUTE_FUNCTION_PARAM(double, luaL_checknumber);
  1102. }
  1103. /** Template specialization. */
  1104. template<> std::string ScriptController::executeFunction<std::string>(const char* func, const char* args, ...)
  1105. {
  1106. SCRIPT_EXECUTE_FUNCTION_PARAM(std::string, luaL_checkstring);
  1107. }
  1108. /** Template specialization. */
  1109. template<> void ScriptController::executeFunction<void>(const char* func, const char* args, va_list* list)
  1110. {
  1111. executeFunctionHelper(0, func, args, list);
  1112. }
  1113. /** Template specialization. */
  1114. template<> bool ScriptController::executeFunction<bool>(const char* func, const char* args, va_list* list)
  1115. {
  1116. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(bool, ScriptUtil::luaCheckBool);
  1117. }
  1118. /** Template specialization. */
  1119. template<> char ScriptController::executeFunction<char>(const char* func, const char* args, va_list* list)
  1120. {
  1121. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(char, luaL_checkint);
  1122. }
  1123. /** Template specialization. */
  1124. template<> short ScriptController::executeFunction<short>(const char* func, const char* args, va_list* list)
  1125. {
  1126. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(short, luaL_checkint);
  1127. }
  1128. /** Template specialization. */
  1129. template<> int ScriptController::executeFunction<int>(const char* func, const char* args, va_list* list)
  1130. {
  1131. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(int, luaL_checkint);
  1132. }
  1133. /** Template specialization. */
  1134. template<> long ScriptController::executeFunction<long>(const char* func, const char* args, va_list* list)
  1135. {
  1136. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(long, luaL_checklong);
  1137. }
  1138. /** Template specialization. */
  1139. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func, const char* args, va_list* list)
  1140. {
  1141. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(unsigned char, luaL_checkunsigned);
  1142. }
  1143. /** Template specialization. */
  1144. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func, const char* args, va_list* list)
  1145. {
  1146. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(unsigned short, luaL_checkunsigned);
  1147. }
  1148. /** Template specialization. */
  1149. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func, const char* args, va_list* list)
  1150. {
  1151. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(unsigned int, luaL_checkunsigned);
  1152. }
  1153. /** Template specialization. */
  1154. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func, const char* args, va_list* list)
  1155. {
  1156. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(unsigned long, luaL_checkunsigned);
  1157. }
  1158. /** Template specialization. */
  1159. template<> float ScriptController::executeFunction<float>(const char* func, const char* args, va_list* list)
  1160. {
  1161. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(float, luaL_checknumber);
  1162. }
  1163. /** Template specialization. */
  1164. template<> double ScriptController::executeFunction<double>(const char* func, const char* args, va_list* list)
  1165. {
  1166. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(double, luaL_checknumber);
  1167. }
  1168. /** Template specialization. */
  1169. template<> std::string ScriptController::executeFunction<std::string>(const char* func, const char* args, va_list* list)
  1170. {
  1171. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(std::string, luaL_checkstring);
  1172. }
  1173. }