ScriptController.cpp 39 KB

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