BsScriptResourceManager.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. /** @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. * Creates a new managed instance and interop object for the specified string table.
  56. *
  57. * @param[in] resourceHandle Native string table resource to wrap in a managed instance.
  58. * @param[out] out Output string table interop object corresponding to the new managed instance.
  59. *
  60. * @note Throws an exception if a managed instance for the provided resource already exists.
  61. */
  62. template<>
  63. void createScriptResource(const ResourceHandle<StringTable>& resourceHandle, ScriptStringTable** out);
  64. /**
  65. * Creates a new managed instance and interop object for the specified resource.
  66. *
  67. * @param[in] resourceHandle Native resource to wrap in a managed instance.
  68. * @param[out] out Output interop object corresponding to the new managed instance.
  69. *
  70. * @note Throws an exception if a managed instance for the provided resource already exists.
  71. */
  72. template<>
  73. void createScriptResource(const HResource& resourceHandle, ScriptResourceBase** out);
  74. /**
  75. * Attempts to find a resource interop object for a resource with the specified UUID. Returns null if the object
  76. * cannot be found.
  77. */
  78. ScriptResourceBase* getScriptResource(const String& UUID);
  79. /**
  80. * Deletes the provided resource interop objects. All resource interop objects should be deleted using this method.
  81. */
  82. void destroyScriptResource(ScriptResourceBase* resource);
  83. private:
  84. /** Triggered when the native resource has been unloaded and therefore destroyed. */
  85. void onResourceDestroyed(const String& UUID);
  86. /** Throws an exception if the provided UUID already exists in the interop object lookup table. */
  87. void throwExceptionIfInvalidOrDuplicate(const String& uuid) const;
  88. UnorderedMap<String, ScriptResourceBase*> mScriptResources;
  89. HEvent mResourceDestroyedConn;
  90. };
  91. /** @} */
  92. }