2
0

BsScriptResource.h 4.4 KB

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