BsManagedResource.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 "Resources/BsResource.h"
  6. namespace bs
  7. {
  8. /** @addtogroup SBansheeEngine
  9. * @{
  10. */
  11. struct ResourceBackupData;
  12. /** Resource that internally wraps a managed resource object that can be of user-defined type. */
  13. class BS_SCR_BE_EXPORT ManagedResource : public Resource
  14. {
  15. public:
  16. /** Returns the internal managed resource object. */
  17. MonoObject* getManagedInstance() const;
  18. /**
  19. * Serializes the internal managed resource.
  20. *
  21. * @return An object containing the serialized resource. You can provide this to restore()
  22. * method to re-create the original resource.
  23. */
  24. ResourceBackupData backup();
  25. /**
  26. * Restores a resource from previously serialized data.
  27. *
  28. * @param[in] data Serialized managed resource data that will be used for initializing the new managed
  29. * instance.
  30. */
  31. void restore(const ResourceBackupData& data);
  32. /**
  33. * Creates a new managed resource wrapper from an actual managed resource object. Caller must ensure the provided
  34. * instance actually derives from Resource class.
  35. */
  36. static HManagedResource create(MonoObject* managedResource);
  37. /**
  38. * Creates an empty managed resource wrapper pointing to no managed instance. You must call setHandle() before use
  39. * manually.
  40. */
  41. static SPtr<ManagedResource> createEmpty();
  42. private:
  43. friend class ScriptManagedResource;
  44. ManagedResource(MonoObject* managedInstance);
  45. /**
  46. * Finalizes construction of the object. Must be called before use or when the managed resource instance changes.
  47. *
  48. * @param[in] object Managed resource instance.
  49. * @param[in] myHandle Handle to myself.
  50. */
  51. void setHandle(MonoObject* object, const HManagedResource& myHandle);
  52. /** @copydoc Resource::destroy */
  53. void destroy() override;
  54. WeakResourceHandle<ManagedResource> mMyHandle;
  55. ScriptManagedResource* mOwner = nullptr;
  56. /************************************************************************/
  57. /* RTTI */
  58. /************************************************************************/
  59. public:
  60. friend class ManagedResourceRTTI;
  61. static RTTITypeBase* getRTTIStatic();
  62. RTTITypeBase* getRTTI() const override;
  63. protected:
  64. ManagedResource(); // Serialization only
  65. };
  66. /** Contains serialized resource data buffer. */
  67. struct ResourceBackupData
  68. {
  69. UINT8* data;
  70. UINT32 size;
  71. };
  72. /** @} */
  73. }