StandaloneFileBrowserMac.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Runtime.InteropServices;
  6. using System.Threading.Tasks;
  7. namespace TriLibCore.SFB
  8. {
  9. public class StandaloneFileBrowserMac : IStandaloneFileBrowser<ItemWithStream>
  10. {
  11. private static Action<IList<ItemWithStream>> _openFileCb;
  12. private static Action<IList<ItemWithStream>> _openFolderCb;
  13. private static Action<ItemWithStream> _saveFileCb;
  14. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  15. public delegate void AsyncCallback(string path);
  16. [AOT.MonoPInvokeCallback(typeof(AsyncCallback))]
  17. private static void openFileCb(string result)
  18. {
  19. _openFileCb.Invoke(ParseResults(result));
  20. }
  21. [AOT.MonoPInvokeCallback(typeof(AsyncCallback))]
  22. private static void openFolderCb(string result)
  23. {
  24. _openFolderCb.Invoke(ParseResults(result));
  25. }
  26. [AOT.MonoPInvokeCallback(typeof(AsyncCallback))]
  27. private static void saveFileCb(string result)
  28. {
  29. _saveFileCb.Invoke(ParseResults(result)?[0]);
  30. }
  31. [DllImport("StandaloneFileBrowser")]
  32. private static extern IntPtr DialogOpenFilePanel(string title, string directory, string extension, bool multiselect);
  33. [DllImport("StandaloneFileBrowser")]
  34. private static extern void DialogOpenFilePanelAsync(string title, string directory, string extension, bool multiselect, AsyncCallback callback);
  35. [DllImport("StandaloneFileBrowser")]
  36. private static extern IntPtr DialogOpenFolderPanel(string title, string directory, bool multiselect);
  37. [DllImport("StandaloneFileBrowser")]
  38. private static extern void DialogOpenFolderPanelAsync(string title, string directory, bool multiselect, AsyncCallback callback);
  39. [DllImport("StandaloneFileBrowser")]
  40. private static extern IntPtr DialogSaveFilePanel(string title, string directory, string defaultName, string extension);
  41. [DllImport("StandaloneFileBrowser")]
  42. private static extern void DialogSaveFilePanelAsync(string title, string directory, string defaultName, string extension, AsyncCallback callback);
  43. public IList<ItemWithStream> OpenFilePanel(string title, string directory, ExtensionFilter[] extensions, bool multiselect)
  44. {
  45. var paths = Marshal.PtrToStringAnsi(DialogOpenFilePanel(
  46. title,
  47. directory,
  48. GetFilterFromFileExtensionList(extensions),
  49. multiselect));
  50. return ParseResults(paths);
  51. }
  52. public void OpenFilePanelAsync(string title, string directory, ExtensionFilter[] extensions, bool multiselect, Action<IList<ItemWithStream>> cb)
  53. {
  54. _openFileCb = cb;
  55. DialogOpenFilePanelAsync(
  56. title,
  57. directory,
  58. GetFilterFromFileExtensionList(extensions),
  59. multiselect,
  60. openFileCb);
  61. }
  62. public IList<ItemWithStream> OpenFolderPanel(string title, string directory, bool multiselect)
  63. {
  64. var paths = Marshal.PtrToStringAnsi(DialogOpenFolderPanel(
  65. title,
  66. directory,
  67. multiselect));
  68. return ParseResults(paths);
  69. }
  70. public void OpenFolderPanelAsync(string title, string directory, bool multiselect, Action<IList<ItemWithStream>> cb)
  71. {
  72. _openFolderCb = cb;
  73. DialogOpenFolderPanelAsync(
  74. title,
  75. directory,
  76. multiselect,
  77. openFolderCb);
  78. }
  79. public ItemWithStream SaveFilePanel(string title, string directory, string defaultName, ExtensionFilter[] extensions)
  80. {
  81. var filename = Marshal.PtrToStringAnsi(DialogSaveFilePanel(
  82. title,
  83. directory,
  84. defaultName,
  85. GetFilterFromFileExtensionList(extensions)));
  86. return new ItemWithStream
  87. {
  88. Name = filename
  89. };
  90. }
  91. public void SaveFilePanelAsync(string title, string directory, string defaultName, ExtensionFilter[] extensions, Action<ItemWithStream> cb)
  92. {
  93. _saveFileCb = cb;
  94. DialogSaveFilePanelAsync(
  95. title,
  96. directory,
  97. defaultName,
  98. GetFilterFromFileExtensionList(extensions),
  99. saveFileCb);
  100. }
  101. private static string GetFilterFromFileExtensionList(ExtensionFilter[] extensions)
  102. {
  103. if (extensions == null)
  104. {
  105. return "";
  106. }
  107. var filterString = "";
  108. foreach (var filter in extensions)
  109. {
  110. var hasFormat = false;
  111. filterString += filter.Name + ";";
  112. foreach (var format in filter.Extensions)
  113. {
  114. if (hasFormat)
  115. {
  116. filterString += ",";
  117. }
  118. filterString += (format[0] == '.' ? format.Substring(1) : format);
  119. hasFormat = true;
  120. }
  121. filterString += "|";
  122. }
  123. filterString = filterString.Remove(filterString.Length - 1);
  124. return filterString;
  125. }
  126. private static IList<ItemWithStream> ParseResults(string paths)
  127. {
  128. if (paths != null)
  129. {
  130. var filenames = paths.Split((char)28);
  131. var result = new ItemWithStream[filenames.Length];
  132. for (var i = 0; i < filenames.Length; i++)
  133. {
  134. result[i] = new ItemWithStream()
  135. {
  136. Name = filenames[i]//,
  137. //Stream = File.OpenRead(filenames[i])
  138. };
  139. }
  140. return result;
  141. }
  142. return null;
  143. }
  144. }
  145. }
  146. #endif