BsManagedResource.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. * @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. UINT32 mGCHandle;
  60. WeakResourceHandle<ManagedResource> mMyHandle;
  61. /************************************************************************/
  62. /* RTTI */
  63. /************************************************************************/
  64. public:
  65. friend class ManagedResourceRTTI;
  66. static RTTITypeBase* getRTTIStatic();
  67. virtual RTTITypeBase* getRTTI() const override;
  68. protected:
  69. ManagedResource(); // Serialization only
  70. };
  71. /** Contains serialized resource data buffer. */
  72. struct ResourceBackupData
  73. {
  74. UINT8* data;
  75. UINT32 size;
  76. };
  77. /** @} */
  78. }