LuaScript.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 at the given stack index.
  43. virtual void AddEventHandler(const String& eventName, int index);
  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 at the given stack index for a specific sender.
  47. virtual void AddEventHandler(Object* sender, const String& eventName, int index);
  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. /// Load script file on file system (i.e. not from resource cache). Return true if successful.
  65. bool LoadRawFile(const String& fileName);
  66. /// Load and execute script file on file system (i.e. not from resource cache). Return true if successful.
  67. bool ExecuteRawFile(const String& fileName);
  68. /// Execute script function.
  69. bool ExecuteFunction(const String& functionName);
  70. /// Set whether to execute engine console commands as script code.
  71. void SetExecuteConsoleCommands(bool enable);
  72. /// Return Lua state.
  73. lua_State* GetState() const { return luaState_; }
  74. /// Return Lua function at the given stack index.
  75. LuaFunction* GetFunction(int index);
  76. /// Return Lua function by function name.
  77. LuaFunction* GetFunction(const String& functionName, bool silentIfNotFound = false);
  78. /// Return whether is executing engine console commands as script code.
  79. bool GetExecuteConsoleCommands() const { return executeConsoleCommands_; }
  80. private:
  81. /// Register loader.
  82. void RegisterLoader();
  83. /// Replace print.
  84. void ReplacePrint();
  85. /// Handle post update.
  86. void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
  87. /// Handle a console command event.
  88. void HandleConsoleCommand(StringHash eventType, VariantMap& eventData);
  89. /// Push script function to stack.
  90. bool PushScriptFunction(const String& functionName, bool silentIfNotFound = false);
  91. /// At panic.
  92. static int AtPanic(lua_State* L);
  93. /// Loader.
  94. static int Loader(lua_State* L);
  95. /// Print function.
  96. static int Print(lua_State* L);
  97. /// Lua state.
  98. lua_State* luaState_;
  99. /// Procedural event invoker.
  100. SharedPtr<LuaScriptEventInvoker> eventInvoker_;
  101. /// Coroutine update function.
  102. LuaFunction* coroutineUpdate_;
  103. /// Flag for executing engine console commands as script code. Default to true.
  104. bool executeConsoleCommands_;
  105. /// Function pointer to function map.
  106. HashMap<const void*, SharedPtr<LuaFunction> > functionPointerToFunctionMap_;
  107. /// Function name to function map.
  108. HashMap<String, SharedPtr<LuaFunction> > functionNameToFunctionMap_;
  109. };
  110. /// Register Lua script library objects.
  111. void URHO3D_API RegisterLuaScriptLibrary(Context* context);
  112. }