ScriptFile.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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_SCRIPTFILE_H
  24. #define SCRIPT_SCRIPTFILE_H
  25. #include "Resource.h"
  26. #include "ScriptEventListener.h"
  27. #include "SharedPtr.h"
  28. #include "Variant.h"
  29. #include <set>
  30. #include <vector>
  31. class ScriptEngine;
  32. class Variant;
  33. class asIScriptContext;
  34. class asIScriptEngine;
  35. class asIScriptFunction;
  36. class asIScriptModule;
  37. class asIScriptObject;
  38. //! A script file resource
  39. class ScriptFile : public Resource, public ScriptEventListener
  40. {
  41. DEFINE_TYPE(ScriptFile);
  42. public:
  43. //! Construct with script engine pointer and name
  44. ScriptFile(ScriptEngine* scriptEngine, const std::string& name = std::string());
  45. //! Destruct
  46. virtual ~ScriptFile();
  47. //! Load resource. Throw exception on error
  48. virtual void load(Deserializer& source, ResourceCache* cache = 0);
  49. //! Add an event handler. Called by script exposed version of subscribeToEvent()
  50. virtual void addEventHandler(StringHash eventType, const std::string& handlerName);
  51. //! Query for a function by declaration and execute if found. If context is null, use the immediate context
  52. bool execute(const std::string& declaration, asIScriptContext* context = 0, const std::vector<Variant>& parameters =
  53. std::vector<Variant>());
  54. //! Execute a function. If context is null, use the immediate context
  55. bool execute(asIScriptFunction* function, asIScriptContext* context = 0, const std::vector<Variant>& parameters =
  56. std::vector<Variant>());
  57. //! Query for an object method by declaration and execute if found. If context is null, use the immediate context
  58. bool execute(asIScriptObject* object, const std::string& declaration, asIScriptContext* context = 0, const std::vector<Variant>&
  59. parameters = std::vector<Variant>());
  60. //! Execute an object method. If context is null, use the immediate context
  61. bool execute(asIScriptObject* object, asIScriptFunction* method, asIScriptContext* context = 0, const std::vector<Variant>&
  62. parameters = std::vector<Variant>());
  63. //! Create a script object. If context is null, use the immediate context
  64. asIScriptObject* createObject(const std::string& className, asIScriptContext* context = 0);
  65. //! Return script engine
  66. ScriptEngine* getScriptEngine() const { return mScriptEngine; }
  67. //! Return script module
  68. asIScriptModule* getScriptModule() const { return mScriptModule; }
  69. //! Return a function by declaration
  70. asIScriptFunction* getFunction(const std::string& declaration) const;
  71. //! Return an object method by declaration
  72. asIScriptFunction* getMethod(asIScriptObject* object, const std::string& declaration) const;
  73. //! Return whether script compiled successfully
  74. bool isCompiled() const { return mCompiled; }
  75. private:
  76. //! Add a script section, checking for includes recursively
  77. void addScriptSection(asIScriptEngine* engine, Deserializer& source, ResourceCache* cache);
  78. //! Set parameters for a function or method
  79. void setParameters(asIScriptContext* context, asIScriptFunction* function, const std::vector<Variant>& parameters);
  80. //! Handle an event with a handler in script
  81. void handleScriptEvent(StringHash eventType, VariantMap& eventData);
  82. //! Script engine
  83. SharedPtr<ScriptEngine> mScriptEngine;
  84. //! Script module
  85. asIScriptModule* mScriptModule;
  86. //! Event handler script context
  87. asIScriptContext* mScriptContext;
  88. //! Compiled flag
  89. bool mCompiled;
  90. //! Encountered include files during script file loading
  91. std::set<std::string> mAllIncludeFiles;
  92. };
  93. //! Get last script file that is executing or has executed script code
  94. ScriptFile* getLastScriptFile();
  95. #endif // SCRIPT_SCRIPTFILE_H