BsScriptResourceManager.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #pragma once
  2. #include "BsScriptEnginePrerequisites.h"
  3. #include "BsModule.h"
  4. #include <mono/jit/jit.h>
  5. namespace BansheeEngine
  6. {
  7. /**
  8. * @brief Handles creation and lookup of script interop objects for resources.
  9. * Since resources can be created in native code yet used by managed code
  10. * this manager provides lookups to find managed equivalents of
  11. * native resources.
  12. */
  13. class BS_SCR_BE_EXPORT ScriptResourceManager : public Module<ScriptResourceManager>
  14. {
  15. public:
  16. ScriptResourceManager();
  17. ~ScriptResourceManager();
  18. /**
  19. * @brief Creates a new managed instance and interop object for the specified resource.
  20. *
  21. * @param resourceHandle Native resource to wrap in a managed instance.
  22. * @param out Output interop object corresponding to the new managed instance.
  23. *
  24. * @note Throws an exception if a managed instance for the provided resource already exists.
  25. */
  26. template<class RetType, class InType>
  27. void createScriptResource(const ResourceHandle<InType>& resourceHandle, RetType** out);
  28. /**
  29. * @brief Creates a new interop object for the specified resource using an existing managed instance.
  30. *
  31. * @param existingInstance Existing managed instance. Caller must ensure the managed instance
  32. * matches the native resource type.
  33. * @param resourceHandle Native resource to link to the managed instance.
  34. * @param out Output interop object corresponding to the new managed instance.
  35. *
  36. * @note Throws an exception if a managed instance for the provided resource already exists.
  37. */
  38. template<class RetType, class InType>
  39. void createScriptResource(MonoObject* existingInstance, const ResourceHandle<InType>& resourceHandle, RetType** out);
  40. /**
  41. * @brief Attempts to find an existing interop object for the specified resource, and optionally
  42. * creates a new one if one cannot be found.
  43. *
  44. * @param resourceHandle Resource to search for.
  45. * @param out Found or created interop object containing the resource.
  46. * @param create If a resource cannot be found new one will be created when this
  47. * is true. If false and the resource doesn't exist it will be null.
  48. */
  49. template<class RetType, class InType>
  50. void getScriptResource(const ResourceHandle<InType>& resourceHandle, RetType** out, bool create = false);
  51. /**
  52. * @brief Creates a new managed instance and interop object for the specified string table.
  53. *
  54. * @param resourceHandle Native string table resource to wrap in a managed instance.
  55. * @param out Output string table interop object corresponding to the new managed instance.
  56. *
  57. * @note Throws an exception if a managed instance for the provided resource already exists.
  58. */
  59. template<>
  60. void createScriptResource(const ResourceHandle<StringTable>& resourceHandle, ScriptStringTable** out);
  61. /**
  62. * @brief Creates a new managed instance and interop object for the specified resource.
  63. *
  64. * @param resourceHandle Native resource to wrap in a managed instance.
  65. * @param out Output interop object corresponding to the new managed instance.
  66. *
  67. * @note Throws an exception if a managed instance for the provided resource already exists.
  68. */
  69. template<>
  70. void createScriptResource(const HResource& resourceHandle, ScriptResourceBase** out);
  71. /**
  72. * @brief Attempts to find a resource interop object for a resource with the specified UUID.
  73. * Returns null if the object cannot be found.
  74. */
  75. ScriptResourceBase* getScriptResource(const String& UUID);
  76. /**
  77. * @brief Deletes the provided resource interop objects. All resource interop objects
  78. * should be deleted using this method.
  79. */
  80. void destroyScriptResource(ScriptResourceBase* resource);
  81. private:
  82. /**
  83. * @brief Triggered when the native resource has been unloaded and therefore destroyed.
  84. */
  85. void onResourceDestroyed(const HResource& resource);
  86. /**
  87. * @brief Throws an exception if the provided UUID already exists in the interop object
  88. * lookup table.
  89. */
  90. void throwExceptionIfInvalidOrDuplicate(const String& uuid) const;
  91. UnorderedMap<String, ScriptResourceBase*> mScriptResources;
  92. HEvent mResourceDestroyedConn;
  93. };
  94. }