CmImporter.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "CmImporter.h"
  2. #include "CmPath.h"
  3. #include "CmResource.h"
  4. #include "CmFileSystem.h"
  5. #include "CmSpecificImporter.h"
  6. #include "CmDebug.h"
  7. #include "CmDataStream.h"
  8. #include "CmException.h"
  9. namespace CamelotEngine
  10. {
  11. Importer::Importer()
  12. { }
  13. Importer::~Importer()
  14. {
  15. for(auto i = mAssetImporters.begin(); i != mAssetImporters.end(); ++i)
  16. {
  17. if((*i) != nullptr)
  18. delete *i;
  19. }
  20. mAssetImporters.clear();
  21. }
  22. bool Importer::supportsFileType(const std::string& extension) const
  23. {
  24. for(auto iter = mAssetImporters.begin(); iter != mAssetImporters.end(); ++iter)
  25. {
  26. if(*iter != nullptr && (*iter)->isExtensionSupported(extension))
  27. return true;
  28. }
  29. return false;
  30. }
  31. bool Importer::supportsFileType(const UINT8* magicNumber, UINT32 magicNumSize) const
  32. {
  33. for(auto iter = mAssetImporters.begin(); iter != mAssetImporters.end(); ++iter)
  34. {
  35. if(*iter != nullptr && (*iter)->isMagicNumberSupported(magicNumber, magicNumSize))
  36. return true;
  37. }
  38. return false;
  39. }
  40. BaseResourceHandle Importer::import(const String& inputFilePath)
  41. {
  42. if(!FileSystem::fileExists(inputFilePath))
  43. {
  44. LOGWRN("Trying to import asset that doesn't exists. Asset path: " + inputFilePath);
  45. return nullptr;
  46. }
  47. String ext = Path::getExtension(inputFilePath);
  48. ext = ext.substr(1, ext.size() - 1); // Remove the .
  49. if(!supportsFileType(ext))
  50. {
  51. LOGWRN("There is no importer for the provided file type. (" + inputFilePath + ")");
  52. return nullptr;
  53. }
  54. SpecificImporter* importer = nullptr;
  55. for(auto iter = mAssetImporters.begin(); iter != mAssetImporters.end(); ++iter)
  56. {
  57. if(*iter != nullptr && (*iter)->isExtensionSupported(ext))
  58. {
  59. importer = *iter;
  60. }
  61. }
  62. BaseResourceHandle importedResource = importer->import(inputFilePath);
  63. return importedResource;
  64. }
  65. void Importer::registerAssetImporter(SpecificImporter* importer)
  66. {
  67. if(!importer)
  68. {
  69. LOGWRN("Trying to register a null asset importer!");
  70. return;
  71. }
  72. mAssetImporters.push_back(importer);
  73. }
  74. }