BsImporter.h 3.9 KB

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