BsManagedResource.h 3.3 KB

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