BsProjectLibrary.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. bool native = false;
  250. };
  251. /**
  252. * Common code for adding a new resource entry to the library.
  253. *
  254. * @param[in] parent Parent of the new entry.
  255. * @param[in] filePath Absolute path to the resource.
  256. * @param[in] importOptions Optional import options to use when importing the resource. Caller must ensure the
  257. * import options are of the correct type for the resource in question. If null is
  258. * provided default import options are used.
  259. * @param[in] forceReimport Should the resource be reimported even if we detect no changes. This should be true
  260. * if import options changed since last import.
  261. * @return Newly added resource entry.
  262. */
  263. FileEntry* addResourceInternal(DirectoryEntry* parent, const Path& filePath,
  264. const SPtr<ImportOptions>& importOptions = nullptr, bool forceReimport = false);
  265. /**
  266. * Common code for adding a new folder entry to the library.
  267. *
  268. * @param[in] parent Parent of the new entry.
  269. * @param[in] dirPath Absolute path to the directory.
  270. * @return Newly added directory entry.
  271. */
  272. DirectoryEntry* addDirectoryInternal(DirectoryEntry* parent, const Path& dirPath);
  273. /**
  274. * Common code for deleting a resource from the library. This code only removes the library entry, not the actual
  275. * resource file.
  276. *
  277. * @param[in] resource Entry to delete.
  278. */
  279. void deleteResourceInternal(FileEntry* resource);
  280. /**
  281. * Common code for deleting a directory from the library. This code only removes the library entry, not the actual
  282. * directory.
  283. *
  284. * @param[in] directory Entry to delete.
  285. */
  286. void deleteDirectoryInternal(DirectoryEntry* directory);
  287. /**
  288. * Triggers a reimport of a resource using the provided import options, if needed. Doesn't import dependencies.
  289. *
  290. * @param[in] file File entry of the resource to reimport.
  291. * @param[in] importOptions Optional import options to use when importing the resource. Caller must ensure
  292. * the import options are of the correct type for the resource in question. If null
  293. * is provided default import options are used.
  294. * @param[in] forceReimport Should the resource be reimported even if we detect no changes. This should be
  295. * true if import options changed since last import.
  296. * @param[in] pruneResourceMetas Determines should resource meta data for resources that no longer exist within
  297. * the file be deleted. Default behaviour is to keep these alive so that if their
  298. * resources are eventually restored, references to them will remain valid. If you
  299. * feel that you need to clear this data, set this to true but be aware that you
  300. * might need to re-apply those references.
  301. * @return Returns true if the resource was queued for import, false otherwise.
  302. */
  303. bool reimportResourceInternal(FileEntry* file, const SPtr<ImportOptions>& importOptions = nullptr,
  304. bool forceReimport = false, bool pruneResourceMetas = false);
  305. /**
  306. * Creates a full hierarchy of directory entries up to the provided directory, if any are needed.
  307. *
  308. * @param[in] fullPath Absolute path to a directory we are creating the hierarchy to.
  309. * @param[in] newHierarchyRoot First directory entry that already existed in our hierarchy.
  310. * @param[in] newHierarchyLeaf Leaf entry corresponding to the exact entry at \p path.
  311. */
  312. void createInternalParentHierarchy(const Path& fullPath, DirectoryEntry** newHierarchyRoot, DirectoryEntry** newHierarchyLeaf);
  313. /** Checks has a file been modified since the last import. */
  314. bool isUpToDate(FileEntry* file) const;
  315. /** Checks is the resource a native engine resource that doesn't require importing. */
  316. bool isNative(const Path& path) const;
  317. /**
  318. * Returns a path to a .meta file based on the resource path.
  319. *
  320. * @param[in] path Absolute path to the resource.
  321. * @return Path to the .meta file.
  322. */
  323. Path getMetaPath(const Path& path) const;
  324. /** Checks does the path represent a .meta file. */
  325. bool isMeta(const Path& fullPath) const;
  326. /**
  327. * Returns a set of resource paths that are dependent on the provided resource entry. (for example a shader file
  328. * might be dependent on shader include file).
  329. */
  330. Vector<Path> getImportDependencies(const FileEntry* entry);
  331. /** Registers any import dependencies for the specified resource. */
  332. void addDependencies(const FileEntry* entry);
  333. /** Removes any import dependencies for the specified resource. */
  334. void removeDependencies(const FileEntry* entry);
  335. /** Finds dependants resource for the specified resource entry and reimports them. */
  336. void reimportDependants(const Path& entryPath);
  337. /** Makes all library entry paths relative to the current resources folder. */
  338. void makeEntriesRelative();
  339. /**
  340. * Makes all library entry paths absolute by appending them to the current resources folder. Entries must have
  341. * previously been made relative by calling makeEntriesRelative().
  342. */
  343. void makeEntriesAbsolute();
  344. /** Deletes all library entries. */
  345. void clearEntries();
  346. static const char* LIBRARY_ENTRIES_FILENAME;
  347. static const char* RESOURCE_MANIFEST_FILENAME;
  348. SPtr<ResourceManifest> mResourceManifest;
  349. DirectoryEntry* mRootEntry;
  350. Path mProjectFolder;
  351. Path mResourcesFolder;
  352. bool mIsLoaded;
  353. Mutex mQueuedImportMutex;
  354. UnorderedMap<FileEntry*, SPtr<QueuedImport>> mQueuedImports;
  355. UnorderedMap<Path, Vector<Path>> mDependencies;
  356. UnorderedMap<UUID, Path> mUUIDToPath;
  357. };
  358. /** Provides easy access to ProjectLibrary. */
  359. BS_ED_EXPORT ProjectLibrary& gProjectLibrary();
  360. /** @} */
  361. }