BsImporter.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 (for example an animation from an
  14. * FBX 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 (for example an FBX a mesh would be imported, but animations
  30. * ignored).
  31. *
  32. * @param[in] inputFilePath Pathname of the input file.
  33. * @param[in] importOptions (optional) Options for controlling the import. Caller must ensure import options
  34. * actually match the type of the importer used for the file type.
  35. * @return Imported resource.
  36. *
  37. * @see createImportOptions
  38. */
  39. HResource import(const Path& inputFilePath, ConstImportOptionsPtr importOptions = nullptr);
  40. /** @copydoc import */
  41. template <class T>
  42. ResourceHandle<T> import(const Path& inputFilePath, ConstImportOptionsPtr importOptions = nullptr)
  43. {
  44. return static_resource_cast<T>(import(inputFilePath, importOptions));
  45. }
  46. /**
  47. * Imports a resource at the specified location, and returns the loaded data. This method returns all imported
  48. * resources, which is relevant for files that can contain multiple resources (for example an FBX which may contain
  49. * both a mesh and animations).
  50. *
  51. * @param[in] inputFilePath Pathname of the input file.
  52. * @param[in] importOptions (optional) Options for controlling the import. Caller must ensure import options
  53. * actually match the type of the importer used for the file type.
  54. * @return A list of all imported resources. The primary resource is always the first returned
  55. * resource.
  56. *
  57. * @see createImportOptions
  58. */
  59. Vector<SubResource> importAll(const Path& inputFilePath, ConstImportOptionsPtr importOptions = nullptr);
  60. /**
  61. * Imports a resource and replaces the contents of the provided existing resource with new imported data.
  62. *
  63. * @param[in] inputFilePath Pathname of the input file.
  64. * @param[in] importOptions (optional) Options for controlling the import. Caller must ensure import options
  65. * actually match the type of the importer used for the file type.
  66. *
  67. * @see createImportOptions
  68. */
  69. void reimport(HResource& existingResource, const Path& inputFilePath, ConstImportOptionsPtr importOptions = nullptr);
  70. /**
  71. * Automatically detects the importer needed for the provided file and returns valid type of import options for
  72. * that importer.
  73. *
  74. * @param[in] inputFilePath Pathname of the input file.
  75. *
  76. * @return The new import options. Null is returned if the file path is not valid, or if a
  77. * valid importer cannot be found for the specified file.
  78. *
  79. * @note
  80. * You will need to type cast the importer options to a valid type, taking into consideration exact importer you
  81. * expect to be used for this file type. If you don't use a proper import options type, an exception will be thrown
  82. * during import.
  83. */
  84. ImportOptionsPtr createImportOptions(const Path& inputFilePath);
  85. /** @copydoc createImportOptions */
  86. template<class T>
  87. SPtr<T> createImportOptions(const Path& inputFilePath)
  88. {
  89. return std::static_pointer_cast<T>(createImportOptions(inputFilePath));
  90. }
  91. /**
  92. * Checks if we can import a file with the specified extension.
  93. *
  94. * @param[in] extension The extension without the leading dot.
  95. */
  96. bool supportsFileType(const WString& extension) const;
  97. /**
  98. * Checks if we can import a file with the specified magic number.
  99. *
  100. * @param[in] magicNumber The buffer containing the magic number.
  101. * @param[in] magicNumSize Size of the magic number buffer.
  102. */
  103. bool supportsFileType(const UINT8* magicNumber, UINT32 magicNumSize) const;
  104. /** @name Internal
  105. * @{
  106. */
  107. /**
  108. * Adds a new asset importer for the specified file extension. If an asset importer for that extension already
  109. * exists, it is removed and replaced with the current one.
  110. *
  111. *
  112. * @param[in] importer The importer that is able to handle files with the specified extension. nullptr if you
  113. * want to remove an asset importer for the extension.
  114. *
  115. * @note Internal method.
  116. * @note This method should only be called by asset importers themselves on startup. Importer takes ownership
  117. * of the provided pointer and will release it. Assumes it is allocated using the general allocator.
  118. */
  119. void _registerAssetImporter(SpecificImporter* importer);
  120. /**
  121. * Imports a resource at the specified location but doesn't create resource handles. This method returns all
  122. * imported resources, which is relevant for files that can contain multiple resources (for example an FBX which may
  123. * contain both a mesh and animations).
  124. *
  125. * @param[in] inputFilePath Pathname of the input file.
  126. * @param[in] importOptions (optional) Options for controlling the import. Caller must ensure import options
  127. * actually match the type of the importer used for the file type.
  128. * @return A list of all imported resources. The primary resource is always the first returned
  129. * resource. Caller is responsible for creating resource handles for the returned
  130. * values.
  131. *
  132. * @see createImportOptions
  133. */
  134. Vector<SubResourceRaw> _importAllRaw(const Path& inputFilePath, ConstImportOptionsPtr importOptions = nullptr);
  135. /** @} */
  136. private:
  137. /**
  138. * Searches available importers and attempts to find one that can import the file of the provided type. Returns null
  139. * if one cannot be found.
  140. */
  141. SpecificImporter* getImporterForFile(const Path& inputFilePath) const;
  142. Vector<SpecificImporter*> mAssetImporters;
  143. };
  144. /** Provides easier access to Importer. */
  145. BS_CORE_EXPORT Importer& gImporter();
  146. /** @} */
  147. }