CmImporter.cpp 2.1 KB

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