Script.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Object.h"
  25. class asIObjectType;
  26. class asIScriptContext;
  27. class asIScriptEngine;
  28. class asIScriptModule;
  29. struct asSMessageInfo;
  30. namespace Urho3D
  31. {
  32. class Scene;
  33. class ScriptFile;
  34. class ScriptInstance;
  35. /// Script engine logging mode.
  36. enum ScriptLogMode
  37. {
  38. LOGMODE_IMMEDIATE = 0,
  39. LOGMODE_RETAINED
  40. };
  41. /// Scripting subsystem. Allows execution of AngelScript.
  42. class Script : public Object
  43. {
  44. OBJECT(Script);
  45. friend class ScriptFile;
  46. public:
  47. /// Construct.
  48. Script(Context* context);
  49. /// Destruct. Release the AngelScript engine.
  50. ~Script();
  51. /// Compile and execute a line of script in immediate mode.
  52. bool Execute(const String& line);
  53. /// Set immediate mode script file.
  54. void SetDefaultScriptFile(ScriptFile* file);
  55. /// Set immediate mode scene.
  56. void SetDefaultScene(Scene* scene);
  57. /// Set script engine logging mode, immediate is default.
  58. void SetLogMode(ScriptLogMode mode);
  59. /// Clear retained mode log messages.
  60. void ClearLogMessages();
  61. /// Print the whole script API (all registered classes, methods and properties) to the log.
  62. void DumpAPI();
  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. /// Return the AngelScript engine.
  68. asIScriptEngine* GetScriptEngine() const { return scriptEngine_; }
  69. /// Return immediate execution script context.
  70. asIScriptContext* GetImmediateContext() const { return immediateContext_; }
  71. /// Return immediate mode script file.
  72. ScriptFile* GetDefaultScriptFile() const;
  73. /// Return immediate mode scene.
  74. Scene* GetDefaultScene() const;
  75. /// Clear the inbuild object type cache.
  76. void ClearObjectTypeCache();
  77. /// Query for an inbuilt object type by constant declaration. Can not be used for script types.
  78. asIObjectType* GetObjectType(const char* declaration);
  79. /// Return logging mode.
  80. ScriptLogMode GetLogMode() const { return logMode_; }
  81. /// Return retained mode log messages.
  82. const String& GetLogMessages() const { return logMessages_; }
  83. private:
  84. /// Increase script nesting level.
  85. void IncScriptNestingLevel() { ++scriptNestingLevel_; }
  86. /// Decrease script nesting level.
  87. void DecScriptNestingLevel() { --scriptNestingLevel_; }
  88. /// Return current script nesting level.
  89. unsigned GetScriptNestingLevel() { return scriptNestingLevel_; }
  90. /// Return a script function/method execution context for the current execution nesting level.
  91. asIScriptContext* GetScriptFileContext();
  92. /// Output a sanitated row of script API.
  93. void OutputAPIRow(const String& row, bool removeReference = false);
  94. /// AngelScript engine.
  95. asIScriptEngine* scriptEngine_;
  96. /// Immediate execution script context.
  97. asIScriptContext* immediateContext_;
  98. /// Immediate execution script file.
  99. WeakPtr<ScriptFile> defaultScriptFile_;
  100. /// Immediate execution scene.
  101. WeakPtr<Scene> defaultScene_;
  102. /// Script function/method execution contexts.
  103. Vector<asIScriptContext*> scriptFileContexts_;
  104. /// Search cache for inbuilt object types.
  105. HashMap<const char*, asIObjectType*> objectTypes_;
  106. /// Script engine logging mode.
  107. ScriptLogMode logMode_;
  108. /// Retained mode log messages.
  109. String logMessages_;
  110. /// Current script execution nesting level.
  111. unsigned scriptNestingLevel_;
  112. };
  113. /// Register Script library objects.
  114. void RegisterScriptLibrary(Context* context);
  115. }