lua_ScriptController.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. {NULL, NULL}
  16. };
  17. const luaL_Reg lua_statics[] =
  18. {
  19. {"print", lua_ScriptController_static_print},
  20. {NULL, NULL}
  21. };
  22. std::vector<std::string> scopePath;
  23. ScriptUtil::registerClass("ScriptController", lua_members, NULL, NULL, lua_statics, scopePath);
  24. }
  25. static ScriptController* getInstance(lua_State* state)
  26. {
  27. void* userdata = luaL_checkudata(state, 1, "ScriptController");
  28. luaL_argcheck(state, userdata != NULL, 1, "'ScriptController' expected.");
  29. return (ScriptController*)((ScriptUtil::LuaObject*)userdata)->instance;
  30. }
  31. int lua_ScriptController_loadScript(lua_State* state)
  32. {
  33. // Get the number of parameters.
  34. int paramCount = lua_gettop(state);
  35. // Attempt to match the parameters to a valid binding.
  36. switch (paramCount)
  37. {
  38. case 2:
  39. {
  40. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  41. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  42. {
  43. // Get parameter 1 off the stack.
  44. const char* param1 = ScriptUtil::getString(2, false);
  45. ScriptController* instance = getInstance(state);
  46. instance->loadScript(param1);
  47. return 0;
  48. }
  49. lua_pushstring(state, "lua_ScriptController_loadScript - Failed to match the given parameters to a valid function signature.");
  50. lua_error(state);
  51. break;
  52. }
  53. case 3:
  54. {
  55. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  56. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) &&
  57. lua_type(state, 3) == LUA_TBOOLEAN)
  58. {
  59. // Get parameter 1 off the stack.
  60. const char* param1 = ScriptUtil::getString(2, false);
  61. // Get parameter 2 off the stack.
  62. bool param2 = ScriptUtil::luaCheckBool(state, 3);
  63. ScriptController* instance = getInstance(state);
  64. instance->loadScript(param1, param2);
  65. return 0;
  66. }
  67. lua_pushstring(state, "lua_ScriptController_loadScript - Failed to match the given parameters to a valid function signature.");
  68. lua_error(state);
  69. break;
  70. }
  71. default:
  72. {
  73. lua_pushstring(state, "Invalid number of parameters (expected 2 or 3).");
  74. lua_error(state);
  75. break;
  76. }
  77. }
  78. return 0;
  79. }
  80. int lua_ScriptController_loadUrl(lua_State* state)
  81. {
  82. // Get the number of parameters.
  83. int paramCount = lua_gettop(state);
  84. // Attempt to match the parameters to a valid binding.
  85. switch (paramCount)
  86. {
  87. case 2:
  88. {
  89. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  90. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  91. {
  92. // Get parameter 1 off the stack.
  93. const char* param1 = ScriptUtil::getString(2, false);
  94. ScriptController* instance = getInstance(state);
  95. std::string result = instance->loadUrl(param1);
  96. // Push the return value onto the stack.
  97. lua_pushstring(state, result.c_str());
  98. return 1;
  99. }
  100. lua_pushstring(state, "lua_ScriptController_loadUrl - Failed to match the given parameters to a valid function signature.");
  101. lua_error(state);
  102. break;
  103. }
  104. default:
  105. {
  106. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  107. lua_error(state);
  108. break;
  109. }
  110. }
  111. return 0;
  112. }
  113. int lua_ScriptController_static_print(lua_State* state)
  114. {
  115. // Get the number of parameters.
  116. int paramCount = lua_gettop(state);
  117. // Attempt to match the parameters to a valid binding.
  118. switch (paramCount)
  119. {
  120. case 1:
  121. {
  122. do
  123. {
  124. if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL))
  125. {
  126. // Get parameter 1 off the stack.
  127. const char* param1 = ScriptUtil::getString(1, false);
  128. ScriptController::print(param1);
  129. return 0;
  130. }
  131. } while (0);
  132. lua_pushstring(state, "lua_ScriptController_static_print - Failed to match the given parameters to a valid function signature.");
  133. lua_error(state);
  134. break;
  135. }
  136. case 2:
  137. {
  138. do
  139. {
  140. if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL) &&
  141. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  142. {
  143. // Get parameter 1 off the stack.
  144. const char* param1 = ScriptUtil::getString(1, false);
  145. // Get parameter 2 off the stack.
  146. const char* param2 = ScriptUtil::getString(2, false);
  147. ScriptController::print(param1, param2);
  148. return 0;
  149. }
  150. } while (0);
  151. lua_pushstring(state, "lua_ScriptController_static_print - Failed to match the given parameters to a valid function signature.");
  152. lua_error(state);
  153. break;
  154. }
  155. default:
  156. {
  157. lua_pushstring(state, "Invalid number of parameters (expected 1 or 2).");
  158. lua_error(state);
  159. break;
  160. }
  161. }
  162. return 0;
  163. }
  164. }