ProjectLibrary.cs 20 KB

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