StandaloneFileBrowserWindows.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Runtime.InteropServices;
  6. using System.Threading.Tasks;
  7. namespace SFB
  8. {
  9. public class StandaloneFileBrowserWindows : IStandaloneFileBrowser<ItemWithStream>
  10. {
  11. private const int BufferSize = 2048;
  12. [DllImport("StandaloneFileBrowser", CharSet = CharSet.Unicode)]
  13. private static extern bool DialogOpenFilePanel(IntPtr buffer, int bufferSize, [MarshalAs(UnmanagedType.LPWStr)]string title, [MarshalAs(UnmanagedType.LPWStr)]string directory, [MarshalAs(UnmanagedType.LPWStr)]string extension, bool multiselect);
  14. [DllImport("StandaloneFileBrowser", CharSet = CharSet.Unicode)]
  15. private static extern bool DialogOpenFilePanelAsync(IntPtr buffer, int bufferSize, [MarshalAs(UnmanagedType.LPWStr)]string title, [MarshalAs(UnmanagedType.LPWStr)]string directory, [MarshalAs(UnmanagedType.LPWStr)]string extension, bool multiselect, AsyncCallback callback);
  16. [DllImport("StandaloneFileBrowser", CharSet = CharSet.Unicode)]
  17. private static extern bool DialogOpenFolderPanel(IntPtr buffer, int bufferSize, [MarshalAs(UnmanagedType.LPWStr)]string title, [MarshalAs(UnmanagedType.LPWStr)]string directory, bool multiselect);
  18. [DllImport("StandaloneFileBrowser", CharSet = CharSet.Unicode)]
  19. private static extern void DialogOpenFolderPanelAsync(IntPtr buffer, int bufferSize, [MarshalAs(UnmanagedType.LPWStr)]string title, [MarshalAs(UnmanagedType.LPWStr)]string directory, bool multiselect, AsyncCallback callback);
  20. [DllImport("StandaloneFileBrowser", CharSet = CharSet.Unicode)]
  21. private static extern bool DialogSaveFilePanel(IntPtr buffer, int bufferSize, [MarshalAs(UnmanagedType.LPWStr)]string title, [MarshalAs(UnmanagedType.LPWStr)]string directory, [MarshalAs(UnmanagedType.LPWStr)]string defaultName, [MarshalAs(UnmanagedType.LPWStr)]string extension);
  22. [DllImport("StandaloneFileBrowser", CharSet = CharSet.Unicode)]
  23. private static extern void DialogSaveFilePanelAsync(IntPtr buffer, int bufferSize, [MarshalAs(UnmanagedType.LPWStr)] string title, [MarshalAs(UnmanagedType.LPWStr)]string directory, [MarshalAs(UnmanagedType.LPWStr)] string defaultName, [MarshalAs(UnmanagedType.LPWStr)]string extension, AsyncCallback callback);
  24. public IList<ItemWithStream> OpenFilePanel(string title, string directory, ExtensionFilter[] extensions, bool multiselect)
  25. {
  26. var results = new List<ItemWithStream>();
  27. var buffer = new char[BufferSize];
  28. var bufferLock = GCHandle.Alloc(buffer, GCHandleType.Pinned);
  29. if (DialogOpenFilePanel(bufferLock.AddrOfPinnedObject(), BufferSize, title, directory, GetFilterFromFileExtensionList(extensions), multiselect))
  30. {
  31. ParseResults(buffer, results, multiselect);
  32. }
  33. bufferLock.Free();
  34. return results.ToArray();
  35. }
  36. public void OpenFilePanelAsync(string title, string directory, ExtensionFilter[] extensions, bool multiselect, Action<IList<ItemWithStream>> cb)
  37. {
  38. //todo: async
  39. cb(OpenFilePanel(title, directory, extensions, multiselect));
  40. }
  41. public IList<ItemWithStream> OpenFolderPanel(string title, string directory, bool multiselect)
  42. {
  43. var results = new List<ItemWithStream>();
  44. var buffer = new char[BufferSize];
  45. var bufferLock = GCHandle.Alloc(buffer, GCHandleType.Pinned);
  46. if (DialogOpenFolderPanel(bufferLock.AddrOfPinnedObject(), BufferSize, title, directory, multiselect))
  47. {
  48. ParseResults(buffer, results, multiselect);
  49. }
  50. bufferLock.Free();
  51. return results;
  52. }
  53. public void OpenFolderPanelAsync(string title, string directory, bool multiselect, Action<IList<ItemWithStream>> cb)
  54. {
  55. //todo: async
  56. cb(OpenFolderPanel(title, directory, multiselect));
  57. }
  58. public ItemWithStream SaveFilePanel(string title, string directory, string defaultName, ExtensionFilter[] extensions)
  59. {
  60. var results = new List<ItemWithStream>();
  61. var buffer = new char[BufferSize];
  62. var bufferLock = GCHandle.Alloc(buffer, GCHandleType.Pinned);
  63. if (DialogSaveFilePanel(bufferLock.AddrOfPinnedObject(), BufferSize, title, directory, defaultName, GetFilterFromFileExtensionList(extensions)))
  64. {
  65. ParseResults(buffer, results, false);
  66. }
  67. bufferLock.Free();
  68. return results.Count > 0 ? results[0] : null;
  69. }
  70. public void SaveFilePanelAsync(string title, string directory, string defaultName, ExtensionFilter[] extensions, Action<ItemWithStream> cb)
  71. {
  72. //todo: async
  73. cb(SaveFilePanel(title, directory, defaultName, extensions));
  74. }
  75. private static string GetFilterFromFileExtensionList(ExtensionFilter[] extensions)
  76. {
  77. var filterString = "";
  78. if (extensions != null)
  79. {
  80. foreach (var extension in extensions)
  81. {
  82. if (!string.IsNullOrWhiteSpace(extension.Name))
  83. {
  84. filterString += extension.Name;
  85. }
  86. else
  87. {
  88. var descriptionString = "";
  89. foreach (var format in extension.Extensions)
  90. {
  91. if (descriptionString != "")
  92. {
  93. descriptionString += ",";
  94. }
  95. descriptionString += "(*" + (format[0] == '.' ? format.Substring(1) : format) + ")";
  96. }
  97. filterString += descriptionString;
  98. }
  99. filterString += "\0";
  100. foreach (var format in extension.Extensions)
  101. {
  102. filterString += "*" + (format[0] == '.' ? format.Substring(1) : format) + ";";
  103. }
  104. filterString += "\0";
  105. }
  106. }
  107. filterString += "\0";
  108. return filterString;
  109. }
  110. private static void ParseResults(char[] buffer, List<ItemWithStream> results, bool multiselect)
  111. {
  112. var currentStringBytes = new List<char>();
  113. foreach (var c in buffer)
  114. {
  115. if (c == 0)
  116. {
  117. var currentString = new string(currentStringBytes.ToArray());
  118. if (!string.IsNullOrWhiteSpace(currentString) && currentString != "\0")
  119. {
  120. var filename = multiselect && results.Count > 0 ? $"{results[0].Name}\\{currentString}" : currentString;
  121. results.Add(new ItemWithStream
  122. {
  123. Name = filename//,
  124. //Stream = File.OpenRead(filename)
  125. });
  126. }
  127. currentStringBytes.Clear();
  128. continue;
  129. }
  130. currentStringBytes.Add(c);
  131. }
  132. if (multiselect && results.Count > 1)
  133. {
  134. results.RemoveAt(0);
  135. }
  136. }
  137. }
  138. }
  139. #endif