LuaScript.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Core/Context.h"
  5. #include "../Core/Object.h"
  6. #include "../LuaScript/LuaScriptEventListener.h"
  7. struct lua_State;
  8. namespace Urho3D
  9. {
  10. extern const char* LOGIC_CATEGORY;
  11. class LuaFunction;
  12. class LuaScriptEventInvoker;
  13. class Scene;
  14. /// Lua script subsystem.
  15. class URHO3D_API LuaScript : public Object, public LuaScriptEventListener
  16. {
  17. URHO3D_OBJECT(LuaScript, Object);
  18. public:
  19. /// Construct.
  20. explicit LuaScript(Context* context);
  21. /// Destruct.
  22. ~LuaScript() override;
  23. /// Add a scripted event handler by function at the given stack index.
  24. void AddEventHandler(const String& eventName, int index) override;
  25. /// Add a scripted event handler by function name.
  26. void AddEventHandler(const String& eventName, const String& functionName) override;
  27. /// Add a scripted event handler by function at the given stack index for a specific sender.
  28. void AddEventHandler(Object* sender, const String& eventName, int index) override;
  29. /// Add a scripted event handler by function name for a specific sender.
  30. void AddEventHandler(Object* sender, const String& eventName, const String& functionName) override;
  31. /// Remove a scripted event handler.
  32. void RemoveEventHandler(const String& eventName) override;
  33. /// Remove a scripted event handler for a specific sender.
  34. void RemoveEventHandler(Object* sender, const String& eventName) override;
  35. /// Remove all scripted event handlers for a specific sender.
  36. void RemoveEventHandlers(Object* sender) override;
  37. /// Remove all scripted event handlers.
  38. void RemoveAllEventHandlers() override;
  39. /// Remove all scripted event handlers, except those listed.
  40. void RemoveEventHandlersExcept(const Vector<String>& exceptionNames) override;
  41. /// Return whether has subscribed to an event.
  42. bool HasEventHandler(const String& eventName) const override;
  43. /// Return whether has subscribed to a specific sender's event.
  44. bool HasEventHandler(Object* sender, const String& eventName) const override;
  45. /// Execute script file. Return true if successful.
  46. bool ExecuteFile(const String& fileName);
  47. /// Execute script string. Return true if successful.
  48. bool ExecuteString(const String& string);
  49. /// Load script file on file system (i.e. not from resource cache). Return true if successful.
  50. bool LoadRawFile(const String& fileName);
  51. /// Load and execute script file on file system (i.e. not from resource cache). Return true if successful.
  52. bool ExecuteRawFile(const String& fileName);
  53. /// Execute script function.
  54. bool ExecuteFunction(const String& functionName);
  55. /// Set whether to execute engine console commands as script code.
  56. void SetExecuteConsoleCommands(bool enable);
  57. /// Return Lua state.
  58. lua_State* GetState() const { return luaState_; }
  59. /// Return Lua function at the given stack index.
  60. LuaFunction* GetFunction(int index);
  61. /// Return Lua function by function name.
  62. LuaFunction* GetFunction(const String& functionName, bool silentIfNotFound = false);
  63. /// Return whether is executing engine console commands as script code.
  64. bool GetExecuteConsoleCommands() const { return executeConsoleCommands_; }
  65. /// Push Lua function to stack. Return true if is successful. Return false on any error and an error string is pushed instead.
  66. static bool PushLuaFunction(lua_State* L, const String& functionName);
  67. private:
  68. /// Register loader.
  69. void RegisterLoader();
  70. /// Replace print.
  71. void ReplacePrint();
  72. /// Handle post update.
  73. void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
  74. /// Handle a console command event.
  75. void HandleConsoleCommand(StringHash eventType, VariantMap& eventData);
  76. /// At panic.
  77. static int AtPanic(lua_State* L);
  78. /// Loader.
  79. static int Loader(lua_State* L);
  80. /// Print function.
  81. static int Print(lua_State* L);
  82. /// Lua state.
  83. lua_State* luaState_;
  84. /// Procedural event invoker.
  85. SharedPtr<LuaScriptEventInvoker> eventInvoker_;
  86. /// Coroutine update function.
  87. LuaFunction* coroutineUpdate_;
  88. /// Flag for executing engine console commands as script code. Default to true.
  89. bool executeConsoleCommands_;
  90. /// Function pointer to function map.
  91. HashMap<const void*, SharedPtr<LuaFunction>> functionPointerToFunctionMap_;
  92. /// Function name to function map.
  93. HashMap<String, SharedPtr<LuaFunction>> functionNameToFunctionMap_;
  94. };
  95. /// Register Lua script library objects.
  96. void URHO3D_API RegisterLuaScriptLibrary(Context* context);
  97. }