BsScriptResource.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #pragma once
  2. #include "BsScriptEnginePrerequisites.h"
  3. #include "BsScriptObject.h"
  4. namespace BansheeEngine
  5. {
  6. /**
  7. * @brief Types of resources accessible from script code.
  8. */
  9. enum class ScriptResourceType // Note: Must be the same as C# enum ScriptResourceType
  10. {
  11. Texture, SpriteTexture, Mesh, Font, Shader, Material, Prefab, PlainText, ScriptCode, StringTable, GUISkin, Undefined
  12. };
  13. /**
  14. * @brief Base class for all resource interop objects.
  15. */
  16. class BS_SCR_BE_EXPORT ScriptResourceBase : public PersistentScriptObjectBase
  17. {
  18. public:
  19. /**
  20. * @brief Returns a generic handle to the internal wrapped resource.
  21. */
  22. virtual HResource getGenericHandle() const = 0;
  23. /**
  24. * @brief Sets the internal resource this object wraps.
  25. */
  26. virtual void setResource(const HResource& resource) = 0;
  27. /**
  28. * @copydoc ScriptObjectBase::beginRefresh
  29. */
  30. virtual ScriptObjectBackup beginRefresh() override;
  31. /**
  32. * @copydoc ScriptObjectBase::endRefresh
  33. */
  34. virtual void endRefresh(const ScriptObjectBackup& backupData) override;
  35. protected:
  36. friend class ScriptResourceManager;
  37. ScriptResourceBase(MonoObject* instance);
  38. virtual ~ScriptResourceBase() {}
  39. /**
  40. * @brief Triggered by the script resource managed when the native resource handle this object point to has
  41. * been destroyed.
  42. */
  43. virtual void notifyResourceDestroyed() { }
  44. /**
  45. * @brief Destroys the interop object, unless refresh is in progress in which case it is just prepared
  46. * for re-creation.
  47. */
  48. void destroy();
  49. bool mRefreshInProgress;
  50. };
  51. /**
  52. * @brief Base class for a specific resource's interop object.
  53. */
  54. template<class ScriptClass, class ResType>
  55. class BS_SCR_BE_EXPORT TScriptResource : public ScriptObject <ScriptClass, ScriptResourceBase>
  56. {
  57. public:
  58. /**
  59. * @copydoc ScriptResourceBase::getGenericHandle
  60. */
  61. HResource getGenericHandle() const override { return mResource; }
  62. /**
  63. * @copydoc ScriptResourceBase::setResource
  64. */
  65. void setResource(const HResource& resource) override { mResource = static_resource_cast<ResType>(resource); }
  66. /**
  67. * @brief Returns a handle to the internal wrapped resource.
  68. */
  69. const ResourceHandle<ResType>& getHandle() const { return mResource; }
  70. protected:
  71. friend class ScriptResourceManager;
  72. TScriptResource(MonoObject* instance, const ResourceHandle<ResType>& resource)
  73. :ScriptObject(instance), mResource(resource)
  74. {
  75. mManagedHandle = mono_gchandle_new(instance, false);
  76. BS_DEBUG_ONLY(mHandleValid = true);
  77. }
  78. virtual ~TScriptResource() {}
  79. /**
  80. * @copydoc ScriptObjectBase::endRefresh
  81. */
  82. virtual void endRefresh(const ScriptObjectBackup& backupData) override
  83. {
  84. BS_ASSERT(!mHandleValid);
  85. mManagedHandle = mono_gchandle_new(mManagedInstance, false);
  86. ScriptObject::endRefresh(backupData);
  87. }
  88. /**
  89. * @copydoc ScriptObjectBase::notifyResourceDestroyed
  90. */
  91. void notifyResourceDestroyed() override
  92. {
  93. mono_gchandle_free(mManagedHandle);
  94. BS_DEBUG_ONLY(mHandleValid = false);
  95. }
  96. /**
  97. * @copydoc ScriptObjectBase::_onManagedInstanceDeleted
  98. */
  99. void _onManagedInstanceDeleted() override
  100. {
  101. mono_gchandle_free(mManagedHandle);
  102. BS_DEBUG_ONLY(mHandleValid = false);
  103. destroy();
  104. }
  105. ResourceHandle<ResType> mResource;
  106. uint32_t mManagedHandle;
  107. BS_DEBUG_ONLY(bool mHandleValid);
  108. };
  109. /**
  110. * @brief Interop class between C++ & CLR for Resource.
  111. */
  112. class BS_SCR_BE_EXPORT ScriptResource : public ScriptObject<ScriptResource, ScriptResourceBase>
  113. {
  114. public:
  115. SCRIPT_OBJ(ENGINE_ASSEMBLY, "BansheeEngine", "Resource")
  116. /**
  117. * @brief Converts a RTTI id belonging to a resource type into a ScriptResourceType.
  118. */
  119. static ScriptResourceType getTypeFromTypeId(UINT32 typeId);
  120. /**
  121. * @brief Converts a ScriptResourceType into a RTTI id belonging to that resource type.
  122. */
  123. static UINT32 getTypeIdFromType(ScriptResourceType type);
  124. private:
  125. ScriptResource(MonoObject* instance)
  126. :ScriptObject(instance)
  127. { }
  128. };
  129. }