BsImporter.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "BsModule.h"
  6. #include "BsSpecificImporter.h"
  7. namespace BansheeEngine
  8. {
  9. /** @addtogroup Importer
  10. * @{
  11. */
  12. /**
  13. * Contains a resource that was imported from a file that contains multiple resources (e.g. an animation from an FBX
  14. * file).
  15. */
  16. struct SubResource
  17. {
  18. WString name; /**< Unique name of the sub-resource. */
  19. HResource value; /**< Contents of the sub-resource. */
  20. };
  21. /** Module responsible for importing various asset types and converting them to types usable by the engine. */
  22. class BS_CORE_EXPORT Importer : public Module<Importer>
  23. {
  24. public:
  25. Importer();
  26. ~Importer();
  27. /**
  28. * Imports a resource at the specified location, and returns the loaded data. If file contains more than one
  29. * resource only the primary resource is imported (e.g. for an FBX a mesh would be imported, but animations ignored).
  30. *
  31. * @param[in] inputFilePath Pathname of the input file.
  32. * @param[in] importOptions (optional) Options for controlling the import. Caller must ensure import options
  33. * actually match the type of the importer used for the file type.
  34. * @return Imported resource.
  35. *
  36. * @see createImportOptions
  37. */
  38. HResource import(const Path& inputFilePath, ConstImportOptionsPtr importOptions = nullptr);
  39. /** @copydoc import */
  40. template <class T>
  41. ResourceHandle<T> import(const Path& inputFilePath, ConstImportOptionsPtr importOptions = nullptr)
  42. {
  43. return static_resource_cast<T>(import(inputFilePath, importOptions));
  44. }
  45. /**
  46. * Imports a resource at the specified location, and returns the loaded data. This method returns all imported
  47. * resources, which is relevant for files that can contain multiple resources (e.g. an FBX which may contain both
  48. * a mesh and animations).
  49. *
  50. * @param[in] inputFilePath Pathname of the input file.
  51. * @param[in] importOptions (optional) Options for controlling the import. Caller must ensure import options
  52. * actually match the type of the importer used for the file type.
  53. * @return A list of all imported resources. The primary resource is always the first returned
  54. * resource.
  55. *
  56. * @see createImportOptions
  57. */
  58. Vector<SubResource> importAll(const Path& inputFilePath, ConstImportOptionsPtr importOptions = nullptr);
  59. /**
  60. * Imports a resource and replaces the contents of the provided existing resource with new imported data.
  61. *
  62. * @param[in] inputFilePath Pathname of the input file.
  63. * @param[in] importOptions (optional) Options for controlling the import. Caller must ensure import options
  64. * actually match the type of the importer used for the file type.
  65. *
  66. * @see createImportOptions
  67. */
  68. void reimport(HResource& existingResource, const Path& inputFilePath, ConstImportOptionsPtr importOptions = nullptr);
  69. /**
  70. * Automatically detects the importer needed for the provided file and returns valid type of import options for
  71. * that importer.
  72. *
  73. * @param[in] inputFilePath Pathname of the input file.
  74. *
  75. * @return The new import options. Null is returned if the file path is not valid, or if a
  76. * valid importer cannot be found for the specified file.
  77. *
  78. * @note
  79. * You will need to type cast the importer options to a valid type, taking into consideration exact importer you
  80. * expect to be used for this file type. If you don't use a proper import options type, an exception will be thrown
  81. * during import.
  82. */
  83. ImportOptionsPtr createImportOptions(const Path& inputFilePath);
  84. /** @copydoc createImportOptions */
  85. template<class T>
  86. SPtr<T> createImportOptions(const Path& inputFilePath)
  87. {
  88. return std::static_pointer_cast<T>(createImportOptions(inputFilePath));
  89. }
  90. /**
  91. * Checks if we can import a file with the specified extension.
  92. *
  93. * @param[in] extension The extension without the leading dot.
  94. */
  95. bool supportsFileType(const WString& extension) const;
  96. /**
  97. * Checks if we can import a file with the specified magic number.
  98. *
  99. * @param[in] magicNumber The buffer containing the magic number.
  100. * @param[in] magicNumSize Size of the magic number buffer.
  101. */
  102. bool supportsFileType(const UINT8* magicNumber, UINT32 magicNumSize) const;
  103. /** @cond INTERNAL */
  104. /**
  105. * Adds a new asset importer for the specified file extension. If an asset importer for that extension already
  106. * exists, it is removed and replaced with the current one.
  107. *
  108. *
  109. * @param [in] importer The importer that is able to handle files with the specified extension. nullptr if you
  110. * want to remove an asset importer for the extension.
  111. *
  112. * @note Internal method.
  113. * @note This method should only be called by asset importers themselves on startup. Importer takes ownership
  114. * of the provided pointer and will release it. Assumes it is allocated using the general allocator.
  115. */
  116. void _registerAssetImporter(SpecificImporter* importer);
  117. /**
  118. * Imports a resource at the specified location but doesn't create resource handles. This method returns all
  119. * imported resources, which is relevant for files that can contain multiple resources (e.g. an FBX which may
  120. * contain both a mesh and animations).
  121. *
  122. * @param[in] inputFilePath Pathname of the input file.
  123. * @param[in] importOptions (optional) Options for controlling the import. Caller must ensure import options
  124. * actually match the type of the importer used for the file type.
  125. * @return A list of all imported resources. The primary resource is always the first returned
  126. * resource. Caller is responsible for creating resource handles for the returned
  127. * values.
  128. *
  129. * @see createImportOptions
  130. */
  131. Vector<SubResourceRaw> _importAllRaw(const Path& inputFilePath, ConstImportOptionsPtr importOptions = nullptr);
  132. /** @endcond */
  133. private:
  134. /**
  135. * Searches available importers and attempts to find one that can import the file of the provided type. Returns null
  136. * if one cannot be found.
  137. */
  138. SpecificImporter* getImporterForFile(const Path& inputFilePath) const;
  139. Vector<SpecificImporter*> mAssetImporters;
  140. };
  141. /** Provides easier access to Importer. */
  142. BS_CORE_EXPORT Importer& gImporter();
  143. /** @} */
  144. }