ScriptFile.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 Script;
  28. class ScriptInstance;
  29. class Variant;
  30. class asIObjectType;
  31. class asIScriptContext;
  32. class asIScriptEngine;
  33. class asIScriptFunction;
  34. class asIScriptModule;
  35. class asIScriptObject;
  36. /// %Script file resource.
  37. class ScriptFile : public Resource, public ScriptEventListener
  38. {
  39. OBJECT(ScriptFile);
  40. public:
  41. /// Construct.
  42. ScriptFile(Context* context);
  43. /// Destruct.
  44. virtual ~ScriptFile();
  45. /// Register object factory.
  46. static void RegisterObject(Context* context);
  47. /// Load resource. Return true if successful.
  48. virtual bool Load(Deserializer& source);
  49. /// Add an event handler. Called by script exposed version of SubscribeToEvent().
  50. virtual void AddEventHandler(StringHash eventType, const String& handlerName);
  51. /// Add an event handler for a specific sender. Called by script exposed version of SubscribeToEvent().
  52. virtual void AddEventHandler(Object* sender, StringHash eventType, const String& handlerName);
  53. /// Query for a function by declaration and execute if found.
  54. bool Execute(const String& declaration, const VariantVector& parameters = VariantVector(), bool unprepare = true);
  55. /// Execute a function.
  56. bool Execute(asIScriptFunction* function, const VariantVector& parameters = VariantVector(), bool unprepare = true);
  57. /// Query for an object method by declaration and execute if found.
  58. bool Execute(asIScriptObject* object, const String& declaration, const VariantVector& parameters =
  59. VariantVector(), bool unprepare = true);
  60. /// Execute an object method.
  61. bool Execute(asIScriptObject* object, asIScriptFunction* method, const VariantVector& parameters = VariantVector(),
  62. bool unprepare = true);
  63. /// Create a script object.
  64. asIScriptObject* CreateObject(const String& className);
  65. /// Return script module.
  66. asIScriptModule* GetScriptModule() const { return scriptModule_; }
  67. /// Return a function by declaration. Will be stored to a search cache so that further searches should be faster.
  68. asIScriptFunction* GetFunction(const String& declaration);
  69. /// Return an object method by declaration.
  70. asIScriptFunction* GetMethod(asIScriptObject* object, const String& declaration);
  71. /// Return whether script compiled successfully.
  72. bool IsCompiled() const { return compiled_; }
  73. private:
  74. /// Add a script section, checking for includes recursively. Return true if successful.
  75. bool AddScriptSection(asIScriptEngine* engine, Deserializer& source);
  76. /// Set parameters for a function or method.
  77. void SetParameters(asIScriptContext* context, asIScriptFunction* function, const VariantVector& parameters);
  78. /// Release the script module.
  79. void ReleaseModule();
  80. /// Handle an event in script.
  81. void HandleScriptEvent(StringHash eventType, VariantMap& eventData);
  82. /// Script subsystem.
  83. SharedPtr<Script> script_;
  84. /// Script module.
  85. asIScriptModule* scriptModule_;
  86. /// Compiled flag.
  87. bool compiled_;
  88. /// Encountered include files during script file loading.
  89. HashSet<String> includeFiles_;
  90. /// Search cache for checking whether script classes implement "ScriptObject" interface.
  91. HashMap<asIObjectType*, bool> validClasses_;
  92. /// Search cache for functions.
  93. HashMap<String, asIScriptFunction*> functions_;
  94. /// Search cache for methods.
  95. HashMap<asIObjectType*, HashMap<String, asIScriptFunction*> > methods_;
  96. };
  97. /// Get currently executing script file.
  98. ScriptFile* GetScriptContextFile();