StandaloneFileBrowserLinux.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #if UNITY_STANDALONE_LINUX || UNITY_EDITOR_LINUX
  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 StandaloneFileBrowserLinux : 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 void DialogInit();
  33. [DllImport("StandaloneFileBrowser")]
  34. private static extern IntPtr DialogOpenFilePanel(string title, string directory, string extension, bool multiselect);
  35. [DllImport("StandaloneFileBrowser")]
  36. private static extern void DialogOpenFilePanelAsync(string title, string directory, string extension, bool multiselect, AsyncCallback callback);
  37. [DllImport("StandaloneFileBrowser")]
  38. private static extern IntPtr DialogOpenFolderPanel(string title, string directory, bool multiselect);
  39. [DllImport("StandaloneFileBrowser")]
  40. private static extern void DialogOpenFolderPanelAsync(string title, string directory, bool multiselect, AsyncCallback callback);
  41. [DllImport("StandaloneFileBrowser")]
  42. private static extern IntPtr DialogSaveFilePanel(string title, string directory, string defaultName, string extension);
  43. [DllImport("StandaloneFileBrowser")]
  44. private static extern void DialogSaveFilePanelAsync(string title, string directory, string defaultName, string extension, AsyncCallback callback);
  45. public StandaloneFileBrowserLinux()
  46. {
  47. DialogInit();
  48. }
  49. public IList<ItemWithStream> OpenFilePanel(string title, string directory, ExtensionFilter[] extensions, bool multiselect)
  50. {
  51. var paths = Marshal.PtrToStringAnsi(DialogOpenFilePanel(
  52. title,
  53. directory,
  54. GetFilterFromFileExtensionList(extensions),
  55. multiselect));
  56. return ParseResults(paths);
  57. }
  58. public void OpenFilePanelAsync(string title, string directory, ExtensionFilter[] extensions, bool multiselect, Action<IList<ItemWithStream>> cb)
  59. {
  60. _openFileCb = cb;
  61. DialogOpenFilePanelAsync(
  62. title,
  63. directory,
  64. GetFilterFromFileExtensionList(extensions),
  65. multiselect,
  66. openFileCb);
  67. }
  68. public IList<ItemWithStream> OpenFolderPanel(string title, string directory, bool multiselect)
  69. {
  70. var paths = Marshal.PtrToStringAnsi(DialogOpenFolderPanel(
  71. title,
  72. directory,
  73. multiselect));
  74. return ParseResults(paths);
  75. }
  76. public void OpenFolderPanelAsync(string title, string directory, bool multiselect, Action<IList<ItemWithStream>> cb)
  77. {
  78. _openFolderCb = cb;
  79. DialogOpenFolderPanelAsync(
  80. title,
  81. directory,
  82. multiselect,
  83. openFolderCb);
  84. }
  85. public ItemWithStream SaveFilePanel(string title, string directory, string defaultName, ExtensionFilter[] extensions)
  86. {
  87. var filename = Marshal.PtrToStringAnsi(DialogSaveFilePanel(
  88. title,
  89. directory,
  90. defaultName,
  91. GetFilterFromFileExtensionList(extensions)));
  92. return new ItemWithStream
  93. {
  94. Name = filename
  95. };
  96. }
  97. public void SaveFilePanelAsync(string title, string directory, string defaultName, ExtensionFilter[] extensions, Action<ItemWithStream> cb)
  98. {
  99. _saveFileCb = cb;
  100. DialogSaveFilePanelAsync(
  101. title,
  102. directory,
  103. defaultName,
  104. GetFilterFromFileExtensionList(extensions),
  105. saveFileCb);
  106. }
  107. private static string GetFilterFromFileExtensionList(ExtensionFilter[] extensions)
  108. {
  109. if (extensions == null)
  110. {
  111. return "";
  112. }
  113. var filterString = "";
  114. foreach (var filter in extensions)
  115. {
  116. filterString += filter.Name + ";";
  117. foreach (var ext in filter.Extensions)
  118. {
  119. filterString += ext + ",";
  120. }
  121. filterString = filterString.Remove(filterString.Length - 1);
  122. filterString += "|";
  123. }
  124. filterString = filterString.Remove(filterString.Length - 1);
  125. return filterString;
  126. }
  127. private static IList<ItemWithStream> ParseResults(string paths)
  128. {
  129. if (paths != null)
  130. {
  131. var filenames = paths.Split((char)28);
  132. var result = new ItemWithStream[filenames.Length];
  133. for (var i = 0; i < filenames.Length; i++)
  134. {
  135. result[i] = new ItemWithStream()
  136. {
  137. Name = filenames[i]//,
  138. //Stream = File.OpenRead(filenames[i])
  139. };
  140. }
  141. return result;
  142. }
  143. return null;
  144. }
  145. }
  146. }
  147. #endif