BsScriptResourceManager.cpp 13 KB

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