ScriptFile.h 7.6 KB

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