lua_ScriptTarget.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "Base.h"
  2. #include "ScriptController.h"
  3. #include "lua_ScriptTarget.h"
  4. #include "Base.h"
  5. #include "ScriptController.h"
  6. #include "ScriptTarget.h"
  7. namespace gameplay
  8. {
  9. void luaRegister_ScriptTarget()
  10. {
  11. const luaL_Reg lua_members[] =
  12. {
  13. {"addScriptCallback", lua_ScriptTarget_addScriptCallback},
  14. {"removeScriptCallback", lua_ScriptTarget_removeScriptCallback},
  15. {NULL, NULL}
  16. };
  17. const luaL_Reg* lua_statics = NULL;
  18. std::vector<std::string> scopePath;
  19. ScriptUtil::registerClass("ScriptTarget", lua_members, NULL, NULL, lua_statics, scopePath);
  20. }
  21. static ScriptTarget* getInstance(lua_State* state)
  22. {
  23. void* userdata = luaL_checkudata(state, 1, "ScriptTarget");
  24. luaL_argcheck(state, userdata != NULL, 1, "'ScriptTarget' expected.");
  25. return (ScriptTarget*)((ScriptUtil::LuaObject*)userdata)->instance;
  26. }
  27. int lua_ScriptTarget_addScriptCallback(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 3:
  35. {
  36. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  37. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) &&
  38. (lua_type(state, 3) == LUA_TSTRING || lua_type(state, 3) == LUA_TNIL))
  39. {
  40. // Get parameter 1 off the stack.
  41. std::string param1 = ScriptUtil::getString(2, true);
  42. // Get parameter 2 off the stack.
  43. std::string param2 = ScriptUtil::getString(3, true);
  44. ScriptTarget* instance = getInstance(state);
  45. instance->addScriptCallback(param1, param2);
  46. return 0;
  47. }
  48. else
  49. {
  50. lua_pushstring(state, "lua_ScriptTarget_addScriptCallback - Failed to match the given parameters to a valid function signature.");
  51. lua_error(state);
  52. }
  53. break;
  54. }
  55. default:
  56. {
  57. lua_pushstring(state, "Invalid number of parameters (expected 3).");
  58. lua_error(state);
  59. break;
  60. }
  61. }
  62. return 0;
  63. }
  64. int lua_ScriptTarget_removeScriptCallback(lua_State* state)
  65. {
  66. // Get the number of parameters.
  67. int paramCount = lua_gettop(state);
  68. // Attempt to match the parameters to a valid binding.
  69. switch (paramCount)
  70. {
  71. case 3:
  72. {
  73. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  74. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) &&
  75. (lua_type(state, 3) == LUA_TSTRING || lua_type(state, 3) == LUA_TNIL))
  76. {
  77. // Get parameter 1 off the stack.
  78. std::string param1 = ScriptUtil::getString(2, true);
  79. // Get parameter 2 off the stack.
  80. std::string param2 = ScriptUtil::getString(3, true);
  81. ScriptTarget* instance = getInstance(state);
  82. instance->removeScriptCallback(param1, param2);
  83. return 0;
  84. }
  85. else
  86. {
  87. lua_pushstring(state, "lua_ScriptTarget_removeScriptCallback - Failed to match the given parameters to a valid function signature.");
  88. lua_error(state);
  89. }
  90. break;
  91. }
  92. default:
  93. {
  94. lua_pushstring(state, "Invalid number of parameters (expected 3).");
  95. lua_error(state);
  96. break;
  97. }
  98. }
  99. return 0;
  100. }
  101. }