lua_ScriptController.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. #include "Base.h"
  2. #include "ScriptController.h"
  3. #include "lua_ScriptController.h"
  4. #include "Base.h"
  5. #include "FileSystem.h"
  6. #include "ScriptController.h"
  7. namespace gameplay
  8. {
  9. void luaRegister_ScriptController()
  10. {
  11. const luaL_Reg lua_members[] =
  12. {
  13. {"loadScript", lua_ScriptController_loadScript},
  14. {"loadScriptIsolated", lua_ScriptController_loadScriptIsolated},
  15. {"loadUrl", lua_ScriptController_loadUrl},
  16. {"registerCallback", lua_ScriptController_registerCallback},
  17. {"unloadScript", lua_ScriptController_unloadScript},
  18. {"unregisterCallback", lua_ScriptController_unregisterCallback},
  19. {NULL, NULL}
  20. };
  21. const luaL_Reg lua_statics[] =
  22. {
  23. {"print", lua_ScriptController_static_print},
  24. {NULL, NULL}
  25. };
  26. std::vector<std::string> scopePath;
  27. gameplay::ScriptUtil::registerClass("ScriptController", lua_members, NULL, NULL, lua_statics, scopePath);
  28. }
  29. static ScriptController* getInstance(lua_State* state)
  30. {
  31. void* userdata = luaL_checkudata(state, 1, "ScriptController");
  32. luaL_argcheck(state, userdata != NULL, 1, "'ScriptController' expected.");
  33. return (ScriptController*)((gameplay::ScriptUtil::LuaObject*)userdata)->instance;
  34. }
  35. int lua_ScriptController_loadScript(lua_State* state)
  36. {
  37. // Get the number of parameters.
  38. int paramCount = lua_gettop(state);
  39. // Attempt to match the parameters to a valid binding.
  40. switch (paramCount)
  41. {
  42. case 2:
  43. {
  44. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  45. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  46. {
  47. // Get parameter 1 off the stack.
  48. const char* param1 = gameplay::ScriptUtil::getString(2, false);
  49. ScriptController* instance = getInstance(state);
  50. bool result = instance->loadScript(param1);
  51. // Push the return value onto the stack.
  52. lua_pushboolean(state, result);
  53. return 1;
  54. }
  55. lua_pushstring(state, "lua_ScriptController_loadScript - Failed to match the given parameters to a valid function signature.");
  56. lua_error(state);
  57. break;
  58. }
  59. case 3:
  60. {
  61. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  62. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) &&
  63. lua_type(state, 3) == LUA_TBOOLEAN)
  64. {
  65. // Get parameter 1 off the stack.
  66. const char* param1 = gameplay::ScriptUtil::getString(2, false);
  67. // Get parameter 2 off the stack.
  68. bool param2 = gameplay::ScriptUtil::luaCheckBool(state, 3);
  69. ScriptController* instance = getInstance(state);
  70. bool result = instance->loadScript(param1, param2);
  71. // Push the return value onto the stack.
  72. lua_pushboolean(state, result);
  73. return 1;
  74. }
  75. lua_pushstring(state, "lua_ScriptController_loadScript - Failed to match the given parameters to a valid function signature.");
  76. lua_error(state);
  77. break;
  78. }
  79. default:
  80. {
  81. lua_pushstring(state, "Invalid number of parameters (expected 2 or 3).");
  82. lua_error(state);
  83. break;
  84. }
  85. }
  86. return 0;
  87. }
  88. int lua_ScriptController_loadScriptIsolated(lua_State* state)
  89. {
  90. // Get the number of parameters.
  91. int paramCount = lua_gettop(state);
  92. // Attempt to match the parameters to a valid binding.
  93. switch (paramCount)
  94. {
  95. case 2:
  96. {
  97. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  98. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  99. {
  100. // Get parameter 1 off the stack.
  101. const char* param1 = gameplay::ScriptUtil::getString(2, false);
  102. ScriptController* instance = getInstance(state);
  103. int result = instance->loadScriptIsolated(param1);
  104. // Push the return value onto the stack.
  105. lua_pushinteger(state, result);
  106. return 1;
  107. }
  108. lua_pushstring(state, "lua_ScriptController_loadScriptIsolated - Failed to match the given parameters to a valid function signature.");
  109. lua_error(state);
  110. break;
  111. }
  112. default:
  113. {
  114. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  115. lua_error(state);
  116. break;
  117. }
  118. }
  119. return 0;
  120. }
  121. int lua_ScriptController_loadUrl(lua_State* state)
  122. {
  123. // Get the number of parameters.
  124. int paramCount = lua_gettop(state);
  125. // Attempt to match the parameters to a valid binding.
  126. switch (paramCount)
  127. {
  128. case 2:
  129. {
  130. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  131. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  132. {
  133. // Get parameter 1 off the stack.
  134. const char* param1 = gameplay::ScriptUtil::getString(2, false);
  135. ScriptController* instance = getInstance(state);
  136. std::string result = instance->loadUrl(param1);
  137. // Push the return value onto the stack.
  138. lua_pushstring(state, result.c_str());
  139. return 1;
  140. }
  141. lua_pushstring(state, "lua_ScriptController_loadUrl - Failed to match the given parameters to a valid function signature.");
  142. lua_error(state);
  143. break;
  144. }
  145. case 3:
  146. {
  147. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  148. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) &&
  149. (lua_type(state, 3) == LUA_TTABLE || lua_type(state, 3) == LUA_TLIGHTUSERDATA))
  150. {
  151. // Get parameter 1 off the stack.
  152. const char* param1 = gameplay::ScriptUtil::getString(2, false);
  153. // Get parameter 2 off the stack.
  154. gameplay::ScriptUtil::LuaArray<int> param2 = gameplay::ScriptUtil::getIntPointer(3);
  155. ScriptController* instance = getInstance(state);
  156. std::string result = instance->loadUrl(param1, param2);
  157. // Push the return value onto the stack.
  158. lua_pushstring(state, result.c_str());
  159. return 1;
  160. }
  161. lua_pushstring(state, "lua_ScriptController_loadUrl - Failed to match the given parameters to a valid function signature.");
  162. lua_error(state);
  163. break;
  164. }
  165. default:
  166. {
  167. lua_pushstring(state, "Invalid number of parameters (expected 2 or 3).");
  168. lua_error(state);
  169. break;
  170. }
  171. }
  172. return 0;
  173. }
  174. int lua_ScriptController_registerCallback(lua_State* state)
  175. {
  176. // Get the number of parameters.
  177. int paramCount = lua_gettop(state);
  178. // Attempt to match the parameters to a valid binding.
  179. switch (paramCount)
  180. {
  181. case 3:
  182. {
  183. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  184. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) &&
  185. (lua_type(state, 3) == LUA_TSTRING || lua_type(state, 3) == LUA_TNIL))
  186. {
  187. // Get parameter 1 off the stack.
  188. const char* param1 = gameplay::ScriptUtil::getString(2, false);
  189. // Get parameter 2 off the stack.
  190. const char* param2 = gameplay::ScriptUtil::getString(3, false);
  191. ScriptController* instance = getInstance(state);
  192. instance->registerCallback(param1, param2);
  193. return 0;
  194. }
  195. lua_pushstring(state, "lua_ScriptController_registerCallback - Failed to match the given parameters to a valid function signature.");
  196. lua_error(state);
  197. break;
  198. }
  199. default:
  200. {
  201. lua_pushstring(state, "Invalid number of parameters (expected 3).");
  202. lua_error(state);
  203. break;
  204. }
  205. }
  206. return 0;
  207. }
  208. int lua_ScriptController_static_print(lua_State* state)
  209. {
  210. // Get the number of parameters.
  211. int paramCount = lua_gettop(state);
  212. // Attempt to match the parameters to a valid binding.
  213. switch (paramCount)
  214. {
  215. case 1:
  216. {
  217. do
  218. {
  219. if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL))
  220. {
  221. // Get parameter 1 off the stack.
  222. const char* param1 = gameplay::ScriptUtil::getString(1, false);
  223. ScriptController::print(param1);
  224. return 0;
  225. }
  226. } while (0);
  227. lua_pushstring(state, "lua_ScriptController_static_print - Failed to match the given parameters to a valid function signature.");
  228. lua_error(state);
  229. break;
  230. }
  231. case 2:
  232. {
  233. do
  234. {
  235. if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL) &&
  236. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  237. {
  238. // Get parameter 1 off the stack.
  239. const char* param1 = gameplay::ScriptUtil::getString(1, false);
  240. // Get parameter 2 off the stack.
  241. const char* param2 = gameplay::ScriptUtil::getString(2, false);
  242. ScriptController::print(param1, param2);
  243. return 0;
  244. }
  245. } while (0);
  246. lua_pushstring(state, "lua_ScriptController_static_print - Failed to match the given parameters to a valid function signature.");
  247. lua_error(state);
  248. break;
  249. }
  250. default:
  251. {
  252. lua_pushstring(state, "Invalid number of parameters (expected 1 or 2).");
  253. lua_error(state);
  254. break;
  255. }
  256. }
  257. return 0;
  258. }
  259. int lua_ScriptController_unloadScript(lua_State* state)
  260. {
  261. // Get the number of parameters.
  262. int paramCount = lua_gettop(state);
  263. // Attempt to match the parameters to a valid binding.
  264. switch (paramCount)
  265. {
  266. case 2:
  267. {
  268. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  269. lua_type(state, 2) == LUA_TNUMBER)
  270. {
  271. // Get parameter 1 off the stack.
  272. int param1 = (int)luaL_checkint(state, 2);
  273. ScriptController* instance = getInstance(state);
  274. instance->unloadScript(param1);
  275. return 0;
  276. }
  277. lua_pushstring(state, "lua_ScriptController_unloadScript - Failed to match the given parameters to a valid function signature.");
  278. lua_error(state);
  279. break;
  280. }
  281. default:
  282. {
  283. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  284. lua_error(state);
  285. break;
  286. }
  287. }
  288. return 0;
  289. }
  290. int lua_ScriptController_unregisterCallback(lua_State* state)
  291. {
  292. // Get the number of parameters.
  293. int paramCount = lua_gettop(state);
  294. // Attempt to match the parameters to a valid binding.
  295. switch (paramCount)
  296. {
  297. case 3:
  298. {
  299. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  300. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) &&
  301. (lua_type(state, 3) == LUA_TSTRING || lua_type(state, 3) == LUA_TNIL))
  302. {
  303. // Get parameter 1 off the stack.
  304. const char* param1 = gameplay::ScriptUtil::getString(2, false);
  305. // Get parameter 2 off the stack.
  306. const char* param2 = gameplay::ScriptUtil::getString(3, false);
  307. ScriptController* instance = getInstance(state);
  308. instance->unregisterCallback(param1, param2);
  309. return 0;
  310. }
  311. lua_pushstring(state, "lua_ScriptController_unregisterCallback - Failed to match the given parameters to a valid function signature.");
  312. lua_error(state);
  313. break;
  314. }
  315. default:
  316. {
  317. lua_pushstring(state, "Invalid number of parameters (expected 3).");
  318. lua_error(state);
  319. break;
  320. }
  321. }
  322. return 0;
  323. }
  324. }