BsImporter.h 6.2 KB

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