BsScriptResource.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. BS_DEBUG_ONLY(mHandleValid = true);
  54. }
  55. virtual ~TScriptResource() {}
  56. /**
  57. * Called after assembly reload starts to give the object a chance to restore the data backed up by the previous
  58. * beginRefresh() call.
  59. */
  60. virtual void endRefresh(const ScriptObjectBackup& backupData) override
  61. {
  62. BS_ASSERT(!mHandleValid);
  63. mManagedHandle = MonoUtil::newGCHandle(this->mManagedInstance);
  64. ScriptObject<ScriptClass, BaseType>::endRefresh(backupData);
  65. }
  66. /**
  67. * Triggered by the script resource managed when the native resource handle this object point to has been destroyed.
  68. */
  69. void notifyResourceDestroyed() override
  70. {
  71. MonoUtil::freeGCHandle(mManagedHandle);
  72. BS_DEBUG_ONLY(mHandleValid = false);
  73. }
  74. /** Called when the managed instance gets finalized by the CLR. */
  75. void _onManagedInstanceDeleted() override
  76. {
  77. MonoUtil::freeGCHandle(mManagedHandle);
  78. BS_DEBUG_ONLY(mHandleValid = false);
  79. this->destroy();
  80. }
  81. ResourceHandle<ResType> mResource;
  82. uint32_t mManagedHandle;
  83. BS_DEBUG_ONLY(bool mHandleValid);
  84. };
  85. /** Interop class between C++ & CLR for Resource. */
  86. class BS_SCR_BE_EXPORT ScriptResource : public ScriptObject<ScriptResource, ScriptResourceBase>
  87. {
  88. public:
  89. SCRIPT_OBJ(ENGINE_ASSEMBLY, "BansheeEngine", "Resource")
  90. private:
  91. ScriptResource(MonoObject* instance)
  92. :ScriptObject(instance)
  93. { }
  94. /************************************************************************/
  95. /* CLR HOOKS */
  96. /************************************************************************/
  97. static MonoString* internal_getName(ScriptResourceBase* nativeInstance);
  98. static MonoString* internal_getUUID(ScriptResourceBase* nativeInstance);
  99. static void internal_release(ScriptResourceBase* nativeInstance);
  100. };
  101. /** @} */
  102. }