ScriptFile.h 5.5 KB

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