ProjectLibrary.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Runtime.CompilerServices;
  5. using BansheeEngine;
  6. namespace BansheeEditor
  7. {
  8. /// <summary>
  9. /// The primary location for interacting with all the resources in the current project. A complete hierarchy of
  10. /// resources is provided which can be interacted with by importing new ones, deleting them, moving, renaming and similar.
  11. /// </summary>
  12. public sealed class ProjectLibrary : ScriptObject
  13. {
  14. /// <summary>
  15. /// Root entry of the project library, referencing the top level resources folder.
  16. /// </summary>
  17. public static DirectoryEntry Root { get { return Internal_GetRoot(); } }
  18. /// <summary>
  19. /// Absolute path to the current project's project library resource folder.
  20. /// </summary>
  21. public static string ResourceFolder { get { return Internal_GetResourceFolder(); } }
  22. /// <summary>
  23. /// Triggered when a new entry is added to the project library. Provided path relative to the project library
  24. /// resources folder.
  25. /// </summary>
  26. public static event Action<string> OnEntryAdded;
  27. /// <summary>
  28. /// Triggered when an entry is removed from the project library. Provided path relative to the project library
  29. /// resources folder.
  30. /// </summary>
  31. public static event Action<string> OnEntryRemoved;
  32. private static HashSet<string> queuedForImport = new HashSet<string>();
  33. private static int numImportedFiles;
  34. private static int totalFilesToImport;
  35. private const float TIME_SLICE_SECONDS = 0.030f;
  36. /// <summary>
  37. /// Checks the project library folder for any modifications and reimports the required resources.
  38. /// </summary>
  39. public static void Refresh()
  40. {
  41. string[] modifiedPaths = Internal_Refresh(ResourceFolder, false);
  42. foreach (var modifiedPath in modifiedPaths)
  43. {
  44. if (queuedForImport.Add(modifiedPath))
  45. totalFilesToImport++;
  46. }
  47. }
  48. /// <summary>
  49. /// Checks the specified folder for any modifications and reimports the required resources.
  50. /// </summary>
  51. /// <param name="path">Path to a file or folder to refresh. Relative to the project library resources folder or
  52. /// absolute.</param>
  53. public static void Refresh(string path)
  54. {
  55. string[] modifiedPaths = Internal_Refresh(path, false);
  56. foreach (var modifiedPath in modifiedPaths)
  57. {
  58. if (queuedForImport.Add(modifiedPath))
  59. totalFilesToImport++;
  60. }
  61. }
  62. /// <summary>
  63. /// Registers a new resource in the library.
  64. /// </summary>
  65. /// <param name="resource">Resource instance to add to the library. A copy of the resource will be saved at the
  66. /// provided path.</param>
  67. /// <param name="path">Path where where to store the resource. Absolute or relative to the resources folder.</param>
  68. public static void Create(Resource resource, string path)
  69. {
  70. Internal_Create(resource, path);
  71. }
  72. /// <summary>
  73. /// Updates a resource that is already in the library.
  74. /// </summary>
  75. /// <param name="resource">Resource to save.</param>
  76. public static void Save(Resource resource)
  77. {
  78. Internal_Save(resource);
  79. }
  80. /// <summary>
  81. /// Loads a resource from the project library.
  82. /// </summary>
  83. /// <typeparam name="T">Type of the resource to load.</typeparam>
  84. /// <param name="path">Path of the resource to load. Absolute or relative to the resources folder.</param>
  85. /// <returns>Instance of the loaded resource, or null if not found.</returns>
  86. public static T Load<T>(string path) where T : Resource
  87. {
  88. return (T) Internal_Load(path);
  89. }
  90. /// <summary>
  91. /// Triggers a reimport of a resource using the provided import options, if needed.
  92. /// </summary>
  93. /// <param name="path">Path to the resource to reimport, absolute or relative to resources folder.</param>
  94. /// <param name="options">ptional import options to use when importing the resource. Caller must ensure the import
  95. /// options are of the correct type for the resource in question. If null is provided default
  96. /// import options are used.</param>
  97. /// <param name="force">Should the resource be reimported even if no changes are detected.</param>
  98. public static void Reimport(string path, ImportOptions options = null, bool force = false)
  99. {
  100. Internal_Reimport(path, options, force);
  101. }
  102. /// <summary>
  103. /// Checks does the project library contain a resource at the specified path.
  104. /// </summary>
  105. /// <param name="path">Path to the resource to check, absolute or relative to resources folder.</param>
  106. /// <returns>True if the resourc exists, false otherwise.</returns>
  107. public static bool Exists(string path)
  108. {
  109. return GetEntry(path) != null;
  110. }
  111. /// <summary>
  112. /// Attempts to locate a library entry that describes a file or a folder in the project library.
  113. /// </summary>
  114. /// <param name="path">Path to the entry to retrieve, absolute or relative to resources folder.</param>
  115. /// <returns>Library entry if found, null otherwise. This object can become invalid on the next library refresh
  116. /// and you are not meant to hold a permanent reference to it.</returns>
  117. public static LibraryEntry GetEntry(string path)
  118. {
  119. return Internal_GetEntry(path);
  120. }
  121. /// <summary>
  122. /// Searches the library for a pattern and returns all entries matching it.
  123. /// </summary>
  124. /// <param name="pattern">Pattern to search for. Use wildcard * to match any character(s).</param>
  125. /// <param name="types">Type of resources to search for. If null all entries will be searched.</param>
  126. /// <returns>A set of entries matching the pattern. These objects can become invalid on the next library refresh
  127. /// and you are not meant to hold a permanent reference to them.</returns>
  128. public static LibraryEntry[] Search(string pattern, ResourceType[] types = null)
  129. {
  130. return Internal_Search(pattern, types);
  131. }
  132. /// <summary>
  133. /// Returns a path to a resource stored in the project library.
  134. /// </summary>
  135. /// <param name="resource">Resource to find the path for.</param>
  136. /// <returns>Path to relative to the project library resources folder if resource was found, null otherwise.
  137. /// </returns>
  138. public static string GetPath(Resource resource)
  139. {
  140. return Internal_GetPath(resource);
  141. }
  142. /// <summary>
  143. /// Returns a path to a resource with the specified UUID stored in the project library.
  144. /// </summary>
  145. /// <param name="uuid">Unique identifier of the resources to retrieve the path of.</param>
  146. /// <returns>Path to relative to the project library resources folder if resource was found, null otherwise.
  147. /// </returns>
  148. public static string GetPath(string uuid)
  149. {
  150. return Internal_GetPathFromUUID(uuid);
  151. }
  152. /// <summary>
  153. /// Deletes a resource in the project library.
  154. /// </summary>
  155. /// <param name="path">Path to the entry to delete, absolute or relative to resources folder.</param>
  156. public static void Delete(string path)
  157. {
  158. Internal_Delete(path);
  159. }
  160. /// <summary>
  161. /// Creates a new folder in the library.
  162. /// </summary>
  163. /// <param name="path">Path of the folder to create. Absolute or relative to the resources folder.</param>
  164. public static void CreateFolder(string path)
  165. {
  166. Internal_CreateFolder(path);
  167. }
  168. /// <summary>
  169. /// Renames an entry in the project library.
  170. /// </summary>
  171. /// <param name="path">Path of the entry to rename, absolute or relative to resources folder.</param>
  172. /// <param name="name">New name of the entry with an extension.</param>
  173. /// <param name="overwrite">Determines should the entry be deleted if one with the provided name already exists. If
  174. /// this is false and an entry already exists, no rename operation will be performed.</param>
  175. public static void Rename(string path, string name, bool overwrite = false)
  176. {
  177. Internal_Rename(path, name, false);
  178. }
  179. /// <summary>
  180. /// Moves an entry in the project library from one path to another.
  181. /// </summary>
  182. /// <param name="oldPath">Source path of the entry, absolute or relative to resources folder.</param>
  183. /// <param name="newPath">Destination path of the entry, absolute or relative to resources folder.</param>
  184. /// <param name="overwrite">Determines should the entry be deleted if one at the destination path already exists. If
  185. /// this is false and an entry already exists, no move operation will be performed.</param>
  186. public static void Move(string oldPath, string newPath, bool overwrite = false)
  187. {
  188. Internal_Move(oldPath, newPath, overwrite);
  189. }
  190. /// <summary>
  191. /// Copies an entry in the project library from one path to another.
  192. /// </summary>
  193. /// <param name="source">Source path of the entry, absolute or relative to resources folder.</param>
  194. /// <param name="destination">Destination path of the entry, absolute or relative to resources folder.</param>
  195. /// <param name="overwrite">Determines should the entry be deleted if one at the destination path already exists. If
  196. /// this is false and an entry already exists, no copy operation will be performed.</param>
  197. public static void Copy(string source, string destination, bool overwrite = false)
  198. {
  199. Internal_Copy(source, destination, overwrite);
  200. }
  201. /// <summary>
  202. /// Controls should a resource be included an a build. All dependant resources will also be included.
  203. /// </summary>
  204. /// <param name="path">Path of the resource to include, absolute or relative to resources folder.</param>
  205. /// <param name="include">True if it should be included, false otherwise.</param>
  206. public static void SetIncludeInBuild(string path, bool include)
  207. {
  208. Internal_SetIncludeInBuild(path, include);
  209. }
  210. /// <summary>
  211. /// Triggers reimport for queued resource. Should be called once per frame.
  212. /// </summary>
  213. internal static void Update()
  214. {
  215. if (queuedForImport.Count > 0)
  216. {
  217. UInt64 start = Time.Precise;
  218. List<string> toRemove = new List<string>();
  219. string lastEntry = "";
  220. foreach (var entry in queuedForImport)
  221. {
  222. lastEntry = entry;
  223. Internal_Refresh(entry, true);
  224. toRemove.Add(entry);
  225. numImportedFiles++;
  226. UInt64 end = Time.Precise;
  227. UInt64 elapsed = end - start;
  228. float elapsedSeconds = elapsed * Time.MicroToSecond;
  229. if (elapsedSeconds > TIME_SLICE_SECONDS)
  230. break;
  231. }
  232. foreach (var entry in toRemove)
  233. queuedForImport.Remove(entry);
  234. if (queuedForImport.Count == 0)
  235. {
  236. numImportedFiles = 0;
  237. totalFilesToImport = 0;
  238. ProgressBar.Hide();
  239. }
  240. else
  241. {
  242. string displayName = lastEntry;
  243. displayName = displayName.Replace("\\", "\\\\");
  244. if (displayName.Length > 60)
  245. {
  246. displayName = displayName.Remove(0, displayName.Length - 60);
  247. displayName = "..." + displayName;
  248. }
  249. float pct = numImportedFiles / (float)totalFilesToImport;
  250. ProgressBar.Show("Importing (" + numImportedFiles + "/" + totalFilesToImport + ")", displayName, pct);
  251. }
  252. }
  253. }
  254. /// <summary>
  255. /// Triggered internally by the runtime when a new entry is added to the project library.
  256. /// </summary>
  257. /// <param name="path">Path relative to the project library resources folder.</param>
  258. private static void Internal_DoOnEntryAdded(string path)
  259. {
  260. if (OnEntryAdded != null)
  261. OnEntryAdded(path);
  262. }
  263. /// <summary>
  264. /// Triggered internally by the runtime when an entry is removed from the project library.
  265. /// </summary>
  266. /// <param name="path">Path relative to the project library resources folder.</param>
  267. private static void Internal_DoOnEntryRemoved(string path)
  268. {
  269. if (OnEntryRemoved != null)
  270. OnEntryRemoved(path);
  271. }
  272. [MethodImpl(MethodImplOptions.InternalCall)]
  273. private static extern string[] Internal_Refresh(string path, bool import);
  274. [MethodImpl(MethodImplOptions.InternalCall)]
  275. private static extern void Internal_Create(Resource resource, string path);
  276. [MethodImpl(MethodImplOptions.InternalCall)]
  277. private static extern Resource Internal_Load(string path);
  278. [MethodImpl(MethodImplOptions.InternalCall)]
  279. private static extern void Internal_Save(Resource resource);
  280. [MethodImpl(MethodImplOptions.InternalCall)]
  281. private static extern DirectoryEntry Internal_GetRoot();
  282. [MethodImpl(MethodImplOptions.InternalCall)]
  283. private static extern void Internal_Reimport(string path, ImportOptions options, bool force);
  284. [MethodImpl(MethodImplOptions.InternalCall)]
  285. private static extern LibraryEntry Internal_GetEntry(string path);
  286. [MethodImpl(MethodImplOptions.InternalCall)]
  287. private static extern LibraryEntry[] Internal_Search(string path, ResourceType[] types);
  288. [MethodImpl(MethodImplOptions.InternalCall)]
  289. private static extern string Internal_GetPath(Resource resource);
  290. [MethodImpl(MethodImplOptions.InternalCall)]
  291. private static extern string Internal_GetPathFromUUID(string uuid);
  292. [MethodImpl(MethodImplOptions.InternalCall)]
  293. private static extern void Internal_Delete(string path);
  294. [MethodImpl(MethodImplOptions.InternalCall)]
  295. private static extern void Internal_CreateFolder(string path);
  296. [MethodImpl(MethodImplOptions.InternalCall)]
  297. private static extern void Internal_Rename(string path, string name, bool overwrite);
  298. [MethodImpl(MethodImplOptions.InternalCall)]
  299. private static extern void Internal_Move(string oldPath, string newPath, bool overwrite);
  300. [MethodImpl(MethodImplOptions.InternalCall)]
  301. private static extern void Internal_Copy(string source, string destination, bool overwrite);
  302. [MethodImpl(MethodImplOptions.InternalCall)]
  303. private static extern string Internal_GetResourceFolder();
  304. [MethodImpl(MethodImplOptions.InternalCall)]
  305. private static extern void Internal_SetIncludeInBuild(string path, bool force);
  306. }
  307. /// <summary>
  308. /// Type of project library entries.
  309. /// </summary>
  310. public enum LibraryEntryType // Note: Must match the C++ enum ProjectLibrary::LibraryEntryType
  311. {
  312. File, Directory
  313. }
  314. /// <summary>
  315. /// Type of resources supported by the project library.
  316. /// </summary>
  317. public enum ResourceType // Note: Must match the C++ enum ScriptResourceType
  318. {
  319. Texture, SpriteTexture, Mesh, Font, Shader, Material, Prefab, PlainText, ScriptCode, StringTable, GUISkin, Undefined
  320. }
  321. /// <summary>
  322. /// A generic project library entry that may be a file or a folder.
  323. /// </summary>
  324. public class LibraryEntry : ScriptObject
  325. {
  326. /// <summary>
  327. /// Path of the library entry, relative to the project library resources folder.
  328. /// </summary>
  329. public string Path { get { return Internal_GetPath(mCachedPtr); } }
  330. /// <summary>
  331. /// Name of the library entry.
  332. /// </summary>
  333. public string Name { get { return Internal_GetName(mCachedPtr); } }
  334. /// <summary>
  335. /// Type of the library entry.
  336. /// </summary>
  337. public LibraryEntryType Type { get { return Internal_GetType(mCachedPtr); } }
  338. /// <summary>
  339. /// Directory entry that contains this entry. This may be null for the root entry.
  340. /// </summary>
  341. public DirectoryEntry Parent { get { return Internal_GetParent(mCachedPtr); } }
  342. [MethodImpl(MethodImplOptions.InternalCall)]
  343. private static extern string Internal_GetPath(IntPtr thisPtr);
  344. [MethodImpl(MethodImplOptions.InternalCall)]
  345. private static extern string Internal_GetName(IntPtr thisPtr);
  346. [MethodImpl(MethodImplOptions.InternalCall)]
  347. private static extern LibraryEntryType Internal_GetType(IntPtr thisPtr);
  348. [MethodImpl(MethodImplOptions.InternalCall)]
  349. private static extern DirectoryEntry Internal_GetParent(IntPtr thisPtr);
  350. }
  351. /// <summary>
  352. /// A project library entry representing a directory that contains other entries.
  353. /// </summary>
  354. public class DirectoryEntry : LibraryEntry
  355. {
  356. /// <summary>
  357. /// A set of entries contained in this entry.
  358. /// </summary>
  359. public LibraryEntry[] Children { get { return Internal_GetChildren(mCachedPtr); } }
  360. [MethodImpl(MethodImplOptions.InternalCall)]
  361. private static extern LibraryEntry[] Internal_GetChildren(IntPtr thisPtr);
  362. }
  363. /// <summary>
  364. /// A library entry representing a resource.
  365. /// </summary>
  366. public class FileEntry : LibraryEntry
  367. {
  368. /// <summary>
  369. /// Import options used for importing the resource.
  370. /// </summary>
  371. public ImportOptions Options { get { return Internal_GetImportOptions(mCachedPtr); } }
  372. /// <summary>
  373. /// Unique identifier of the resource.
  374. /// </summary>
  375. public string UUID { get { return Internal_GetUUID(mCachedPtr); } }
  376. /// <summary>
  377. /// Custom icon for the resource to display in the editor, if the resource has one.
  378. /// </summary>
  379. public Texture2D Icon { get { return Internal_GetIcon(mCachedPtr); } }
  380. /// <summary>
  381. /// Type of the resource referenced by this entry.
  382. /// </summary>
  383. public ResourceType ResType { get { return Internal_GetResourceType(mCachedPtr); } }
  384. /// <summary>
  385. /// Determines will the resource be included in the project build.
  386. /// </summary>
  387. public bool IncludeInBuild { get { return Internal_GetIncludeInBuild(mCachedPtr); } }
  388. [MethodImpl(MethodImplOptions.InternalCall)]
  389. private static extern ImportOptions Internal_GetImportOptions(IntPtr thisPtr);
  390. [MethodImpl(MethodImplOptions.InternalCall)]
  391. private static extern string Internal_GetUUID(IntPtr thisPtr);
  392. [MethodImpl(MethodImplOptions.InternalCall)]
  393. private static extern Texture2D Internal_GetIcon(IntPtr thisPtr);
  394. [MethodImpl(MethodImplOptions.InternalCall)]
  395. private static extern ResourceType Internal_GetResourceType(IntPtr thisPtr);
  396. [MethodImpl(MethodImplOptions.InternalCall)]
  397. private static extern bool Internal_GetIncludeInBuild(IntPtr thisPtr);
  398. }
  399. }