ScriptEngine.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #ifndef SCRIPT_SCRIPTENGINE_H
  24. #define SCRIPT_SCRIPTENGINE_H
  25. #include "SharedPtr.h"
  26. class ScriptFile;
  27. class asIScriptEngine;
  28. class asIScriptContext;
  29. struct asSMessageInfo;
  30. //! Script engine logging mode
  31. enum ScriptLogMode
  32. {
  33. LOGMODE_IMMEDIATE = 0,
  34. LOGMODE_RETAINED
  35. };
  36. //! Maximum function/method nesting level
  37. static const unsigned MAX_SCRIPT_NESTING_LEVEL = 32;
  38. //! Utilizes the AngelScript library for executing scripts
  39. class ScriptEngine : public RefCounted
  40. {
  41. public:
  42. //! Construct. Create the AngelScript engine and register string & array classes
  43. ScriptEngine();
  44. //! Destruct. Release the AngelScript engine
  45. ~ScriptEngine();
  46. //! Compile and execute a line of script in immediate mode
  47. bool execute(const std::string& line);
  48. //! Perform garbage collection
  49. void garbageCollect(bool fullCycle);
  50. //! Set immediate mode script file
  51. void setImmediateScriptFile(ScriptFile* file);
  52. //! Set script engine logging mode, immediate is default
  53. void setLogMode(ScriptLogMode mode);
  54. //! Clear retained mode log messages
  55. void clearLogMessages();
  56. //! Log a message from the script engine
  57. void logMessage(const asSMessageInfo* msg);
  58. //! Return the AngelScript engine
  59. asIScriptEngine* getAngelScriptEngine() const { return mAngelScriptEngine; }
  60. //! Return immediate execution script context
  61. asIScriptContext* getImmediateContext() const { return mImmediateContext; }
  62. //! Return a script function/method execution context
  63. asIScriptContext* getScriptFileContext(unsigned nestingLevel) const;
  64. //! Return logging mode
  65. ScriptLogMode getLogMode() const { return mLogMode; }
  66. //! Return retained mode log messages
  67. const std::string& getLogMessages() const { return mLogMessages; }
  68. private:
  69. //! AngelScript engine
  70. asIScriptEngine* mAngelScriptEngine;
  71. //! Immediate execution script context
  72. asIScriptContext* mImmediateContext;
  73. //! Immediate execution script file
  74. WeakPtr<ScriptFile> mImmediateScriptFile;
  75. //! Script function/method execution contexts
  76. std::vector<asIScriptContext*> mScriptFileContexts;
  77. //! Script engine logging mode
  78. ScriptLogMode mLogMode;
  79. //! Retained mode log messages
  80. std::string mLogMessages;
  81. };
  82. #endif // SCRIPT_SCRIPTENGINE_H