BsScriptResourceManager.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. namespace bs
  7. {
  8. namespace Detail { }
  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 interop object for the specified builtin resource.
  23. *
  24. * @param[in] resource Native resource to link to the managed instance.
  25. * @param[in] existingInstance Existing managed instance. Caller must ensure the managed instance matches the
  26. * native resource type. If not provided new object instance will be created
  27. * internally.
  28. * @return Interop object corresponding to the managed instance.
  29. *
  30. * @note Throws an exception if a managed instance for the provided resource already exists.
  31. */
  32. ScriptResourceBase* createBuiltinScriptResource(const HResource& resource, MonoObject* existingInstance = nullptr);
  33. /**
  34. * Creates a new interop object for the specified custom managed resource.
  35. *
  36. * @param[in] resource Native resource to link to the managed instance.
  37. * @param[in] existingInstance Existing managed instance of the resource.
  38. * @return Interop object corresponding to the managed instance.
  39. *
  40. * @note Throws an exception if a managed instance for the provided resource already exists.
  41. */
  42. ScriptManagedResource* createManagedScriptResource(const HManagedResource& resource, MonoObject* existingInstance);
  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] resource Resource to search for.
  48. * @param[in] create If a resource cannot be found new one will be created when this is true. If false
  49. * and the resource doesn't exist it will be null.
  50. * @return Found or created interop object containing the resource.
  51. */
  52. ScriptResourceBase* getScriptResource(const HResource& resource, bool create = false);
  53. /**
  54. * Attempts to find a resource interop object for a resource with the specified UUID. Returns null if the object
  55. * cannot be found.
  56. */
  57. ScriptResourceBase* getScriptResource(const UUID& UUID);
  58. /**
  59. * Deletes the provided resource interop objects. All resource interop objects should be deleted using this method.
  60. */
  61. void destroyScriptResource(ScriptResourceBase* resource);
  62. /** Throws an exception if the provided UUID already exists in the interop object lookup table. */
  63. void _throwExceptionIfInvalidOrDuplicate(const UUID& uuid) const;
  64. private:
  65. /** Triggered when the native resource has been unloaded and therefore destroyed. */
  66. void onResourceDestroyed(const UUID& UUID);
  67. UnorderedMap<UUID, ScriptResourceBase*> mScriptResources;
  68. HEvent mResourceDestroyedConn;
  69. };
  70. /** @} */
  71. }