BsScriptResource.h 4.9 KB

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