ProjectLibrary.cs 25 KB

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