Script.h 5.7 KB

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