Script.h 4.5 KB

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