LuaScript.pkg 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. $#include "LuaScript/LuaScript.h"
  2. void LuaScriptAddEventHandler @ SubscribeToEvent(const String eventName, void* functionOrFunctionName);
  3. void LuaScriptAddEventHandler @ SubscribeToEvent(void* sender, const String eventName, void* functionOrFunctionName);
  4. void LuaScriptRemoveEventHandler @ UnsubscribeFromEvent(const String eventName);
  5. void LuaScriptRemoveEventHandler @ UnsubscribeFromEvent(Object* sender, const String eventName);
  6. void LuaScriptRemoveEventHandlers @ UnsubscribeFromEvents(Object* sender);
  7. void LuaScriptRemoveAllEventHandlers @ UnsubscribeFromAllEvents();
  8. void LuaScriptRemoveEventHandlersExcept @ UnsubscribeFromAllEventsExcept(const Vector<String>& exceptionNames);
  9. bool LuaScriptHasSubscribedToEvent @ HasSubscribedToEvent(const String eventName);
  10. bool LuaScriptHasSubscribedToEvent @ HasSubscribedToEvent(Object* sender, const String eventName);
  11. void LuaScriptSendEvent @ SendEvent(const String eventName, VariantMap& eventData);
  12. void LuaScriptSetExecuteConsoleCommands @ SetExecuteConsoleCommands(bool enable);
  13. bool LuaScriptGetExecuteConsoleCommands @ GetExecuteConsoleCommands();
  14. void LuaScriptSetGlobalVar @ SetGlobalVar(const String key, Variant value);
  15. Variant LuaScriptGetGlobalVar @ GetGlobalVar(const String key);
  16. VariantMap& LuaScriptGetGlobalVars @ GetGlobalVars();
  17. void RegisterEventName(const String eventName);
  18. ${
  19. static void RegisterEventName(const String eventName)
  20. {
  21. EventNameRegistrar::RegisterEventName(eventName.CString());
  22. }
  23. static LuaScript* GetLuaScript(lua_State* L)
  24. {
  25. return GetContext(L)->GetSubsystem<LuaScript>();
  26. }
  27. #define LuaScriptAddEventHandler GetLuaScript(tolua_S)->AddEventHandler
  28. #define LuaScriptRemoveEventHandler GetLuaScript(tolua_S)->RemoveEventHandler
  29. #define LuaScriptRemoveEventHandlers GetLuaScript(tolua_S)->RemoveEventHandlers
  30. #define LuaScriptRemoveAllEventHandlers GetLuaScript(tolua_S)->RemoveAllEventHandlers
  31. #define LuaScriptRemoveEventHandlersExcept GetLuaScript(tolua_S)->RemoveEventHandlersExcept
  32. #define LuaScriptHasSubscribedToEvent GetLuaScript(tolua_S)->HasEventHandler
  33. #define LuaScriptSendEvent GetLuaScript(tolua_S)->SendEvent
  34. #define LuaScriptSetExecuteConsoleCommands GetLuaScript(tolua_S)->SetExecuteConsoleCommands
  35. #define LuaScriptGetExecuteConsoleCommands GetLuaScript(tolua_S)->GetExecuteConsoleCommands
  36. #define LuaScriptSetGlobalVar GetLuaScript(tolua_S)->SetGlobalVar
  37. #define LuaScriptGetGlobalVar GetLuaScript(tolua_S)->GetGlobalVar
  38. #define LuaScriptGetGlobalVars GetLuaScript(tolua_S)->GetGlobalVars
  39. static bool tolua_isfunctionorurho3dstring(lua_State* L, int lo, int def, tolua_Error* err)
  40. {
  41. return lua_isfunction(L, lo) || tolua_isurho3dstring(L, lo, def, err);
  42. }
  43. static int tolua_LuaScriptLuaAPI_SubscribeToEvent(lua_State* tolua_S)
  44. {
  45. int args = lua_gettop(tolua_S);
  46. tolua_Error tolua_err;
  47. #ifndef TOLUA_RELEASE
  48. if (args == 2)
  49. {
  50. // SubscribeToEvent(const String eventName, void* functionOrFunctionName);
  51. if (!tolua_isurho3dstring(tolua_S,1,0,&tolua_err) ||
  52. !tolua_isfunctionorurho3dstring(tolua_S,2,0,&tolua_err))
  53. goto tolua_lerror;
  54. }
  55. else if (args == 3)
  56. {
  57. // SubscribeToEvent(Object* sender, const String eventName, void* functionOrFunctionName);
  58. if (!tolua_isuserdata(tolua_S,1,0,&tolua_err) ||
  59. !tolua_isurho3dstring(tolua_S,2,0,&tolua_err) ||
  60. !tolua_isfunctionorurho3dstring(tolua_S,3,0,&tolua_err))
  61. goto tolua_lerror;
  62. }
  63. else
  64. goto tolua_lerror;
  65. #endif
  66. if (args == 2)
  67. {
  68. // SubscribeToEvent(const String eventName, void* functionOrFunctionName);
  69. const String eventName = ((const String) tolua_tourho3dstring(tolua_S,1,0));
  70. if (lua_isfunction(tolua_S,2))
  71. LuaScriptAddEventHandler(eventName,2);
  72. else
  73. {
  74. const String functionName = (const String)tolua_tourho3dstring(tolua_S,2,0);
  75. LuaScriptAddEventHandler(eventName,functionName);
  76. }
  77. }
  78. else if (args == 3)
  79. {
  80. // SubscribeToEvent(Object* sender, const String eventName, void* functionOrFunctionName);
  81. Object* sender = ((Object*) tolua_touserdata(tolua_S,1,0));
  82. const String eventName = ((const String) tolua_tourho3dstring(tolua_S,2,0));
  83. if (lua_isfunction(tolua_S,3))
  84. LuaScriptAddEventHandler(sender,eventName,3);
  85. else
  86. {
  87. const String functionName = (const String)tolua_tourho3dstring(tolua_S,3,0);
  88. LuaScriptAddEventHandler(sender,eventName,functionName);
  89. }
  90. }
  91. return 0;
  92. #ifndef TOLUA_RELEASE
  93. tolua_lerror:
  94. tolua_error(tolua_S,"#ferror in function 'SubscribeToEvent'.",&tolua_err);
  95. return 0;
  96. #endif
  97. }
  98. #define TOLUA_DISABLE_tolua_LuaScriptLuaAPI_SubscribeToEvent00
  99. static int tolua_LuaScriptLuaAPI_SubscribeToEvent00(lua_State* tolua_S)
  100. {
  101. return tolua_LuaScriptLuaAPI_SubscribeToEvent(tolua_S);
  102. }
  103. #define TOLUA_DISABLE_tolua_LuaScriptLuaAPI_SubscribeToEvent01
  104. static int tolua_LuaScriptLuaAPI_SubscribeToEvent01(lua_State* tolua_S)
  105. {
  106. return tolua_LuaScriptLuaAPI_SubscribeToEvent(tolua_S);
  107. }
  108. static int tolua_LuaScriptLuaAPI_UnsubscribeFromEvent(lua_State* tolua_S)
  109. {
  110. int args = lua_gettop(tolua_S);
  111. tolua_Error tolua_err;
  112. #ifndef TOLUA_RELEASE
  113. if (args == 1)
  114. {
  115. // UnsubscribeFromEvent(const String eventName);
  116. if (!tolua_isurho3dstring(tolua_S,1,0,&tolua_err))
  117. goto tolua_lerror;
  118. }
  119. else if (args == 2)
  120. {
  121. // UnsubscribeFromEvent(Object* sender, const String eventName);
  122. if (!tolua_isuserdata(tolua_S,1,0,&tolua_err) ||
  123. !tolua_isurho3dstring(tolua_S,2,0,&tolua_err))
  124. goto tolua_lerror;
  125. }
  126. else
  127. goto tolua_lerror;
  128. #endif
  129. if (args == 1)
  130. {
  131. // UnsubscribeFromEvent(const String eventName);
  132. const String eventName = ((const String) tolua_tourho3dstring(tolua_S,1,0));
  133. LuaScriptRemoveEventHandler(eventName);
  134. }
  135. else if (args == 2)
  136. {
  137. // UnsubscribeFromEvent(Object* sender, const String eventName);
  138. Object* sender = ((Object*) tolua_touserdata(tolua_S,1,0));
  139. const String eventName = ((const String) tolua_tourho3dstring(tolua_S,2,0));
  140. LuaScriptRemoveEventHandler(sender, eventName);
  141. }
  142. return 0;
  143. #ifndef TOLUA_RELEASE
  144. tolua_lerror:
  145. tolua_error(tolua_S,"#ferror in function 'UnsubscribeFromEvent'.",&tolua_err);
  146. return 0;
  147. #endif
  148. }
  149. #define TOLUA_DISABLE_tolua_LuaScriptLuaAPI_UnsubscribeFromEvent00
  150. static int tolua_LuaScriptLuaAPI_UnsubscribeFromEvent00(lua_State* tolua_S)
  151. {
  152. return tolua_LuaScriptLuaAPI_UnsubscribeFromEvent(tolua_S);
  153. }
  154. #define TOLUA_DISABLE_tolua_LuaScriptLuaAPI_UnsubscribeFromEvent01
  155. static int tolua_LuaScriptLuaAPI_UnsubscribeFromEvent01(lua_State* tolua_S)
  156. {
  157. return tolua_LuaScriptLuaAPI_UnsubscribeFromEvent(tolua_S);
  158. }
  159. $}