BsScriptResource.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. /** Returns a generic handle to the internal wrapped resource. */
  47. HResource getGenericHandle() const override { return mResource; }
  48. /** Sets the internal resource this object wraps. */
  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. /**
  62. * Called after assembly reload starts to give the object a chance to restore the data backed up by the previous
  63. * beginRefresh() call.
  64. */
  65. virtual void endRefresh(const ScriptObjectBackup& backupData) override
  66. {
  67. BS_ASSERT(!mHandleValid);
  68. mManagedHandle = mono_gchandle_new(mManagedInstance, false);
  69. ScriptObject::endRefresh(backupData);
  70. }
  71. /**
  72. * Triggered by the script resource managed when the native resource handle this object point to has been destroyed.
  73. */
  74. void notifyResourceDestroyed() override
  75. {
  76. mono_gchandle_free(mManagedHandle);
  77. BS_DEBUG_ONLY(mHandleValid = false);
  78. }
  79. /** Called when the managed instance gets finalized by the CLR. */
  80. void _onManagedInstanceDeleted() override
  81. {
  82. mono_gchandle_free(mManagedHandle);
  83. BS_DEBUG_ONLY(mHandleValid = false);
  84. destroy();
  85. }
  86. ResourceHandle<ResType> mResource;
  87. uint32_t mManagedHandle;
  88. BS_DEBUG_ONLY(bool mHandleValid);
  89. };
  90. /** Interop class between C++ & CLR for Resource. */
  91. class BS_SCR_BE_EXPORT ScriptResource : public ScriptObject<ScriptResource, ScriptResourceBase>
  92. {
  93. public:
  94. SCRIPT_OBJ(ENGINE_ASSEMBLY, "BansheeEngine", "Resource")
  95. /** Converts a RTTI id belonging to a resource type into a ScriptResourceType. */
  96. static ScriptResourceType getTypeFromTypeId(UINT32 typeId);
  97. /** Converts a ScriptResourceType into a RTTI id belonging to that resource type. */
  98. static UINT32 getTypeIdFromType(ScriptResourceType type);
  99. /** Converts a RTTI id belonging to a resource type into a managed class representing that type. */
  100. static MonoClass* ScriptResource::getClassFromTypeId(UINT32 typeId);
  101. private:
  102. ScriptResource(MonoObject* instance)
  103. :ScriptObject(instance)
  104. { }
  105. /************************************************************************/
  106. /* CLR HOOKS */
  107. /************************************************************************/
  108. static MonoString* internal_getName(ScriptResourceBase* nativeInstance);
  109. static MonoString* internal_getUUID(ScriptResourceBase* nativeInstance);
  110. static void internal_release(ScriptResourceBase* nativeInstance);
  111. };
  112. /** @} */
  113. }