ScriptFile.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 "SharedPtr.h"
  27. #include "Variant.h"
  28. #include <vector>
  29. class ScriptEngine;
  30. class Variant;
  31. class asIScriptModule;
  32. class asIScriptFunction;
  33. class asIScriptObject;
  34. //! A script file resource
  35. class ScriptFile : public Resource
  36. {
  37. DEFINE_TYPE(ScriptFile);
  38. public:
  39. //! Construct with script engine pointer and name
  40. ScriptFile(ScriptEngine* scriptEngine, const std::string& name = std::string());
  41. //! Destruct
  42. virtual ~ScriptFile();
  43. //! Load resource
  44. virtual void load(Deserializer& source, ResourceCache* cache = 0);
  45. //! Query for a function by declaration and execute if found. If context is null, use the immediate context
  46. bool execute(const std::string& declaration, asIScriptContext* context = 0, const std::vector<Variant>& parameters = std::vector<Variant>());
  47. //! Execute a function. If context is null, use the immediate context
  48. bool execute(asIScriptFunction* function, asIScriptContext* context = 0, const std::vector<Variant>& parameters = std::vector<Variant>());
  49. //! Query for an object method by declaration and execute if found. If context is null, use the immediate context
  50. bool execute(asIScriptObject* object, const std::string& declaration, asIScriptContext* context = 0, const std::vector<Variant>& parameters = std::vector<Variant>());
  51. //! Execute an object method. If context is null, use the immediate context
  52. bool execute(asIScriptObject* object, asIScriptFunction* method, asIScriptContext* context = 0, const std::vector<Variant>& parameters = std::vector<Variant>());
  53. //! Create a script object. If context is null, use the immediate context
  54. asIScriptObject* createObject(const std::string& className, asIScriptContext* context = 0);
  55. //! Return script engine
  56. ScriptEngine* getScriptEngine() const { return mScriptEngine; }
  57. //! Return script module
  58. asIScriptModule* getScriptModule() const { return mScriptModule; }
  59. //! Return a function by declaration
  60. asIScriptFunction* getFunction(const std::string& declaration) const;
  61. //! Return an object method by declaration
  62. asIScriptFunction* getMethod(asIScriptObject* object, const std::string& declaration) const;
  63. //! Return whether script compiled successfully
  64. bool isCompiled() const { return mCompiled; }
  65. private:
  66. //! Set parameters for a function or method
  67. void setParameters(asIScriptContext* context, asIScriptFunction* function, const std::vector<Variant>& parameters);
  68. //! Script engine
  69. SharedPtr<ScriptEngine> mScriptEngine;
  70. //! Script module
  71. asIScriptModule* mScriptModule;
  72. //! Compiled flag
  73. bool mCompiled;
  74. };
  75. #endif // SCRIPT_SCRIPTFILE_H