BsScriptResourceManager.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 "Utility/BsModule.h"
  6. #include "Wrappers/BsScriptResource.h"
  7. namespace bs
  8. {
  9. class ScriptRRefBase;
  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 interop object for the specified builtin resource.
  24. *
  25. * @param[in] resource Native resource to link to the managed instance.
  26. * @param[in] existingInstance Existing managed instance. Caller must ensure the managed instance matches the
  27. * native resource type. If not provided new object instance will be created
  28. * internally.
  29. * @return Interop object corresponding to the managed instance.
  30. *
  31. * @note Throws an exception if a managed instance for the provided resource already exists.
  32. */
  33. ScriptResourceBase* createBuiltinScriptResource(const HResource& resource, MonoObject* existingInstance = nullptr);
  34. /**
  35. * Creates a new interop object for the specified custom managed resource.
  36. *
  37. * @param[in] resource Native resource to link to the managed instance.
  38. * @param[in] existingInstance Existing managed instance of the resource.
  39. * @return Interop object corresponding to the managed instance.
  40. *
  41. * @note Throws an exception if a managed instance for the provided resource already exists.
  42. */
  43. ScriptManagedResource* createManagedScriptResource(const HManagedResource& resource, MonoObject* existingInstance);
  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] resource Resource to search for.
  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. * @return Found or created interop object containing the resource.
  52. */
  53. ScriptResourceBase* getScriptResource(const HResource& resource, 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 UUID& uuid);
  59. /**
  60. * Attempts to find an existing interop object for the specified resource reference, or creates a new object if one
  61. * cannot be found.
  62. *
  63. * @param[in] resource Resource handle to create the reference wrapper object for.
  64. * @param[in] rrefClass Class of the managed RRef object to create.
  65. */
  66. ScriptRRefBase* getScriptRRef(const HResource& resource, ::MonoClass* rrefClass);
  67. /**
  68. * Same as getScriptRRef(const HResource&, MonoClass*) except it automatically deduced the resource class from
  69. * the provided template parameter.
  70. */
  71. template<class T>
  72. ScriptRRefBase* getScriptRRef(const ResourceHandle<T>& resource)
  73. {
  74. ::MonoClass* rrefClass = ScriptResourceBase::getRRefClass(T::getRTTIStatic()->getRTTIId());
  75. return getScriptRRef(resource, rrefClass);
  76. }
  77. /**
  78. * Deletes the provided resource interop objects. All resource interop objects should be deleted using this method.
  79. */
  80. void destroyScriptResource(ScriptResourceBase* resource);
  81. /** Throws an exception if the provided UUID already exists in the interop object lookup table. */
  82. void _throwExceptionIfInvalidOrDuplicate(const UUID& uuid) const;
  83. private:
  84. /** Triggered when the native resource has been unloaded and therefore destroyed. */
  85. void onResourceDestroyed(const UUID& UUID);
  86. /**
  87. * Clears all cached RRefs. Should be called before assembly refresh since the refs will no longer be valid
  88. * after.
  89. */
  90. void clearRRefs();
  91. UnorderedMap<UUID, ScriptResourceBase*> mScriptResources;
  92. UnorderedMap<::MonoClass*, UnorderedMap<UUID, ScriptRRefBase*>> mScriptRRefsPerType;
  93. HEvent mResourceDestroyedConn;
  94. HEvent mDomainUnloadedConn;
  95. };
  96. /** @} */
  97. }