ScriptController.h 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. #ifndef SCRIPTCONTROLLER_H_
  2. #define SCRIPTCONTROLLER_H_
  3. #include "Script.h"
  4. #include "Game.h"
  5. namespace gameplay
  6. {
  7. /**
  8. * Controls and manages all scripts.
  9. */
  10. class ScriptController
  11. {
  12. friend class Game;
  13. friend class Platform;
  14. friend class Script;
  15. friend class ScriptUtil;
  16. friend class ScriptTimeListener;
  17. public:
  18. /**
  19. * Loads the given script file and executes its code (if it is not
  20. * alreay loaded).
  21. *
  22. * The script is loaded into an environment that is defined by the scope parameter.
  23. * If the script scope is GLOBAL and the forceReload parameter is false, a
  24. * previously-loaded script object may be returned. PROTECTED scope always results
  25. * in a new script being loaded and executed.
  26. *
  27. * @param path The path to the script.
  28. * @param scope The scope for the script to be executed in.
  29. * @param forceReload Whether the script should be reloaded if it has already been loaded
  30. * (applicable for GLOBAL scripts only).
  31. *
  32. * @return The loaded script, or NULL if the script could not be loaded.
  33. */
  34. Script* loadScript(const char* path, Script::Scope scope = Script::GLOBAL, bool forceReload = false);
  35. /**
  36. * Calls a zero-parameter global function.
  37. *
  38. * @param func The name of the function to call.
  39. *
  40. * @return The return value of the executed Lua function.
  41. *
  42. * @script{ignore}
  43. */
  44. template<typename T> T executeFunction(const char* func);
  45. /**
  46. * Calls a zero-parameter function.
  47. *
  48. * @param script The script to execute the function on, or NULL for the global script environment.
  49. * @param func The name of the function to call.
  50. *
  51. * @return The return value of the executed Lua function.
  52. *
  53. * @script{ignore}
  54. */
  55. template<typename T> T executeFunction(Script* script, const char* func);
  56. /**
  57. * Calls a global function using the given parameters.
  58. *
  59. * @param func The name of the function to call.
  60. * @param args The argument signature of the function. Of the form 'xxx', where each 'x' is a parameter type and must be one of:
  61. * - 'b' - bool
  62. * - 'c' - char
  63. * - 'h' - short
  64. * - 'i' - int
  65. * - 'l' - long
  66. * - 'f' - float
  67. * - 'd' - double
  68. * - 'ui' - unsigned int
  69. * - 'ul' - unsigned long
  70. * - 'uc' - unsigned char
  71. * - 'uh' - unsigned short
  72. * - 's' - string
  73. * - 'p' - pointer
  74. * - '<object-type>' - a <b>pointer</b> to an object of the given type (where the qualified type name is enclosed by angle brackets).
  75. * - '[enum-type]' - an enumerated value of the given type (where the qualified type name is enclosed by square brackets).
  76. * @param ... Parameters to pass to the script fucntion, as specified by the args parameter.
  77. *
  78. * @return The return value of the executed Lua function.
  79. *
  80. * @script{ignore}
  81. */
  82. template<typename T> T executeFunction(const char* func, const char* args, ...);
  83. /**
  84. * Calls the a function from the specified script, using the given parameters.
  85. *
  86. * @param script The script to execute the function on, or NULL for the global script environment.
  87. * @param func The name of the function to call.
  88. * @param args The argument signature of the function. Of the form 'xxx', where each 'x' is a parameter type and must be one of:
  89. * - 'b' - bool
  90. * - 'c' - char
  91. * - 'h' - short
  92. * - 'i' - int
  93. * - 'l' - long
  94. * - 'f' - float
  95. * - 'd' - double
  96. * - 'ui' - unsigned int
  97. * - 'ul' - unsigned long
  98. * - 'uc' - unsigned char
  99. * - 'uh' - unsigned short
  100. * - 's' - string
  101. * - 'p' - pointer
  102. * - '<object-type>' - a <b>pointer</b> to an object of the given type (where the qualified type name is enclosed by angle brackets).
  103. * - '[enum-type]' - an enumerated value of the given type (where the qualified type name is enclosed by square brackets).
  104. * @param ... Parameters to pass to the script fucntion, as specified by the args parameter.
  105. *
  106. * @return The return value of the executed Lua function.
  107. *
  108. * @script{ignore}
  109. */
  110. template<typename T> T executeFunction(Script* script, const char* func, const char* args, ...);
  111. /**
  112. * Calls the specified global Lua function using the given parameters.
  113. *
  114. * @param func The name of the function to call.
  115. * @param args The optional argument signature of the function. Of the form 'xxx', where each 'x' is a parameter type and must be one of:
  116. * - 'b' - bool
  117. * - 'c' - char
  118. * - 'h' - short
  119. * - 'i' - int
  120. * - 'l' - long
  121. * - 'f' - float
  122. * - 'd' - double
  123. * - 'ui' - unsigned int
  124. * - 'ul' - unsigned long
  125. * - 'uc' - unsigned char
  126. * - 'uh' - unsigned short
  127. * - 's' - string
  128. * - 'p' - pointer
  129. * - '<object-type>' - a <b>pointer</b> to an object of the given type (where the qualified type name is enclosed by angle brackets).
  130. * - '[enum-type]' - an enumerated value of the given type (where the qualified type name is enclosed by square brackets).
  131. * @param list The variable argument list containing the function's parameters, or NULL for an empty parameter list.
  132. *
  133. * @return The return value of the executed Lua function.
  134. *
  135. * @script{ignore}
  136. */
  137. template<typename T> T executeFunction(const char* func, const char* args, va_list* list);
  138. /**
  139. * Calls the specified global Lua function using the given parameters.
  140. *
  141. * @param script Optional script to use, or NULL for the global script environment.
  142. * @param func The name of the function to call.
  143. * @param args The optional argument signature of the function. Of the form 'xxx', where each 'x' is a parameter type and must be one of:
  144. * - 'b' - bool
  145. * - 'c' - char
  146. * - 'h' - short
  147. * - 'i' - int
  148. * - 'l' - long
  149. * - 'f' - float
  150. * - 'd' - double
  151. * - 'ui' - unsigned int
  152. * - 'ul' - unsigned long
  153. * - 'uc' - unsigned char
  154. * - 'uh' - unsigned short
  155. * - 's' - string
  156. * - 'p' - pointer
  157. * - '<object-type>' - a <b>pointer</b> to an object of the given type (where the qualified type name is enclosed by angle brackets).
  158. * - '[enum-type]' - an enumerated value of the given type (where the qualified type name is enclosed by square brackets).
  159. * @param list The variable argument list containing the function's parameters, or NULL for an empty parameter list.
  160. *
  161. * @return The return value of the executed Lua function.
  162. *
  163. * @script{ignore}
  164. */
  165. template<typename T> T executeFunction(Script* script, const char* func, const char* args, va_list* list);
  166. /**
  167. * Gets the global boolean script variable with the given name.
  168. *
  169. * @param name The name of the variable.
  170. * @param defaultValue The default value to return if the variable is not a bool.
  171. * @param script Optional script to use, or NULL for the global script environment.
  172. *
  173. * @return The global boolean script variable.
  174. *
  175. * @script{ignore}
  176. */
  177. bool getBool(const char* name, bool defaultValue = false, Script* script = NULL);
  178. /**
  179. * Gets the global char script variable with the given name.
  180. *
  181. * @param name The name of the variable.
  182. * @param defaultValue The default value to return if the variable is not a number.
  183. * @param script Optional script to use, or NULL for the global script environment.
  184. *
  185. * @return The global char script variable.
  186. *
  187. * @script{ignore}
  188. */
  189. char getChar(const char* name, char defaultValue = 0, Script* script = NULL);
  190. /**
  191. * Gets the global short script variable with the given name.
  192. *
  193. * @param name The name of the variable.
  194. * @param defaultValue The default value to return if the variable is not a number.
  195. * @param script Optional script to use, or NULL for the global script environment.
  196. *
  197. * @return The global short script variable.
  198. *
  199. * @script{ignore}
  200. */
  201. short getShort(const char* name, short defaultValue = 0, Script* script = NULL);
  202. /**
  203. * Gets the global int script variable with the given name.
  204. *
  205. * @param name The name of the variable.
  206. * @param defaultValue The default value to return if the variable is not a number.
  207. * @param script Optional script to use, or NULL for the global script environment.
  208. *
  209. * @return The global int script variable.
  210. *
  211. * @script{ignore}
  212. */
  213. int getInt(const char* name, int defaultValue = 0, Script* script = NULL);
  214. /**
  215. * Gets the global long script variable with the given name.
  216. *
  217. * @param name The name of the variable.
  218. * @param defaultValue The default value to return if the variable is not a number.
  219. * @param script Optional script to use, or NULL for the global script environment.
  220. *
  221. * @return The global long script variable.
  222. *
  223. * @script{ignore}
  224. */
  225. long getLong(const char* name, long defaultValue = 0, Script* script = NULL);
  226. /**
  227. * Gets the global unsigned char script variable with the given name.
  228. *
  229. * @param name The name of the variable.
  230. * @param defaultValue The default value to return if the variable is not a number.
  231. * @param script Optional script to use, or NULL for the global script environment.
  232. *
  233. * @return The global unsigned char script variable.
  234. *
  235. * @script{ignore}
  236. */
  237. unsigned char getUnsignedChar(const char* name, unsigned char defaultValue = 0, Script* script = NULL);
  238. /**
  239. * Gets the global unsigned short script variable with the given name.
  240. *
  241. * @param name The name of the variable.
  242. * @param defaultValue The default value to return if the variable is not a number.
  243. * @param script Optional script to use, or NULL for the global script environment.
  244. *
  245. * @return The global unsigned short script variable.
  246. *
  247. * @script{ignore}
  248. */
  249. unsigned short getUnsignedShort(const char* name, unsigned short defaultValue = 0, Script* script = NULL);
  250. /**
  251. * Gets the global unsigned int script variable with the given name.
  252. *
  253. * @param name The name of the variable.
  254. * @param defaultValue The default value to return if the variable is not a number.
  255. * @param script Optional script to use, or NULL for the global script environment.
  256. *
  257. * @return The global unsigned int script variable.
  258. *
  259. * @script{ignore}
  260. */
  261. unsigned int getUnsignedInt(const char* name, unsigned int defaultValue = 0, Script* script = NULL);
  262. /**
  263. * Gets the global unsigned long script variable with the given name.
  264. *
  265. * @param name The name of the variable.
  266. * @param defaultValue The default value to return if the variable is not a number.
  267. * @param script Optional script to use, or NULL for the global script environment.
  268. *
  269. * @return The global unsigned long script variable.
  270. *
  271. * @script{ignore}
  272. */
  273. unsigned long getUnsignedLong(const char* name, unsigned long defaultValue = 0, Script* script = NULL);
  274. /**
  275. * Gets the global float script variable with the given name.
  276. *
  277. * @param name The name of the variable.
  278. * @param defaultValue The default value to return if the variable is not a number.
  279. * @param script Optional script to use, or NULL for the global script environment.
  280. *
  281. * @return The global float script variable.
  282. *
  283. * @script{ignore}
  284. */
  285. float getFloat(const char* name, float defaultValue = 0, Script* script = NULL);
  286. /**
  287. * Gets the global double script variable with the given name.
  288. *
  289. * @param name The name of the variable.
  290. * @param defaultValue The default value to return if the variable is not a number.
  291. * @param script Optional script to use, or NULL for the global script environment.
  292. *
  293. * @return The global double script variable.
  294. *
  295. * @script{ignore}
  296. */
  297. double getDouble(const char* name, double defaultValue = 0, Script* script = NULL);
  298. /**
  299. * Gets the global string variable with the given name.
  300. *
  301. * The returned character string is temporary and is not gauranteed to live forever, therefore you
  302. * should not store a pointer to the returned value. If long-term access to the returned string is
  303. * required, it should be copied into another string for storage.
  304. *
  305. * @param name The name of the variable.
  306. * @param script Optional script to use, or NULL for the global script environment.
  307. *
  308. * @return The string variable or NULL if the variable is not a string.
  309. *
  310. * @script{ignore}
  311. */
  312. const char* getString(const char* name, Script* script = NULL);
  313. /**
  314. * Gets the global pointer script variable of the given type with the given name.
  315. *
  316. * @param type The type of the variable in Lua.
  317. * @param name The name of the variable.
  318. * @param script Optional script to use, or NULL for the global script environment.
  319. *
  320. * @return The global pointer script variable.
  321. *
  322. * @script{ignore}
  323. */
  324. void* getObjectPointer(const char* type, const char* name, Script* script = NULL);
  325. /**
  326. * Sets the global boolean script variable with the given name to the given value.
  327. *
  328. * @param name The name of the script variable.
  329. * @param v The boolean value.
  330. * @param script Optional script to use, or NULL for the global script environment.
  331. *
  332. * @script{ignore}
  333. */
  334. void setBool(const char* name, bool v, Script* script = NULL);
  335. /**
  336. * Sets the global char script variable with the given name to the given value.
  337. *
  338. * @param name The name of the script variable.
  339. * @param v The char value.
  340. * @param script Optional script to use, or NULL for the global script environment.
  341. *
  342. * @script{ignore}
  343. */
  344. void setChar(const char* name, char v, Script* script = NULL);
  345. /**
  346. * Sets the global short script variable with the given name to the given value.
  347. *
  348. * @param name The name of the script variable.
  349. * @param v The short value.
  350. * @param script Optional script to use, or NULL for the global script environment.
  351. *
  352. * @script{ignore}
  353. */
  354. void setShort(const char* name, short v, Script* script = NULL);
  355. /**
  356. * Sets the global int script variable with the given name to the given value.
  357. *
  358. * @param name The name of the script variable.
  359. * @param v The int value.
  360. * @param script Optional script to use, or NULL for the global script environment.
  361. *
  362. * @script{ignore}
  363. */
  364. void setInt(const char* name, int v, Script* script = NULL);
  365. /**
  366. * Sets the global long script variable with the given name to the given value.
  367. *
  368. * @param name The name of the script variable.
  369. * @param v The long value.
  370. * @param script Optional script to use, or NULL for the global script environment.
  371. *
  372. * @script{ignore}
  373. */
  374. void setLong(const char* name, long v, Script* script = NULL);
  375. /**
  376. * Gets the global unsigned char script variable with the given name to the given value.
  377. *
  378. * @param name The name of the script variable.
  379. * @param v The unsigned char value.
  380. * @param script Optional script to use, or NULL for the global script environment.
  381. *
  382. * @script{ignore}
  383. */
  384. void setUnsignedChar(const char* name, unsigned char v, Script* script = NULL);
  385. /**
  386. * Sets the global unsigned short script variable with the given name to the given value.
  387. *
  388. * @param name The name of the script variable.
  389. * @param v The unsigned short value.
  390. * @param script Optional script to use, or NULL for the global script environment.
  391. *
  392. * @script{ignore}
  393. */
  394. void setUnsignedShort(const char* name, unsigned short v, Script* script = NULL);
  395. /**
  396. * Sets the global unsigned int script variable with the given name to the given value.
  397. *
  398. * @param name The name of the script variable.
  399. * @param v The unsigned int value.
  400. * @param script Optional script to use, or NULL for the global script environment.
  401. *
  402. * @script{ignore}
  403. */
  404. void setUnsignedInt(const char* name, unsigned int v, Script* script = NULL);
  405. /**
  406. * Sets the global unsigned long script variable with the given name to the given value.
  407. *
  408. * @param name The name of the script variable.
  409. * @param v The unsigned long value.
  410. * @param script Optional script to use, or NULL for the global script environment.
  411. *
  412. * @script{ignore}
  413. */
  414. void setUnsignedLong(const char* name, unsigned long v, Script* script = NULL);
  415. /**
  416. * Sets the global float script variable with the given name to the given value.
  417. *
  418. * @param name The name of the script variable.
  419. * @param v The float value.
  420. * @param script Optional script to use, or NULL for the global script environment.
  421. *
  422. * @script{ignore}
  423. */
  424. void setFloat(const char* name, float v, Script* script = NULL);
  425. /**
  426. * Sets the global double script variable with the given name to the given value.
  427. *
  428. * @param name The name of the script variable.
  429. * @param v The double value.
  430. * @param script Optional script to use, or NULL for the global script environment.
  431. *
  432. * @script{ignore}
  433. */
  434. void setDouble(const char* name, double v, Script* script = NULL);
  435. /**
  436. * Sets the global string script variable with the given name to the given value.
  437. *
  438. * @param name The name of the script variable.
  439. * @param v The string value.
  440. * @param script Optional script to use, or NULL for the global script environment.
  441. *
  442. * @script{ignore}
  443. */
  444. void setString(const char* name, const char* v, Script* script = NULL);
  445. /**
  446. * Sets the global pointer script variable of the given type with the given name to the given value.
  447. *
  448. * When passing an argument for the 'v' parameter, it is recommended that the object passed in
  449. * be converted to void* using 'dynamic_cast<void*>(o)', to ensure that the address of the
  450. * most derived class possible be stored. In this case, the 'type' parameter should also be
  451. * set to the most derived type to ensure that the full set of methods for that object are
  452. * available to the script engine.
  453. *
  454. * @param type The type of the object point value being set.
  455. * @param name The name of the variable.
  456. * @param v The pointer value.
  457. * @param script Optional script to use, or NULL for the global script environment.
  458. *
  459. * @script{ignore}
  460. */
  461. void setObjectPointer(const char* type, const char* name, void* v, Script* script = NULL);
  462. /**
  463. * Determines if there exists a function with the specified name in the given script environment.
  464. *
  465. * @param name The name of the function to check.
  466. * @param script Optional script to inspect, or NULL to inspect the global script environment.
  467. *
  468. * @return True if the specified function exists, false otherwise.
  469. */
  470. bool functionExists(const char* name, const Script* script = NULL) const;
  471. /**
  472. * Returns the currently executing script.
  473. *
  474. * Note that this function returns the currently executing Script object,
  475. * which is not guaranteed to be valid unless the returned script's
  476. * reference count is increased while the caller holds onto it.
  477. *
  478. * @return The currently executing script, or NULL if either there is no currently
  479. * executing script or the global script environment is current.
  480. */
  481. Script* getCurrentScript() const;
  482. /**
  483. * Prints the string to the platform's output stream or log file.
  484. * Used for overriding Lua's print function.
  485. *
  486. * @param str The string to print.
  487. */
  488. static void print(const char* str);
  489. /**
  490. * Prints the strings to the platform's output stream or log file.
  491. * Used for overriding Lua's print function.
  492. *
  493. * @param str1 The first string to print.
  494. * @param str2 The second string to print on the same line as str1.
  495. */
  496. static void print(const char* str1, const char* str2);
  497. private:
  498. /**
  499. * Allows time listener interaction from Lua scripts.
  500. */
  501. struct ScriptTimeListener : public TimeListener
  502. {
  503. /**
  504. * Constructor.
  505. */
  506. ScriptTimeListener(Script* script, const char* function);
  507. /**
  508. * Destructor.
  509. */
  510. ~ScriptTimeListener();
  511. /**
  512. * @see TimeListener#timeEvent(long, void*)
  513. */
  514. void timeEvent(long timeDiff, void* cookie);
  515. /** Holds the script to execute the function within. */
  516. Script* script;
  517. /** Holds the name of the Lua script function to call back. */
  518. std::string function;
  519. };
  520. /**
  521. * Constructor.
  522. */
  523. ScriptController();
  524. /**
  525. * Copy constructor.
  526. */
  527. ScriptController(const ScriptController& copy);
  528. /**
  529. * Destructor.
  530. */
  531. ~ScriptController();
  532. /**
  533. * Called to initialize the script controller.
  534. */
  535. void initialize();
  536. /*
  537. * Called to finalize the script controller.
  538. */
  539. void finalize();
  540. /**
  541. * Internal loadScript variant that supports loading into an existing Script object
  542. * for reloading purposes.
  543. *
  544. * @param script A Script object containing a valid path and scope to load.
  545. *
  546. * @return True if the script is successfully loaded, false otherwise.
  547. *
  548. * @see loadScript(const char*, Script::scope, bool)
  549. */
  550. bool loadScript(Script* script);
  551. /**
  552. * Attempts to unload the specified script.
  553. *
  554. * Unloading a script causes the non-global data within the script to be
  555. * released such that a reference is no longer held to it. It will then
  556. * be garbage collected at some point in the future. If the script stored
  557. * any data in the global environment or if any other code still references
  558. * data from the script, that data will not be freed.
  559. *
  560. * Global scripts cannot be explicitly unloaded and this function will do
  561. * nothing for such scripts.
  562. *
  563. * @param script The script to be unloaded.
  564. */
  565. void unloadScript(Script* script);
  566. /**
  567. * Calls the specified Lua function using the given parameters.
  568. *
  569. * @param resultCount The expected number of returned values.
  570. * @param func The name of the function to call.
  571. * @param args The optional argument signature of the function, as a string of the form
  572. * 'xxx', where each 'x' is a parameter type and must be one of:
  573. * - 'b' - bool
  574. * - 'c' - char
  575. * - 'h' - short
  576. * - 'i' - int
  577. * - 'l' - long
  578. * - 'f' - float
  579. * - 'd' - double
  580. * - 'ui' - unsigned int
  581. * - 'ul' - unsigned long
  582. * - 'uc' - unsigned char
  583. * - 'uh' - unsigned short
  584. * - 's' - string
  585. * - 'p' - pointer
  586. * - '<object-type>' - a <b>pointer</b> to an object of the given type (where the qualified type name is enclosed by angle brackets).
  587. * - '[enum-type]' - an enumerated value of the given type (where the qualified type name is enclosed by square brackets).
  588. * @param list The variable argument list.
  589. * @param script Optional script to execute the function in, or NULL for to execute it in the global environment.
  590. */
  591. void executeFunctionHelper(int resultCount, const char* func, const char* args, va_list* list, Script* script = NULL);
  592. /**
  593. * Converts a Gameplay userdata value to the type with the given class name.
  594. * This function will change the metatable of the userdata value to the metatable that matches the given string.
  595. *
  596. * Example:
  597. * <code>
  598. * local launchButton = form:getControl("launch")
  599. * convert(launchButton, "Button")
  600. * print("Button text: " .. launchButton:getText())
  601. * </code>
  602. *
  603. * <code>
  604. * -- The signature of the lua function:
  605. * -- param: object A userdata object that represents a Gameplay object.
  606. * -- param: className The name of the class to convert the object to. (Examples: "Button", "PhysicsRigidBody")
  607. * function convert(object, className)
  608. * </code>
  609. *
  610. * @param state The Lua state.
  611. *
  612. * @return The number of values being returned by this function.
  613. *
  614. * @script{ignore}
  615. */
  616. static int convert(lua_State* state);
  617. /**
  618. * Schedules a script function to execute after the given time interval.
  619. *
  620. * This function is executed in the environment of the script that calls this function.
  621. *
  622. * @param timeOffset The number of game milliseconds in the future to schedule the event to be fired.
  623. * @param function The Lua script function that will receive the event.
  624. */
  625. void schedule(float timeOffset, const char* function);
  626. void pushScript(Script* script);
  627. void popScript();
  628. lua_State* _lua;
  629. unsigned int _returnCount;
  630. std::map<std::string, std::vector<std::string> > _hierarchy;
  631. std::map<std::string, std::vector<Script*> > _scripts;
  632. std::vector<Script*> _envStack;
  633. std::list<ScriptTimeListener*> _timeListeners;
  634. };
  635. /** Template specialization. */
  636. template<> void ScriptController::executeFunction<void>(const char* func);
  637. /** Template specialization. */
  638. template<> bool ScriptController::executeFunction<bool>(const char* func);
  639. /** Template specialization. */
  640. template<> char ScriptController::executeFunction<char>(const char* func);
  641. /** Template specialization. */
  642. template<> short ScriptController::executeFunction<short>(const char* func);
  643. /** Template specialization. */
  644. template<> int ScriptController::executeFunction<int>(const char* func);
  645. /** Template specialization. */
  646. template<> long ScriptController::executeFunction<long>(const char* func);
  647. /** Template specialization. */
  648. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func);
  649. /** Template specialization. */
  650. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func);
  651. /** Template specialization. */
  652. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func);
  653. /** Template specialization. */
  654. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func);
  655. /** Template specialization. */
  656. template<> float ScriptController::executeFunction<float>(const char* func);
  657. /** Template specialization. */
  658. template<> double ScriptController::executeFunction<double>(const char* func);
  659. /** Template specialization. */
  660. template<> std::string ScriptController::executeFunction<std::string>(const char* func);
  661. /** Template specialization. */
  662. template<> void ScriptController::executeFunction<void>(Script* script, const char* func);
  663. /** Template specialization. */
  664. template<> bool ScriptController::executeFunction<bool>(Script* script, const char* func);
  665. /** Template specialization. */
  666. template<> char ScriptController::executeFunction<char>(Script* script, const char* func);
  667. /** Template specialization. */
  668. template<> short ScriptController::executeFunction<short>(Script* script, const char* func);
  669. /** Template specialization. */
  670. template<> int ScriptController::executeFunction<int>(Script* script, const char* func);
  671. /** Template specialization. */
  672. template<> long ScriptController::executeFunction<long>(Script* script, const char* func);
  673. /** Template specialization. */
  674. template<> unsigned char ScriptController::executeFunction<unsigned char>(Script* script, const char* func);
  675. /** Template specialization. */
  676. template<> unsigned short ScriptController::executeFunction<unsigned short>(Script* script, const char* func);
  677. /** Template specialization. */
  678. template<> unsigned int ScriptController::executeFunction<unsigned int>(Script* script, const char* func);
  679. /** Template specialization. */
  680. template<> unsigned long ScriptController::executeFunction<unsigned long>(Script* script, const char* func);
  681. /** Template specialization. */
  682. template<> float ScriptController::executeFunction<float>(Script* script, const char* func);
  683. /** Template specialization. */
  684. template<> double ScriptController::executeFunction<double>(Script* script, const char* func);
  685. /** Template specialization. */
  686. template<> std::string ScriptController::executeFunction<std::string>(Script* script, const char* func);
  687. /** Template specialization. */
  688. template<> void ScriptController::executeFunction<void>(const char* func, const char* args, ...);
  689. /** Template specialization. */
  690. template<> bool ScriptController::executeFunction<bool>(const char* func, const char* args, ...);
  691. /** Template specialization. */
  692. template<> char ScriptController::executeFunction<char>(const char* func, const char* args, ...);
  693. /** Template specialization. */
  694. template<> short ScriptController::executeFunction<short>(const char* func, const char* args, ...);
  695. /** Template specialization. */
  696. template<> int ScriptController::executeFunction<int>(const char* func, const char* args, ...);
  697. /** Template specialization. */
  698. template<> long ScriptController::executeFunction<long>(const char* func, const char* args, ...);
  699. /** Template specialization. */
  700. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func, const char* args, ...);
  701. /** Template specialization. */
  702. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func, const char* args, ...);
  703. /** Template specialization. */
  704. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func, const char* args, ...);
  705. /** Template specialization. */
  706. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func, const char* args, ...);
  707. /** Template specialization. */
  708. template<> float ScriptController::executeFunction<float>(const char* func, const char* args, ...);
  709. /** Template specialization. */
  710. template<> double ScriptController::executeFunction<double>(const char* func, const char* args, ...);
  711. /** Template specialization. */
  712. template<> std::string ScriptController::executeFunction<std::string>(const char* func, const char* args, ...);
  713. /** Template specialization. */
  714. template<> void ScriptController::executeFunction<void>(Script* script, const char* func, const char* args, ...);
  715. /** Template specialization. */
  716. template<> bool ScriptController::executeFunction<bool>(Script* script, const char* func, const char* args, ...);
  717. /** Template specialization. */
  718. template<> char ScriptController::executeFunction<char>(Script* script, const char* func, const char* args, ...);
  719. /** Template specialization. */
  720. template<> short ScriptController::executeFunction<short>(Script* script, const char* func, const char* args, ...);
  721. /** Template specialization. */
  722. template<> int ScriptController::executeFunction<int>(Script* script, const char* func, const char* args, ...);
  723. /** Template specialization. */
  724. template<> long ScriptController::executeFunction<long>(Script* script, const char* func, const char* args, ...);
  725. /** Template specialization. */
  726. template<> unsigned char ScriptController::executeFunction<unsigned char>(Script* script, const char* func, const char* args, ...);
  727. /** Template specialization. */
  728. template<> unsigned short ScriptController::executeFunction<unsigned short>(Script* script, const char* func, const char* args, ...);
  729. /** Template specialization. */
  730. template<> unsigned int ScriptController::executeFunction<unsigned int>(Script* script, const char* func, const char* args, ...);
  731. /** Template specialization. */
  732. template<> unsigned long ScriptController::executeFunction<unsigned long>(Script* script, const char* func, const char* args, ...);
  733. /** Template specialization. */
  734. template<> float ScriptController::executeFunction<float>(Script* script, const char* func, const char* args, ...);
  735. /** Template specialization. */
  736. template<> double ScriptController::executeFunction<double>(Script* script, const char* func, const char* args, ...);
  737. /** Template specialization. */
  738. template<> std::string ScriptController::executeFunction<std::string>(Script* script, const char* func, const char* args, ...);
  739. /** Template specialization. */
  740. template<> void ScriptController::executeFunction<void>(const char* func, const char* args, va_list* list);
  741. /** Template specialization. */
  742. template<> bool ScriptController::executeFunction<bool>(const char* func, const char* args, va_list* list);
  743. /** Template specialization. */
  744. template<> char ScriptController::executeFunction<char>(const char* func, const char* args, va_list* list);
  745. /** Template specialization. */
  746. template<> short ScriptController::executeFunction<short>(const char* func, const char* args, va_list* list);
  747. /** Template specialization. */
  748. template<> int ScriptController::executeFunction<int>(const char* func, const char* args, va_list* list);
  749. /** Template specialization. */
  750. template<> long ScriptController::executeFunction<long>(const char* func, const char* args, va_list* list);
  751. /** Template specialization. */
  752. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func, const char* args, va_list* list);
  753. /** Template specialization. */
  754. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func, const char* args, va_list* list);
  755. /** Template specialization. */
  756. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func, const char* args, va_list* list);
  757. /** Template specialization. */
  758. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func, const char* args, va_list* list);
  759. /** Template specialization. */
  760. template<> float ScriptController::executeFunction<float>(const char* func, const char* args, va_list* list);
  761. /** Template specialization. */
  762. template<> double ScriptController::executeFunction<double>(const char* func, const char* args, va_list* list);
  763. /** Template specialization. */
  764. template<> std::string ScriptController::executeFunction<std::string>(const char* func, const char* args, va_list* list);
  765. /** Template specialization. */
  766. template<> void ScriptController::executeFunction<void>(Script* script, const char* func, const char* args, va_list* list);
  767. /** Template specialization. */
  768. template<> bool ScriptController::executeFunction<bool>(Script* script, const char* func, const char* args, va_list* list);
  769. /** Template specialization. */
  770. template<> char ScriptController::executeFunction<char>(Script* script, const char* func, const char* args, va_list* list);
  771. /** Template specialization. */
  772. template<> short ScriptController::executeFunction<short>(Script* script, const char* func, const char* args, va_list* list);
  773. /** Template specialization. */
  774. template<> int ScriptController::executeFunction<int>(Script* script, const char* func, const char* args, va_list* list);
  775. /** Template specialization. */
  776. template<> long ScriptController::executeFunction<long>(Script* script, const char* func, const char* args, va_list* list);
  777. /** Template specialization. */
  778. template<> unsigned char ScriptController::executeFunction<unsigned char>(Script* script, const char* func, const char* args, va_list* list);
  779. /** Template specialization. */
  780. template<> unsigned short ScriptController::executeFunction<unsigned short>(Script* script, const char* func, const char* args, va_list* list);
  781. /** Template specialization. */
  782. template<> unsigned int ScriptController::executeFunction<unsigned int>(Script* script, const char* func, const char* args, va_list* list);
  783. /** Template specialization. */
  784. template<> unsigned long ScriptController::executeFunction<unsigned long>(Script* script, const char* func, const char* args, va_list* list);
  785. /** Template specialization. */
  786. template<> float ScriptController::executeFunction<float>(Script* script, const char* func, const char* args, va_list* list);
  787. /** Template specialization. */
  788. template<> double ScriptController::executeFunction<double>(Script* script, const char* func, const char* args, va_list* list);
  789. /** Template specialization. */
  790. template<> std::string ScriptController::executeFunction<std::string>(Script* script, const char* func, const char* args, va_list* list);
  791. /**
  792. * Functions and structures used by the generated Lua script bindings.
  793. *
  794. * This class is used internally by the generated script bindings and is not intended
  795. * to be used directly.
  796. *
  797. * @script{ignore}
  798. */
  799. class ScriptUtil
  800. {
  801. public:
  802. /**
  803. * Represents a C++ object from within Lua.
  804. *
  805. * @script{ignore}
  806. */
  807. struct LuaObject
  808. {
  809. /** The actual object instance. */
  810. void* instance;
  811. /** Whether object is owned by Lua. */
  812. bool owns;
  813. };
  814. /**
  815. * Stores a Lua parameter of an array/pointer type that is passed from Lua to C.
  816. * Handles automatic cleanup of any temporary memory associated with the array.
  817. *
  818. * @script{ignore}
  819. */
  820. template <typename T>
  821. class LuaArray
  822. {
  823. public:
  824. /**
  825. * Creates a LuaArray to store a single pointer value.
  826. */
  827. LuaArray(T* param);
  828. /**
  829. * Allocates a LuaArray to store an array of values.
  830. *
  831. * Individual items in the array can be set using the
  832. * set(unsigned int, const T&) method.
  833. *
  834. * @param count Number of elements to store in the parameter.
  835. */
  836. LuaArray(int count);
  837. /**
  838. * Copy constructor.
  839. */
  840. LuaArray(const LuaArray<T>& copy);
  841. /**
  842. * Destructor.
  843. */
  844. ~LuaArray();
  845. /**
  846. * Assignment operator.
  847. */
  848. LuaArray<T>& operator = (const LuaArray<T>& p);
  849. /**
  850. * Copies the value of the object pointed to by itemPtr into the specified
  851. * index of this LuaArray's array.
  852. */
  853. void set(unsigned int index, const T* itemPtr);
  854. /**
  855. * Conversion operator from LuaArray to T*.
  856. */
  857. operator T* () const;
  858. /**
  859. * Overloads [] operator to get/set item value at index.
  860. */
  861. T& operator[] (int index);
  862. private:
  863. struct Data
  864. {
  865. Data() : value(NULL), refCount(0) { }
  866. T* value;
  867. int refCount;
  868. };
  869. Data* _data;
  870. };
  871. /**
  872. * Registers the given library with Lua.
  873. *
  874. * @param name The name of the library from within Lua.
  875. * @param functions The library function mapping (Lua function names to C++ functions).
  876. */
  877. static void registerLibrary(const char* name, const luaL_Reg* functions);
  878. /**
  879. * Registers the given boolean constant as valid for the given scope path.
  880. *
  881. * @param name The name of the constant (what the user would use from Lua).
  882. * @param value The constant's value.
  883. * @param scopePath The list of containing classes, going inward from the most outer class.
  884. */
  885. static void registerConstantBool(const std::string& name, bool value, const std::vector<std::string>& scopePath);
  886. /**
  887. * Registers the given number constant as valid for the given scope path.
  888. *
  889. * @param name The name of the constant (what the user would use from Lua).
  890. * @param value The constant's value.
  891. * @param scopePath The list of containing classes, going inward from the most outer class.
  892. */
  893. static void registerConstantNumber(const std::string& name, double value, const std::vector<std::string>& scopePath);
  894. /**
  895. * Registers the given string constant as valid for the given scope path.
  896. *
  897. * @param name The name of the constant (what the user would use from Lua).
  898. * @param value The constant's value.
  899. * @param scopePath The list of containing classes, going inward from the most outer class.
  900. */
  901. static void registerConstantString(const std::string& name, const std::string& value, const std::vector<std::string>& scopePath);
  902. /**
  903. * Registers the given enum value as valid for the given scope path.
  904. *
  905. * @param enumValue The enumeration value, expressed as an integer.
  906. * @param enumValueString The string representation of the enum value (what the user would use from Lua).
  907. * @param scopePath The list of containing classes, going inward from the most outer class.
  908. */
  909. static void registerEnumValue(int enumValue, const std::string& enumValueString, const std::vector<std::string>& scopePath);
  910. /**
  911. * Registers the given class type with Lua.
  912. *
  913. * @param name The name of the class from within Lua.
  914. * @param members The library function mapping for all the member functions (Lua function names to C++ functions).
  915. * @param newFunction The function to call that creates an instance of the class.
  916. * @param deleteFunction The function to call that destroys an instance of the class.
  917. * @param statics The library function mapping for all the static functions (Lua function names to C++ functions).
  918. * @param scopePath For an inner class, this is a list of its containing classes, going inward from the most outer class.
  919. */
  920. static void registerClass(const char* name, const luaL_Reg* members, lua_CFunction newFunction, lua_CFunction deleteFunction, const luaL_Reg* statics,
  921. const std::vector<std::string>& scopePath);
  922. /**
  923. * Register a function with Lua.
  924. *
  925. * @param luaFunction The name of the function from within Lua.
  926. * @param cppFunction The C++ function pointer.
  927. */
  928. static void registerFunction(const char* luaFunction, lua_CFunction cppFunction);
  929. /**
  930. * Sets an inheritance pair within the global inheritance hierarchy (base, derived).
  931. *
  932. * @param base The base class of the inheritance pair.
  933. * @param derived The derived class of the inheritance pair.
  934. */
  935. static void setGlobalHierarchyPair(const std::string& base, const std::string& derived);
  936. /**
  937. * Gets a pointer to a bool (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  938. *
  939. * @param index The stack index.
  940. *
  941. * @return The pointer.
  942. */
  943. static LuaArray<bool> getBoolPointer(int index);
  944. /**
  945. * Gets a pointer to a short (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  946. *
  947. * @param index The stack index.
  948. *
  949. * @return The pointer.
  950. */
  951. static LuaArray<short> getShortPointer(int index);
  952. /**
  953. * Gets a pointer to an int (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  954. *
  955. * @param index The stack index.
  956. *
  957. * @return The pointer.
  958. */
  959. static LuaArray<int> getIntPointer(int index);
  960. /**
  961. * Gets a pointer to a long (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  962. *
  963. * @param index The stack index.
  964. *
  965. * @return The pointer.
  966. */
  967. static LuaArray<long> getLongPointer(int index);
  968. /**
  969. * Gets a pointer to an unsigned char (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  970. *
  971. * @param index The stack index.
  972. *
  973. * @return The pointer.
  974. */
  975. static LuaArray<unsigned char> getUnsignedCharPointer(int index);
  976. /**
  977. * Gets a pointer to an unsigned short (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  978. *
  979. * @param index The stack index.
  980. *
  981. * @return The pointer.
  982. */
  983. static LuaArray<unsigned short> getUnsignedShortPointer(int index);
  984. /**
  985. * Gets a pointer to an unsigned int (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  986. *
  987. * @param index The stack index.
  988. *
  989. * @return The pointer.
  990. */
  991. static LuaArray<unsigned int> getUnsignedIntPointer(int index);
  992. /**
  993. * Gets a pointer to an unsigned long (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  994. *
  995. * @param index The stack index.
  996. *
  997. * @return The pointer.
  998. */
  999. static LuaArray<unsigned long> getUnsignedLongPointer(int index);
  1000. /**
  1001. * Gets a pointer to a float (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  1002. *
  1003. * @param index The stack index.
  1004. *
  1005. * @return The pointer.
  1006. */
  1007. static LuaArray<float> getFloatPointer(int index);
  1008. /**
  1009. * Gets a pointer to a double (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  1010. *
  1011. * @param index The stack index.
  1012. *
  1013. * @return The pointer.
  1014. */
  1015. static LuaArray<double> getDoublePointer(int index);
  1016. /**
  1017. * Gets an object pointer of the given type for the given stack index.
  1018. *
  1019. * @param type The type of object pointer to retrieve.
  1020. * @param index The stack index.
  1021. * @param nonNull Whether the pointer must be non-null (e.g. if the parameter we
  1022. * are retrieving is actually a reference or by-value parameter).
  1023. * @param success An out parameter that is set to true if the Lua parameter was successfully
  1024. * converted to a valid object, or false if it was unable to perform a valid conversion.
  1025. *
  1026. * @return The object pointer or <code>NULL</code> if the data at the stack index
  1027. * is not an object or if the object is not derived from the given type.
  1028. */
  1029. template <typename T>
  1030. static LuaArray<T> getObjectPointer(int index, const char* type, bool nonNull, bool* success);
  1031. /**
  1032. * Gets a string for the given stack index.
  1033. *
  1034. * @param index The stack index.
  1035. * @param isStdString Whether the string being retrieved is a std::string object or not.
  1036. *
  1037. * @return The string or <code>NULL</code>.
  1038. */
  1039. static const char* getString(int index, bool isStdString);
  1040. /**
  1041. * Checks that the parameter at the given stack position is a boolean and returns it.
  1042. *
  1043. * @param state The Lua state.
  1044. * @param n The stack index.
  1045. *
  1046. * @return The boolean (if successful; otherwise it logs an error).
  1047. */
  1048. static bool luaCheckBool(lua_State* state, int n);
  1049. };
  1050. }
  1051. #include "ScriptController.inl"
  1052. #endif