BsScriptResourceManager.cpp 13 KB

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