ScriptFile.h 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 <vector>
  30. class ScriptEngine;
  31. class Variant;
  32. class asIScriptContext;
  33. class asIScriptFunction;
  34. class asIScriptModule;
  35. class asIScriptObject;
  36. //! A script file resource
  37. class ScriptFile : public Resource, public ScriptEventListener
  38. {
  39. DEFINE_TYPE(ScriptFile);
  40. public:
  41. //! Construct with script engine pointer and name
  42. ScriptFile(ScriptEngine* scriptEngine, const std::string& name = std::string());
  43. //! Destruct
  44. virtual ~ScriptFile();
  45. //! Load resource
  46. virtual void load(Deserializer& source, ResourceCache* cache = 0);
  47. //! Add an event handler. Called by script exposed subscribeToEvent() function
  48. virtual void addEventHandler(StringHash eventType, const std::string& handlerName);
  49. //! Query for a function by declaration and execute if found. If context is null, use the immediate context
  50. bool execute(const std::string& declaration, asIScriptContext* context = 0, const std::vector<Variant>& parameters = std::vector<Variant>());
  51. //! Execute a function. If context is null, use the immediate context
  52. bool execute(asIScriptFunction* function, asIScriptContext* context = 0, const std::vector<Variant>& parameters = std::vector<Variant>());
  53. //! Query for an object method by declaration and execute if found. If context is null, use the immediate context
  54. bool execute(asIScriptObject* object, const std::string& declaration, asIScriptContext* context = 0, const std::vector<Variant>& parameters = std::vector<Variant>());
  55. //! Execute an object method. If context is null, use the immediate context
  56. bool execute(asIScriptObject* object, asIScriptFunction* method, asIScriptContext* context = 0, const std::vector<Variant>& parameters = std::vector<Variant>());
  57. //! Create a script object. If context is null, use the immediate context
  58. asIScriptObject* createObject(const std::string& className, asIScriptContext* context = 0);
  59. //! Return script engine
  60. ScriptEngine* getScriptEngine() const { return mScriptEngine; }
  61. //! Return script module
  62. asIScriptModule* getScriptModule() const { return mScriptModule; }
  63. //! Return a function by declaration
  64. asIScriptFunction* getFunction(const std::string& declaration) const;
  65. //! Return an object method by declaration
  66. asIScriptFunction* getMethod(asIScriptObject* object, const std::string& declaration) const;
  67. //! Return whether script compiled successfully
  68. bool isCompiled() const { return mCompiled; }
  69. private:
  70. //! Set parameters for a function or method
  71. void setParameters(asIScriptContext* context, asIScriptFunction* function, const std::vector<Variant>& parameters);
  72. //! Handle an event with a handler in script
  73. void handleScriptEvent(StringHash eventType, VariantMap& eventData);
  74. //! Script engine
  75. SharedPtr<ScriptEngine> mScriptEngine;
  76. //! Script module
  77. asIScriptModule* mScriptModule;
  78. //! Event handler script context
  79. asIScriptContext* mScriptContext;
  80. //! Compiled flag
  81. bool mCompiled;
  82. };
  83. //! Get last script file that is executing or has executed script code
  84. ScriptFile* getLastScriptFile();
  85. #endif // SCRIPT_SCRIPTFILE_H