BsScriptResource.h 4.7 KB

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