BsProjectLibrary.h 18 KB

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