Script.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // Copyright (c) 2008-2017 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/Mutex.h"
  24. #include "../Core/Object.h"
  25. class asIScriptContext;
  26. class asIScriptEngine;
  27. class asIScriptModule;
  28. class asITypeInfo;
  29. struct asSMessageInfo;
  30. namespace Urho3D
  31. {
  32. extern const char* LOGIC_CATEGORY;
  33. class ResourceRouter;
  34. class Scene;
  35. class ScriptFile;
  36. class ScriptInstance;
  37. /// Output mode for DumpAPI method.
  38. enum DumpMode
  39. {
  40. DOXYGEN = 0,
  41. C_HEADER,
  42. MAX_DUMP_MODES
  43. };
  44. /// Scripting subsystem. Allows execution of AngelScript.
  45. class URHO3D_API Script : public Object
  46. {
  47. URHO3D_OBJECT(Script, Object);
  48. friend class ScriptFile;
  49. public:
  50. /// Construct.
  51. Script(Context* context);
  52. /// Destruct. Release the AngelScript engine.
  53. virtual ~Script() override;
  54. /// Compile and execute a line of script in immediate mode.
  55. bool Execute(const String& line);
  56. /// Set immediate mode script file.
  57. void SetDefaultScriptFile(ScriptFile* file);
  58. /// Set immediate mode scene.
  59. void SetDefaultScene(Scene* scene);
  60. /// Set whether to execute engine console commands as script code.
  61. void SetExecuteConsoleCommands(bool enable);
  62. /// Print the whole script API (all registered classes, methods and properties) to the log. No-ops when URHO3D_LOGGING not defined.
  63. void DumpAPI(DumpMode mode = DOXYGEN, const String& sourceTree = String::EMPTY);
  64. /// Log a message from the script engine.
  65. void MessageCallback(const asSMessageInfo* msg);
  66. /// Handle a script exception.
  67. void ExceptionCallback(asIScriptContext* context);
  68. /// Get call stack.
  69. static String GetCallStack(asIScriptContext* context);
  70. /// Return the AngelScript engine.
  71. asIScriptEngine* GetScriptEngine() const { return scriptEngine_; }
  72. /// Return immediate execution script context.
  73. asIScriptContext* GetImmediateContext() const { return immediateContext_; }
  74. /// Return immediate mode script file.
  75. ScriptFile* GetDefaultScriptFile() const;
  76. /// Return immediate mode scene.
  77. Scene* GetDefaultScene() const;
  78. /// Return whether is executing engine console commands as script code.
  79. bool GetExecuteConsoleCommands() const { return executeConsoleCommands_; }
  80. /// Clear the inbuild object type cache.
  81. void ClearObjectTypeCache();
  82. /// Query for an inbuilt object type by constant declaration. Can not be used for script types.
  83. asITypeInfo* GetObjectType(const char* declaration);
  84. /// Return the script module create/delete mutex.
  85. Mutex& GetModuleMutex() { return moduleMutex_; }
  86. private:
  87. /// Increase script nesting level.
  88. void IncScriptNestingLevel() { ++scriptNestingLevel_; }
  89. /// Decrease script nesting level.
  90. void DecScriptNestingLevel() { --scriptNestingLevel_; }
  91. /// Return current script nesting level.
  92. unsigned GetScriptNestingLevel() { return scriptNestingLevel_; }
  93. /// Return a script function/method execution context for the current execution nesting level.
  94. asIScriptContext* GetScriptFileContext();
  95. /// Output a sanitated row of script API. No-ops when URHO3D_LOGGING not defined.
  96. void OutputAPIRow(DumpMode mode, const String& row, bool removeReference = false, const String& separator = ";");
  97. /// Handle a console command event.
  98. void HandleConsoleCommand(StringHash eventType, VariantMap& eventData);
  99. /// AngelScript engine.
  100. asIScriptEngine* scriptEngine_;
  101. /// Immediate execution script context.
  102. asIScriptContext* immediateContext_;
  103. /// Immediate execution script file.
  104. WeakPtr<ScriptFile> defaultScriptFile_;
  105. /// Immediate execution scene.
  106. WeakPtr<Scene> defaultScene_;
  107. /// Script function/method execution contexts.
  108. Vector<asIScriptContext*> scriptFileContexts_;
  109. /// Search cache for inbuilt object types.
  110. HashMap<const char*, asITypeInfo*> objectTypes_;
  111. /// AngelScript resource router.
  112. SharedPtr<ResourceRouter> router_;
  113. /// Script module create/delete mutex.
  114. Mutex moduleMutex_;
  115. /// Current script execution nesting level.
  116. unsigned scriptNestingLevel_;
  117. /// Flag for executing engine console commands as script code. Default to true.
  118. bool executeConsoleCommands_;
  119. };
  120. /// Register Script library objects.
  121. void URHO3D_API RegisterScriptLibrary(Context* context);
  122. }