BsImporter.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsImporter.h"
  4. #include "BsResource.h"
  5. #include "BsFileSystem.h"
  6. #include "BsSpecificImporter.h"
  7. #include "BsShaderIncludeImporter.h"
  8. #include "BsImportOptions.h"
  9. #include "BsDebug.h"
  10. #include "BsDataStream.h"
  11. #include "BsException.h"
  12. #include "BsUUID.h"
  13. #include "BsResources.h"
  14. namespace BansheeEngine
  15. {
  16. Importer::Importer()
  17. {
  18. _registerAssetImporter(bs_new<ShaderIncludeImporter>());
  19. }
  20. Importer::~Importer()
  21. {
  22. for(auto i = mAssetImporters.begin(); i != mAssetImporters.end(); ++i)
  23. {
  24. if((*i) != nullptr)
  25. bs_delete(*i);
  26. }
  27. mAssetImporters.clear();
  28. }
  29. bool Importer::supportsFileType(const WString& extension) const
  30. {
  31. for(auto iter = mAssetImporters.begin(); iter != mAssetImporters.end(); ++iter)
  32. {
  33. if(*iter != nullptr && (*iter)->isExtensionSupported(extension))
  34. return true;
  35. }
  36. return false;
  37. }
  38. bool Importer::supportsFileType(const UINT8* magicNumber, UINT32 magicNumSize) const
  39. {
  40. for(auto iter = mAssetImporters.begin(); iter != mAssetImporters.end(); ++iter)
  41. {
  42. if(*iter != nullptr && (*iter)->isMagicNumberSupported(magicNumber, magicNumSize))
  43. return true;
  44. }
  45. return false;
  46. }
  47. HResource Importer::import(const Path& inputFilePath, ConstImportOptionsPtr importOptions)
  48. {
  49. if(!FileSystem::isFile(inputFilePath))
  50. {
  51. LOGWRN("Trying to import asset that doesn't exists. Asset path: " + inputFilePath.toString());
  52. return HResource();
  53. }
  54. SpecificImporter* importer = getImporterForFile(inputFilePath);
  55. if(importer == nullptr)
  56. return HResource();
  57. if(importOptions == nullptr)
  58. importOptions = importer->getDefaultImportOptions();
  59. else
  60. {
  61. ConstImportOptionsPtr defaultImportOptions = importer->getDefaultImportOptions();
  62. if(importOptions->getTypeId() != defaultImportOptions->getTypeId())
  63. {
  64. BS_EXCEPT(InvalidParametersException, "Provided import options is not of valid type. " \
  65. "Expected: " + defaultImportOptions->getTypeName() + ". Got: " + importOptions->getTypeName() + ".");
  66. }
  67. }
  68. ResourcePtr importedResource = importer->import(inputFilePath, importOptions);
  69. return gResources()._createResourceHandle(importedResource);
  70. }
  71. Vector<SubResource> Importer::importAll(const Path& inputFilePath, ConstImportOptionsPtr importOptions)
  72. {
  73. if (!FileSystem::isFile(inputFilePath))
  74. {
  75. LOGWRN("Trying to import asset that doesn't exists. Asset path: " + inputFilePath.toString());
  76. return Vector<SubResource>();
  77. }
  78. SpecificImporter* importer = getImporterForFile(inputFilePath);
  79. if (importer == nullptr)
  80. return Vector<SubResource>();
  81. if (importOptions == nullptr)
  82. importOptions = importer->getDefaultImportOptions();
  83. else
  84. {
  85. ConstImportOptionsPtr defaultImportOptions = importer->getDefaultImportOptions();
  86. if (importOptions->getTypeId() != defaultImportOptions->getTypeId())
  87. {
  88. BS_EXCEPT(InvalidParametersException, "Provided import options is not of valid type. " \
  89. "Expected: " + defaultImportOptions->getTypeName() + ". Got: " + importOptions->getTypeName() + ".");
  90. }
  91. }
  92. Vector<SubResource> output;
  93. Vector<SubResourceRaw> importedResource = importer->importAll(inputFilePath, importOptions);
  94. for(auto& entry : importedResource)
  95. {
  96. HResource handle = gResources()._createResourceHandle(entry.value);
  97. output.push_back({ entry.name, handle });
  98. }
  99. return output;
  100. }
  101. void Importer::reimport(HResource& existingResource, const Path& inputFilePath, ConstImportOptionsPtr importOptions)
  102. {
  103. if(!FileSystem::isFile(inputFilePath))
  104. {
  105. LOGWRN("Trying to import asset that doesn't exists. Asset path: " + inputFilePath.toString());
  106. return;
  107. }
  108. SpecificImporter* importer = getImporterForFile(inputFilePath);
  109. if(importer == nullptr)
  110. return;
  111. if(importOptions == nullptr)
  112. importOptions = importer->getDefaultImportOptions();
  113. else
  114. {
  115. ConstImportOptionsPtr defaultImportOptions = importer->getDefaultImportOptions();
  116. if(importOptions->getTypeId() != defaultImportOptions->getTypeId())
  117. {
  118. BS_EXCEPT(InvalidParametersException, "Provided import options is not of valid type. " \
  119. "Expected: " + defaultImportOptions->getTypeName() + ". Got: " + importOptions->getTypeName() + ".");
  120. }
  121. }
  122. ResourcePtr importedResource = importer->import(inputFilePath, importOptions);
  123. gResources().update(existingResource, importedResource);
  124. }
  125. ImportOptionsPtr Importer::createImportOptions(const Path& inputFilePath)
  126. {
  127. if(!FileSystem::isFile(inputFilePath))
  128. {
  129. LOGWRN("Trying to import asset that doesn't exists. Asset path: " + inputFilePath.toString());
  130. return nullptr;
  131. }
  132. SpecificImporter* importer = getImporterForFile(inputFilePath);
  133. if(importer == nullptr)
  134. return nullptr;
  135. return importer->createImportOptions();
  136. }
  137. void Importer::_registerAssetImporter(SpecificImporter* importer)
  138. {
  139. if(!importer)
  140. {
  141. LOGWRN("Trying to register a null asset importer!");
  142. return;
  143. }
  144. mAssetImporters.push_back(importer);
  145. }
  146. SpecificImporter* Importer::getImporterForFile(const Path& inputFilePath) const
  147. {
  148. WString ext = inputFilePath.getWExtension();
  149. if (ext.empty())
  150. return nullptr;
  151. ext = ext.substr(1, ext.size() - 1); // Remove the .
  152. if(!supportsFileType(ext))
  153. {
  154. LOGWRN("There is no importer for the provided file type. (" + inputFilePath.toString() + ")");
  155. return nullptr;
  156. }
  157. for(auto iter = mAssetImporters.begin(); iter != mAssetImporters.end(); ++iter)
  158. {
  159. if(*iter != nullptr && (*iter)->isExtensionSupported(ext))
  160. {
  161. return *iter;
  162. }
  163. }
  164. return nullptr;
  165. }
  166. BS_CORE_EXPORT Importer& gImporter()
  167. {
  168. return Importer::instance();
  169. }
  170. }