ScriptFile.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../AngelScript/ScriptEventListener.h"
  24. #include "../Container/ArrayPtr.h"
  25. #include "../Container/HashSet.h"
  26. #include "../Resource/Resource.h"
  27. class asIScriptContext;
  28. class asIScriptEngine;
  29. class asIScriptFunction;
  30. class asIScriptModule;
  31. class asIScriptObject;
  32. class asITypeInfo;
  33. namespace Urho3D
  34. {
  35. class Script;
  36. class ScriptEventInvoker;
  37. class ScriptInstance;
  38. class Variant;
  39. /// %Script file resource.
  40. class URHO3D_API ScriptFile : public Resource, public ScriptEventListener
  41. {
  42. URHO3D_OBJECT(ScriptFile, Resource);
  43. public:
  44. /// Construct.
  45. explicit ScriptFile(Context* context);
  46. /// Destruct.
  47. ~ScriptFile() override;
  48. /// Register object factory.
  49. /// @nobind
  50. static void RegisterObject(Context* context);
  51. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  52. bool BeginLoad(Deserializer& source) override;
  53. /// Finish resource loading. Always called from the main thread. Return true if successful.
  54. bool EndLoad() override;
  55. /// Add a scripted event handler.
  56. void AddEventHandler(StringHash eventType, const String& handlerName) override;
  57. /// Add a scripted event handler for a specific sender.
  58. void AddEventHandler(Object* sender, StringHash eventType, const String& handlerName) override;
  59. /// Remove a scripted event handler.
  60. void RemoveEventHandler(StringHash eventType) override;
  61. /// Remove a scripted event handler for a specific sender.
  62. void RemoveEventHandler(Object* sender, StringHash eventType) override;
  63. /// Remove all scripted event handlers for a specific sender.
  64. void RemoveEventHandlers(Object* sender) override;
  65. /// Remove all scripted event handlers.
  66. void RemoveEventHandlers() override;
  67. /// Remove all scripted event handlers, except those listed.
  68. void RemoveEventHandlersExcept(const PODVector<StringHash>& exceptions) override;
  69. /// Return whether has subscribed to an event.
  70. bool HasEventHandler(StringHash eventType) const override;
  71. /// Return whether has subscribed to a specific sender's event.
  72. bool HasEventHandler(Object* sender, StringHash eventType) const override;
  73. /// Query for a function by declaration and execute if found.
  74. bool Execute(const String& declaration, const VariantVector& parameters = Variant::emptyVariantVector,
  75. Variant* functionReturn = nullptr, bool unprepare = true);
  76. /// Execute a function.
  77. bool Execute(asIScriptFunction* function, const VariantVector& parameters = Variant::emptyVariantVector,
  78. Variant* functionReturn = nullptr, bool unprepare = true);
  79. /// Query for an object method by declaration and execute if found.
  80. bool Execute(asIScriptObject* object, const String& declaration, const VariantVector& parameters = Variant::emptyVariantVector,
  81. Variant* functionReturn = nullptr, bool unprepare = true);
  82. /// Execute an object method.
  83. bool Execute(asIScriptObject* object, asIScriptFunction* method, const VariantVector& parameters = Variant::emptyVariantVector,
  84. Variant* functionReturn = nullptr, bool unprepare = true);
  85. /// Add a delay-executed function call, optionally repeating.
  86. void DelayedExecute
  87. (float delay, bool repeat, const String& declaration, const VariantVector& parameters = Variant::emptyVariantVector);
  88. /// Clear pending delay-executed function calls. If empty declaration given, clears all.
  89. void ClearDelayedExecute(const String& declaration = String::EMPTY);
  90. /// Create a script object. Optionally search for the first class in the module that implements the specified interface.
  91. asIScriptObject* CreateObject(const String& className, bool useInterface = false);
  92. /// Save the script bytecode. Return true if successful.
  93. bool SaveByteCode(Serializer& dest);
  94. /// Return script module.
  95. asIScriptModule* GetScriptModule() const { return scriptModule_; }
  96. /// Return a function by declaration. Will be stored to a search cache so that further searches should be faster.
  97. asIScriptFunction* GetFunction(const String& declaration);
  98. /// Return an object method by declaration.
  99. asIScriptFunction* GetMethod(asIScriptObject* object, const String& declaration);
  100. /// Return whether script compiled successfully.
  101. bool IsCompiled() const { return compiled_; }
  102. /// Clean up an event invoker object when its associated script object no longer exists.
  103. void CleanupEventInvoker(asIScriptObject* object);
  104. private:
  105. /// Add an event handler and create the necessary proxy object.
  106. void AddEventHandlerInternal(Object* sender, StringHash eventType, const String& handlerName);
  107. /// Add a script section, checking for includes recursively. Return true if successful.
  108. bool AddScriptSection(asIScriptEngine* engine, Deserializer& source);
  109. /// Set parameters for a function or method.
  110. void SetParameters(asIScriptContext* context, asIScriptFunction* function, const VariantVector& parameters);
  111. /// Release the script module.
  112. void ReleaseModule();
  113. /// Handle application update event.
  114. void HandleUpdate(StringHash eventType, VariantMap& eventData);
  115. /// Script subsystem.
  116. SharedPtr<Script> script_;
  117. /// Script module.
  118. asIScriptModule* scriptModule_{};
  119. /// Compiled flag.
  120. bool compiled_{};
  121. /// Subscribed to application update event flag.
  122. bool subscribed_{};
  123. /// Encountered include files during script file loading.
  124. HashSet<String> includeFiles_;
  125. /// Search cache for checking whether script classes implement "ScriptObject" interface.
  126. HashMap<asITypeInfo*, bool> validClasses_;
  127. /// Search cache for functions.
  128. HashMap<String, asIScriptFunction*> functions_;
  129. /// Search cache for methods.
  130. HashMap<asITypeInfo*, HashMap<String, asIScriptFunction*> > methods_;
  131. /// Delayed function calls.
  132. Vector<DelayedCall> delayedCalls_;
  133. /// Event helper objects for handling procedural or non-ScriptInstance script events.
  134. HashMap<asIScriptObject*, SharedPtr<ScriptEventInvoker> > eventInvokers_;
  135. /// Byte code for asynchronous loading.
  136. SharedArrayPtr<unsigned char> loadByteCode_;
  137. /// Byte code size for asynchronous loading.
  138. unsigned loadByteCodeSize_{};
  139. };
  140. /// Helper class for forwarding events to script objects that are not part of a scene.
  141. class URHO3D_API ScriptEventInvoker : public Object
  142. {
  143. URHO3D_OBJECT(ScriptEventInvoker, Object);
  144. public:
  145. /// Constructor, will create the asILockableSharedBool if a ScriptObject is passed in.
  146. explicit ScriptEventInvoker(ScriptFile* file, asIScriptObject* object = nullptr);
  147. /// Destructor, release the ref it we still hold it.
  148. ~ScriptEventInvoker() override;
  149. /// Get the asIScriptObject to call the method on, can be null.
  150. asIScriptObject* GetObject() const { return object_; }
  151. /// Returns whether the ScriptObject is still alive. Will return true if there is no reference and object.
  152. bool IsObjectAlive() const;
  153. /// Handle an event in script.
  154. void HandleScriptEvent(StringHash eventType, VariantMap& eventData);
  155. private:
  156. /// Parent script file.
  157. ScriptFile* file_;
  158. /// Shared boolean for checking the continued existence of the script object.
  159. asILockableSharedBool* sharedBool_;
  160. /// Script object that the handler method belongs to. Null for procedural event handling.
  161. asIScriptObject* object_;
  162. };
  163. /// Get currently executing script file.
  164. URHO3D_API ScriptFile* GetScriptContextFile();
  165. }