LuaScript.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // Copyright (c) 2008-2014 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 "Context.h"
  24. #include "Object.h"
  25. struct lua_State;
  26. namespace Urho3D
  27. {
  28. extern const char* LOGIC_CATEGORY;
  29. class LuaFunction;
  30. class Scene;
  31. /// Lua script subsystem.
  32. class URHO3D_API LuaScript : public Object
  33. {
  34. OBJECT(LuaScript);
  35. public:
  36. /// Construct.
  37. LuaScript(Context* context);
  38. /// Destruct.
  39. ~LuaScript();
  40. /// Execute script file. Return true if successful.
  41. bool ExecuteFile(const String& fileName);
  42. /// Execute script string. Return true if successful.
  43. bool ExecuteString(const String& string);
  44. /// Execute script function.
  45. bool ExecuteFunction(const String& functionName);
  46. /// Script send event.
  47. void ScriptSendEvent(const String& eventName, VariantMap& eventData);
  48. /// Script subscribe to an event that can by send by any sender.
  49. void ScriptSubscribeToEvent(const String& eventName, int functionIndex);
  50. /// Script subscribe to an event that can by send by any sender.
  51. void ScriptSubscribeToEvent(const String& eventName, const String& functionName);
  52. /// Script unsubscribe from an event.
  53. void ScriptUnsubscribeFromEvent(const String& eventName);
  54. /// Script unsubscribe from an event.
  55. void ScriptUnsubscribeFromEvent(const String& eventName, int functionIndex);
  56. /// Script unsubscribe from an event.
  57. void ScriptUnsubscribeFromEvent(const String& eventName, const String& functionName);
  58. /// Script unsubscribe from all events.
  59. void ScriptUnsubscribeFromAllEvents();
  60. /// Script subscribe to a specific sender's event.
  61. void ScriptSubscribeToEvent(void* sender, const String& eventName, int functionIndex);
  62. /// Script subscribe to a specific sender's event.
  63. void ScriptSubscribeToEvent(void* sender, const String& eventName, const String& functionName);
  64. /// Script unsubscribe from a specific sender's event.
  65. void ScriptUnsubscribeFromEvent(void* sender, const String& eventName);
  66. /// Script unsubscribe from a specific sender's event.
  67. void ScriptUnsubscribeFromEvent(void* sender, const String& eventName, int functionIndex);
  68. /// Script unsubscribe from a specific sender's event.
  69. void ScriptUnsubscribeFromEvent(void* sender, const String& eventName, const String& functionName);
  70. /// Script unsubscribe from a specific sender's all events.
  71. void ScriptUnsubscribeFromEvents(void* sender);
  72. /// Set whether to execute engine console commands as script code.
  73. void SetExecuteConsoleCommands(bool enable);
  74. /// Return Lua state.
  75. lua_State* GetState() const { return luaState_; }
  76. /// Return Lua function by function stack index.
  77. WeakPtr<LuaFunction> GetFunction(int functionIndex);
  78. /// Return Lua function by function name.
  79. WeakPtr<LuaFunction> GetFunction(const String& functionName, bool silentIfNotfound = false);
  80. /// Return whether is executing engine console commands as script code.
  81. bool GetExecuteConsoleCommands() const { return executeConsoleCommands_; }
  82. private:
  83. /// Register loader.
  84. void RegisterLoader();
  85. /// Replace print.
  86. void ReplacePrint();
  87. /// Handle event.
  88. void HandleEvent(StringHash eventType, VariantMap& eventData);
  89. /// Handle object event.
  90. void HandleObjectEvent(StringHash eventType, VariantMap& eventData);
  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. /// Coroutine update function.
  106. WeakPtr<LuaFunction> coroutineUpdate_;
  107. /// Function pointer to function map.
  108. HashMap<const void*, SharedPtr<LuaFunction> > functionPointerToFunctionMap_;
  109. /// Function name to function map.
  110. HashMap<String, SharedPtr<LuaFunction> > functionNameToFunctionMap_;
  111. /// Typedef Lua function vector.
  112. typedef Vector<WeakPtr<LuaFunction> > LuaFunctionVector;
  113. /// Event handle functions.
  114. HashMap<StringHash, LuaFunctionVector> eventHandleFunctions_;
  115. /// Object event handle functions.
  116. HashMap<Object*, HashMap<StringHash, LuaFunctionVector> > objectHandleFunctions_;
  117. /// Internally used events, which should not be unsubscribed from.
  118. PODVector<StringHash> internalEvents_;
  119. /// Flag for executing engine console commands as script code. Default to true.
  120. bool executeConsoleCommands_;
  121. };
  122. /// Register Lua script library objects.
  123. void URHO3D_API RegisterLuaScriptLibrary(Context* context);
  124. }