LuaScript.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // Copyright (c) 2008-2020 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. URHO3D_OBJECT(LuaScript, Object);
  37. public:
  38. /// Construct.
  39. explicit LuaScript(Context* context);
  40. /// Destruct.
  41. ~LuaScript() override;
  42. /// Add a scripted event handler by function at the given stack index.
  43. void AddEventHandler(const String& eventName, int index) override;
  44. /// Add a scripted event handler by function name.
  45. void AddEventHandler(const String& eventName, const String& functionName) override;
  46. /// Add a scripted event handler by function at the given stack index for a specific sender.
  47. void AddEventHandler(Object* sender, const String& eventName, int index) override;
  48. /// Add a scripted event handler by function name for a specific sender.
  49. void AddEventHandler(Object* sender, const String& eventName, const String& functionName) override;
  50. /// Remove a scripted event handler.
  51. void RemoveEventHandler(const String& eventName) override;
  52. /// Remove a scripted event handler for a specific sender.
  53. void RemoveEventHandler(Object* sender, const String& eventName) override;
  54. /// Remove all scripted event handlers for a specific sender.
  55. void RemoveEventHandlers(Object* sender) override;
  56. /// Remove all scripted event handlers.
  57. void RemoveAllEventHandlers() override;
  58. /// Remove all scripted event handlers, except those listed.
  59. void RemoveEventHandlersExcept(const Vector<String>& exceptionNames) override;
  60. /// Return whether has subscribed to an event.
  61. bool HasEventHandler(const String& eventName) const override;
  62. /// Return whether has subscribed to a specific sender's event.
  63. bool HasEventHandler(Object* sender, const String& eventName) const override;
  64. /// Execute script file. Return true if successful.
  65. bool ExecuteFile(const String& fileName);
  66. /// Execute script string. Return true if successful.
  67. bool ExecuteString(const String& string);
  68. /// Load script file on file system (i.e. not from resource cache). Return true if successful.
  69. bool LoadRawFile(const String& fileName);
  70. /// Load and execute script file on file system (i.e. not from resource cache). Return true if successful.
  71. bool ExecuteRawFile(const String& fileName);
  72. /// Execute script function.
  73. bool ExecuteFunction(const String& functionName);
  74. /// Set whether to execute engine console commands as script code.
  75. void SetExecuteConsoleCommands(bool enable);
  76. /// Return Lua state.
  77. lua_State* GetState() const { return luaState_; }
  78. /// Return Lua function at the given stack index.
  79. LuaFunction* GetFunction(int index);
  80. /// Return Lua function by function name.
  81. LuaFunction* GetFunction(const String& functionName, bool silentIfNotFound = false);
  82. /// Return whether is executing engine console commands as script code.
  83. bool GetExecuteConsoleCommands() const { return executeConsoleCommands_; }
  84. /// Push Lua function to stack. Return true if is successful. Return false on any error and an error string is pushed instead.
  85. static bool PushLuaFunction(lua_State* L, const String& functionName);
  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. /// At panic.
  96. static int AtPanic(lua_State* L);
  97. /// Loader.
  98. static int Loader(lua_State* L);
  99. /// Print function.
  100. static int Print(lua_State* L);
  101. /// Lua state.
  102. lua_State* luaState_;
  103. /// Procedural event invoker.
  104. SharedPtr<LuaScriptEventInvoker> eventInvoker_;
  105. /// Coroutine update function.
  106. LuaFunction* coroutineUpdate_;
  107. /// Flag for executing engine console commands as script code. Default to true.
  108. bool executeConsoleCommands_;
  109. /// Function pointer to function map.
  110. HashMap<const void*, SharedPtr<LuaFunction> > functionPointerToFunctionMap_;
  111. /// Function name to function map.
  112. HashMap<String, SharedPtr<LuaFunction> > functionNameToFunctionMap_;
  113. };
  114. /// Register Lua script library objects.
  115. void URHO3D_API RegisterLuaScriptLibrary(Context* context);
  116. }