BsScriptResourceManager.h 4.4 KB

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