ScriptFile.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // Copyright (c) 2008-2013 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 ScriptInstance;
  36. class Variant;
  37. /// %Script file resource.
  38. class URHO3D_API ScriptFile : public Resource, public ScriptEventListener
  39. {
  40. OBJECT(ScriptFile);
  41. public:
  42. /// Construct.
  43. ScriptFile(Context* context);
  44. /// Destruct.
  45. virtual ~ScriptFile();
  46. /// Register object factory.
  47. static void RegisterObject(Context* context);
  48. /// Load resource. Return true if successful.
  49. virtual bool Load(Deserializer& source);
  50. /// Add an event handler. Called by script exposed version of SubscribeToEvent().
  51. virtual void AddEventHandler(StringHash eventType, const String& handlerName);
  52. /// Add an event handler for a specific sender. Called by script exposed version of SubscribeToEvent().
  53. virtual void AddEventHandler(Object* sender, StringHash eventType, const String& handlerName);
  54. /// Query for a function by declaration and execute if found.
  55. bool Execute(const String& declaration, const VariantVector& parameters = Variant::emptyVariantVector, bool unprepare = true);
  56. /// Execute a function.
  57. bool Execute(asIScriptFunction* function, const VariantVector& parameters = Variant::emptyVariantVector, bool unprepare = true);
  58. /// Query for an object method by declaration and execute if found.
  59. bool Execute(asIScriptObject* object, const String& declaration, const VariantVector& parameters = Variant::emptyVariantVector, bool unprepare = true);
  60. /// Execute an object method.
  61. bool Execute(asIScriptObject* object, asIScriptFunction* method, const VariantVector& parameters = Variant::emptyVariantVector, bool unprepare = true);
  62. /// Add a delay-executed function call, optionally repeating.
  63. void DelayedExecute(float delay, bool repeat, const String& declaration, const VariantVector& parameters = Variant::emptyVariantVector);
  64. /// Clear pending delay-executed function calls. If empty declaration given, clears all.
  65. void ClearDelayedExecute(const String& declaration = String::EMPTY);
  66. /// Create a script object.
  67. asIScriptObject* CreateObject(const String& className);
  68. /// Save the script bytecode. Return true if successful.
  69. bool SaveByteCode(Serializer& dest);
  70. /// Return script module.
  71. asIScriptModule* GetScriptModule() const { return scriptModule_; }
  72. /// Return a function by declaration. Will be stored to a search cache so that further searches should be faster.
  73. asIScriptFunction* GetFunction(const String& declaration);
  74. /// Return an object method by declaration.
  75. asIScriptFunction* GetMethod(asIScriptObject* object, const String& declaration);
  76. /// Return whether script compiled successfully.
  77. bool IsCompiled() const { return compiled_; }
  78. private:
  79. /// Add a script section, checking for includes recursively. Return true if successful.
  80. bool AddScriptSection(asIScriptEngine* engine, Deserializer& source);
  81. /// Set parameters for a function or method.
  82. void SetParameters(asIScriptContext* context, asIScriptFunction* function, const VariantVector& parameters);
  83. /// Release the script module.
  84. void ReleaseModule();
  85. /// Handle an event in script.
  86. void HandleScriptEvent(StringHash eventType, VariantMap& eventData);
  87. /// Handle application update event.
  88. void HandleUpdate(StringHash eventType, VariantMap& eventData);
  89. /// Script subsystem.
  90. SharedPtr<Script> script_;
  91. /// Script module.
  92. asIScriptModule* scriptModule_;
  93. /// Compiled flag.
  94. bool compiled_;
  95. /// Subscribed to application update event flag.
  96. bool subscribed_;
  97. /// Encountered include files during script file loading.
  98. HashSet<String> includeFiles_;
  99. /// Search cache for checking whether script classes implement "ScriptObject" interface.
  100. HashMap<asIObjectType*, bool> validClasses_;
  101. /// Search cache for functions.
  102. HashMap<String, asIScriptFunction*> functions_;
  103. /// Search cache for methods.
  104. HashMap<asIObjectType*, HashMap<String, asIScriptFunction*> > methods_;
  105. /// Delayed function calls.
  106. Vector<DelayedCall> delayedCalls_;
  107. };
  108. /// Get currently executing script file.
  109. URHO3D_API ScriptFile* GetScriptContextFile();
  110. }