Script.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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. /// Maximum function/method nesting level
  40. static const unsigned MAX_SCRIPT_NESTING_LEVEL = 32;
  41. /// Script subsystem. Allows execution of AngelScript
  42. class Script : public Object
  43. {
  44. OBJECT(Script);
  45. public:
  46. /// Construct
  47. Script(Context* context);
  48. /// Destruct. Release the AngelScript engine
  49. ~Script();
  50. /// Compile and execute a line of script in immediate mode
  51. bool Execute(const std::string& line);
  52. /// Perform garbage collection
  53. void GarbageCollect(bool fullCycle);
  54. /// Set immediate mode script file
  55. void SetDefaultScriptFile(ScriptFile* file);
  56. /// Set immediate mode scene
  57. void SetDefaultScene(Scene* scene);
  58. /// Set script engine logging mode, immediate is default
  59. void SetLogMode(ScriptLogMode mode);
  60. /// Clear retained mode log messages
  61. void ClearLogMessages();
  62. /// Print the whole script API (all registered classes, methods and properties) to the log
  63. void DumpAPI();
  64. /// Log a message from the script engine
  65. void MessageCallback(const asSMessageInfo* msg);
  66. /// Handle a script exception
  67. void ExceptionCallback(asIScriptContext* context);
  68. /// Return the AngelScript engine
  69. asIScriptEngine* GetScriptEngine() const { return scriptEngine_; }
  70. /// Return immediate execution script context
  71. asIScriptContext* GetImmediateContext() const { return immediateContext_; }
  72. /// Return a script function/method execution context for the current execution nesting level
  73. asIScriptContext* GetScriptFileContext() const;
  74. /// Return immediate mode script file
  75. ScriptFile* GetDefaultScriptFile() const;
  76. /// Return immediate mode scene
  77. Scene* GetDefaultScene() const;
  78. /// Query for an inbuilt object type by constant declaration. Can not be used for script types
  79. asIObjectType* GetObjectType(const char* declaration);
  80. /// Return logging mode
  81. ScriptLogMode GetLogMode() const { return logMode_; }
  82. /// Return retained mode log messages
  83. const std::string& GetLogMessages() const { return logMessages_; }
  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 script module to script file map
  91. std::map<asIScriptModule*, ScriptFile*>& GetModuleMap() { return moduleMap_; }
  92. /// Return script object to script instance map
  93. std::map<void*, ScriptInstance*>& GetObjectMap() { return objectMap_; }
  94. private:
  95. /// Output a sanitated row of script API
  96. void OutputAPIRow(const std::string& row, bool removeReference = false);
  97. /// AngelScript engine
  98. asIScriptEngine* scriptEngine_;
  99. /// Immediate execution script context
  100. asIScriptContext* immediateContext_;
  101. /// Immediate execution script file
  102. WeakPtr<ScriptFile> defaultScriptFile_;
  103. /// Immediate execution scene
  104. WeakPtr<Scene> defaultScene_;
  105. /// Script function/method execution contexts
  106. std::vector<asIScriptContext*> scriptFileContexts_;
  107. /// Map of script modules to script files
  108. std::map<asIScriptModule*, ScriptFile*> moduleMap_;
  109. /// Map of script objects to script instance components
  110. std::map<void*, ScriptInstance*> objectMap_;
  111. /// Search cache for inbuilt object types (constant declaration)
  112. std::map<const char*, asIObjectType*> objectTypes_;
  113. /// Script engine logging mode
  114. ScriptLogMode logMode_;
  115. /// Retained mode log messages
  116. std::string logMessages_;
  117. /// Current script execution nesting level
  118. unsigned scriptNestingLevel_;
  119. };
  120. /// Register Script library objects
  121. void RegisterScriptLibrary(Context* context);