BsScriptResourceManager.cpp 17 KB

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