2
0

ProjectLibrary.cs 21 KB

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