lua_ScriptTarget.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. gameplay::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*)((gameplay::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 = gameplay::ScriptUtil::getString(2, true);
  42. // Get parameter 2 off the stack.
  43. std::string param2 = gameplay::ScriptUtil::getString(3, true);
  44. ScriptTarget* instance = getInstance(state);
  45. instance->addScriptCallback(param1, param2);
  46. return 0;
  47. }
  48. lua_pushstring(state, "lua_ScriptTarget_addScriptCallback - Failed to match the given parameters to a valid function signature.");
  49. lua_error(state);
  50. break;
  51. }
  52. default:
  53. {
  54. lua_pushstring(state, "Invalid number of parameters (expected 3).");
  55. lua_error(state);
  56. break;
  57. }
  58. }
  59. return 0;
  60. }
  61. int lua_ScriptTarget_removeScriptCallback(lua_State* state)
  62. {
  63. // Get the number of parameters.
  64. int paramCount = lua_gettop(state);
  65. // Attempt to match the parameters to a valid binding.
  66. switch (paramCount)
  67. {
  68. case 3:
  69. {
  70. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  71. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) &&
  72. (lua_type(state, 3) == LUA_TSTRING || lua_type(state, 3) == LUA_TNIL))
  73. {
  74. // Get parameter 1 off the stack.
  75. std::string param1 = gameplay::ScriptUtil::getString(2, true);
  76. // Get parameter 2 off the stack.
  77. std::string param2 = gameplay::ScriptUtil::getString(3, true);
  78. ScriptTarget* instance = getInstance(state);
  79. instance->removeScriptCallback(param1, param2);
  80. return 0;
  81. }
  82. lua_pushstring(state, "lua_ScriptTarget_removeScriptCallback - Failed to match the given parameters to a valid function signature.");
  83. lua_error(state);
  84. break;
  85. }
  86. default:
  87. {
  88. lua_pushstring(state, "Invalid number of parameters (expected 3).");
  89. lua_error(state);
  90. break;
  91. }
  92. }
  93. return 0;
  94. }
  95. }