BsProjectLibrary.h 17 KB

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