LuaScript.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.
  51. virtual void RemoveEventHandler(const String& eventName);
  52. /// Remove a scripted event handler for a specific sender.
  53. virtual void RemoveEventHandler(Object* sender, const String& eventName);
  54. /// Remove all scripted event handlers for a specific sender.
  55. virtual void RemoveEventHandlers(Object* sender);
  56. /// Remove all scripted event handlers.
  57. virtual void RemoveAllEventHandlers();
  58. /// Remove all scripted event handlers, except those listed.
  59. virtual void RemoveEventHandlersExcept(const Vector<String>& exceptionNames);
  60. /// Execute script file. Return true if successful.
  61. bool ExecuteFile(const String& fileName);
  62. /// Execute script string. Return true if successful.
  63. bool ExecuteString(const String& string);
  64. /// Execute script function.
  65. bool ExecuteFunction(const String& functionName);
  66. /// Send event.
  67. void SendEvent(const String& eventName, VariantMap& eventData);
  68. /// Set whether to execute engine console commands as script code.
  69. void SetExecuteConsoleCommands(bool enable);
  70. /// Return Lua state.
  71. lua_State* GetState() const { return luaState_; }
  72. /// Return Lua function by function stack index.
  73. WeakPtr<LuaFunction> GetFunction(int functionIndex);
  74. /// Return Lua function by function name.
  75. WeakPtr<LuaFunction> GetFunction(const String& functionName, bool silentIfNotfound = false);
  76. /// Return whether is executing engine console commands as script code.
  77. bool GetExecuteConsoleCommands() const { return executeConsoleCommands_; }
  78. private:
  79. /// Register loader.
  80. void RegisterLoader();
  81. /// Replace print.
  82. void ReplacePrint();
  83. /// Handle post update.
  84. void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
  85. /// Handle a console command event.
  86. void HandleConsoleCommand(StringHash eventType, VariantMap& eventData);
  87. /// Push script function.
  88. bool PushScriptFunction(const String& functionName, bool silentIfNotfound = false);
  89. /// At panic.
  90. static int AtPanic(lua_State* L);
  91. /// Loader.
  92. static int Loader(lua_State* L);
  93. /// Print function.
  94. static int Print(lua_State* L);
  95. /// Lua state.
  96. lua_State* luaState_;
  97. /// Event invoker.
  98. SharedPtr<LuaScriptEventInvoker> eventInvoker_;
  99. /// Coroutine update function.
  100. WeakPtr<LuaFunction> coroutineUpdate_;
  101. /// Flag for executing engine console commands as script code. Default to true.
  102. bool executeConsoleCommands_;
  103. /// Function pointer to function map.
  104. HashMap<const void*, SharedPtr<LuaFunction> > functionPointerToFunctionMap_;
  105. /// Function name to function map.
  106. HashMap<String, SharedPtr<LuaFunction> > functionNameToFunctionMap_;
  107. };
  108. /// Register Lua script library objects.
  109. void URHO3D_API RegisterLuaScriptLibrary(Context* context);
  110. }