LuaScript.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Core/Context.h"
  24. #include "../Core/Object.h"
  25. #include "../LuaScript/LuaScriptEventListener.h"
  26. struct lua_State;
  27. namespace Urho3D
  28. {
  29. extern const char* LOGIC_CATEGORY;
  30. class LuaFunction;
  31. class LuaScriptEventInvoker;
  32. class Scene;
  33. /// Lua script subsystem.
  34. class URHO3D_API LuaScript : public Object, public LuaScriptEventListener
  35. {
  36. OBJECT(LuaScript);
  37. public:
  38. /// Construct.
  39. LuaScript(Context* context);
  40. /// Destruct.
  41. ~LuaScript();
  42. /// Add a scripted event handler by function.
  43. virtual void AddEventHandler(const String& eventName, int functionIndex);
  44. /// Add a scripted event handler by function name.
  45. virtual void AddEventHandler(const String& eventName, const String& functionName);
  46. /// Add a scripted event handler by function for a specific sender.
  47. virtual void AddEventHandler(Object* sender, const String& eventName, int functionIndex);
  48. /// Add a scripted event handler by function name for a specific sender.
  49. virtual void AddEventHandler(Object* sender, const String& eventName, const String& functionName);
  50. /// Remove a scripted event handler by function.
  51. virtual void RemoveEventHandler(const String& eventName, int functionIndex);
  52. /// Remove a scripted event handler by function name.
  53. virtual void RemoveEventHandler(const String& eventName, const String& functionName);
  54. /// Remove a scripted event handler.
  55. virtual void RemoveEventHandler(const String& eventName);
  56. /// Remove a scripted event handler for a specific sender by function.
  57. virtual void RemoveEventHandler(Object* sender, const String& eventName, int functionIndex);
  58. /// Remove a scripted event handler for a specific sender by function name.
  59. virtual void RemoveEventHandler(Object* sender, const String& eventName, const String& functionName);
  60. /// Remove a scripted event handler for a specific sender.
  61. virtual void RemoveEventHandler(Object* sender, const String& eventName);
  62. /// Remove all scripted event handlers for a specific sender.
  63. virtual void RemoveEventHandlers(Object* sender);
  64. /// Remove all scripted event handlers.
  65. virtual void RemoveAllEventHandlers();
  66. /// Remove all scripted event handlers, except those listed.
  67. virtual void RemoveEventHandlersExcept(const Vector<String>& exceptionNames);
  68. /// Execute script file. Return true if successful.
  69. bool ExecuteFile(const String& fileName);
  70. /// Execute script string. Return true if successful.
  71. bool ExecuteString(const String& string);
  72. /// Execute script function.
  73. bool ExecuteFunction(const String& functionName);
  74. /// Send event.
  75. void SendEvent(const String& eventName, VariantMap& eventData);
  76. /// Set whether to execute engine console commands as script code.
  77. void SetExecuteConsoleCommands(bool enable);
  78. /// Return Lua state.
  79. lua_State* GetState() const { return luaState_; }
  80. /// Return Lua function by function stack index.
  81. WeakPtr<LuaFunction> GetFunction(int functionIndex);
  82. /// Return Lua function by function name.
  83. WeakPtr<LuaFunction> GetFunction(const String& functionName, bool silentIfNotfound = false);
  84. /// Return whether is executing engine console commands as script code.
  85. bool GetExecuteConsoleCommands() const { return executeConsoleCommands_; }
  86. private:
  87. /// Register loader.
  88. void RegisterLoader();
  89. /// Replace print.
  90. void ReplacePrint();
  91. /// Handle post update.
  92. void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
  93. /// Handle a console command event.
  94. void HandleConsoleCommand(StringHash eventType, VariantMap& eventData);
  95. /// Push script function.
  96. bool PushScriptFunction(const String& functionName, bool silentIfNotfound = false);
  97. /// At panic.
  98. static int AtPanic(lua_State* L);
  99. /// Loader.
  100. static int Loader(lua_State* L);
  101. /// Print function.
  102. static int Print(lua_State* L);
  103. /// Lua state.
  104. lua_State* luaState_;
  105. /// Event invoker.
  106. SharedPtr<LuaScriptEventInvoker> eventInvoker_;
  107. /// Coroutine update function.
  108. WeakPtr<LuaFunction> coroutineUpdate_;
  109. /// Flag for executing engine console commands as script code. Default to true.
  110. bool executeConsoleCommands_;
  111. /// Function pointer to function map.
  112. HashMap<const void*, SharedPtr<LuaFunction> > functionPointerToFunctionMap_;
  113. /// Function name to function map.
  114. HashMap<String, SharedPtr<LuaFunction> > functionNameToFunctionMap_;
  115. };
  116. class LuaScriptEventInvoker : public Object
  117. {
  118. OBJECT(LuaScriptEventInvoker);
  119. public:
  120. /// Construct.
  121. LuaScriptEventInvoker(Context* context);
  122. /// Destruct.
  123. virtual ~LuaScriptEventInvoker();
  124. /// Add a scripted event handler.
  125. void AddEventHandler(Object* sender, const String& eventName, WeakPtr<LuaFunction> function);
  126. /// Remove a scripted event handler.
  127. void RemoveEventHandler(Object* sender, const String& eventName, WeakPtr<LuaFunction> function);
  128. /// Remove all scripted event handlers.
  129. void RemoveAllEventHandlers(Object* sender);
  130. /// Remove all scripted event handlers, except those listed.
  131. void RemoveEventHandlersExcept(const Vector<String>& exceptionNames);
  132. private:
  133. /// Handle script event in Lua script.
  134. void HandleLuaScriptEvent(StringHash eventType, VariantMap& eventData);
  135. typedef Vector<WeakPtr<LuaFunction> > LuaFunctionVector;
  136. typedef HashMap<StringHash, LuaFunctionVector> EventTypeToLuaFunctionVectorMap;
  137. /// Return event type to Lua function vector map.
  138. EventTypeToLuaFunctionVectorMap& GetEventTypeToLuaFunctionVectorMap(Object* sender)
  139. {
  140. if (!sender)
  141. return eventTypeToLuaFunctionVectorMap;
  142. return senderEventTypeToLuaFunctionVectorMap[sender];
  143. }
  144. /// Event type to Lua function vector map.
  145. EventTypeToLuaFunctionVectorMap eventTypeToLuaFunctionVectorMap;
  146. /// Event type to Lua function vector map for specific sender.
  147. HashMap<Object*, EventTypeToLuaFunctionVectorMap> senderEventTypeToLuaFunctionVectorMap;
  148. };
  149. /// Register Lua script library objects.
  150. void URHO3D_API RegisterLuaScriptLibrary(Context* context);
  151. }