BsScriptResource.h 5.0 KB

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