2
0

BsManagedResource.h 3.3 KB

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