2
0

BsScriptResourceManager.h 4.6 KB

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