BsProjectLibrary.h 14 KB

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