LuaScript.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // Copyright (c) 2008-2013 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 "Object.h"
  24. struct lua_State;
  25. namespace Urho3D
  26. {
  27. extern const char* LOGIC_CATEGORY;
  28. class Scene;
  29. /// Lua script subsystem.
  30. class LuaScript : public Object
  31. {
  32. OBJECT(LuaScript);
  33. public:
  34. /// Construct.
  35. LuaScript(Context* context);
  36. /// Destruct. Release the AngelLuna engine.
  37. ~LuaScript();
  38. /// Execute lua script file.
  39. bool ExecuteFile(const char* fileName);
  40. /// Execute lua string.
  41. bool ExecuteString(const char* string);
  42. /// Execute lua function.
  43. bool ExecuteFunction(const char* funcName);
  44. /// Return the lua state.
  45. lua_State* GetLuaState() const { return luaState_; }
  46. /// Scene default scene.
  47. void SetDefaultScene(Scene* scene);
  48. /// Return default scene.
  49. Scene* GetDefaultScene() const;
  50. /// Subscribe lua event.
  51. void SubscribeLuaEvent(const char* event, const char* function);
  52. private:
  53. /// Replace print function.
  54. void ReplacePrintFunction();
  55. /// Print function.
  56. static int Print(lua_State* L);
  57. /// PCall callback.
  58. static int PCallCallback(lua_State* L);
  59. /// Handle event.
  60. void HandleEvent(StringHash eventType, VariantMap& eventData);
  61. /// Handle a console command event.
  62. void HandleConsoleCommand(StringHash eventType, VariantMap& eventData);
  63. private:
  64. /// Lua state.
  65. lua_State* luaState_;
  66. /// Default scene.
  67. WeakPtr<Scene> defaultScene_;
  68. /// Event type to Lua function name map.
  69. HashMap<StringHash, Vector<String> > eventFunctionMap_;
  70. };
  71. Context* GetLuaScriptContext();
  72. void SubscribeToEvent(const char* eventType, const char* funcName);
  73. }