BsProjectLibrary.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsEditorPrerequisites.h"
  5. #include "BsModule.h"
  6. namespace BansheeEngine
  7. {
  8. /**
  9. * Project library is the primary location for interacting with all the resources in the current project. A complete
  10. * hierarchy of resources is provided which can be interacted with by importing new ones, deleting them, moving,
  11. * renaming and similar.
  12. */
  13. class BS_ED_EXPORT ProjectLibrary : public Module<ProjectLibrary>
  14. {
  15. public:
  16. struct LibraryEntry;
  17. struct FileEntry;
  18. struct DirectoryEntry;
  19. /** Types of elements in the library, either a file or a folder. */
  20. enum class LibraryEntryType
  21. {
  22. File,
  23. Directory
  24. };
  25. /** A generic library entry that may be a file or a folder depending on its type. */
  26. struct LibraryEntry
  27. {
  28. LibraryEntry();
  29. LibraryEntry(const Path& path, const WString& name, DirectoryEntry* parent, LibraryEntryType type);
  30. LibraryEntryType type; /**< Specific type of this entry. */
  31. Path path; /**< Absolute path to the entry. */
  32. WString elementName; /**< Name of the entry. */
  33. DirectoryEntry* parent; /**< Folder this entry is located in. */
  34. };
  35. /** A library entry representing a file. Each file can have one or multiple resources. */
  36. struct FileEntry : public LibraryEntry
  37. {
  38. FileEntry();
  39. FileEntry(const Path& path, const WString& name, DirectoryEntry* parent);
  40. ProjectFileMetaPtr meta; /**< Meta file containing various information about the resource(s). */
  41. std::time_t lastUpdateTime; /**< Timestamp of when we last imported the resource. */
  42. };
  43. /** A library entry representing a folder that contains other entries. */
  44. struct DirectoryEntry : public LibraryEntry
  45. {
  46. DirectoryEntry();
  47. DirectoryEntry(const Path& path, const WString& name, DirectoryEntry* parent);
  48. Vector<LibraryEntry*> mChildren; /**< Child files or folders. */
  49. };
  50. public:
  51. ProjectLibrary();
  52. ~ProjectLibrary();
  53. /**
  54. * Checks if any resources at the specified path have been modified, added or deleted, and updates the internal
  55. * hierarchy accordingly. Automatically imports dirty resources.
  56. *
  57. * @param[in] path Absolute path of the file or folder to check. If a folder is provided all its children will
  58. * be checked recursively.
  59. */
  60. void checkForModifications(const Path& path);
  61. /**
  62. * Checks if any resources at the specified path have been modified, added or deleted, and updates the internal
  63. * hierarchy accordingly.
  64. *
  65. * @param[in] path Absolute path of the file or folder to check. If a folder is provided all its
  66. * children will be checked recursively.
  67. * @param[in] import Should the dirty resources be automatically reimported.
  68. * @param[in] dirtyResources A list of resources that should be reimported.
  69. */
  70. void checkForModifications(const Path& path, bool import, Vector<Path>& dirtyResources);
  71. /** Returns the root library entry that references the entire library hierarchy. */
  72. const LibraryEntry* getRootEntry() const { return mRootEntry; }
  73. /**
  74. * Attempts to a find a library entry at the specified path.
  75. *
  76. * @param[in] path Path to the entry, either absolute or relative to resources folder.
  77. * @return Found entry, or null if not found. Value returned by this method is transient, it may be
  78. * destroyed on any following ProjectLibrary call.
  79. */
  80. LibraryEntry* findEntry(const Path& path) const;
  81. /**
  82. * Checks whether the provided path points to a sub-resource. Sub-resource is any resource that is not the primary
  83. * resource in the file.
  84. */
  85. bool isSubresource(const Path& path) const;
  86. /**
  87. * Attempts to a find a meta information for a resource at the specified path.
  88. *
  89. * @param[in] path Path to the entry, either absolute or relative to resources folder. If a sub-resource within
  90. * a file is needed, append the name of the subresource to the path
  91. * (e.g. mymesh.fbx/my_animation).
  92. * @return Found meta information for the resource, or null if not found.
  93. */
  94. ProjectResourceMetaPtr findResourceMeta(const Path& path) const;
  95. /**
  96. * Searches the library for a pattern and returns all entries matching it.
  97. *
  98. * @param[in] pattern Pattern to search for. Use wildcard * to match any character(s).
  99. * @return A list of entries matching the pattern. Values returned by this method are transient, they may be
  100. * destroyed on any following ProjectLibrary call.
  101. */
  102. Vector<LibraryEntry*> search(const WString& pattern);
  103. /**
  104. * Searches the library for a pattern, but only among specific resource types.
  105. *
  106. * @param[in] pattern Pattern to search for. Use wildcard * to match any character(s).
  107. * @param[in] typeIds RTTI type IDs of the resource types we're interested in searching.
  108. * @return A list of entries matching the pattern. Values returned by this method are transient, they may be
  109. * destroyed on any following ProjectLibrary call.
  110. */
  111. Vector<LibraryEntry*> search(const WString& pattern, const Vector<UINT32>& typeIds);
  112. /**
  113. * Returns resource path based on its UUID.
  114. *
  115. * @param[in] uuid UUID of the resource to look for.
  116. * @return Absolute path to the resource.
  117. */
  118. Path uuidToPath(const String& uuid) const;
  119. /**
  120. * Registers a new resource in the library.
  121. *
  122. * @param[in] resource Resource instance to add to the library. A copy of the resource will be saved at the
  123. * provided path.
  124. * @param[in] path Path where where to store the resource. Absolute or relative to the resources folder.
  125. */
  126. void createEntry(const HResource& resource, const Path& path);
  127. /**
  128. * Creates a new folder in the library.
  129. *
  130. * @param[in] path Path where where to store the folder. Absolute or relative to the resources folder.
  131. */
  132. void createFolderEntry(const Path& path);
  133. /** Updates a resource that is already in the library. */
  134. void saveEntry(const HResource& resource);
  135. /**
  136. * Moves a library entry from one path to another.
  137. *
  138. * @param[in] oldPath Source path of the entry, absolute or relative to resources folder.
  139. * @param[in] newPath Destination path of the entry, absolute or relative to resources folder.
  140. * @param[in] overwrite If an entry already exists at the destination path, should it be overwritten.
  141. */
  142. void moveEntry(const Path& oldPath, const Path& newPath, bool overwrite = true);
  143. /**
  144. * Copies a library entry from one path to another.
  145. *
  146. * @param[in] oldPath Source path of the entry, absolute or relative to resources folder.
  147. * @param[in] newPath Destination path of the entry, absolute or relative to resources folder.
  148. * @param[in] overwrite If an entry already exists at the destination path, should it be overwritten.
  149. */
  150. void copyEntry(const Path& oldPath, const Path& newPath, bool overwrite = true);
  151. /**
  152. * Deletes an entry from the library.
  153. *
  154. * @param[in] path Path of the entry, absolute or relative to resources folder.
  155. */
  156. void deleteEntry(const Path& path);
  157. /**
  158. * Triggers a reimport of a resource using the provided import options, if needed.
  159. *
  160. * @param[in] path Path to the resource to reimport, absolute or relative to resources folder.
  161. * @param[in] importOptions Optional import options to use when importing the resource. Caller must ensure the
  162. * import options are of the correct type for the resource in question. If null is
  163. * provided default import options are used.
  164. * @param[in] forceReimport Should the resource be reimported even if no changes are detected. This should be
  165. * true if import options changed since last import.
  166. */
  167. void reimport(const Path& path, const ImportOptionsPtr& importOptions = nullptr, bool forceReimport = false);
  168. /**
  169. * Determines if this resource will always be included in the build, regardless if it's being referenced or not.
  170. *
  171. * @param[in] path Path to the resource to modify, absolute or relative to resources folder.
  172. * @param[in] force True if we want the resource to be included in the build, false otherwise.
  173. */
  174. void setIncludeInBuild(const Path& path, bool force);
  175. /**
  176. * Finds all top-level resource entries that should be included in a build. Values returned by this method are
  177. * transient, they may be destroyed on any following ProjectLibrary call.
  178. */
  179. Vector<FileEntry*> getResourcesForBuild() const;
  180. /**
  181. * Loads a resource at the specified path, synchronously.
  182. *
  183. * @param[in] path Path of the resource, absolute or relative to resources folder. If a sub-resource within
  184. * a file is needed, append the name of the subresource to the path
  185. * (e.g. mymesh.fbx/my_animation).
  186. * @return Loaded resource, or null handle if one is not found.
  187. */
  188. HResource load(const Path& path);
  189. /** Returns the path to the project's resource folder where all the assets are stored. */
  190. const Path& getResourcesFolder() const { return mResourcesFolder; }
  191. /** Returns the resource manifest managed by the project library. */
  192. const ResourceManifestPtr& _getManifest() const { return mResourceManifest; }
  193. /**
  194. * Saves all the project library data so it may be restored later, at the default save location in the project
  195. * folder. Project must be loaded when calling this.
  196. */
  197. void saveLibrary();
  198. /**
  199. * Loads previously saved project library data from the default save location in the project folder. Nothing is
  200. * loaded if it doesn't exist.Project must be loaded when calling this.
  201. */
  202. void loadLibrary();
  203. /** Clears all library data. */
  204. void unloadLibrary();
  205. /** Triggered whenever an entry is removed from the library. Path provided is absolute. */
  206. Event<void(const Path&)> onEntryRemoved;
  207. /** Triggered whenever an entry is added to the library. Path provided is absolute. */
  208. Event<void(const Path&)> onEntryAdded;
  209. /** Triggered when a resource is being (re)imported. Path provided is absolute. */
  210. Event<void(const Path&)> onEntryImported;
  211. static const Path RESOURCES_DIR;
  212. static const Path INTERNAL_RESOURCES_DIR;
  213. private:
  214. /**
  215. * Common code for adding a new resource entry to the library.
  216. *
  217. * @param[in] parent Parent of the new entry.
  218. * @param[in] filePath Absolute path to the resource.
  219. * @param[in] importOptions Optional import options to use when importing the resource. Caller must ensure the
  220. * import options are of the correct type for the resource in question. If null is
  221. * provided default import options are used.
  222. * @param[in] forceReimport Should the resource be reimported even if we detect no changes. This should be true
  223. * if import options changed since last import.
  224. * @return Newly added resource entry.
  225. */
  226. FileEntry* addResourceInternal(DirectoryEntry* parent, const Path& filePath,
  227. const ImportOptionsPtr& importOptions = nullptr, bool forceReimport = false);
  228. /**
  229. * Common code for adding a new folder entry to the library.
  230. *
  231. * @param[in] parent Parent of the new entry.
  232. * @param[in] dirPath Absolute path to the directory.
  233. * @return Newly added directory entry.
  234. */
  235. DirectoryEntry* addDirectoryInternal(DirectoryEntry* parent, const Path& dirPath);
  236. /**
  237. * Common code for deleting a resource from the library. This code only removes the library entry, not the actual
  238. * resource file.
  239. *
  240. * @param[in] resource Entry to delete.
  241. */
  242. void deleteResourceInternal(FileEntry* resource);
  243. /**
  244. * Common code for deleting a directory from the library. This code only removes the library entry, not the actual
  245. * directory.
  246. *
  247. * @param[in] resource Entry to delete.
  248. */
  249. void deleteDirectoryInternal(DirectoryEntry* directory);
  250. /**
  251. * Triggers a reimport of a resource using the provided import options, if needed. Doesn't import dependencies.
  252. *
  253. * @param[in] path Absolute Path to the resource to reimport.
  254. * @param[in] importOptions Optional import options to use when importing the resource. Caller must ensure
  255. * the import options are of the correct type for the resource in question. If null
  256. * is provided default import options are used.
  257. * @param[in] forceReimport Should the resource be reimported even if we detect no changes. This should be
  258. * true if import options changed since last import.
  259. * @param[in] pruneResourceMetas Determines should resource meta data for resources that no longer exist within
  260. * the file be deleted. Default behaviour is to keep these alive so that if their
  261. * resources are eventually restored, references to them will remain valid. If you
  262. * feel that you need to clear this data, set this to true but be aware that you
  263. * might need to re-apply those references.
  264. */
  265. void reimportResourceInternal(FileEntry* file, const ImportOptionsPtr& importOptions = nullptr,
  266. bool forceReimport = false, bool pruneResourceMetas = false);
  267. /**
  268. * Creates a full hierarchy of directory entries up to the provided directory, if any are needed.
  269. *
  270. * @param[in] fullPath Absolute path to a directory we are creating the hierarchy to.
  271. * @param[in] newHierarchyRoot First directory entry that already existed in our hierarchy.
  272. * @param[in] newHierarchyLeaf Leaf entry corresponding to the exact entry at \p path.
  273. */
  274. void createInternalParentHierarchy(const Path& fullPath, DirectoryEntry** newHierarchyRoot, DirectoryEntry** newHierarchyLeaf);
  275. /** Checks has a file been modified since the last import. */
  276. bool isUpToDate(FileEntry* file) const;
  277. /** Checks is the resource a native engine resource that doesn't require importing. */
  278. bool isNative(const Path& path) const;
  279. /**
  280. * Returns a path to a .meta file based on the resource path.
  281. *
  282. * @param[in] path Absolute path to the resource.
  283. * @return Path to the .meta file.
  284. */
  285. Path getMetaPath(const Path& path) const;
  286. /** Checks does the path represent a .meta file. */
  287. bool isMeta(const Path& fullPath) const;
  288. /**
  289. * Returns a set of resource paths that are dependent on the provided resource entry. (e.g. a shader file might be
  290. * dependent on shader include file).
  291. */
  292. Vector<Path> getImportDependencies(const FileEntry* entry);
  293. /** Registers any import dependencies for the specified resource. */
  294. void addDependencies(const FileEntry* entry);
  295. /** Removes any import dependencies for the specified resource. */
  296. void removeDependencies(const FileEntry* entry);
  297. /** Finds dependants resource for the specified resource entry and reimports them. */
  298. void reimportDependants(const Path& entryPath);
  299. /** Makes all library entry paths relative to the current resources folder. */
  300. void makeEntriesRelative();
  301. /**
  302. * Makes all library entry paths absolute by appending them to the current resources folder. Entries must have
  303. * previously been made relative by calling makeEntriesRelative().
  304. */
  305. void makeEntriesAbsolute();
  306. /** Deletes all library entries. */
  307. void clearEntries();
  308. static const WString LIBRARY_ENTRIES_FILENAME;
  309. static const WString RESOURCE_MANIFEST_FILENAME;
  310. ResourceManifestPtr mResourceManifest;
  311. DirectoryEntry* mRootEntry;
  312. Path mProjectFolder;
  313. Path mResourcesFolder;
  314. bool mIsLoaded;
  315. UnorderedMap<Path, Vector<Path>> mDependencies;
  316. UnorderedMap<String, Path> mUUIDToPath;
  317. };
  318. /** Provides easy access to ProjectLibrary. */
  319. BS_ED_EXPORT ProjectLibrary& gProjectLibrary();
  320. }