BsScriptResourceManager.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #include "BsScriptResourceManager.h"
  2. #include "BsMonoManager.h"
  3. #include "BsMonoAssembly.h"
  4. #include "BsMonoClass.h"
  5. #include "BsResources.h"
  6. #include "BsScriptTexture2D.h"
  7. #include "BsScriptTexture3D.h"
  8. #include "BsScriptTextureCube.h"
  9. #include "BsScriptSpriteTexture.h"
  10. #include "BsScriptPlainText.h"
  11. #include "BsScriptScriptCode.h"
  12. #include "BsScriptShader.h"
  13. #include "BsScriptShaderInclude.h"
  14. #include "BsScriptMaterial.h"
  15. #include "BsScriptMesh.h"
  16. #include "BsScriptFont.h"
  17. #include "BsScriptPrefab.h"
  18. #include "BsScriptStringTable.h"
  19. #include "BsScriptGUISkin.h"
  20. #include "BsScriptManagedResource.h"
  21. #include "BsScriptAssemblyManager.h"
  22. using namespace std::placeholders;
  23. namespace BansheeEngine
  24. {
  25. ScriptResourceManager::ScriptResourceManager()
  26. {
  27. mResourceDestroyedConn = gResources().onResourceDestroyed.connect(std::bind(&ScriptResourceManager::onResourceDestroyed, this, _1));
  28. }
  29. ScriptResourceManager::~ScriptResourceManager()
  30. {
  31. mResourceDestroyedConn.disconnect();
  32. }
  33. template<class RetType, class InType>
  34. void ScriptResourceManager::createScriptResource(const ResourceHandle<InType>& resourceHandle, RetType** out)
  35. {
  36. MonoClass* resourceClass = RetType::getMetaData()->scriptClass;
  37. MonoObject* monoInstance = resourceClass->createInstance();
  38. createScriptResource(monoInstance, resourceHandle, out);
  39. }
  40. template<class RetType, class InType>
  41. void ScriptResourceManager::createScriptResource(MonoObject* instance, const ResourceHandle<InType>& resourceHandle, RetType** out)
  42. {
  43. const String& uuid = resourceHandle.getUUID();
  44. #if BS_DEBUG_MODE
  45. throwExceptionIfInvalidOrDuplicate(uuid);
  46. #endif
  47. RetType* scriptResource = new (bs_alloc<RetType>()) RetType(instance, resourceHandle);
  48. mScriptResources[uuid] = scriptResource;
  49. *out = scriptResource;
  50. }
  51. template<class RetType, class InType>
  52. void ScriptResourceManager::getScriptResource(const ResourceHandle<InType>& resourceHandle, RetType** out, bool create)
  53. {
  54. String uuid = resourceHandle.getUUID();
  55. if (!uuid.empty())
  56. {
  57. *out = static_cast<RetType*>(getScriptResource(uuid));
  58. if (*out == nullptr && create)
  59. createScriptResource(resourceHandle, out);
  60. }
  61. else
  62. *out = nullptr;
  63. }
  64. template<>
  65. void ScriptResourceManager::createScriptResource(const ResourceHandle<StringTable>& resourceHandle, ScriptStringTable** out)
  66. {
  67. MonoClass* resourceClass = ScriptStringTable::getMetaData()->scriptClass;
  68. bool dummy = true;
  69. void* params = { &dummy };
  70. MonoObject* monoInstance = resourceClass->createInstance(&params, 1);
  71. createScriptResource(monoInstance, resourceHandle, out);
  72. }
  73. template<>
  74. void ScriptResourceManager::createScriptResource(const ResourceHandle<Texture>& resourceHandle, ScriptTextureBase** out)
  75. {
  76. TextureType type = resourceHandle->getProperties().getTextureType();
  77. if (type == TEX_TYPE_3D)
  78. return createScriptResource(resourceHandle, (ScriptTexture3D**)out);
  79. else if (type == TEX_TYPE_CUBE_MAP)
  80. return createScriptResource(resourceHandle, (ScriptTextureCube**)out);
  81. else
  82. return createScriptResource(resourceHandle, (ScriptTexture2D**)out);
  83. }
  84. template<>
  85. void ScriptResourceManager::createScriptResource(const HResource& resourceHandle, ScriptResourceBase** out)
  86. {
  87. #if BS_DEBUG_MODE
  88. throwExceptionIfInvalidOrDuplicate(resourceHandle.getUUID());
  89. #endif
  90. UINT32 resTypeID = resourceHandle->getTypeId();
  91. switch (resTypeID)
  92. {
  93. case TID_Texture:
  94. {
  95. HTexture texture = static_resource_cast<Texture>(resourceHandle);
  96. TextureType type = texture->getProperties().getTextureType();
  97. if (type == TEX_TYPE_3D)
  98. return createScriptResource(texture, (ScriptTexture3D**)out);
  99. else if (type == TEX_TYPE_CUBE_MAP)
  100. return createScriptResource(texture, (ScriptTextureCube**)out);
  101. else
  102. return createScriptResource(texture, (ScriptTexture2D**)out);
  103. }
  104. case TID_SpriteTexture:
  105. return createScriptResource(static_resource_cast<SpriteTexture>(resourceHandle), (ScriptSpriteTexture**)out);
  106. case TID_Font:
  107. return createScriptResource(static_resource_cast<Font>(resourceHandle), (ScriptFont**)out);
  108. case TID_PlainText:
  109. return createScriptResource(static_resource_cast<PlainText>(resourceHandle), (ScriptPlainText**)out);
  110. case TID_ScriptCode:
  111. return createScriptResource(static_resource_cast<ScriptCode>(resourceHandle), (ScriptScriptCode**)out);
  112. case TID_Shader:
  113. return createScriptResource(static_resource_cast<Shader>(resourceHandle), (ScriptShader**)out);
  114. case TID_ShaderInclude:
  115. return createScriptResource(static_resource_cast<ShaderInclude>(resourceHandle), (ScriptShaderInclude**)out);
  116. case TID_Prefab:
  117. return createScriptResource(static_resource_cast<Prefab>(resourceHandle), (ScriptPrefab**)out);
  118. case TID_StringTable:
  119. return createScriptResource(static_resource_cast<StringTable>(resourceHandle), (ScriptStringTable**)out);
  120. case TID_Material:
  121. return createScriptResource(static_resource_cast<Material>(resourceHandle), (ScriptMaterial**)out);
  122. case TID_Mesh:
  123. return createScriptResource(static_resource_cast<Mesh>(resourceHandle), (ScriptMesh**)out);
  124. case TID_GUISkin:
  125. return createScriptResource(static_resource_cast<GUISkin>(resourceHandle), (ScriptGUISkin**)out);
  126. case TID_ManagedResource:
  127. BS_EXCEPT(InternalErrorException, "Managed resources must have a managed instance by default, this call is invalid.")
  128. break;
  129. default:
  130. BS_EXCEPT(NotImplementedException, "Attempting to load a resource type that is not supported. Type ID: " + toString(resTypeID));
  131. break;
  132. }
  133. }
  134. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(const ResourceHandle<Texture>&, ScriptTexture2D**);
  135. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(const ResourceHandle<Texture>&, ScriptTexture3D**);
  136. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(const ResourceHandle<Texture>&, ScriptTextureCube**);
  137. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(const ResourceHandle<Texture>&, ScriptTextureBase**);
  138. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(const ResourceHandle<SpriteTexture>&, ScriptSpriteTexture**);
  139. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(const ResourceHandle<Mesh>&, ScriptMesh**);
  140. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(const ResourceHandle<Material>&, ScriptMaterial**);
  141. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(const ResourceHandle<Shader>&, ScriptShader**);
  142. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(const ResourceHandle<ShaderInclude>&, ScriptShaderInclude**);
  143. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(const ResourceHandle<Prefab>&, ScriptPrefab**);
  144. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(const ResourceHandle<Font>&, ScriptFont**);
  145. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(const ResourceHandle<PlainText>&, ScriptPlainText**);
  146. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(const ResourceHandle<ScriptCode>&, ScriptScriptCode**);
  147. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(const ResourceHandle<StringTable>&, ScriptStringTable**);
  148. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(const ResourceHandle<GUISkin>&, ScriptGUISkin**);
  149. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(MonoObject*, const ResourceHandle<Texture>&, ScriptTexture2D**);
  150. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(MonoObject*, const ResourceHandle<Texture>&, ScriptTexture3D**);
  151. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(MonoObject*, const ResourceHandle<Texture>&, ScriptTextureCube**);
  152. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(MonoObject*, const ResourceHandle<SpriteTexture>&, ScriptSpriteTexture**);
  153. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(MonoObject*, const ResourceHandle<Mesh>&, ScriptMesh**);
  154. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(MonoObject*, const ResourceHandle<Material>&, ScriptMaterial**);
  155. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(MonoObject*, const ResourceHandle<Shader>&, ScriptShader**);
  156. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(MonoObject*, const ResourceHandle<ShaderInclude>&, ScriptShaderInclude**);
  157. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(MonoObject*, const ResourceHandle<Prefab>&, ScriptPrefab**);
  158. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(MonoObject*, const ResourceHandle<Font>&, ScriptFont**);
  159. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(MonoObject*, const ResourceHandle<PlainText>&, ScriptPlainText**);
  160. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(MonoObject*, const ResourceHandle<ScriptCode>&, ScriptScriptCode**);
  161. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(MonoObject*, const ResourceHandle<StringTable>&, ScriptStringTable**);
  162. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(MonoObject*, const ResourceHandle<GUISkin>&, ScriptGUISkin**);
  163. template BS_SCR_BE_EXPORT void ScriptResourceManager::createScriptResource(MonoObject*, const ResourceHandle<ManagedResource>&, ScriptManagedResource**);
  164. template BS_SCR_BE_EXPORT void ScriptResourceManager::getScriptResource(const ResourceHandle<Texture>& resourceHandle, ScriptTexture2D** out, bool create);
  165. template BS_SCR_BE_EXPORT void ScriptResourceManager::getScriptResource(const ResourceHandle<Texture>& resourceHandle, ScriptTexture3D** out, bool create);
  166. template BS_SCR_BE_EXPORT void ScriptResourceManager::getScriptResource(const ResourceHandle<Texture>& resourceHandle, ScriptTextureCube** out, bool create);
  167. template BS_SCR_BE_EXPORT void ScriptResourceManager::getScriptResource(const ResourceHandle<Texture>& resourceHandle, ScriptTextureBase** out, bool create);
  168. template BS_SCR_BE_EXPORT void ScriptResourceManager::getScriptResource(const ResourceHandle<SpriteTexture>& resourceHandle, ScriptSpriteTexture** out, bool create);
  169. template BS_SCR_BE_EXPORT void ScriptResourceManager::getScriptResource(const ResourceHandle<Mesh>& resourceHandle, ScriptMesh** out, bool create);
  170. template BS_SCR_BE_EXPORT void ScriptResourceManager::getScriptResource(const ResourceHandle<Material>& resourceHandle, ScriptMaterial** out, bool create);
  171. template BS_SCR_BE_EXPORT void ScriptResourceManager::getScriptResource(const ResourceHandle<Shader>& resourceHandle, ScriptShader** out, bool create);
  172. template BS_SCR_BE_EXPORT void ScriptResourceManager::getScriptResource(const ResourceHandle<ShaderInclude>& resourceHandle, ScriptShaderInclude** out, bool create);
  173. template BS_SCR_BE_EXPORT void ScriptResourceManager::getScriptResource(const ResourceHandle<Prefab>& resourceHandle, ScriptPrefab** out, bool create);
  174. template BS_SCR_BE_EXPORT void ScriptResourceManager::getScriptResource(const ResourceHandle<Font>& resourceHandle, ScriptFont** out, bool create);
  175. template BS_SCR_BE_EXPORT void ScriptResourceManager::getScriptResource(const ResourceHandle<PlainText>& resourceHandle, ScriptPlainText** out, bool create);
  176. template BS_SCR_BE_EXPORT void ScriptResourceManager::getScriptResource(const ResourceHandle<ScriptCode>& resourceHandle, ScriptScriptCode** out, bool create);
  177. template BS_SCR_BE_EXPORT void ScriptResourceManager::getScriptResource(const ResourceHandle<StringTable>& resourceHandle, ScriptStringTable** out, bool create);
  178. template BS_SCR_BE_EXPORT void ScriptResourceManager::getScriptResource(const ResourceHandle<GUISkin>& resourceHandle, ScriptGUISkin** out, bool create);
  179. template BS_SCR_BE_EXPORT void ScriptResourceManager::getScriptResource(const ResourceHandle<ManagedResource>& resourceHandle, ScriptManagedResource** out, bool create);
  180. template BS_SCR_BE_EXPORT void ScriptResourceManager::getScriptResource(const ResourceHandle<Resource>& resourceHandle, ScriptResourceBase** out, bool create);
  181. ScriptResourceBase* ScriptResourceManager::getScriptResource(const String& uuid)
  182. {
  183. if (uuid == "")
  184. return nullptr;
  185. auto findIter = mScriptResources.find(uuid);
  186. if(findIter != mScriptResources.end())
  187. return findIter->second;
  188. return nullptr;
  189. }
  190. void ScriptResourceManager::destroyScriptResource(ScriptResourceBase* resource)
  191. {
  192. HResource resourceHandle = resource->getGenericHandle();
  193. const String& uuid = resourceHandle.getUUID();
  194. if(uuid == "")
  195. BS_EXCEPT(InvalidParametersException, "Provided resource handle has an undefined resource UUID.");
  196. (resource)->~ScriptResourceBase();
  197. MemoryAllocator<GenAlloc>::free(resource);
  198. auto findIter = mScriptResources.erase(uuid);
  199. }
  200. void ScriptResourceManager::onResourceDestroyed(const HResource& resource)
  201. {
  202. const String& uuid = resource.getUUID();
  203. auto findIter = mScriptResources.find(uuid);
  204. if (findIter != mScriptResources.end())
  205. {
  206. findIter->second->notifyResourceDestroyed();
  207. mScriptResources.erase(findIter);
  208. }
  209. }
  210. void ScriptResourceManager::throwExceptionIfInvalidOrDuplicate(const String& uuid) const
  211. {
  212. if(uuid == "")
  213. BS_EXCEPT(InvalidParametersException, "Provided resource handle has an undefined resource UUID.");
  214. auto findIter = mScriptResources.find(uuid);
  215. if(findIter != mScriptResources.end())
  216. {
  217. BS_EXCEPT(InvalidStateException, "Provided resource handle already has a script resource. \
  218. Retrieve the existing instance instead of creating a new one.");
  219. }
  220. }
  221. }