BsScriptResourceManager.cpp 17 KB

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