ProjectLibrary.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. public sealed class ProjectLibrary : ScriptObject
  9. {
  10. public static DirectoryEntry Root { get { return Internal_GetRoot(); } }
  11. public static string ResourceFolder { get { return Internal_GetResourceFolder(); } }
  12. public static event Action<string> OnEntryAdded;
  13. public static event Action<string> OnEntryRemoved;
  14. private static HashSet<string> queuedForImport = new HashSet<string>();
  15. private static int numImportedFiles;
  16. private static int totalFilesToImport;
  17. private const float TIME_SLICE_SECONDS = 0.030f;
  18. public static void Refresh()
  19. {
  20. string[] modifiedPaths = Internal_Refresh(ResourceFolder, false);
  21. foreach (var modifiedPath in modifiedPaths)
  22. {
  23. if (queuedForImport.Add(modifiedPath))
  24. totalFilesToImport++;
  25. }
  26. }
  27. public static void Refresh(string path)
  28. {
  29. string[] modifiedPaths = Internal_Refresh(path, false);
  30. foreach (var modifiedPath in modifiedPaths)
  31. {
  32. if (queuedForImport.Add(modifiedPath))
  33. totalFilesToImport++;
  34. }
  35. }
  36. public static void Create(Resource resource, string path)
  37. {
  38. if (Path.IsPathRooted(path))
  39. throw new ArgumentException("Provided path must be relative.", "path");
  40. Internal_Create(resource, path);
  41. }
  42. public static void Save(Resource resource)
  43. {
  44. Internal_Save(resource);
  45. }
  46. public static T Load<T>(string path) where T : Resource
  47. {
  48. if (Path.IsPathRooted(path))
  49. throw new ArgumentException("Provided path must be relative.", "path");
  50. return (T) Internal_Load(path);
  51. }
  52. public static void Reimport(string path, ImportOptions options = null, bool force = false)
  53. {
  54. Internal_Reimport(path, options, force);
  55. }
  56. public static bool Exists(string path)
  57. {
  58. return GetEntry(path) != null;
  59. }
  60. public static LibraryEntry GetEntry(string path)
  61. {
  62. return Internal_GetEntry(path);
  63. }
  64. public static LibraryEntry[] Search(string pattern, ResourceType[] types = null)
  65. {
  66. return Internal_Search(pattern, types);
  67. }
  68. public static string GetPath(Resource resource)
  69. {
  70. return Internal_GetPath(resource);
  71. }
  72. public static void Delete(string path)
  73. {
  74. Internal_Delete(path);
  75. }
  76. public static void CreateFolder(string path)
  77. {
  78. Internal_CreateFolder(path);
  79. }
  80. public static void Rename(string path, string name)
  81. {
  82. Internal_Rename(path, name);
  83. }
  84. public static void Move(string oldPath, string newPath, bool overwrite = false)
  85. {
  86. Internal_Move(oldPath, newPath, overwrite);
  87. }
  88. public static void Copy(string source, string destination, bool overwrite = false)
  89. {
  90. Internal_Copy(source, destination, overwrite);
  91. }
  92. internal static void Update()
  93. {
  94. if (queuedForImport.Count > 0)
  95. {
  96. UInt64 start = Time.Precise;
  97. List<string> toRemove = new List<string>();
  98. foreach (var entry in queuedForImport)
  99. {
  100. float pct = numImportedFiles/(float)totalFilesToImport;
  101. ProgressBar.Show("Importing (" + numImportedFiles + "/" + totalFilesToImport + ")", entry, pct);
  102. Internal_Refresh(entry, true);
  103. toRemove.Add(entry);
  104. numImportedFiles++;
  105. UInt64 end = Time.Precise;
  106. UInt64 elapsed = end - start;
  107. float elapsedSeconds = elapsed * Time.MicroToSecond;
  108. if (elapsedSeconds > TIME_SLICE_SECONDS)
  109. break;
  110. }
  111. foreach (var entry in toRemove)
  112. queuedForImport.Remove(entry);
  113. if (queuedForImport.Count == 0)
  114. {
  115. numImportedFiles = 0;
  116. totalFilesToImport = 0;
  117. ProgressBar.Hide();
  118. }
  119. }
  120. }
  121. private static void Internal_DoOnEntryAdded(string path)
  122. {
  123. if (OnEntryAdded != null)
  124. OnEntryAdded(path);
  125. }
  126. private static void Internal_DoOnEntryRemoved(string path)
  127. {
  128. if (OnEntryRemoved != null)
  129. OnEntryRemoved(path);
  130. }
  131. [MethodImpl(MethodImplOptions.InternalCall)]
  132. private static extern string[] Internal_Refresh(string path, bool import);
  133. [MethodImpl(MethodImplOptions.InternalCall)]
  134. private static extern void Internal_Create(Resource resource, string path);
  135. [MethodImpl(MethodImplOptions.InternalCall)]
  136. private static extern Resource Internal_Load(string path);
  137. [MethodImpl(MethodImplOptions.InternalCall)]
  138. private static extern void Internal_Save(Resource resource);
  139. [MethodImpl(MethodImplOptions.InternalCall)]
  140. private static extern DirectoryEntry Internal_GetRoot();
  141. [MethodImpl(MethodImplOptions.InternalCall)]
  142. private static extern void Internal_Reimport(string path, ImportOptions options, bool force);
  143. [MethodImpl(MethodImplOptions.InternalCall)]
  144. private static extern LibraryEntry Internal_GetEntry(string path);
  145. [MethodImpl(MethodImplOptions.InternalCall)]
  146. private static extern LibraryEntry[] Internal_Search(string path, ResourceType[] types);
  147. [MethodImpl(MethodImplOptions.InternalCall)]
  148. private static extern string Internal_GetPath(Resource resource);
  149. [MethodImpl(MethodImplOptions.InternalCall)]
  150. private static extern void Internal_Delete(string path);
  151. [MethodImpl(MethodImplOptions.InternalCall)]
  152. private static extern void Internal_CreateFolder(string path);
  153. [MethodImpl(MethodImplOptions.InternalCall)]
  154. private static extern void Internal_Rename(string path, string name);
  155. [MethodImpl(MethodImplOptions.InternalCall)]
  156. private static extern void Internal_Move(string oldPath, string newPath, bool overwrite);
  157. [MethodImpl(MethodImplOptions.InternalCall)]
  158. private static extern void Internal_Copy(string source, string destination, bool overwrite);
  159. [MethodImpl(MethodImplOptions.InternalCall)]
  160. private static extern string Internal_GetResourceFolder();
  161. }
  162. // Note: Must be the same as C++ enum ProjectLibrary::LibraryEntryType
  163. public enum LibraryEntryType
  164. {
  165. File, Directory
  166. }
  167. // Note: Must be the same as C++ enum ScriptResourceType
  168. public enum ResourceType
  169. {
  170. Texture, SpriteTexture, Mesh, Font, Shader, Material, PlainText, ScriptCode, Undefined
  171. }
  172. public class LibraryEntry : ScriptObject
  173. {
  174. public string Path { get { return Internal_GetPath(mCachedPtr); } }
  175. public string Name { get { return Internal_GetName(mCachedPtr); } }
  176. public LibraryEntryType Type { get { return Internal_GetType(mCachedPtr); } }
  177. public DirectoryEntry Parent { get { return Internal_GetParent(mCachedPtr); } }
  178. [MethodImpl(MethodImplOptions.InternalCall)]
  179. private static extern string Internal_GetPath(IntPtr thisPtr);
  180. [MethodImpl(MethodImplOptions.InternalCall)]
  181. private static extern string Internal_GetName(IntPtr thisPtr);
  182. [MethodImpl(MethodImplOptions.InternalCall)]
  183. private static extern LibraryEntryType Internal_GetType(IntPtr thisPtr);
  184. [MethodImpl(MethodImplOptions.InternalCall)]
  185. private static extern DirectoryEntry Internal_GetParent(IntPtr thisPtr);
  186. }
  187. public class DirectoryEntry : LibraryEntry
  188. {
  189. public LibraryEntry[] Children { get { return Internal_GetChildren(mCachedPtr); } }
  190. [MethodImpl(MethodImplOptions.InternalCall)]
  191. private static extern LibraryEntry[] Internal_GetChildren(IntPtr thisPtr);
  192. }
  193. public class FileEntry : LibraryEntry
  194. {
  195. public ImportOptions Options { get { return Internal_GetImportOptions(mCachedPtr); } }
  196. public string UUID { get { return Internal_GetUUID(mCachedPtr); } }
  197. public Texture2D Icon { get { return Internal_GetIcon(mCachedPtr); } }
  198. public ResourceType ResType { get { return Internal_GetResourceType(mCachedPtr); } }
  199. [MethodImpl(MethodImplOptions.InternalCall)]
  200. private static extern ImportOptions Internal_GetImportOptions(IntPtr thisPtr);
  201. [MethodImpl(MethodImplOptions.InternalCall)]
  202. private static extern string Internal_GetUUID(IntPtr thisPtr);
  203. [MethodImpl(MethodImplOptions.InternalCall)]
  204. private static extern Texture2D Internal_GetIcon(IntPtr thisPtr);
  205. [MethodImpl(MethodImplOptions.InternalCall)]
  206. private static extern ResourceType Internal_GetResourceType(IntPtr thisPtr);
  207. }
  208. }