ScriptFile.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. //! Add an event handler for a specific sender. Called by script exposed version of subscribeToEvent()
  54. virtual void addEventHandler(EventListener* sender, StringHash eventType, const std::string& handlerName);
  55. //! Query for a function by declaration and execute if found
  56. bool execute(const std::string& declaration, const std::vector<Variant>& parameters = std::vector<Variant>(), bool unprepare = true);
  57. //! Execute a function
  58. bool execute(asIScriptFunction* function, const std::vector<Variant>& parameters = std::vector<Variant>(), bool unprepare = true);
  59. //! Query for an object method by declaration and execute if found
  60. bool execute(asIScriptObject* object, const std::string& declaration, const std::vector<Variant>& parameters =
  61. std::vector<Variant>(), bool unprepare = true);
  62. //! Execute an object method
  63. bool execute(asIScriptObject* object, asIScriptFunction* method, const std::vector<Variant>& parameters = std::vector<Variant>(),
  64. bool unprepare = true);
  65. //! Create a script object
  66. asIScriptObject* createObject(const std::string& className);
  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);
  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. //! Release the script module
  87. void releaseModule();
  88. //! Handle an event in script
  89. void handleScriptEvent(StringHash eventType, VariantMap& eventData);
  90. //! Handle a specific sender's event in script
  91. void handleSpecificScriptEvent(StringHash eventType, VariantMap& eventData);
  92. //! Script engine
  93. SharedPtr<ScriptEngine> mScriptEngine;
  94. //! Script module
  95. asIScriptModule* mScriptModule;
  96. //! Compiled flag
  97. bool mCompiled;
  98. //! Encountered include files during script file loading
  99. std::set<std::string> mAllIncludeFiles;
  100. //! Search cache for checking whether script classes implement "ScriptObject" interface
  101. std::map<asIObjectType*, bool> mCheckedClasses;
  102. //! Search cache for functions
  103. std::map<std::string, asIScriptFunction*> mFunctions;
  104. //! Search cache for methods
  105. std::map<asIObjectType*, std::map<std::string, asIScriptFunction*> > mMethods;
  106. //! ScriptInstances that have created objects from this script file
  107. std::vector<ScriptInstance*> mScriptInstances;
  108. };
  109. //! Get currently executing script file
  110. ScriptFile* getScriptContextFile();
  111. //! Get current script execution nesting level
  112. unsigned getScriptNestingLevel();
  113. #endif // SCRIPT_SCRIPTFILE_H