BsProjectLibrary.h 16 KB

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