lua_ScriptController.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 = NULL;
  18. std::vector<std::string> scopePath;
  19. ScriptUtil::registerClass("ScriptController", lua_members, NULL, NULL, lua_statics, scopePath);
  20. }
  21. static ScriptController* getInstance(lua_State* state)
  22. {
  23. void* userdata = luaL_checkudata(state, 1, "ScriptController");
  24. luaL_argcheck(state, userdata != NULL, 1, "'ScriptController' expected.");
  25. return (ScriptController*)((ScriptUtil::LuaObject*)userdata)->instance;
  26. }
  27. int lua_ScriptController_loadScript(lua_State* state)
  28. {
  29. // Get the number of parameters.
  30. int paramCount = lua_gettop(state);
  31. // Attempt to match the parameters to a valid binding.
  32. switch (paramCount)
  33. {
  34. case 2:
  35. {
  36. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  37. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  38. {
  39. // Get parameter 1 off the stack.
  40. const char* param1 = ScriptUtil::getString(2, false);
  41. ScriptController* instance = getInstance(state);
  42. instance->loadScript(param1);
  43. return 0;
  44. }
  45. else
  46. {
  47. lua_pushstring(state, "lua_ScriptController_loadScript - Failed to match the given parameters to a valid function signature.");
  48. lua_error(state);
  49. }
  50. break;
  51. }
  52. case 3:
  53. {
  54. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  55. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) &&
  56. lua_type(state, 3) == LUA_TBOOLEAN)
  57. {
  58. // Get parameter 1 off the stack.
  59. const char* param1 = ScriptUtil::getString(2, false);
  60. // Get parameter 2 off the stack.
  61. bool param2 = ScriptUtil::luaCheckBool(state, 3);
  62. ScriptController* instance = getInstance(state);
  63. instance->loadScript(param1, param2);
  64. return 0;
  65. }
  66. else
  67. {
  68. lua_pushstring(state, "lua_ScriptController_loadScript - Failed to match the given parameters to a valid function signature.");
  69. lua_error(state);
  70. }
  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 = 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. else
  103. {
  104. lua_pushstring(state, "lua_ScriptController_loadUrl - Failed to match the given parameters to a valid function signature.");
  105. lua_error(state);
  106. }
  107. break;
  108. }
  109. default:
  110. {
  111. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  112. lua_error(state);
  113. break;
  114. }
  115. }
  116. return 0;
  117. }
  118. }