ScriptFile.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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
  54. bool execute(const std::string& declaration, const std::vector<Variant>& parameters = std::vector<Variant>());
  55. //! Execute a function
  56. bool execute(asIScriptFunction* function, const std::vector<Variant>& parameters = std::vector<Variant>());
  57. //! Query for an object method by declaration and execute if found
  58. bool execute(asIScriptObject* object, const std::string& declaration, const std::vector<Variant>& parameters =
  59. std::vector<Variant>());
  60. //! Execute an object method
  61. bool execute(asIScriptObject* object, asIScriptFunction* method, const std::vector<Variant>& parameters = std::vector<Variant>());
  62. //! Create a script object
  63. asIScriptObject* createObject(const std::string& className);
  64. //! Return script engine
  65. ScriptEngine* getScriptEngine() const { return mScriptEngine; }
  66. //! Return script module
  67. asIScriptModule* getScriptModule() const { return mScriptModule; }
  68. //! Return a function by declaration. Will be stored to a search cache so that further searches should be faster
  69. asIScriptFunction* getFunction(const std::string& declaration);
  70. //! Return an object method by declaration
  71. asIScriptFunction* getMethod(asIScriptObject* object, const std::string& declaration);
  72. //! Return whether script compiled successfully
  73. bool isCompiled() const { return mCompiled; }
  74. //! Add script instance to keep track of in case of script reload
  75. void addScriptInstance(ScriptInstance* instance);
  76. //! Remove script instance to keep track of
  77. void removeScriptInstance(ScriptInstance* instance);
  78. private:
  79. //! Add a script section, checking for includes recursively
  80. void addScriptSection(asIScriptEngine* engine, Deserializer& source, ResourceCache* cache);
  81. //! Set parameters for a function or method
  82. void setParameters(asIScriptContext* context, asIScriptFunction* function, const std::vector<Variant>& parameters);
  83. //! Handle an event with a handler in script
  84. void handleScriptEvent(StringHash eventType, VariantMap& eventData);
  85. //! Script engine
  86. SharedPtr<ScriptEngine> mScriptEngine;
  87. //! Script module
  88. asIScriptModule* mScriptModule;
  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> mCheckedClasses;
  95. //! Search cache for functions
  96. std::map<std::string, asIScriptFunction*> mFunctions;
  97. //! Search cache for methods
  98. std::map<asIObjectType*, std::map<std::string, asIScriptFunction*> > mMethods;
  99. //! ScriptInstances that have created objects from this script file
  100. std::vector<ScriptInstance*> mScriptInstances;
  101. };
  102. //! Get last script file that is executing or has executed script functions
  103. ScriptFile* getLastScriptFile();
  104. //! Get current script execution nesting level
  105. unsigned getScriptNestingLevel();
  106. //! Return highest script execution nesting level last frame, and clear
  107. unsigned getHighestScriptNestingLevel();
  108. #endif // SCRIPT_SCRIPTFILE_H