Script.h 5.2 KB

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