BsImporter.h 6.0 KB

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