BsScriptResourceManager.h 4.7 KB

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