Script.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 Scene;
  26. class ScriptFile;
  27. class ScriptInstance;
  28. class asIObjectType;
  29. class asIScriptContext;
  30. class asIScriptEngine;
  31. class asIScriptModule;
  32. struct asSMessageInfo;
  33. /// Script engine logging mode.
  34. enum ScriptLogMode
  35. {
  36. LOGMODE_IMMEDIATE = 0,
  37. LOGMODE_RETAINED
  38. };
  39. /// Scripting subsystem. Allows execution of AngelScript.
  40. class Script : public Object
  41. {
  42. OBJECT(Script);
  43. friend class ScriptFile;
  44. public:
  45. /// Construct.
  46. Script(Context* context);
  47. /// Destruct. Release the AngelScript engine.
  48. ~Script();
  49. /// Compile and execute a line of script in immediate mode.
  50. bool Execute(const String& line);
  51. /// Set immediate mode script file.
  52. void SetDefaultScriptFile(ScriptFile* file);
  53. /// Set immediate mode scene.
  54. void SetDefaultScene(Scene* scene);
  55. /// Set script engine logging mode, immediate is default.
  56. void SetLogMode(ScriptLogMode mode);
  57. /// Clear retained mode log messages.
  58. void ClearLogMessages();
  59. /// Print the whole script API (all registered classes, methods and properties) to the log.
  60. void DumpAPI();
  61. /// Log a message from the script engine.
  62. void MessageCallback(const asSMessageInfo* msg);
  63. /// Handle a script exception.
  64. void ExceptionCallback(asIScriptContext* context);
  65. /// Return the AngelScript engine.
  66. asIScriptEngine* GetScriptEngine() const { return scriptEngine_; }
  67. /// Return immediate execution script context.
  68. asIScriptContext* GetImmediateContext() const { return immediateContext_; }
  69. /// Return immediate mode script file.
  70. ScriptFile* GetDefaultScriptFile() const;
  71. /// Return immediate mode scene.
  72. Scene* GetDefaultScene() const;
  73. /// Clear the inbuild object type cache.
  74. void ClearObjectTypeCache();
  75. /// Query for an inbuilt object type by constant declaration. Can not be used for script types.
  76. asIObjectType* GetObjectType(const char* declaration);
  77. /// Return logging mode.
  78. ScriptLogMode GetLogMode() const { return logMode_; }
  79. /// Return retained mode log messages.
  80. const String& GetLogMessages() const { return logMessages_; }
  81. private:
  82. /// Increase script nesting level.
  83. void IncScriptNestingLevel() { ++scriptNestingLevel_; }
  84. /// Decrease script nesting level.
  85. void DecScriptNestingLevel() { --scriptNestingLevel_; }
  86. /// Return current script nesting level.
  87. unsigned GetScriptNestingLevel() { return scriptNestingLevel_; }
  88. /// Return a script function/method execution context for the current execution nesting level.
  89. asIScriptContext* GetScriptFileContext();
  90. /// Output a sanitated row of script API.
  91. void OutputAPIRow(const String& row, bool removeReference = false);
  92. /// AngelScript engine.
  93. asIScriptEngine* scriptEngine_;
  94. /// Immediate execution script context.
  95. asIScriptContext* immediateContext_;
  96. /// Immediate execution script file.
  97. WeakPtr<ScriptFile> defaultScriptFile_;
  98. /// Immediate execution scene.
  99. WeakPtr<Scene> defaultScene_;
  100. /// Script function/method execution contexts.
  101. Vector<asIScriptContext*> scriptFileContexts_;
  102. /// Search cache for inbuilt object types.
  103. HashMap<const char*, asIObjectType*> objectTypes_;
  104. /// Script engine logging mode.
  105. ScriptLogMode logMode_;
  106. /// Retained mode log messages.
  107. String logMessages_;
  108. /// Current script execution nesting level.
  109. unsigned scriptNestingLevel_;
  110. };
  111. /// Register Script library objects.
  112. void RegisterScriptLibrary(Context* context);