BsScriptResourceManager.cpp 16 KB

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