CmImporter.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "CmImporter.h"
  2. #include "CmPath.h"
  3. #include "CmSpecificImporter.h"
  4. #include "CmDebug.h"
  5. #include "CmException.h"
  6. namespace CamelotEngine
  7. {
  8. Importer::~Importer()
  9. {
  10. for(auto i = mAssetImporters.begin(); i != mAssetImporters.end(); ++i)
  11. {
  12. SpecificImporter* curImporter = i->second;
  13. if(curImporter != nullptr)
  14. {
  15. const std::vector<String>& supportedExtensions = curImporter->extensions();
  16. for(auto j = supportedExtensions.begin(); j != supportedExtensions.end(); ++j)
  17. {
  18. if(mAssetImporters[*j] == curImporter)
  19. mAssetImporters[*j] = nullptr;
  20. }
  21. delete curImporter;
  22. }
  23. }
  24. mAssetImporters.clear();
  25. }
  26. bool Importer::supportsFileType(const std::string& extension)
  27. {
  28. auto found = mAssetImporters.find(extension);
  29. if(found != mAssetImporters.end())
  30. return found->second != nullptr; // Even if we found it, it can still be null
  31. return false;
  32. }
  33. void Importer::import(const String& inputFilePath, const String& outputFilePath, bool keepReferences)
  34. {
  35. if(!Path::exists(inputFilePath))
  36. {
  37. LOGWRN("Trying to import asset that doesn't exists. Asset path: " + inputFilePath);
  38. return;
  39. }
  40. String ext = Path::getExtension(inputFilePath);
  41. ext = ext.substr(1, ext.size() - 1); // Remove the .
  42. if(!supportsFileType(ext))
  43. {
  44. LOGWRN("There is no importer for the provided file type. (" + inputFilePath + ")");
  45. return;
  46. }
  47. SpecificImporter* importer = mAssetImporters[ext];
  48. ResourcePtr importedResource = importer->import(inputFilePath);
  49. // TODO - Use AssetDatabase for loading the resource
  50. // TODO - Serialize the resource to output location
  51. }
  52. void Importer::registerAssetImporter(SpecificImporter* importer)
  53. {
  54. if(!importer)
  55. {
  56. LOGWRN("Trying to register a null asset importer!");
  57. return;
  58. }
  59. const std::vector<String>& supportedExtensions = importer->extensions();
  60. for(auto i = supportedExtensions.begin(); i != supportedExtensions.end(); ++i)
  61. {
  62. SpecificImporter* existingImporter = mAssetImporters[*i];
  63. if(existingImporter != nullptr)
  64. delete existingImporter;
  65. mAssetImporters[*i] = importer;
  66. }
  67. }
  68. }