ScriptController.cpp 42 KB

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