BsManagedResource.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 "BsResource.h"
  6. namespace BansheeEngine
  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 { return mManagedInstance; }
  18. /**
  19. * Serializes the internal managed resource.
  20. *
  21. * @param[in] clearExisting Should the managed resource handle be released. (Will trigger a finalizer if this is
  22. * the last reference to it)
  23. * @return An object containing the serialized resource. You can provide this to restore()
  24. * method to re-create the original resource.
  25. */
  26. ResourceBackupData backup(bool clearExisting = true);
  27. /**
  28. * Restores a resource from previously serialized data.
  29. *
  30. * @param[in] instance New instance of the managed resource. Must be of the valid resource type this object was
  31. * originally created from. Can be null if the type cannot be found (can happen after an
  32. * assembly refresh).
  33. * @param[in] data Serialized managed resource data that will be used for initializing the new managed
  34. * instance.
  35. */
  36. void restore(MonoObject* instance, const ResourceBackupData& data);
  37. /**
  38. * Creates a new managed resource wrapper from an actual managed resource object. Caller must ensure the provided
  39. * instance actually derives from Resource class.
  40. */
  41. static HManagedResource create(MonoObject* managedResource);
  42. /**
  43. * Creates an empty managed resource wrapper pointing to no managed instance. You must call setHandle() before use
  44. * manually.
  45. */
  46. static SPtr<ManagedResource> createEmpty();
  47. private:
  48. friend class ScriptManagedResource;
  49. ManagedResource(MonoObject* managedInstance);
  50. /**
  51. * Finalizes construction of the object. Must be called before use or when the managed resource instance changes.
  52. *
  53. * @param[in] object Managed resource instance.
  54. * @param[in] myHandle Handle to myself.
  55. */
  56. void setHandle(MonoObject* object, const HManagedResource& myHandle);
  57. /** @copydoc Resource::destroy */
  58. void destroy() override;
  59. MonoObject* mManagedInstance;
  60. uint32_t mManagedHandle;
  61. WeakResourceHandle<ManagedResource> mMyHandle;
  62. /************************************************************************/
  63. /* RTTI */
  64. /************************************************************************/
  65. public:
  66. friend class ManagedResourceRTTI;
  67. static RTTITypeBase* getRTTIStatic();
  68. virtual RTTITypeBase* getRTTI() const override;
  69. protected:
  70. ManagedResource(); // Serialization only
  71. };
  72. /** Contains serialized resource data buffer. */
  73. struct ResourceBackupData
  74. {
  75. UINT8* data;
  76. UINT32 size;
  77. };
  78. /** @} */
  79. }