ScriptFile.h 5.0 KB

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