ScriptFile.h 8.0 KB

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