StandaloneFileBrowserIOS.cs 5.7 KB

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