BsScriptResourceManager.h 4.0 KB

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