ScriptFile.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../AngelScript/ScriptEventListener.h"
  5. #include "../Container/ArrayPtr.h"
  6. #include "../Container/HashSet.h"
  7. #include "../Resource/Resource.h"
  8. class asIScriptContext;
  9. class asIScriptEngine;
  10. class asIScriptFunction;
  11. class asIScriptModule;
  12. class asIScriptObject;
  13. class asITypeInfo;
  14. namespace Urho3D
  15. {
  16. class Script;
  17. class ScriptEventInvoker;
  18. class ScriptInstance;
  19. class Variant;
  20. /// %Script file resource.
  21. class URHO3D_API ScriptFile : public Resource, public ScriptEventListener
  22. {
  23. URHO3D_OBJECT(ScriptFile, Resource);
  24. public:
  25. /// Construct.
  26. explicit ScriptFile(Context* context);
  27. /// Destruct.
  28. ~ScriptFile() override;
  29. /// Register object factory.
  30. /// @nobind
  31. static void RegisterObject(Context* context);
  32. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  33. bool BeginLoad(Deserializer& source) override;
  34. /// Finish resource loading. Always called from the main thread. Return true if successful.
  35. bool EndLoad() override;
  36. /// Add a scripted event handler.
  37. void AddEventHandler(StringHash eventType, const String& handlerName) override;
  38. /// Add a scripted event handler for a specific sender.
  39. void AddEventHandler(Object* sender, StringHash eventType, const String& handlerName) override;
  40. /// Remove a scripted event handler.
  41. void RemoveEventHandler(StringHash eventType) override;
  42. /// Remove a scripted event handler for a specific sender.
  43. void RemoveEventHandler(Object* sender, StringHash eventType) override;
  44. /// Remove all scripted event handlers for a specific sender.
  45. void RemoveEventHandlers(Object* sender) override;
  46. /// Remove all scripted event handlers.
  47. void RemoveEventHandlers() override;
  48. /// Remove all scripted event handlers, except those listed.
  49. void RemoveEventHandlersExcept(const PODVector<StringHash>& exceptions) override;
  50. /// Return whether has subscribed to an event.
  51. bool HasEventHandler(StringHash eventType) const override;
  52. /// Return whether has subscribed to a specific sender's event.
  53. bool HasEventHandler(Object* sender, StringHash eventType) const override;
  54. /// Query for a function by declaration and execute if found.
  55. bool Execute(const String& declaration, const VariantVector& parameters = Variant::emptyVariantVector,
  56. Variant* functionReturn = nullptr, bool unprepare = true);
  57. /// Execute a function.
  58. bool Execute(asIScriptFunction* function, const VariantVector& parameters = Variant::emptyVariantVector,
  59. Variant* functionReturn = nullptr, bool unprepare = true);
  60. /// Query for an object method by declaration and execute if found.
  61. bool Execute(asIScriptObject* object, const String& declaration, const VariantVector& parameters = Variant::emptyVariantVector,
  62. Variant* functionReturn = nullptr, bool unprepare = true);
  63. /// Execute an object method.
  64. bool Execute(asIScriptObject* object, asIScriptFunction* method, const VariantVector& parameters = Variant::emptyVariantVector,
  65. Variant* functionReturn = nullptr, bool unprepare = true);
  66. /// Add a delay-executed function call, optionally repeating.
  67. void DelayedExecute
  68. (float delay, bool repeat, const String& declaration, const VariantVector& parameters = Variant::emptyVariantVector);
  69. /// Clear pending delay-executed function calls. If empty declaration given, clears all.
  70. void ClearDelayedExecute(const String& declaration = String::EMPTY);
  71. /// Create a script object. Optionally search for the first class in the module that implements the specified interface.
  72. asIScriptObject* CreateObject(const String& className, bool useInterface = false);
  73. /// Save the script bytecode. Return true if successful.
  74. bool SaveByteCode(Serializer& dest);
  75. /// Return script module.
  76. asIScriptModule* GetScriptModule() const { return scriptModule_; }
  77. /// Return a function by declaration. Will be stored to a search cache so that further searches should be faster.
  78. asIScriptFunction* GetFunction(const String& declaration);
  79. /// Return an object method by declaration.
  80. asIScriptFunction* GetMethod(asIScriptObject* object, const String& declaration);
  81. /// Return whether script compiled successfully.
  82. bool IsCompiled() const { return compiled_; }
  83. /// Clean up an event invoker object when its associated script object no longer exists.
  84. void CleanupEventInvoker(asIScriptObject* object);
  85. void SetOnlyCompile() { onlyCompile_ = true; }
  86. Script* GetScript() const { return script_; }
  87. private:
  88. /// Add an event handler and create the necessary proxy object.
  89. void AddEventHandlerInternal(Object* sender, StringHash eventType, const String& handlerName);
  90. /// Add a script section, checking for includes recursively. Return true if successful.
  91. bool AddScriptSection(asIScriptEngine* engine, Deserializer& source);
  92. /// Set parameters for a function or method.
  93. void SetParameters(asIScriptContext* context, asIScriptFunction* function, const VariantVector& parameters);
  94. /// Release the script module.
  95. void ReleaseModule();
  96. /// Handle application update event.
  97. void HandleUpdate(StringHash eventType, VariantMap& eventData);
  98. /// Script subsystem.
  99. SharedPtr<Script> script_;
  100. /// Script module.
  101. asIScriptModule* scriptModule_{};
  102. /// Compiled flag.
  103. bool compiled_{};
  104. /// Subscribed to application update event flag.
  105. bool subscribed_{};
  106. /// Encountered include files during script file loading.
  107. HashSet<String> includeFiles_;
  108. /// Search cache for checking whether script classes implement "ScriptObject" interface.
  109. HashMap<asITypeInfo*, bool> validClasses_;
  110. /// Search cache for functions.
  111. HashMap<String, asIScriptFunction*> functions_;
  112. /// Search cache for methods.
  113. HashMap<asITypeInfo*, HashMap<String, asIScriptFunction*>> methods_;
  114. /// Delayed function calls.
  115. Vector<DelayedCall> delayedCalls_;
  116. /// Event helper objects for handling procedural or non-ScriptInstance script events.
  117. HashMap<asIScriptObject*, SharedPtr<ScriptEventInvoker>> eventInvokers_;
  118. /// Byte code for asynchronous loading.
  119. SharedArrayPtr<unsigned char> loadByteCode_;
  120. bool onlyCompile_{false};
  121. /// Byte code size for asynchronous loading.
  122. unsigned loadByteCodeSize_{};
  123. };
  124. /// Helper class for forwarding events to script objects that are not part of a scene.
  125. class URHO3D_API ScriptEventInvoker : public Object
  126. {
  127. URHO3D_OBJECT(ScriptEventInvoker, Object);
  128. public:
  129. /// Constructor, will create the asILockableSharedBool if a ScriptObject is passed in.
  130. explicit ScriptEventInvoker(ScriptFile* file, asIScriptObject* object = nullptr);
  131. /// Destructor, release the ref it we still hold it.
  132. ~ScriptEventInvoker() override;
  133. /// Get the asIScriptObject to call the method on, can be null.
  134. asIScriptObject* GetObject() const { return object_; }
  135. /// Returns whether the ScriptObject is still alive. Will return true if there is no reference and object.
  136. bool IsObjectAlive() const;
  137. /// Handle an event in script.
  138. void HandleScriptEvent(StringHash eventType, VariantMap& eventData);
  139. private:
  140. /// Parent script file.
  141. ScriptFile* file_;
  142. /// Shared boolean for checking the continued existence of the script object.
  143. asILockableSharedBool* sharedBool_;
  144. /// Script object that the handler method belongs to. Null for procedural event handling.
  145. asIScriptObject* object_;
  146. };
  147. /// Get currently executing script file.
  148. URHO3D_API ScriptFile* GetScriptContextFile();
  149. }