lua_ScriptController.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. {"loadUrl", lua_ScriptController_loadUrl},
  15. {"registerCallback", lua_ScriptController_registerCallback},
  16. {"unregisterCallback", lua_ScriptController_unregisterCallback},
  17. {NULL, NULL}
  18. };
  19. const luaL_Reg lua_statics[] =
  20. {
  21. {"print", lua_ScriptController_static_print},
  22. {NULL, NULL}
  23. };
  24. std::vector<std::string> scopePath;
  25. gameplay::ScriptUtil::registerClass("ScriptController", lua_members, NULL, NULL, lua_statics, scopePath);
  26. }
  27. static ScriptController* getInstance(lua_State* state)
  28. {
  29. void* userdata = luaL_checkudata(state, 1, "ScriptController");
  30. luaL_argcheck(state, userdata != NULL, 1, "'ScriptController' expected.");
  31. return (ScriptController*)((gameplay::ScriptUtil::LuaObject*)userdata)->instance;
  32. }
  33. int lua_ScriptController_loadScript(lua_State* state)
  34. {
  35. // Get the number of parameters.
  36. int paramCount = lua_gettop(state);
  37. // Attempt to match the parameters to a valid binding.
  38. switch (paramCount)
  39. {
  40. case 2:
  41. {
  42. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  43. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  44. {
  45. // Get parameter 1 off the stack.
  46. const char* param1 = gameplay::ScriptUtil::getString(2, false);
  47. ScriptController* instance = getInstance(state);
  48. instance->loadScript(param1);
  49. return 0;
  50. }
  51. lua_pushstring(state, "lua_ScriptController_loadScript - Failed to match the given parameters to a valid function signature.");
  52. lua_error(state);
  53. break;
  54. }
  55. case 3:
  56. {
  57. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  58. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) &&
  59. lua_type(state, 3) == LUA_TBOOLEAN)
  60. {
  61. // Get parameter 1 off the stack.
  62. const char* param1 = gameplay::ScriptUtil::getString(2, false);
  63. // Get parameter 2 off the stack.
  64. bool param2 = gameplay::ScriptUtil::luaCheckBool(state, 3);
  65. ScriptController* instance = getInstance(state);
  66. instance->loadScript(param1, param2);
  67. return 0;
  68. }
  69. lua_pushstring(state, "lua_ScriptController_loadScript - Failed to match the given parameters to a valid function signature.");
  70. lua_error(state);
  71. break;
  72. }
  73. default:
  74. {
  75. lua_pushstring(state, "Invalid number of parameters (expected 2 or 3).");
  76. lua_error(state);
  77. break;
  78. }
  79. }
  80. return 0;
  81. }
  82. int lua_ScriptController_loadUrl(lua_State* state)
  83. {
  84. // Get the number of parameters.
  85. int paramCount = lua_gettop(state);
  86. // Attempt to match the parameters to a valid binding.
  87. switch (paramCount)
  88. {
  89. case 2:
  90. {
  91. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  92. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  93. {
  94. // Get parameter 1 off the stack.
  95. const char* param1 = gameplay::ScriptUtil::getString(2, false);
  96. ScriptController* instance = getInstance(state);
  97. std::string result = instance->loadUrl(param1);
  98. // Push the return value onto the stack.
  99. lua_pushstring(state, result.c_str());
  100. return 1;
  101. }
  102. lua_pushstring(state, "lua_ScriptController_loadUrl - Failed to match the given parameters to a valid function signature.");
  103. lua_error(state);
  104. break;
  105. }
  106. default:
  107. {
  108. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  109. lua_error(state);
  110. break;
  111. }
  112. }
  113. return 0;
  114. }
  115. int lua_ScriptController_registerCallback(lua_State* state)
  116. {
  117. // Get the number of parameters.
  118. int paramCount = lua_gettop(state);
  119. // Attempt to match the parameters to a valid binding.
  120. switch (paramCount)
  121. {
  122. case 3:
  123. {
  124. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  125. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) &&
  126. (lua_type(state, 3) == LUA_TSTRING || lua_type(state, 3) == LUA_TNIL))
  127. {
  128. // Get parameter 1 off the stack.
  129. const char* param1 = gameplay::ScriptUtil::getString(2, false);
  130. // Get parameter 2 off the stack.
  131. const char* param2 = gameplay::ScriptUtil::getString(3, false);
  132. ScriptController* instance = getInstance(state);
  133. instance->registerCallback(param1, param2);
  134. return 0;
  135. }
  136. lua_pushstring(state, "lua_ScriptController_registerCallback - Failed to match the given parameters to a valid function signature.");
  137. lua_error(state);
  138. break;
  139. }
  140. default:
  141. {
  142. lua_pushstring(state, "Invalid number of parameters (expected 3).");
  143. lua_error(state);
  144. break;
  145. }
  146. }
  147. return 0;
  148. }
  149. int lua_ScriptController_static_print(lua_State* state)
  150. {
  151. // Get the number of parameters.
  152. int paramCount = lua_gettop(state);
  153. // Attempt to match the parameters to a valid binding.
  154. switch (paramCount)
  155. {
  156. case 1:
  157. {
  158. do
  159. {
  160. if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL))
  161. {
  162. // Get parameter 1 off the stack.
  163. const char* param1 = gameplay::ScriptUtil::getString(1, false);
  164. ScriptController::print(param1);
  165. return 0;
  166. }
  167. } while (0);
  168. lua_pushstring(state, "lua_ScriptController_static_print - Failed to match the given parameters to a valid function signature.");
  169. lua_error(state);
  170. break;
  171. }
  172. case 2:
  173. {
  174. do
  175. {
  176. if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL) &&
  177. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  178. {
  179. // Get parameter 1 off the stack.
  180. const char* param1 = gameplay::ScriptUtil::getString(1, false);
  181. // Get parameter 2 off the stack.
  182. const char* param2 = gameplay::ScriptUtil::getString(2, false);
  183. ScriptController::print(param1, param2);
  184. return 0;
  185. }
  186. } while (0);
  187. lua_pushstring(state, "lua_ScriptController_static_print - Failed to match the given parameters to a valid function signature.");
  188. lua_error(state);
  189. break;
  190. }
  191. default:
  192. {
  193. lua_pushstring(state, "Invalid number of parameters (expected 1 or 2).");
  194. lua_error(state);
  195. break;
  196. }
  197. }
  198. return 0;
  199. }
  200. int lua_ScriptController_unregisterCallback(lua_State* state)
  201. {
  202. // Get the number of parameters.
  203. int paramCount = lua_gettop(state);
  204. // Attempt to match the parameters to a valid binding.
  205. switch (paramCount)
  206. {
  207. case 3:
  208. {
  209. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  210. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) &&
  211. (lua_type(state, 3) == LUA_TSTRING || lua_type(state, 3) == LUA_TNIL))
  212. {
  213. // Get parameter 1 off the stack.
  214. const char* param1 = gameplay::ScriptUtil::getString(2, false);
  215. // Get parameter 2 off the stack.
  216. const char* param2 = gameplay::ScriptUtil::getString(3, false);
  217. ScriptController* instance = getInstance(state);
  218. instance->unregisterCallback(param1, param2);
  219. return 0;
  220. }
  221. lua_pushstring(state, "lua_ScriptController_unregisterCallback - Failed to match the given parameters to a valid function signature.");
  222. lua_error(state);
  223. break;
  224. }
  225. default:
  226. {
  227. lua_pushstring(state, "Invalid number of parameters (expected 3).");
  228. lua_error(state);
  229. break;
  230. }
  231. }
  232. return 0;
  233. }
  234. }