BsManagedResource.h 2.9 KB

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