ScriptController.cpp 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
  1. #include "Base.h"
  2. #include "FileSystem.h"
  3. #include "ScriptController.h"
  4. #ifndef GP_NO_LUA_BINDINGS
  5. #include "lua/lua_all_bindings.h"
  6. #endif
  7. #define GENERATE_LUA_GET_POINTER(type, checkFunc) \
  8. ScriptController* sc = Game::getInstance()->getScriptController(); \
  9. /* Check that the parameter is the correct type. */ \
  10. if (!lua_istable(sc->_lua, index)) \
  11. { \
  12. if (lua_islightuserdata(sc->_lua, index)) \
  13. return LuaArray<type>((type*)lua_touserdata(sc->_lua, index)); \
  14. lua_pushfstring(sc->_lua, "Expected a " #type " pointer (an array represented as a Lua table), got '%s' instead.", \
  15. luaL_typename(sc->_lua, index)); \
  16. lua_error(sc->_lua); \
  17. return LuaArray<type>((type*)NULL); \
  18. } \
  19. \
  20. /* Get the size of the array. */ \
  21. lua_len(sc->_lua, index); \
  22. int size = luaL_checkint(sc->_lua, -1); \
  23. lua_pop(sc->_lua, 1); \
  24. if (size <= 0) \
  25. return LuaArray<type>((type*)NULL); \
  26. \
  27. /* Declare a LuaArray to store the values. */ \
  28. LuaArray<type> arr(size); \
  29. \
  30. /* Push the first key. */ \
  31. lua_pushnil(sc->_lua); \
  32. int i = 0; \
  33. for (; lua_next(sc->_lua, index) != 0 && i < size; i++) \
  34. { \
  35. arr[i] = (checkFunc(sc->_lua, -1)); \
  36. \
  37. /* Remove the value we just retrieved, but leave the key for the next iteration. */ \
  38. lua_pop(sc->_lua, 1); \
  39. } \
  40. \
  41. return arr
  42. #define PUSH_NESTED_VARIABLE(name, defaultValue, script) \
  43. int top = lua_gettop(_lua); \
  44. if (!getNestedVariable(_lua, name, script ? script->_env : 0)) \
  45. { \
  46. lua_settop(_lua, top); \
  47. return (defaultValue); \
  48. }
  49. #define POP_NESTED_VARIABLE() \
  50. lua_settop(_lua, top)
  51. namespace gameplay
  52. {
  53. extern void splitURL(const std::string& url, std::string* file, std::string* id);
  54. /**
  55. * Pushes onto the stack, the value of the variable 'name' or the nested table value if 'name' is a '.' separated
  56. * 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.
  57. *
  58. * This function will not restore the stack if there is an error.
  59. *
  60. * @param lua The Lua state.
  61. * @param name The name of a variable or a '.' separated list of nested tables ending with a variable name.
  62. * The name value may be in the format "A.B.C.D" where A is a table and B, C are child tables.
  63. * D is any type, which will be accessed by the calling function.
  64. * @param env Optional script environment ID, or zero for the global script environment.
  65. *
  66. * @return True if the tables were pushed on the stack or the variable was pushed. Returns false on error.
  67. */
  68. static bool getNestedVariable(lua_State* lua, const char* name, int env = 0)
  69. {
  70. if (strchr(name, '.') == NULL)
  71. {
  72. // Just a field name, no nested tables
  73. if (env)
  74. {
  75. lua_rawgeti(lua, LUA_REGISTRYINDEX, env);
  76. lua_pushstring(lua, name);
  77. lua_rawget(lua, -2);
  78. }
  79. else
  80. {
  81. lua_getglobal(lua, name);
  82. }
  83. return true;
  84. }
  85. static std::string str;
  86. // Copy the input string to a std::string so we can modify it because
  87. // some of the Lua functions require NULL terminated c-strings.
  88. str.assign(name);
  89. // Find the first table, which will be a global variable.
  90. char* start = const_cast<char*>(str.c_str());
  91. char* end = strchr(start, '.');
  92. if (end == NULL)
  93. {
  94. return false;
  95. }
  96. ++end;
  97. *(end - 1) = '\0';
  98. if (env)
  99. {
  100. lua_rawgeti(lua, LUA_REGISTRYINDEX, env);
  101. lua_pushstring(lua, name);
  102. lua_rawget(lua, -2);
  103. }
  104. else
  105. {
  106. lua_getglobal(lua, start);
  107. }
  108. *(end - 1) = '.';
  109. if (!lua_istable(lua, -1))
  110. {
  111. return false;
  112. }
  113. // Push the nested tables
  114. for (;;)
  115. {
  116. start = end;
  117. end = strchr(start, '.');
  118. if (end == NULL || *end == '\0')
  119. {
  120. // push the last variable
  121. lua_pushstring(lua, start);
  122. lua_gettable(lua, -2);
  123. return true;
  124. }
  125. else
  126. {
  127. // Push the next table
  128. *end = '\0';
  129. lua_pushstring(lua, start);
  130. *end = '.';
  131. lua_gettable(lua, -2);
  132. if (!lua_istable(lua, -1))
  133. {
  134. return false;
  135. }
  136. ++end;
  137. if (*end == '.')
  138. {
  139. return false;
  140. }
  141. }
  142. }
  143. return false;
  144. }
  145. Script* ScriptController::loadScript(const char* path, Script::Scope scope, bool forceReload)
  146. {
  147. GP_ASSERT(path);
  148. Script* script = NULL;
  149. // For global scripts, check if a script with the same path and scope is already loaded.
  150. // Protected scripts are always loaded into a new instance.
  151. if (scope == Script::GLOBAL)
  152. {
  153. std::map<std::string, std::vector<Script*>>::iterator itr = _scripts.find(path);
  154. if (itr != _scripts.end())
  155. {
  156. for (size_t i = 0, count = itr->second.size(); i < count; ++i)
  157. {
  158. if (itr->second[i]->_scope == scope)
  159. {
  160. // Found a script with the same path and scope that's already loaded
  161. script = itr->second[i];
  162. break;
  163. }
  164. }
  165. }
  166. }
  167. // If the script is already loaded and forceReload was not specified, increase its
  168. // reference count and return it
  169. if (script && !forceReload)
  170. {
  171. script->addRef();
  172. return script;
  173. }
  174. // Create a new script object if neccessary
  175. if (script == NULL)
  176. {
  177. script = new Script();
  178. script->_path = path;
  179. script->_scope = scope;
  180. }
  181. // Attempt to load the script into the given scope
  182. if (!loadScript(script))
  183. {
  184. // Freeing the script will cause it to be removed from _scripts
  185. SAFE_RELEASE(script);
  186. }
  187. return script;
  188. }
  189. bool ScriptController::loadScript(Script* script)
  190. {
  191. GP_ASSERT(script);
  192. if (!FileSystem::fileExists(script->_path.c_str()))
  193. {
  194. GP_WARN("Failed to load script: %s. File does not exist.", script->_path.c_str());
  195. return false;
  196. }
  197. // Insert an entry into _scripts before loading the script, to prevent load recursion
  198. std::vector<Script*>& scripts = _scripts[script->_path];
  199. scripts.push_back(script);
  200. // Load the contents of the script, but don't execute it yet
  201. const char* scriptSource = FileSystem::readAll(script->_path.c_str());
  202. int ret = luaL_loadstring(_lua, scriptSource); // [chunk]
  203. SAFE_DELETE_ARRAY(scriptSource);
  204. if (ret == LUA_OK)
  205. {
  206. // If the requested scope is protected, create a new script env table to execute
  207. // the script within, using a metatable to fallback to the global table (_G)
  208. if (script->_scope == Script::PROTECTED)
  209. {
  210. // Create a new table as an environment for the new script
  211. lua_newtable(_lua); // new ENV for script [chunk, env]
  212. // Store a ref to the table in the registry (this pops the table) [chunk]
  213. script->_env = luaL_ref(_lua, LUA_REGISTRYINDEX);
  214. // Put the env table back on top of the stack
  215. lua_rawgeti(_lua, LUA_REGISTRYINDEX, script->_env); // [chunk, env]
  216. // Create a metatable that forwards missed lookups to global table _G
  217. lua_newtable(_lua); // metatable [chunk, env, meta]
  218. lua_pushglobaltable(_lua); // pushes _G, which will be the __index metatable entry [chunk, env, meta, _G]
  219. // Set the __index property of the metatable to _G
  220. lua_setfield(_lua, -2, "__index"); // metatable on top [chunk, env, meta]
  221. // Set the metatable for our new environment table
  222. lua_setmetatable(_lua, -2); // [chunk, env]
  223. // Store a pointer to the ENV table in the table itself, so it can refer to itself.
  224. // This is similar to how the _G field works for accessing the global table, and
  225. // how _G._G is valid.
  226. lua_pushvalue(_lua, -1); // [chunk, env, env]
  227. lua_setfield(_lua, -2, "_THIS"); // [chunk, env]
  228. // Set the first upvalue (_ENV) for our chunk to the new environment table
  229. if (lua_setupvalue(_lua, -2, 1) == NULL) // [chunk]
  230. {
  231. GP_WARN("Error setting environment table for script: %s.", script->_path.c_str());
  232. }
  233. }
  234. // Execute the script
  235. ret = lua_pcall(_lua, 0, 0, 0);
  236. }
  237. if (ret != LUA_OK)
  238. {
  239. script->_env = 0; // clear _env on failure
  240. GP_WARN("Failed to load script: %s. %s.", script->_path.c_str(), lua_tostring(_lua, -1));
  241. return false;
  242. }
  243. return true;
  244. }
  245. void ScriptController::unloadScript(Script* script)
  246. {
  247. if (script->_env != 0)
  248. {
  249. // Release the reference to the environment table for this non-global script
  250. luaL_unref(_lua, LUA_REGISTRYINDEX, script->_env);
  251. script->_env = 0;
  252. }
  253. // TODO: What else can we clean up here?
  254. // Can we test this with manual GC and breaking on gameplay object constructors that were delcared in the script?
  255. // Remove the script from our managed list
  256. std::map<std::string, std::vector<Script*>>::iterator itr = _scripts.find(script->_path);
  257. if (itr != _scripts.end())
  258. {
  259. std::vector<Script*>& scripts = itr->second;
  260. for (size_t i = 0, count = scripts.size(); i < count; ++i)
  261. {
  262. if (scripts[i] == script)
  263. {
  264. scripts.erase(scripts.begin() + i);
  265. break;
  266. }
  267. }
  268. if (scripts.empty())
  269. {
  270. _scripts.erase(itr);
  271. }
  272. }
  273. }
  274. bool ScriptController::getBool(const char* name, bool defaultValue, Script* script)
  275. {
  276. PUSH_NESTED_VARIABLE(name, defaultValue, script);
  277. bool b = lua_isboolean(_lua, -1) ? ScriptUtil::luaCheckBool(_lua, -1) : defaultValue;
  278. POP_NESTED_VARIABLE();
  279. return b;
  280. }
  281. char ScriptController::getChar(const char* name, char defaultValue, Script* script)
  282. {
  283. PUSH_NESTED_VARIABLE(name, defaultValue, script);
  284. char c = lua_isnumber(_lua, -1) ? (char)luaL_checkint(_lua, -1) : defaultValue;
  285. POP_NESTED_VARIABLE();
  286. return c;
  287. }
  288. short ScriptController::getShort(const char* name, short defaultValue, Script* script)
  289. {
  290. PUSH_NESTED_VARIABLE(name, defaultValue, script);
  291. short n = lua_isnumber(_lua, -1) ? (short)luaL_checkint(_lua, -1) : defaultValue;
  292. POP_NESTED_VARIABLE();
  293. return n;
  294. }
  295. int ScriptController::getInt(const char* name, int defaultValue, Script* script)
  296. {
  297. PUSH_NESTED_VARIABLE(name, defaultValue, script);
  298. int n = lua_isnumber(_lua, -1) ? luaL_checkint(_lua, -1) : defaultValue;
  299. POP_NESTED_VARIABLE();
  300. return n;
  301. }
  302. long ScriptController::getLong(const char* name, long defaultValue, Script* script)
  303. {
  304. PUSH_NESTED_VARIABLE(name, defaultValue, script);
  305. long n = lua_isnumber(_lua, -1) ? luaL_checklong(_lua, -1) : defaultValue;
  306. POP_NESTED_VARIABLE();
  307. return n;
  308. }
  309. unsigned char ScriptController::getUnsignedChar(const char* name, unsigned char defaultValue, Script* script)
  310. {
  311. PUSH_NESTED_VARIABLE(name, defaultValue, script);
  312. unsigned char c = lua_isnumber(_lua, -1) ? (unsigned char)luaL_checkunsigned(_lua, -1) : defaultValue;
  313. POP_NESTED_VARIABLE();
  314. return c;
  315. }
  316. unsigned short ScriptController::getUnsignedShort(const char* name, unsigned short defaultValue, Script* script)
  317. {
  318. PUSH_NESTED_VARIABLE(name, defaultValue, script);
  319. unsigned short n = lua_isnumber(_lua, -1) ? (unsigned short)luaL_checkunsigned(_lua, -1) : defaultValue;
  320. POP_NESTED_VARIABLE();
  321. return n;
  322. }
  323. unsigned int ScriptController::getUnsignedInt(const char* name, unsigned int defaultValue, Script* script)
  324. {
  325. PUSH_NESTED_VARIABLE(name, defaultValue, script);
  326. unsigned int n = lua_isnumber(_lua, -1) ? (unsigned int)luaL_checkunsigned(_lua, -1) : defaultValue;
  327. POP_NESTED_VARIABLE();
  328. return n;
  329. }
  330. unsigned long ScriptController::getUnsignedLong(const char* name, unsigned long defaultValue, Script* script)
  331. {
  332. PUSH_NESTED_VARIABLE(name, defaultValue, script);
  333. unsigned long n = lua_isnumber(_lua, -1) ? (unsigned long)luaL_checkunsigned(_lua, -1) : defaultValue;
  334. POP_NESTED_VARIABLE();
  335. return n;
  336. }
  337. float ScriptController::getFloat(const char* name, float defaultValue, Script* script)
  338. {
  339. PUSH_NESTED_VARIABLE(name, defaultValue, script);
  340. float f = lua_isnumber(_lua, -1) ? (float)luaL_checknumber(_lua, -1) : defaultValue;
  341. POP_NESTED_VARIABLE();
  342. return f;
  343. }
  344. double ScriptController::getDouble(const char* name, double defaultValue, Script* script)
  345. {
  346. PUSH_NESTED_VARIABLE(name, defaultValue, script);
  347. double n = lua_isnumber(_lua, -1) ? (double)luaL_checknumber(_lua, -1) : defaultValue;
  348. POP_NESTED_VARIABLE();
  349. return n;
  350. }
  351. const char* ScriptController::getString(const char* name, Script* script)
  352. {
  353. PUSH_NESTED_VARIABLE(name, NULL, script);
  354. const char* s = lua_isstring(_lua, -1) ? luaL_checkstring(_lua, -1) : NULL;
  355. POP_NESTED_VARIABLE();
  356. return s;
  357. }
  358. void* ScriptController::getObjectPointer(const char* type, const char* name, Script* script)
  359. {
  360. PUSH_NESTED_VARIABLE(name, NULL, script);
  361. void* userData = lua_isuserdata(_lua, -1) ? luaL_checkudata(_lua, -1, type) : NULL;
  362. POP_NESTED_VARIABLE();
  363. return ((ScriptUtil::LuaObject*)userData)->instance;
  364. }
  365. void ScriptController::setBool(const char* name, bool v, Script* script)
  366. {
  367. // TODO: Support setting variables in nested tables. Should just need to execute code similar to
  368. // that in getNestedVariable, except we only need to push the parent tables onto the stack,
  369. // NOT the actual field/variable (since we're going to set it).
  370. if (script && script->_env)
  371. {
  372. lua_rawgeti(_lua, LUA_REGISTRYINDEX, script->_env);
  373. lua_pushboolean(_lua, v);
  374. lua_setfield(_lua, -2, name);
  375. }
  376. else
  377. {
  378. lua_pushboolean(_lua, v);
  379. lua_setglobal(_lua, name);
  380. }
  381. }
  382. void ScriptController::setChar(const char* name, char v, Script* script)
  383. {
  384. if (script && script->_env)
  385. {
  386. lua_rawgeti(_lua, LUA_REGISTRYINDEX, script->_env);
  387. lua_pushinteger(_lua, v);
  388. lua_setfield(_lua, -2, name);
  389. }
  390. else
  391. {
  392. lua_pushinteger(_lua, v);
  393. lua_setglobal(_lua, name);
  394. }
  395. }
  396. void ScriptController::setShort(const char* name, short v, Script* script)
  397. {
  398. if (script && script->_env)
  399. {
  400. lua_rawgeti(_lua, LUA_REGISTRYINDEX, script->_env);
  401. lua_pushinteger(_lua, v);
  402. lua_setfield(_lua, -2, name);
  403. }
  404. else
  405. {
  406. lua_pushinteger(_lua, v);
  407. lua_setglobal(_lua, name);
  408. }
  409. }
  410. void ScriptController::setInt(const char* name, int v, Script* script)
  411. {
  412. if (script && script->_env)
  413. {
  414. lua_rawgeti(_lua, LUA_REGISTRYINDEX, script->_env);
  415. lua_pushinteger(_lua, v);
  416. lua_setfield(_lua, -2, name);
  417. }
  418. else
  419. {
  420. lua_pushinteger(_lua, v);
  421. lua_setglobal(_lua, name);
  422. }
  423. }
  424. void ScriptController::setLong(const char* name, long v, Script* script)
  425. {
  426. if (script && script->_env)
  427. {
  428. lua_rawgeti(_lua, LUA_REGISTRYINDEX, script->_env);
  429. lua_pushinteger(_lua, v);
  430. lua_setfield(_lua, -2, name);
  431. }
  432. else
  433. {
  434. lua_pushinteger(_lua, v);
  435. lua_setglobal(_lua, name);
  436. }
  437. }
  438. void ScriptController::setUnsignedChar(const char* name, unsigned char v, Script* script)
  439. {
  440. if (script && script->_env)
  441. {
  442. lua_rawgeti(_lua, LUA_REGISTRYINDEX, script->_env);
  443. lua_pushunsigned(_lua, v);
  444. lua_setfield(_lua, -2, name);
  445. }
  446. else
  447. {
  448. lua_pushunsigned(_lua, v);
  449. lua_setglobal(_lua, name);
  450. }
  451. }
  452. void ScriptController::setUnsignedShort(const char* name, unsigned short v, Script* script)
  453. {
  454. if (script && script->_env)
  455. {
  456. lua_rawgeti(_lua, LUA_REGISTRYINDEX, script->_env);
  457. lua_pushunsigned(_lua, v);
  458. lua_setfield(_lua, -2, name);
  459. }
  460. else
  461. {
  462. lua_pushunsigned(_lua, v);
  463. lua_setglobal(_lua, name);
  464. }
  465. }
  466. void ScriptController::setUnsignedInt(const char* name, unsigned int v, Script* script)
  467. {
  468. if (script && script->_env)
  469. {
  470. lua_rawgeti(_lua, LUA_REGISTRYINDEX, script->_env);
  471. lua_pushunsigned(_lua, v);
  472. lua_setfield(_lua, -2, name);
  473. }
  474. else
  475. {
  476. lua_pushunsigned(_lua, v);
  477. lua_setglobal(_lua, name);
  478. }
  479. }
  480. void ScriptController::setUnsignedLong(const char* name, unsigned long v, Script* script)
  481. {
  482. if (script && script->_env)
  483. {
  484. lua_rawgeti(_lua, LUA_REGISTRYINDEX, script->_env);
  485. lua_pushunsigned(_lua, v);
  486. lua_setfield(_lua, -2, name);
  487. }
  488. else
  489. {
  490. lua_pushunsigned(_lua, v);
  491. lua_setglobal(_lua, name);
  492. }
  493. }
  494. void ScriptController::setFloat(const char* name, float v, Script* script)
  495. {
  496. if (script && script->_env)
  497. {
  498. lua_rawgeti(_lua, LUA_REGISTRYINDEX, script->_env);
  499. lua_pushnumber(_lua, v);
  500. lua_setfield(_lua, -2, name);
  501. }
  502. else
  503. {
  504. lua_pushnumber(_lua, v);
  505. lua_setglobal(_lua, name);
  506. }
  507. }
  508. void ScriptController::setDouble(const char* name, double v, Script* script)
  509. {
  510. if (script && script->_env)
  511. {
  512. lua_rawgeti(_lua, LUA_REGISTRYINDEX, script->_env);
  513. lua_pushnumber(_lua, v);
  514. lua_setfield(_lua, -2, name);
  515. }
  516. else
  517. {
  518. lua_pushnumber(_lua, v);
  519. lua_setglobal(_lua, name);
  520. }
  521. }
  522. void ScriptController::setString(const char* name, const char* v, Script* script)
  523. {
  524. if (script && script->_env)
  525. {
  526. lua_rawgeti(_lua, LUA_REGISTRYINDEX, script->_env);
  527. lua_pushstring(_lua, v);
  528. lua_setfield(_lua, -2, name);
  529. }
  530. else
  531. {
  532. lua_pushstring(_lua, v);
  533. lua_setglobal(_lua, name);
  534. }
  535. }
  536. void ScriptController::setObjectPointer(const char* type, const char* name, void* v, Script* script)
  537. {
  538. if (script && script->_env)
  539. {
  540. lua_rawgeti(_lua, LUA_REGISTRYINDEX, script->_env);
  541. }
  542. // Push a new user data onto the stack (and setup its metatable)
  543. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(_lua, sizeof(ScriptUtil::LuaObject));
  544. object->instance = v;
  545. object->owns = false;
  546. luaL_getmetatable(_lua, type);
  547. lua_setmetatable(_lua, -2);
  548. if (script && script->_env)
  549. {
  550. lua_setfield(_lua, -2, name);
  551. }
  552. else
  553. {
  554. lua_setglobal(_lua, name);
  555. }
  556. }
  557. bool ScriptController::functionExists(const char* name, const Script* script) const
  558. {
  559. PUSH_NESTED_VARIABLE(name, false, script);
  560. bool result = lua_isfunction(_lua, -1) ? true : false;
  561. POP_NESTED_VARIABLE();
  562. return result;
  563. }
  564. Script* ScriptController::getCurrentScript() const
  565. {
  566. return _envStack.empty() ? NULL : _envStack.back();
  567. }
  568. void ScriptController::print(const char* str)
  569. {
  570. gameplay::print("%s", str);
  571. }
  572. void ScriptController::print(const char* str1, const char* str2)
  573. {
  574. gameplay::print("%s%s", str1, str2);
  575. }
  576. ScriptController::ScriptController() : _lua(NULL)
  577. {
  578. }
  579. ScriptController::~ScriptController()
  580. {
  581. }
  582. static const char* lua_print_function =
  583. "function print(...)\n"
  584. " ScriptController.print(table.concat({...},\"\\t\"), \"\\n\")\n"
  585. "end\n";
  586. static const char* lua_loadfile_function =
  587. "do\n"
  588. " local oldLoadfile = loadfile\n"
  589. " loadfile = function(filename)\n"
  590. " if filename ~= nil and not FileSystem.isAbsolutePath(filename) then\n"
  591. " FileSystem.createFileFromAsset(filename)\n"
  592. " filename = FileSystem.getResourcePath() .. filename\n"
  593. " end\n"
  594. " return oldLoadfile(filename)\n"
  595. " end\n"
  596. "end\n";
  597. static const char* lua_dofile_function =
  598. "do\n"
  599. " local oldDofile = dofile\n"
  600. " dofile = function(filename)\n"
  601. " if filename ~= nil and not FileSystem.isAbsolutePath(filename) then\n"
  602. " FileSystem.createFileFromAsset(filename)\n"
  603. " filename = FileSystem.getResourcePath() .. filename\n"
  604. " end\n"
  605. " return oldDofile(filename)\n"
  606. " end\n"
  607. "end\n";
  608. /**
  609. * @script{ignore}
  610. */
  611. void appendLuaPath(lua_State* state, const char* path)
  612. {
  613. lua_getglobal(state, "package");
  614. // Get the current path string from top of stack
  615. lua_getfield(state, -1, "path");
  616. std::string cur_path = lua_tostring(state, -1);
  617. lua_pop(state, 1);
  618. // Append our game resource path to the path
  619. cur_path += ';';
  620. cur_path += path;
  621. cur_path += "?.lua";
  622. // Push the new path
  623. lua_pushstring(state, cur_path.c_str());
  624. lua_setfield(state, -2, "path");
  625. lua_pop(state, 1);
  626. }
  627. void ScriptController::initialize()
  628. {
  629. _lua = luaL_newstate();
  630. if (!_lua)
  631. GP_ERROR("Failed to initialize Lua scripting engine.");
  632. luaL_openlibs(_lua);
  633. #ifndef GP_NO_LUA_BINDINGS
  634. lua_RegisterAllBindings();
  635. ScriptUtil::registerFunction("convert", ScriptController::convert);
  636. #endif
  637. // Append to the LUA_PATH to allow scripts to be found in the resource folder on all platforms
  638. appendLuaPath(_lua, FileSystem::getResourcePath());
  639. // Create our own print() function that uses gameplay::print.
  640. if (luaL_dostring(_lua, lua_print_function))
  641. GP_ERROR("Failed to load custom print() function with error: '%s'.", lua_tostring(_lua, -1));
  642. // Change the functions that read a file to use FileSystem.getResourcePath as their base path.
  643. if (luaL_dostring(_lua, lua_loadfile_function))
  644. GP_ERROR("Failed to load custom loadfile() function with error: '%s'.", lua_tostring(_lua, -1));
  645. if (luaL_dostring(_lua, lua_dofile_function))
  646. GP_ERROR("Failed to load custom dofile() function with error: '%s'.", lua_tostring(_lua, -1));
  647. // Write game command-line arguments to a global lua "arg" table
  648. std::ostringstream args;
  649. int argc;
  650. char** argv;
  651. Game::getInstance()->getArguments(&argc, &argv);
  652. args << "arg = { }\n";
  653. for (int i = 0; i < argc; ++i)
  654. {
  655. args << "arg[" << (i) << "] = [[" << argv[i] << "]]\n";
  656. }
  657. std::string argsStr = args.str();
  658. if (argsStr.length() > 0)
  659. {
  660. if (luaL_dostring(_lua, argsStr.c_str()))
  661. GP_ERROR("Failed to pass command-line arguments with error: '%s'.", lua_tostring(_lua, -1));
  662. }
  663. }
  664. void ScriptController::finalize()
  665. {
  666. // Cleanup any outstanding time listeners
  667. for (std::list<ScriptTimeListener*>::iterator itr = _timeListeners.begin(); itr != _timeListeners.end(); ++itr)
  668. {
  669. SAFE_DELETE(*itr);
  670. }
  671. _timeListeners.clear();
  672. if (_lua)
  673. {
  674. // Perform a full garbage collection cycle.
  675. // Note that this does NOT free any global variables declared in scripts, since
  676. // they are stored in the global state and are still referenced. Only after
  677. // closing the state (lua_close) will those variables be released.
  678. lua_gc(_lua, LUA_GCCOLLECT, 0);
  679. lua_close(_lua);
  680. _lua = NULL;
  681. }
  682. }
  683. void ScriptController::executeFunctionHelper(int resultCount, const char* func, const char* args, va_list* list, Script* script)
  684. {
  685. if (!_lua)
  686. return; // handles calling this method after script is finalized
  687. if (func == NULL)
  688. {
  689. GP_ERROR("Lua function name must be non-null.");
  690. return;
  691. }
  692. // If script was not specified and we are currently executing another function,
  693. // call this function in the previous call's environment (stored in _envStack).
  694. // This allows gameplay script callbacks (such as Scene.visit) to work locally when called
  695. // from an sandboxed script.
  696. if (!script && !_envStack.empty())
  697. {
  698. // Execute in the currently running script's environment
  699. script = _envStack.back();
  700. }
  701. int env = script ? script->_env : 0;
  702. if (!getNestedVariable(_lua, func, env))
  703. {
  704. GP_WARN("Failed to call function '%s'", func);
  705. return;
  706. }
  707. const char* sig = args;
  708. int argumentCount = 0;
  709. // Push the arguments to the Lua stack if there are any.
  710. if (sig)
  711. {
  712. while (true)
  713. {
  714. if (!(*sig))
  715. break;
  716. switch(*sig++)
  717. {
  718. // Signed integers.
  719. case 'c':
  720. case 'h':
  721. case 'i':
  722. case 'l':
  723. lua_pushinteger(_lua, va_arg(*list, int));
  724. break;
  725. // Unsigned integers.
  726. case 'u':
  727. // Skip past the actual type (long, int, short, char).
  728. sig++;
  729. lua_pushunsigned(_lua, va_arg(*list, int));
  730. break;
  731. // Booleans.
  732. case 'b':
  733. lua_pushboolean(_lua, va_arg(*list, int));
  734. break;
  735. // Floating point numbers.
  736. case 'f':
  737. case 'd':
  738. lua_pushnumber(_lua, va_arg(*list, double));
  739. break;
  740. // Strings.
  741. case 's':
  742. lua_pushstring(_lua, va_arg(*list, char*));
  743. break;
  744. // Pointers.
  745. case 'p':
  746. lua_pushlightuserdata(_lua, va_arg(*list, void*));
  747. break;
  748. // Enums.
  749. case '[':
  750. {
  751. std::string type = sig;
  752. type = type.substr(0, type.find("]"));
  753. // Skip past the closing ']' (the semi-colon here is intentional-do not remove).
  754. while (*sig++ != ']');
  755. // We simply push enums as the integer values they represent
  756. lua_pushnumber(_lua, va_arg(*list, int));
  757. break;
  758. }
  759. // Object references/pointers (Lua userdata).
  760. case '<':
  761. {
  762. std::string type = sig;
  763. type = type.substr(0, type.find(">"));
  764. // Skip past the closing '>' (the semi-colon here is intentional-do not remove).
  765. while (*sig++ != '>');
  766. // Calculate the unique Lua type name.
  767. size_t i = type.find("::");
  768. while (i != std::string::npos)
  769. {
  770. // We use "" as the replacement here-this must match the preprocessor
  771. // define SCOPE_REPLACEMENT from the gameplay-luagen project.
  772. type.replace(i, 2, "");
  773. i = type.find("::");
  774. }
  775. void* ptr = va_arg(*list, void*);
  776. if (ptr == NULL)
  777. {
  778. lua_pushnil(_lua);
  779. }
  780. else
  781. {
  782. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(_lua, sizeof(ScriptUtil::LuaObject));
  783. object->instance = ptr;
  784. object->owns = false;
  785. luaL_getmetatable(_lua, type.c_str());
  786. lua_setmetatable(_lua, -2);
  787. }
  788. break;
  789. }
  790. default:
  791. GP_ERROR("Invalid argument type '%d'.", *(sig - 1));
  792. break;
  793. }
  794. argumentCount++;
  795. luaL_checkstack(_lua, 1, "Too many arguments.");
  796. }
  797. }
  798. pushScript(script);
  799. // Perform the function call.
  800. if (lua_pcall(_lua, argumentCount, resultCount, 0) != 0)
  801. GP_WARN("Failed to call function '%s' with error '%s'.", func, lua_tostring(_lua, -1));
  802. popScript();
  803. }
  804. int ScriptController::convert(lua_State* state)
  805. {
  806. // Get the number of parameters.
  807. int paramCount = lua_gettop(state);
  808. // Attempt to match the parameters to a valid binding.
  809. switch (paramCount)
  810. {
  811. case 2:
  812. {
  813. if (lua_type(state, 1) == LUA_TUSERDATA && lua_type(state, 2) == LUA_TSTRING )
  814. {
  815. // Get parameter 2
  816. const char* param2 = ScriptUtil::getString(2, false);
  817. if (param2 != NULL)
  818. {
  819. luaL_getmetatable(state, param2);
  820. lua_setmetatable(state, -3);
  821. }
  822. return 0;
  823. }
  824. lua_pushstring(state, "lua_convert - Failed to match the given parameters to a valid function signature.");
  825. lua_error(state);
  826. break;
  827. }
  828. default:
  829. {
  830. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  831. lua_error(state);
  832. break;
  833. }
  834. }
  835. return 0;
  836. }
  837. void ScriptController::schedule(float timeOffset, const char* function)
  838. {
  839. // Get the currently execute script
  840. Script* script = _envStack.empty() ? NULL : _envStack.back();
  841. if (script)
  842. {
  843. // Increase the reference count of the script while we hold it so it doesn't
  844. // get destroyed while waiting for the event to fire.
  845. script->addRef();
  846. }
  847. ScriptTimeListener* listener = new ScriptTimeListener(script, function);
  848. _timeListeners.push_back(listener);
  849. Game::getInstance()->schedule(timeOffset, listener, NULL);
  850. }
  851. void ScriptController::pushScript(Script* script)
  852. {
  853. // Increase the reference count of the script while it's pushed,
  854. // to prevent it from being destroyed during this time.
  855. if (script)
  856. script->addRef();
  857. _envStack.push_back(script);
  858. }
  859. void ScriptController::popScript()
  860. {
  861. GP_ASSERT(!_envStack.empty());
  862. Script* script = _envStack.back();
  863. _envStack.pop_back();
  864. SAFE_RELEASE(script);
  865. }
  866. ScriptController::ScriptTimeListener::ScriptTimeListener(Script* script, const char* function) : script(script), function(function)
  867. {
  868. }
  869. ScriptController::ScriptTimeListener::~ScriptTimeListener()
  870. {
  871. // Release
  872. SAFE_RELEASE(script);
  873. }
  874. void ScriptController::ScriptTimeListener::timeEvent(long timeDiff, void* cookie)
  875. {
  876. // Remove ourself from the script controller's list
  877. std::list<ScriptTimeListener*>& list = Game::getInstance()->getScriptController()->_timeListeners;
  878. std::list<ScriptTimeListener*>::iterator itr = std::find(list.begin(), list.end(), this);
  879. if (itr != list.end())
  880. list.erase(itr);
  881. // Call the script function
  882. Game::getInstance()->getScriptController()->executeFunction<void>(script, function.c_str(), "l", timeDiff);
  883. // Free ourself.
  884. // IMPORTANT: Don't do anything else after this line!!
  885. delete this;
  886. }
  887. // Helper macros.
  888. #define SCRIPT_EXECUTE_FUNCTION_NO_PARAM(script, type, checkfunc) \
  889. int top = lua_gettop(_lua); \
  890. executeFunctionHelper(1, func, NULL, NULL, script); \
  891. type value = (type)checkfunc(_lua, -1); \
  892. lua_pop(_lua, -1); \
  893. lua_settop(_lua, top); \
  894. return value;
  895. #define SCRIPT_EXECUTE_FUNCTION_PARAM(script, type, checkfunc) \
  896. int top = lua_gettop(_lua); \
  897. va_list list; \
  898. va_start(list, args); \
  899. executeFunctionHelper(1, func, args, &list, script); \
  900. type value = (type)checkfunc(_lua, -1); \
  901. lua_pop(_lua, -1); \
  902. va_end(list); \
  903. lua_settop(_lua, top); \
  904. return value;
  905. #define SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(script, type, checkfunc) \
  906. int top = lua_gettop(_lua); \
  907. executeFunctionHelper(1, func, args, list, script); \
  908. type value = (type)checkfunc(_lua, -1); \
  909. lua_pop(_lua, -1); \
  910. lua_settop(_lua, top); \
  911. return value;
  912. template<> void ScriptController::executeFunction<void>(const char* func)
  913. {
  914. executeFunction<void>((Script*)NULL, func);
  915. }
  916. template<> bool ScriptController::executeFunction<bool>(const char* func)
  917. {
  918. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(NULL, bool, ScriptUtil::luaCheckBool);
  919. }
  920. template<> char ScriptController::executeFunction<char>(const char* func)
  921. {
  922. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(NULL, char, luaL_checkint);
  923. }
  924. template<> short ScriptController::executeFunction<short>(const char* func)
  925. {
  926. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(NULL, short, luaL_checkint);
  927. }
  928. template<> int ScriptController::executeFunction<int>(const char* func)
  929. {
  930. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(NULL, int, luaL_checkint);
  931. }
  932. template<> long ScriptController::executeFunction<long>(const char* func)
  933. {
  934. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(NULL, long, luaL_checklong);
  935. }
  936. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func)
  937. {
  938. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(NULL, unsigned char, luaL_checkunsigned);
  939. }
  940. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func)
  941. {
  942. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(NULL, unsigned short, luaL_checkunsigned);
  943. }
  944. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func)
  945. {
  946. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(NULL, unsigned int, luaL_checkunsigned);
  947. }
  948. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func)
  949. {
  950. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(NULL, unsigned long, luaL_checkunsigned);
  951. }
  952. template<> float ScriptController::executeFunction<float>(const char* func)
  953. {
  954. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(NULL, float, luaL_checknumber);
  955. }
  956. template<> double ScriptController::executeFunction<double>(const char* func)
  957. {
  958. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(NULL, double, luaL_checknumber);
  959. }
  960. template<> std::string ScriptController::executeFunction<std::string>(const char* func)
  961. {
  962. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(NULL, std::string, luaL_checkstring);
  963. }
  964. template<> void ScriptController::executeFunction<void>(Script* script, const char* func)
  965. {
  966. int top = lua_gettop(_lua);
  967. executeFunctionHelper(0, func, NULL, NULL, script);
  968. lua_settop(_lua, top);
  969. }
  970. template<> bool ScriptController::executeFunction<bool>(Script* script, const char* func)
  971. {
  972. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(script, bool, ScriptUtil::luaCheckBool);
  973. }
  974. template<> char ScriptController::executeFunction<char>(Script* script, const char* func)
  975. {
  976. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(script, char, luaL_checkint);
  977. }
  978. template<> short ScriptController::executeFunction<short>(Script* script, const char* func)
  979. {
  980. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(script, short, luaL_checkint);
  981. }
  982. template<> int ScriptController::executeFunction<int>(Script* script, const char* func)
  983. {
  984. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(script, int, luaL_checkint);
  985. }
  986. template<> long ScriptController::executeFunction<long>(Script* script, const char* func)
  987. {
  988. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(script, long, luaL_checklong);
  989. }
  990. template<> unsigned char ScriptController::executeFunction<unsigned char>(Script* script, const char* func)
  991. {
  992. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(script, unsigned char, luaL_checkunsigned);
  993. }
  994. template<> unsigned short ScriptController::executeFunction<unsigned short>(Script* script, const char* func)
  995. {
  996. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(script, unsigned short, luaL_checkunsigned);
  997. }
  998. template<> unsigned int ScriptController::executeFunction<unsigned int>(Script* script, const char* func)
  999. {
  1000. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(script, unsigned int, luaL_checkunsigned);
  1001. }
  1002. template<> unsigned long ScriptController::executeFunction<unsigned long>(Script* script, const char* func)
  1003. {
  1004. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(script, unsigned long, luaL_checkunsigned);
  1005. }
  1006. template<> float ScriptController::executeFunction<float>(Script* script, const char* func)
  1007. {
  1008. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(script, float, luaL_checknumber);
  1009. }
  1010. template<> double ScriptController::executeFunction<double>(Script* script, const char* func)
  1011. {
  1012. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(script, double, luaL_checknumber);
  1013. }
  1014. template<> std::string ScriptController::executeFunction<std::string>(Script* script, const char* func)
  1015. {
  1016. SCRIPT_EXECUTE_FUNCTION_NO_PARAM(script, std::string, luaL_checkstring);
  1017. }
  1018. /** Template specialization. */
  1019. template<> void ScriptController::executeFunction<void>(const char* func, const char* args, ...)
  1020. {
  1021. int top = lua_gettop(_lua);
  1022. va_list list;
  1023. va_start(list, args);
  1024. executeFunctionHelper(0, func, args, &list, NULL);
  1025. va_end(list);
  1026. lua_settop(_lua, top);
  1027. }
  1028. /** Template specialization. */
  1029. template<> bool ScriptController::executeFunction<bool>(const char* func, const char* args, ...)
  1030. {
  1031. SCRIPT_EXECUTE_FUNCTION_PARAM(NULL, bool, ScriptUtil::luaCheckBool);
  1032. }
  1033. /** Template specialization. */
  1034. template<> char ScriptController::executeFunction<char>(const char* func, const char* args, ...)
  1035. {
  1036. SCRIPT_EXECUTE_FUNCTION_PARAM(NULL, char, luaL_checkint);
  1037. }
  1038. /** Template specialization. */
  1039. template<> short ScriptController::executeFunction<short>(const char* func, const char* args, ...)
  1040. {
  1041. SCRIPT_EXECUTE_FUNCTION_PARAM(NULL, short, luaL_checkint);
  1042. }
  1043. /** Template specialization. */
  1044. template<> int ScriptController::executeFunction<int>(const char* func, const char* args, ...)
  1045. {
  1046. SCRIPT_EXECUTE_FUNCTION_PARAM(NULL, int, luaL_checkint);
  1047. }
  1048. /** Template specialization. */
  1049. template<> long ScriptController::executeFunction<long>(const char* func, const char* args, ...)
  1050. {
  1051. SCRIPT_EXECUTE_FUNCTION_PARAM(NULL, long, luaL_checklong);
  1052. }
  1053. /** Template specialization. */
  1054. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func, const char* args, ...)
  1055. {
  1056. SCRIPT_EXECUTE_FUNCTION_PARAM(NULL, unsigned char, luaL_checkunsigned);
  1057. }
  1058. /** Template specialization. */
  1059. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func, const char* args, ...)
  1060. {
  1061. SCRIPT_EXECUTE_FUNCTION_PARAM(NULL, unsigned short, luaL_checkunsigned);
  1062. }
  1063. /** Template specialization. */
  1064. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func, const char* args, ...)
  1065. {
  1066. SCRIPT_EXECUTE_FUNCTION_PARAM(NULL, unsigned int, luaL_checkunsigned);
  1067. }
  1068. /** Template specialization. */
  1069. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func, const char* args, ...)
  1070. {
  1071. SCRIPT_EXECUTE_FUNCTION_PARAM(NULL, unsigned long, luaL_checkunsigned);
  1072. }
  1073. /** Template specialization. */
  1074. template<> float ScriptController::executeFunction<float>(const char* func, const char* args, ...)
  1075. {
  1076. SCRIPT_EXECUTE_FUNCTION_PARAM(NULL, float, luaL_checknumber);
  1077. }
  1078. /** Template specialization. */
  1079. template<> double ScriptController::executeFunction<double>(const char* func, const char* args, ...)
  1080. {
  1081. SCRIPT_EXECUTE_FUNCTION_PARAM(NULL, double, luaL_checknumber);
  1082. }
  1083. /** Template specialization. */
  1084. template<> std::string ScriptController::executeFunction<std::string>(const char* func, const char* args, ...)
  1085. {
  1086. SCRIPT_EXECUTE_FUNCTION_PARAM(NULL, std::string, luaL_checkstring);
  1087. }
  1088. /** Template specialization. */
  1089. template<> void ScriptController::executeFunction<void>(Script* script, const char* func, const char* args, ...)
  1090. {
  1091. int top = lua_gettop(_lua);
  1092. va_list list;
  1093. va_start(list, args);
  1094. executeFunctionHelper(0, func, args, &list, script);
  1095. va_end(list);
  1096. lua_settop(_lua, top);
  1097. }
  1098. /** Template specialization. */
  1099. template<> bool ScriptController::executeFunction<bool>(Script* script, const char* func, const char* args, ...)
  1100. {
  1101. SCRIPT_EXECUTE_FUNCTION_PARAM(script, bool, ScriptUtil::luaCheckBool);
  1102. }
  1103. /** Template specialization. */
  1104. template<> char ScriptController::executeFunction<char>(Script* script, const char* func, const char* args, ...)
  1105. {
  1106. SCRIPT_EXECUTE_FUNCTION_PARAM(script, char, luaL_checkint);
  1107. }
  1108. /** Template specialization. */
  1109. template<> short ScriptController::executeFunction<short>(Script* script, const char* func, const char* args, ...)
  1110. {
  1111. SCRIPT_EXECUTE_FUNCTION_PARAM(script, short, luaL_checkint);
  1112. }
  1113. /** Template specialization. */
  1114. template<> int ScriptController::executeFunction<int>(Script* script, const char* func, const char* args, ...)
  1115. {
  1116. SCRIPT_EXECUTE_FUNCTION_PARAM(script, int, luaL_checkint);
  1117. }
  1118. /** Template specialization. */
  1119. template<> long ScriptController::executeFunction<long>(Script* script, const char* func, const char* args, ...)
  1120. {
  1121. SCRIPT_EXECUTE_FUNCTION_PARAM(script, long, luaL_checklong);
  1122. }
  1123. /** Template specialization. */
  1124. template<> unsigned char ScriptController::executeFunction<unsigned char>(Script* script, const char* func, const char* args, ...)
  1125. {
  1126. SCRIPT_EXECUTE_FUNCTION_PARAM(script, unsigned char, luaL_checkunsigned);
  1127. }
  1128. /** Template specialization. */
  1129. template<> unsigned short ScriptController::executeFunction<unsigned short>(Script* script, const char* func, const char* args, ...)
  1130. {
  1131. SCRIPT_EXECUTE_FUNCTION_PARAM(script, unsigned short, luaL_checkunsigned);
  1132. }
  1133. /** Template specialization. */
  1134. template<> unsigned int ScriptController::executeFunction<unsigned int>(Script* script, const char* func, const char* args, ...)
  1135. {
  1136. SCRIPT_EXECUTE_FUNCTION_PARAM(script, unsigned int, luaL_checkunsigned);
  1137. }
  1138. /** Template specialization. */
  1139. template<> unsigned long ScriptController::executeFunction<unsigned long>(Script* script, const char* func, const char* args, ...)
  1140. {
  1141. SCRIPT_EXECUTE_FUNCTION_PARAM(script, unsigned long, luaL_checkunsigned);
  1142. }
  1143. /** Template specialization. */
  1144. template<> float ScriptController::executeFunction<float>(Script* script, const char* func, const char* args, ...)
  1145. {
  1146. SCRIPT_EXECUTE_FUNCTION_PARAM(script, float, luaL_checknumber);
  1147. }
  1148. /** Template specialization. */
  1149. template<> double ScriptController::executeFunction<double>(Script* script, const char* func, const char* args, ...)
  1150. {
  1151. SCRIPT_EXECUTE_FUNCTION_PARAM(script, double, luaL_checknumber);
  1152. }
  1153. /** Template specialization. */
  1154. template<> std::string ScriptController::executeFunction<std::string>(Script* script, const char* func, const char* args, ...)
  1155. {
  1156. SCRIPT_EXECUTE_FUNCTION_PARAM(script, std::string, luaL_checkstring);
  1157. }
  1158. /** Template specialization. */
  1159. template<> void ScriptController::executeFunction<void>(const char* func, const char* args, va_list* list)
  1160. {
  1161. executeFunctionHelper(0, func, args, list, NULL);
  1162. }
  1163. /** Template specialization. */
  1164. template<> bool ScriptController::executeFunction<bool>(const char* func, const char* args, va_list* list)
  1165. {
  1166. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(NULL, bool, ScriptUtil::luaCheckBool);
  1167. }
  1168. /** Template specialization. */
  1169. template<> char ScriptController::executeFunction<char>(const char* func, const char* args, va_list* list)
  1170. {
  1171. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(NULL, char, luaL_checkint);
  1172. }
  1173. /** Template specialization. */
  1174. template<> short ScriptController::executeFunction<short>(const char* func, const char* args, va_list* list)
  1175. {
  1176. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(NULL, short, luaL_checkint);
  1177. }
  1178. /** Template specialization. */
  1179. template<> int ScriptController::executeFunction<int>(const char* func, const char* args, va_list* list)
  1180. {
  1181. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(NULL, int, luaL_checkint);
  1182. }
  1183. /** Template specialization. */
  1184. template<> long ScriptController::executeFunction<long>(const char* func, const char* args, va_list* list)
  1185. {
  1186. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(NULL, long, luaL_checklong);
  1187. }
  1188. /** Template specialization. */
  1189. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func, const char* args, va_list* list)
  1190. {
  1191. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(NULL, unsigned char, luaL_checkunsigned);
  1192. }
  1193. /** Template specialization. */
  1194. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func, const char* args, va_list* list)
  1195. {
  1196. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(NULL, unsigned short, luaL_checkunsigned);
  1197. }
  1198. /** Template specialization. */
  1199. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func, const char* args, va_list* list)
  1200. {
  1201. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(NULL, unsigned int, luaL_checkunsigned);
  1202. }
  1203. /** Template specialization. */
  1204. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func, const char* args, va_list* list)
  1205. {
  1206. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(NULL, unsigned long, luaL_checkunsigned);
  1207. }
  1208. /** Template specialization. */
  1209. template<> float ScriptController::executeFunction<float>(const char* func, const char* args, va_list* list)
  1210. {
  1211. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(NULL, float, luaL_checknumber);
  1212. }
  1213. /** Template specialization. */
  1214. template<> double ScriptController::executeFunction<double>(const char* func, const char* args, va_list* list)
  1215. {
  1216. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(NULL, double, luaL_checknumber);
  1217. }
  1218. /** Template specialization. */
  1219. template<> std::string ScriptController::executeFunction<std::string>(const char* func, const char* args, va_list* list)
  1220. {
  1221. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(NULL, std::string, luaL_checkstring);
  1222. }
  1223. /** Template specialization. */
  1224. template<> void ScriptController::executeFunction<void>(Script* script, const char* func, const char* args, va_list* list)
  1225. {
  1226. executeFunctionHelper(0, func, args, list, script);
  1227. }
  1228. /** Template specialization. */
  1229. template<> bool ScriptController::executeFunction<bool>(Script* script, const char* func, const char* args, va_list* list)
  1230. {
  1231. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(script, bool, ScriptUtil::luaCheckBool);
  1232. }
  1233. /** Template specialization. */
  1234. template<> char ScriptController::executeFunction<char>(Script* script, const char* func, const char* args, va_list* list)
  1235. {
  1236. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(script, char, luaL_checkint);
  1237. }
  1238. /** Template specialization. */
  1239. template<> short ScriptController::executeFunction<short>(Script* script, const char* func, const char* args, va_list* list)
  1240. {
  1241. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(script, short, luaL_checkint);
  1242. }
  1243. /** Template specialization. */
  1244. template<> int ScriptController::executeFunction<int>(Script* script, const char* func, const char* args, va_list* list)
  1245. {
  1246. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(script, int, luaL_checkint);
  1247. }
  1248. /** Template specialization. */
  1249. template<> long ScriptController::executeFunction<long>(Script* script, const char* func, const char* args, va_list* list)
  1250. {
  1251. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(script, long, luaL_checklong);
  1252. }
  1253. /** Template specialization. */
  1254. template<> unsigned char ScriptController::executeFunction<unsigned char>(Script* script, const char* func, const char* args, va_list* list)
  1255. {
  1256. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(script, unsigned char, luaL_checkunsigned);
  1257. }
  1258. /** Template specialization. */
  1259. template<> unsigned short ScriptController::executeFunction<unsigned short>(Script* script, const char* func, const char* args, va_list* list)
  1260. {
  1261. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(script, unsigned short, luaL_checkunsigned);
  1262. }
  1263. /** Template specialization. */
  1264. template<> unsigned int ScriptController::executeFunction<unsigned int>(Script* script, const char* func, const char* args, va_list* list)
  1265. {
  1266. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(script, unsigned int, luaL_checkunsigned);
  1267. }
  1268. /** Template specialization. */
  1269. template<> unsigned long ScriptController::executeFunction<unsigned long>(Script* script, const char* func, const char* args, va_list* list)
  1270. {
  1271. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(script, unsigned long, luaL_checkunsigned);
  1272. }
  1273. /** Template specialization. */
  1274. template<> float ScriptController::executeFunction<float>(Script* script, const char* func, const char* args, va_list* list)
  1275. {
  1276. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(script, float, luaL_checknumber);
  1277. }
  1278. /** Template specialization. */
  1279. template<> double ScriptController::executeFunction<double>(Script* script, const char* func, const char* args, va_list* list)
  1280. {
  1281. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(script, double, luaL_checknumber);
  1282. }
  1283. /** Template specialization. */
  1284. template<> std::string ScriptController::executeFunction<std::string>(Script* script, const char* func, const char* args, va_list* list)
  1285. {
  1286. SCRIPT_EXECUTE_FUNCTION_PARAM_LIST(script, std::string, luaL_checkstring);
  1287. }
  1288. void ScriptUtil::registerLibrary(const char* name, const luaL_Reg* functions)
  1289. {
  1290. ScriptController* sc = Game::getInstance()->getScriptController();
  1291. lua_newtable(sc->_lua);
  1292. // Go through the list of functions and add them to the table.
  1293. const luaL_Reg* iter = functions;
  1294. for (; iter && iter->name; iter++)
  1295. {
  1296. lua_pushcfunction(sc->_lua, iter->func);
  1297. lua_setfield(sc->_lua, -2, iter->name);
  1298. }
  1299. lua_setglobal(sc->_lua, name);
  1300. }
  1301. void ScriptUtil::registerConstantBool(const std::string& name, bool value, const std::vector<std::string>& scopePath)
  1302. {
  1303. ScriptController* sc = Game::getInstance()->getScriptController();
  1304. // If the constant is within a scope, get the correct parent
  1305. // table on the stack before setting its value.
  1306. if (!scopePath.empty())
  1307. {
  1308. lua_getglobal(sc->_lua, scopePath[0].c_str());
  1309. for (unsigned int i = 1; i < scopePath.size(); i++)
  1310. {
  1311. lua_pushstring(sc->_lua, scopePath[i].c_str());
  1312. lua_gettable(sc->_lua, -2);
  1313. }
  1314. // Add the constant to the parent table.
  1315. lua_pushboolean(sc->_lua, value);
  1316. lua_setfield(sc->_lua, -2, name.c_str());
  1317. // Pop all the parent tables off the stack.
  1318. int size = (int)scopePath.size();
  1319. lua_pop(sc->_lua, size);
  1320. }
  1321. else
  1322. {
  1323. // TODO: Currently unsupported (we don't parse for this yet).
  1324. // If the constant is global, add it to the global table.
  1325. lua_pushboolean(sc->_lua, value);
  1326. lua_pushvalue(sc->_lua, -1);
  1327. lua_setglobal(sc->_lua, name.c_str());
  1328. }
  1329. }
  1330. void ScriptUtil::registerConstantNumber(const std::string& name, double value, const std::vector<std::string>& scopePath)
  1331. {
  1332. ScriptController* sc = Game::getInstance()->getScriptController();
  1333. // If the constant is within a scope, get the correct parent
  1334. // table on the stack before setting its value.
  1335. if (!scopePath.empty())
  1336. {
  1337. lua_getglobal(sc->_lua, scopePath[0].c_str());
  1338. for (unsigned int i = 1; i < scopePath.size(); i++)
  1339. {
  1340. lua_pushstring(sc->_lua, scopePath[i].c_str());
  1341. lua_gettable(sc->_lua, -2);
  1342. }
  1343. // Add the constant to the parent table.
  1344. lua_pushnumber(sc->_lua, value);
  1345. lua_setfield(sc->_lua, -2, name.c_str());
  1346. // Pop all the parent tables off the stack.
  1347. int size = (int)scopePath.size();
  1348. lua_pop(sc->_lua, size);
  1349. }
  1350. else
  1351. {
  1352. // TODO: Currently unsupported (we don't parse for this yet).
  1353. // If the constant is global, add it to the global table.
  1354. lua_pushnumber(sc->_lua, value);
  1355. lua_pushvalue(sc->_lua, -1);
  1356. lua_setglobal(sc->_lua, name.c_str());
  1357. }
  1358. }
  1359. void ScriptUtil::registerConstantString(const std::string& name, const std::string& value, const std::vector<std::string>& scopePath)
  1360. {
  1361. ScriptController* sc = Game::getInstance()->getScriptController();
  1362. // If the constant is within a scope, get the correct parent
  1363. // table on the stack before setting its value.
  1364. if (!scopePath.empty())
  1365. {
  1366. lua_getglobal(sc->_lua, scopePath[0].c_str());
  1367. for (unsigned int i = 1; i < scopePath.size(); i++)
  1368. {
  1369. lua_pushstring(sc->_lua, scopePath[i].c_str());
  1370. lua_gettable(sc->_lua, -2);
  1371. }
  1372. // Add the constant to the parent table.
  1373. lua_pushstring(sc->_lua, value.c_str());
  1374. lua_setfield(sc->_lua, -2, name.c_str());
  1375. // Pop all the parent tables off the stack.
  1376. int size = (int)scopePath.size();
  1377. lua_pop(sc->_lua, size);
  1378. }
  1379. else
  1380. {
  1381. // TODO: Currently unsupported (we don't parse for this yet).
  1382. // If the constant is global, add it to the global table.
  1383. lua_pushstring(sc->_lua, value.c_str());
  1384. lua_pushvalue(sc->_lua, -1);
  1385. lua_setglobal(sc->_lua, name.c_str());
  1386. }
  1387. }
  1388. void ScriptUtil::registerEnumValue(int enumValue, const std::string& enumValueString, const std::vector<std::string>& scopePath)
  1389. {
  1390. ScriptController* sc = Game::getInstance()->getScriptController();
  1391. // If the constant is within a scope, get the correct parent
  1392. // table on the stack before setting its value.
  1393. if (!scopePath.empty())
  1394. {
  1395. lua_getglobal(sc->_lua, scopePath[0].c_str());
  1396. for (unsigned int i = 1; i < scopePath.size(); i++)
  1397. {
  1398. lua_pushstring(sc->_lua, scopePath[i].c_str());
  1399. lua_gettable(sc->_lua, -2);
  1400. }
  1401. // Add the enum value to the parent table.
  1402. lua_pushnumber(sc->_lua, enumValue);
  1403. lua_setfield(sc->_lua, -2, enumValueString.c_str());
  1404. // Pop all the parent tables off the stack.
  1405. int size = (int)scopePath.size();
  1406. lua_pop(sc->_lua, size);
  1407. }
  1408. else
  1409. {
  1410. // TODO: Currently unsupported (we don't parse for this yet).
  1411. // If the constant is global, add it to the global table.
  1412. lua_pushnumber(sc->_lua, enumValue);
  1413. lua_pushvalue(sc->_lua, -1);
  1414. lua_setglobal(sc->_lua, enumValueString.c_str());
  1415. }
  1416. }
  1417. void ScriptUtil::registerClass(const char* name, const luaL_Reg* members, lua_CFunction newFunction,
  1418. lua_CFunction deleteFunction, const luaL_Reg* statics, const std::vector<std::string>& scopePath)
  1419. {
  1420. ScriptController* sc = Game::getInstance()->getScriptController();
  1421. // If the type is an inner type, get the correct parent
  1422. // table on the stack before creating the table for the class.
  1423. if (!scopePath.empty())
  1424. {
  1425. std::string tablename = name;
  1426. // Strip off the scope path part of the name.
  1427. lua_getglobal(sc->_lua, scopePath[0].c_str());
  1428. std::size_t index = tablename.find(scopePath[0]);
  1429. if (index != std::string::npos)
  1430. tablename = tablename.substr(index + scopePath[0].size());
  1431. for (unsigned int i = 1; i < scopePath.size(); i++)
  1432. {
  1433. lua_pushstring(sc->_lua, scopePath[i].c_str());
  1434. lua_gettable(sc->_lua, -2);
  1435. index = tablename.find(scopePath[i]);
  1436. if (index != std::string::npos)
  1437. tablename = tablename.substr(index + scopePath[i].size());
  1438. }
  1439. lua_pushstring(sc->_lua, tablename.c_str());
  1440. lua_newtable(sc->_lua);
  1441. }
  1442. else
  1443. {
  1444. // If the type is not an inner type, set it as a global table.
  1445. lua_newtable(sc->_lua);
  1446. lua_pushvalue(sc->_lua, -1);
  1447. lua_setglobal(sc->_lua, name);
  1448. }
  1449. // Create the metatable and populate it with the member functions.
  1450. lua_pushliteral(sc->_lua, "__metatable");
  1451. luaL_newmetatable(sc->_lua, name);
  1452. if (members)
  1453. luaL_setfuncs(sc->_lua, members, 0);
  1454. lua_pushstring(sc->_lua, "__index");
  1455. lua_pushvalue(sc->_lua, -2);
  1456. lua_settable(sc->_lua, -3);
  1457. // Add the delete function if it was specified.
  1458. if (deleteFunction)
  1459. {
  1460. lua_pushstring(sc->_lua, "__gc");
  1461. lua_pushcfunction(sc->_lua, deleteFunction);
  1462. lua_settable(sc->_lua, -3);
  1463. }
  1464. // Set the metatable on the main table.
  1465. lua_settable(sc->_lua, -3);
  1466. // Populate the main table with the static functions.
  1467. if (statics)
  1468. luaL_setfuncs(sc->_lua, statics, 0);
  1469. // Set the new function(s) for the class.
  1470. if (newFunction)
  1471. {
  1472. lua_pushliteral(sc->_lua, "new");
  1473. lua_pushcfunction(sc->_lua, newFunction);
  1474. lua_settable(sc->_lua, -3);
  1475. }
  1476. // Set the table we just created within the correct parent table.
  1477. if (!scopePath.empty())
  1478. {
  1479. lua_settable(sc->_lua, -3);
  1480. // Pop all the parent tables off the stack.
  1481. int size = (int)scopePath.size();
  1482. lua_pop(sc->_lua, size);
  1483. }
  1484. else
  1485. {
  1486. // Pop the main table off the stack.
  1487. lua_pop(sc->_lua, 1);
  1488. }
  1489. }
  1490. void ScriptUtil::registerFunction(const char* luaFunction, lua_CFunction cppFunction)
  1491. {
  1492. lua_pushcfunction(Game::getInstance()->getScriptController()->_lua, cppFunction);
  1493. lua_setglobal(Game::getInstance()->getScriptController()->_lua, luaFunction);
  1494. }
  1495. void ScriptUtil::setGlobalHierarchyPair(const std::string& base, const std::string& derived)
  1496. {
  1497. Game::getInstance()->getScriptController()->_hierarchy[base].push_back(derived);
  1498. }
  1499. ScriptUtil::LuaArray<bool> ScriptUtil::getBoolPointer(int index)
  1500. {
  1501. GENERATE_LUA_GET_POINTER(bool, luaCheckBool);
  1502. }
  1503. ScriptUtil::LuaArray<short> ScriptUtil::getShortPointer(int index)
  1504. {
  1505. GENERATE_LUA_GET_POINTER(short, (short)luaL_checkint);
  1506. }
  1507. ScriptUtil::LuaArray<int> ScriptUtil::getIntPointer(int index)
  1508. {
  1509. GENERATE_LUA_GET_POINTER(int, (int)luaL_checkint);
  1510. }
  1511. ScriptUtil::LuaArray<long> ScriptUtil::getLongPointer(int index)
  1512. {
  1513. GENERATE_LUA_GET_POINTER(long, (long)luaL_checkint);
  1514. }
  1515. ScriptUtil::LuaArray<unsigned char> ScriptUtil::getUnsignedCharPointer(int index)
  1516. {
  1517. GENERATE_LUA_GET_POINTER(unsigned char, (unsigned char)luaL_checkunsigned);
  1518. }
  1519. ScriptUtil::LuaArray<unsigned short> ScriptUtil::getUnsignedShortPointer(int index)
  1520. {
  1521. GENERATE_LUA_GET_POINTER(unsigned short, (unsigned short)luaL_checkunsigned);
  1522. }
  1523. ScriptUtil::LuaArray<unsigned int> ScriptUtil::getUnsignedIntPointer(int index)
  1524. {
  1525. GENERATE_LUA_GET_POINTER(unsigned int, (unsigned int)luaL_checkunsigned);
  1526. }
  1527. ScriptUtil::LuaArray<unsigned long> ScriptUtil::getUnsignedLongPointer(int index)
  1528. {
  1529. GENERATE_LUA_GET_POINTER(unsigned long, (unsigned long)luaL_checkunsigned);
  1530. }
  1531. ScriptUtil::LuaArray<float> ScriptUtil::getFloatPointer(int index)
  1532. {
  1533. GENERATE_LUA_GET_POINTER(float, (float)luaL_checknumber);
  1534. }
  1535. ScriptUtil::LuaArray<double> ScriptUtil::getDoublePointer(int index)
  1536. {
  1537. GENERATE_LUA_GET_POINTER(double, (double)luaL_checknumber);
  1538. }
  1539. const char* ScriptUtil::getString(int index, bool isStdString)
  1540. {
  1541. if (lua_type(Game::getInstance()->getScriptController()->_lua, index) == LUA_TSTRING)
  1542. return luaL_checkstring(Game::getInstance()->getScriptController()->_lua, index);
  1543. else if (lua_type(Game::getInstance()->getScriptController()->_lua, index) == LUA_TNIL && !isStdString)
  1544. return NULL;
  1545. else
  1546. {
  1547. GP_ERROR("Invalid string parameter (index = %d).", index);
  1548. return NULL;
  1549. }
  1550. }
  1551. bool ScriptUtil::luaCheckBool(lua_State* state, int n)
  1552. {
  1553. if (!lua_isboolean(state, n))
  1554. {
  1555. const char* msg = lua_pushfstring(state, "%s expected, got %s", lua_typename(state, LUA_TBOOLEAN), luaL_typename(state, n));
  1556. luaL_argerror(state, n, msg);
  1557. return false;
  1558. }
  1559. return (lua_toboolean(state, n) != 0);
  1560. }
  1561. }