BsImporter.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsModule.h"
  4. namespace BansheeEngine
  5. {
  6. /**
  7. * @brief Module responsible for importing various asset types and converting
  8. * them to types usable by the engine.
  9. */
  10. class BS_CORE_EXPORT Importer : public Module<Importer>
  11. {
  12. public:
  13. Importer();
  14. ~Importer();
  15. /**
  16. * @brief Imports a resource at the specified location, and returns the loaded data.
  17. *
  18. * @param inputFilePath Pathname of the input file.
  19. * @param importOptions (optional) Options for controlling the import. Caller must
  20. * ensure import options actually match the type of the importer used
  21. * for the file type.
  22. *
  23. * @see createImportOptions
  24. */
  25. HResource import(const Path& inputFilePath, ConstImportOptionsPtr importOptions = nullptr);
  26. /**
  27. * @copydoc import
  28. */
  29. template <class T>
  30. ResourceHandle<T> import(const Path& inputFilePath, ConstImportOptionsPtr importOptions = nullptr)
  31. {
  32. return static_resource_cast<T>(import(inputFilePath, importOptions));
  33. }
  34. /**
  35. * @brief Imports a resource and replaces the contents of the provided existing resource with new imported data.
  36. *
  37. * @param inputFilePath Pathname of the input file.
  38. * @param importOptions (optional) Options for controlling the import. Caller must
  39. * ensure import options actually match the type of the importer used
  40. * for the file type.
  41. *
  42. * @see createImportOptions
  43. */
  44. void reimport(HResource& existingResource, const Path& inputFilePath, ConstImportOptionsPtr importOptions = nullptr);
  45. /**
  46. * @brief Automatically detects the importer needed for the provided file and returns valid type of
  47. * import options for that importer.
  48. *
  49. * @param inputFilePath Pathname of the input file.
  50. *
  51. * @return The new import options.
  52. *
  53. * @note You will need to type cast the importer options to a valid type,
  54. * taking into consideration exact importer you expect to be used for this file type.
  55. * If you don't use a proper import options type, an exception will be thrown during import.
  56. *
  57. * nullptr is returned if the file path is not valid, or if a valid importer cannot be found for
  58. * the specified file.
  59. */
  60. ImportOptionsPtr createImportOptions(const Path& inputFilePath);
  61. /**
  62. * @copydoc createImportOptions
  63. */
  64. template<class T>
  65. SPtr<T> createImportOptions(const Path& inputFilePath)
  66. {
  67. return std::static_pointer_cast<T>(createImportOptions(inputFilePath));
  68. }
  69. /**
  70. * @brief Checks if we can import a file with the specified extension.
  71. *
  72. * @param extension The extension without the leading dot.
  73. */
  74. bool supportsFileType(const WString& extension) const;
  75. /**
  76. * @brief Checks if we can import a file with the specified magic number.
  77. *
  78. * @param magicNumber The buffer containing the magic number.
  79. * @param magicNumSize Size of the magic number buffer.
  80. */
  81. bool supportsFileType(const UINT8* magicNumber, UINT32 magicNumSize) const;
  82. /**
  83. * @brief Adds a new asset importer for the specified file extension. If an asset importer for that extension
  84. * already exists, it is removed and replaced with the current one.
  85. *
  86. * @note Internal method. This method should only be called by asset importers themselves on startup. Importer takes ownership
  87. * of the provided pointer and will release it. Assumes it is allocated using the general allocator.
  88. *
  89. * @param [in] importer The importer that is able to handle files with the specified extension. nullptr if you
  90. * want to remove an asset importer for the extension.
  91. */
  92. void _registerAssetImporter(SpecificImporter* importer);
  93. private:
  94. Vector<SpecificImporter*> mAssetImporters;
  95. SpecificImporter* getImporterForFile(const Path& inputFilePath) const;
  96. };
  97. /**
  98. * @brief Provides global access to the importer.
  99. */
  100. BS_CORE_EXPORT Importer& gImporter();
  101. }