BsScriptResource.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 "BsScriptObject.h"
  6. #include "BsMonoUtil.h"
  7. namespace bs
  8. {
  9. /** @addtogroup ScriptInteropEngine
  10. * @{
  11. */
  12. /** Base class for all resource interop objects. */
  13. class BS_SCR_BE_EXPORT ScriptResourceBase : public PersistentScriptObjectBase
  14. {
  15. public:
  16. /** Returns a generic handle to the internal wrapped resource. */
  17. virtual HResource getGenericHandle() const = 0;
  18. /** Sets the internal resource this object wraps. */
  19. virtual void setResource(const HResource& resource) = 0;
  20. /** @copydoc ScriptObjectBase::beginRefresh */
  21. ScriptObjectBackup beginRefresh() override;
  22. /** @copydoc ScriptObjectBase::endRefresh */
  23. void endRefresh(const ScriptObjectBackup& backupData) override;
  24. protected:
  25. friend class ScriptResourceManager;
  26. ScriptResourceBase(MonoObject* instance);
  27. virtual ~ScriptResourceBase() {}
  28. /**
  29. * Triggered by the script resource managed when the native resource handle this object point to has been destroyed.
  30. */
  31. virtual void notifyResourceDestroyed() { }
  32. /** Destroys the interop object, unless refresh is in progress in which case it is just prepared for re-creation. */
  33. void destroy();
  34. bool mRefreshInProgress;
  35. };
  36. /** Base class for a specific resource's interop object. */
  37. template<class ScriptClass, class ResType, class BaseType = ScriptResourceBase>
  38. class BS_SCR_BE_EXPORT TScriptResource : public ScriptObject <ScriptClass, BaseType>
  39. {
  40. public:
  41. /** Returns a generic handle to the internal wrapped resource. */
  42. HResource getGenericHandle() const override { return mResource; }
  43. /** Sets the internal resource this object wraps. */
  44. void setResource(const HResource& resource) override { mResource = static_resource_cast<ResType>(resource); }
  45. /** Returns a handle to the internal wrapped resource. */
  46. const ResourceHandle<ResType>& getHandle() const { return mResource; }
  47. protected:
  48. friend class ScriptResourceManager;
  49. TScriptResource(MonoObject* instance, const ResourceHandle<ResType>& resource)
  50. :ScriptObject<ScriptClass, BaseType>(instance), mResource(resource)
  51. {
  52. mManagedHandle = MonoUtil::newGCHandle(instance);
  53. this->mManagedInstance = MonoUtil::getObjectFromGCHandle(mManagedHandle);
  54. BS_DEBUG_ONLY(mHandleValid = true);
  55. }
  56. virtual ~TScriptResource() {}
  57. /**
  58. * Called after assembly reload starts to give the object a chance to restore the data backed up by the previous
  59. * beginRefresh() call.
  60. */
  61. virtual void endRefresh(const ScriptObjectBackup& backupData) override
  62. {
  63. BS_ASSERT(!mHandleValid);
  64. mManagedHandle = MonoUtil::newGCHandle(this->mManagedInstance);
  65. this->mManagedInstance = MonoUtil::getObjectFromGCHandle(mManagedHandle);
  66. ScriptObject<ScriptClass, BaseType>::endRefresh(backupData);
  67. }
  68. /**
  69. * Triggered by the script resource managed when the native resource handle this object point to has been destroyed.
  70. */
  71. void notifyResourceDestroyed() override
  72. {
  73. MonoUtil::freeGCHandle(mManagedHandle);
  74. BS_DEBUG_ONLY(mHandleValid = false);
  75. }
  76. /** Called when the managed instance gets finalized by the CLR. */
  77. void _onManagedInstanceDeleted() override
  78. {
  79. MonoUtil::freeGCHandle(mManagedHandle);
  80. BS_DEBUG_ONLY(mHandleValid = false);
  81. this->destroy();
  82. }
  83. ResourceHandle<ResType> mResource;
  84. uint32_t mManagedHandle;
  85. BS_DEBUG_ONLY(bool mHandleValid);
  86. };
  87. /** Interop class between C++ & CLR for Resource. */
  88. class BS_SCR_BE_EXPORT ScriptResource : public ScriptObject<ScriptResource, ScriptResourceBase>
  89. {
  90. public:
  91. SCRIPT_OBJ(ENGINE_ASSEMBLY, "BansheeEngine", "Resource")
  92. private:
  93. ScriptResource(MonoObject* instance)
  94. :ScriptObject(instance)
  95. { }
  96. /************************************************************************/
  97. /* CLR HOOKS */
  98. /************************************************************************/
  99. static MonoString* internal_getName(ScriptResourceBase* nativeInstance);
  100. static void internal_getUUID(ScriptResourceBase* nativeInstance, UUID* uuid);
  101. static void internal_release(ScriptResourceBase* nativeInstance);
  102. };
  103. /** Interop class between C++ & CLR for UUID. */
  104. class BS_SCR_BE_EXPORT ScriptUUID : public ScriptObject<ScriptUUID>
  105. {
  106. public:
  107. SCRIPT_OBJ(ENGINE_ASSEMBLY, "BansheeEngine", "UUID")
  108. /** Unboxes a boxed managed UUID struct and returns the native version of the structure. */
  109. static UUID unbox(MonoObject* obj);
  110. /** Boxes a native UUID struct and returns a managed object containing it. */
  111. static MonoObject* box(const UUID& value);
  112. private:
  113. ScriptUUID(MonoObject* instance);
  114. };
  115. /** @} */
  116. }