123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- #if UNITY_IOS && !UNITY_EDITOR
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Threading.Tasks;
- namespace TriLibCore.SFB
- {
- public class StandaloneFileBrowserIOS : IStandaloneFileBrowser<ItemWithStream>
- {
- private static Action<IList<ItemWithStream>> _openFileCb;
- private static Action<IList<ItemWithStream>> _openFolderCb;
- private static Action<ItemWithStream> _saveFileCb;
- [UnmanagedFunctionPointer(CallingConvention.StdCall)]
- public delegate void AsyncCallback(string path);
- [AOT.MonoPInvokeCallback(typeof(AsyncCallback))]
- private static void openFileCb(string result)
- {
- _openFileCb.Invoke(ParseResults(result));
- }
- [AOT.MonoPInvokeCallback(typeof(AsyncCallback))]
- private static void openFolderCb(string result)
- {
- _openFolderCb.Invoke(ParseResults(result));
- }
- [AOT.MonoPInvokeCallback(typeof(AsyncCallback))]
- private static void saveFileCb(string result)
- {
- _saveFileCb.Invoke(ParseResults(result)?[0]);
- }
- [DllImport("__Internal")]
- private static extern IntPtr DialogOpenFilePanel(string title, string directory, string extension, bool multiselect);
- [DllImport("__Internal")]
- private static extern void DialogOpenFilePanelAsync(string title, string directory, string extension, bool multiselect, AsyncCallback callback);
- [DllImport("__Internal")]
- private static extern IntPtr DialogOpenFolderPanel(string title, string directory, bool multiselect);
- [DllImport("__Internal")]
- private static extern void DialogOpenFolderPanelAsync(string title, string directory, bool multiselect, AsyncCallback callback);
- [DllImport("__Internal")]
- private static extern IntPtr DialogSaveFilePanel(string title, string directory, string defaultName, string extension);
- [DllImport("__Internal")]
- private static extern void DialogSaveFilePanelAsync(string title, string directory, string defaultName, string extension, AsyncCallback callback);
- public IList<ItemWithStream> OpenFilePanel(string title, string directory, ExtensionFilter[] extensions, bool multiselect)
- {
- OpenFilePanelAsync(title, directory, extensions, multiselect, null);
- return null;
- }
- public void OpenFilePanelAsync(string title, string directory, ExtensionFilter[] extensions, bool multiselect, Action<IList<ItemWithStream>> cb)
- {
- _openFileCb = cb;
- DialogOpenFilePanelAsync(
- title,
- directory,
- GetFilterFromFileExtensionList(extensions),
- multiselect,
- openFileCb);
- }
- public IList<ItemWithStream> OpenFolderPanel(string title, string directory, bool multiselect)
- {
- var paths = Marshal.PtrToStringAnsi(DialogOpenFolderPanel(
- title,
- directory,
- multiselect));
- return ParseResults(paths);
- }
- public void OpenFolderPanelAsync(string title, string directory, bool multiselect, Action<IList<ItemWithStream>> cb)
- {
- _openFolderCb = cb;
- DialogOpenFolderPanelAsync(
- title,
- directory,
- multiselect,
- openFolderCb);
- }
- public ItemWithStream SaveFilePanel(string title, string directory, string defaultName, ExtensionFilter[] extensions)
- {
- var path = Marshal.PtrToStringAnsi(DialogSaveFilePanel(
- title,
- directory,
- defaultName,
- GetFilterFromFileExtensionList(extensions)));
- return new ItemWithStream()
- {
- Name = path
- };
- }
- public void SaveFilePanelAsync(string title, string directory, string defaultName, ExtensionFilter[] extensions,Action<ItemWithStream> cb)
- {
- _saveFileCb = cb;
- DialogSaveFilePanelAsync(
- title,
- directory,
- defaultName,
- GetFilterFromFileExtensionList(extensions),
- saveFileCb);
- }
- private static string GetFilterFromFileExtensionList(ExtensionFilter[] extensions)
- {
- if (extensions == null)
- {
- return "";
- }
- var filterString = "";
- foreach (var filter in extensions)
- {
- filterString += filter.Name + ";";
- foreach (var ext in filter.Extensions)
- {
- filterString += ext + ",";
- }
- filterString = filterString.Remove(filterString.Length - 1);
- filterString += "|";
- }
- filterString = filterString.Remove(filterString.Length - 1);
- return filterString;
- }
- private static IList<ItemWithStream> ParseResults(string paths)
- {
- if (paths != null)
- {
- var filenames = paths.Split((char)28);
- var result = new ItemWithStream[filenames.Length];
- for (var i = 0; i < filenames.Length; i++)
- {
- result[i] = new ItemWithStream()
- {
- Name = filenames[i]//,
- //Stream = File.OpenRead(filenames[i])
- };
- }
- return result;
- }
- return null;
- }
- }
- }
- #endif
|