ScriptFile.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  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 ScriptInstance;
  37. class Variant;
  38. /// %Script file resource.
  39. class 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 an event handler. Called by script exposed version of SubscribeToEvent().
  52. virtual void AddEventHandler(StringHash eventType, const String& handlerName);
  53. /// Add an event handler for a specific sender. Called by script exposed version of SubscribeToEvent().
  54. virtual void AddEventHandler(Object* sender, StringHash eventType, const String& handlerName);
  55. /// Query for a function by declaration and execute if found.
  56. bool Execute(const String& declaration, const VariantVector& parameters = VariantVector(), bool unprepare = true);
  57. /// Execute a function.
  58. bool Execute(asIScriptFunction* function, const VariantVector& parameters = VariantVector(), bool unprepare = true);
  59. /// Query for an object method by declaration and execute if found.
  60. bool Execute(asIScriptObject* object, const String& declaration, const VariantVector& parameters =
  61. VariantVector(), bool unprepare = true);
  62. /// Execute an object method.
  63. bool Execute(asIScriptObject* object, asIScriptFunction* method, const VariantVector& parameters = VariantVector(),
  64. bool unprepare = true);
  65. /// Create a script object.
  66. asIScriptObject* CreateObject(const String& className);
  67. /// Return script module.
  68. asIScriptModule* GetScriptModule() const { return scriptModule_; }
  69. /// Return a function by declaration. Will be stored to a search cache so that further searches should be faster.
  70. asIScriptFunction* GetFunction(const String& declaration);
  71. /// Return an object method by declaration.
  72. asIScriptFunction* GetMethod(asIScriptObject* object, const String& declaration);
  73. /// Return whether script compiled successfully.
  74. bool IsCompiled() const { return compiled_; }
  75. private:
  76. /// Add a script section, checking for includes recursively. Return true if successful.
  77. bool AddScriptSection(asIScriptEngine* engine, Deserializer& source);
  78. /// Set parameters for a function or method.
  79. void SetParameters(asIScriptContext* context, asIScriptFunction* function, const VariantVector& parameters);
  80. /// Release the script module.
  81. void ReleaseModule();
  82. /// Handle an event in script.
  83. void HandleScriptEvent(StringHash eventType, VariantMap& eventData);
  84. /// Script subsystem.
  85. SharedPtr<Script> script_;
  86. /// Script module.
  87. asIScriptModule* scriptModule_;
  88. /// Compiled flag.
  89. bool compiled_;
  90. /// Encountered include files during script file loading.
  91. HashSet<String> includeFiles_;
  92. /// Search cache for checking whether script classes implement "ScriptObject" interface.
  93. HashMap<asIObjectType*, bool> validClasses_;
  94. /// Search cache for functions.
  95. HashMap<String, asIScriptFunction*> functions_;
  96. /// Search cache for methods.
  97. HashMap<asIObjectType*, HashMap<String, asIScriptFunction*> > methods_;
  98. };
  99. /// Get currently executing script file.
  100. ScriptFile* GetScriptContextFile();
  101. }