BsProjectLibrary.h 15 KB

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