ScriptFile.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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>());
  57. //! Execute a function
  58. bool execute(asIScriptFunction* function, const std::vector<Variant>& parameters = std::vector<Variant>());
  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>());
  62. //! Execute an object method
  63. bool execute(asIScriptObject* object, asIScriptFunction* method, const std::vector<Variant>& parameters = std::vector<Variant>());
  64. //! Create a script object
  65. asIScriptObject* createObject(const std::string& className);
  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. Will be stored to a search cache so that further searches should be faster
  71. asIScriptFunction* getFunction(const std::string& declaration);
  72. //! Return an object method by declaration
  73. asIScriptFunction* getMethod(asIScriptObject* object, const std::string& declaration);
  74. //! Return whether script compiled successfully
  75. bool isCompiled() const { return mCompiled; }
  76. //! Add script instance to keep track of in case of script reload
  77. void addScriptInstance(ScriptInstance* instance);
  78. //! Remove script instance to keep track of
  79. void removeScriptInstance(ScriptInstance* instance);
  80. private:
  81. //! Add a script section, checking for includes recursively
  82. void addScriptSection(asIScriptEngine* engine, Deserializer& source, ResourceCache* cache);
  83. //! Set parameters for a function or method
  84. void setParameters(asIScriptContext* context, asIScriptFunction* function, const std::vector<Variant>& parameters);
  85. //! Handle an event in script
  86. void handleScriptEvent(StringHash eventType, VariantMap& eventData);
  87. //! Handle a specific sender's event in script
  88. void handleSpecificScriptEvent(StringHash eventType, VariantMap& eventData);
  89. //! Script engine
  90. SharedPtr<ScriptEngine> mScriptEngine;
  91. //! Script module
  92. asIScriptModule* mScriptModule;
  93. //! Compiled flag
  94. bool mCompiled;
  95. //! Encountered include files during script file loading
  96. std::set<std::string> mAllIncludeFiles;
  97. //! Search cache for checking whether script classes implement "ScriptObject" interface
  98. std::map<asIObjectType*, bool> mCheckedClasses;
  99. //! Search cache for functions
  100. std::map<std::string, asIScriptFunction*> mFunctions;
  101. //! Search cache for methods
  102. std::map<asIObjectType*, std::map<std::string, asIScriptFunction*> > mMethods;
  103. //! ScriptInstances that have created objects from this script file
  104. std::vector<ScriptInstance*> mScriptInstances;
  105. };
  106. //! Get currently executing script file
  107. ScriptFile* getScriptContextFile();
  108. //! Get current script execution nesting level
  109. unsigned getScriptNestingLevel();
  110. //! Return highest script execution nesting level, optionally reset it
  111. unsigned getHighestScriptNestingLevel(bool reset = true);
  112. #endif // SCRIPT_SCRIPTFILE_H