Script.h 4.6 KB

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