ScriptFile.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #pragma once
  24. #include "Resource.h"
  25. #include "ScriptEventListener.h"
  26. #include "SharedPtr.h"
  27. #include "Variant.h"
  28. #include <set>
  29. #include <vector>
  30. class Script;
  31. class ScriptInstance;
  32. class Variant;
  33. class asIObjectType;
  34. class asIScriptContext;
  35. class asIScriptEngine;
  36. class asIScriptFunction;
  37. class asIScriptModule;
  38. class asIScriptObject;
  39. /// Script file resource
  40. class ScriptFile : public Resource, public ScriptEventListener
  41. {
  42. OBJECT(ScriptFile);
  43. public:
  44. /// Construct
  45. ScriptFile(Context* context);
  46. /// Destruct
  47. virtual ~ScriptFile();
  48. /// Register object factory
  49. static void RegisterObject(Context* context);
  50. /// Load resource. Return true if successful
  51. virtual bool Load(Deserializer& source);
  52. /// Add an event handler. Called by script exposed version of SubscribeToEvent()
  53. virtual void AddEventHandler(StringHash eventType, const std::string& handlerName);
  54. /// Add an event handler for a specific sender. Called by script exposed version of SubscribeToEvent()
  55. virtual void AddEventHandler(Object* sender, StringHash eventType, const std::string& handlerName);
  56. /// Query for a function by declaration and execute if found
  57. bool Execute(const std::string& declaration, const VariantVector& parameters = VariantVector(), bool unprepare = true);
  58. /// Execute a function
  59. bool Execute(asIScriptFunction* function, const VariantVector& parameters = VariantVector(), bool unprepare = true);
  60. /// Query for an object method by declaration and execute if found
  61. bool Execute(asIScriptObject* object, const std::string& declaration, const VariantVector& parameters =
  62. VariantVector(), bool unprepare = true);
  63. /// Execute an object method
  64. bool Execute(asIScriptObject* object, asIScriptFunction* method, const VariantVector& parameters = VariantVector(),
  65. bool unprepare = true);
  66. /// Create a script object
  67. asIScriptObject* CreateObject(const std::string& className);
  68. /// Return script module
  69. asIScriptModule* GetScriptModule() const { return scriptModule_; }
  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 compiled_; }
  76. private:
  77. /// Add a script section, checking for includes recursively. Return true if successful
  78. bool AddScriptSection(asIScriptEngine* engine, Deserializer& source);
  79. /// Set parameters for a function or method
  80. void SetParameters(asIScriptContext* context, asIScriptFunction* function, const VariantVector& parameters);
  81. /// Release the script module
  82. void ReleaseModule();
  83. /// Handle an event in script
  84. void HandleScriptEvent(StringHash eventType, VariantMap& eventData);
  85. /// Script subsystem
  86. SharedPtr<Script> script_;
  87. /// Script module
  88. asIScriptModule* scriptModule_;
  89. /// Compiled flag
  90. bool compiled_;
  91. /// Encountered include files during script file loading
  92. std::set<std::string> includeFiles_;
  93. /// Search cache for checking whether script classes implement "ScriptObject" interface
  94. std::map<asIObjectType*, bool> checkedClasses_;
  95. /// Search cache for functions
  96. std::map<std::string, asIScriptFunction*> functions_;
  97. /// Search cache for methods
  98. std::map<asIObjectType*, std::map<std::string, asIScriptFunction*> > methods_;
  99. };
  100. /// Get currently executing script file
  101. ScriptFile* GetScriptContextFile();