BsScriptResourceManager.cpp 14 KB

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