|
| 1 | +using System.Buffers; |
| 2 | +using System.Runtime.CompilerServices; |
| 3 | +using System.Runtime.Versioning; |
| 4 | +using System.Text; |
| 5 | +using TerraFX.Interop.Windows; |
| 6 | + |
| 7 | +namespace AssetRipper.NativeDialogs; |
| 8 | + |
| 9 | +public static class OpenFilesDialog |
| 10 | +{ |
| 11 | + public static bool Supported => |
| 12 | + OperatingSystem.IsWindows() || |
| 13 | + OperatingSystem.IsMacOS() || |
| 14 | + (OperatingSystem.IsLinux() && Gtk.Global.IsSupported); |
| 15 | + |
| 16 | + public static Task<string[]?> OpenFilesAsync(OpenFileDialogOptions? options = null) |
| 17 | + { |
| 18 | + options ??= OpenFileDialogOptions.Default; |
| 19 | + if (OperatingSystem.IsWindows()) |
| 20 | + { |
| 21 | + return OpenFilesAsyncWindows(options); |
| 22 | + } |
| 23 | + else if (OperatingSystem.IsMacOS()) |
| 24 | + { |
| 25 | + return OpenFilesAsyncMacOS(options); |
| 26 | + } |
| 27 | + else if (OperatingSystem.IsLinux()) |
| 28 | + { |
| 29 | + return OpenFilesAsyncLinux(options); |
| 30 | + } |
| 31 | + else |
| 32 | + { |
| 33 | + return Task.FromResult<string[]?>(null); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + [SupportedOSPlatform("windows")] |
| 38 | + private unsafe static Task<string[]?> OpenFilesAsyncWindows(OpenFileDialogOptions options) |
| 39 | + { |
| 40 | + // https://learn.microsoft.com/en-us/windows/win32/api/commdlg/ns-commdlg-openfilenamew |
| 41 | + |
| 42 | + char[] buffer = ArrayPool<char>.Shared.Rent(ushort.MaxValue + 1); // Should be enough for the overwhelming majority of cases. |
| 43 | + new Span<char>(buffer).Clear(); |
| 44 | + |
| 45 | + string filter; |
| 46 | + if (options.Filters.Count == 0) |
| 47 | + { |
| 48 | + filter = "All Files\0*.*\0\0"; |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + StringBuilder filterBuilder = new(); |
| 53 | + foreach (KeyValuePair<string, string> pair in options.Filters) |
| 54 | + { |
| 55 | + filterBuilder |
| 56 | + .Append(pair.Key).Append('\0') |
| 57 | + .Append("*.").Append(pair.Value).Append('\0'); |
| 58 | + } |
| 59 | + filterBuilder.Append('\0'); // End of filter list |
| 60 | + filter = filterBuilder.ToString(); |
| 61 | + } |
| 62 | + |
| 63 | + fixed (char* bufferPtr = buffer) |
| 64 | + fixed (char* filterPtr = filter) |
| 65 | + { |
| 66 | + OPENFILENAMEW ofn = default; |
| 67 | + ofn.lStructSize = (uint)Unsafe.SizeOf<OPENFILENAMEW>(); |
| 68 | + ofn.hwndOwner = default; // No owner window. |
| 69 | + ofn.lpstrFile = bufferPtr; |
| 70 | + ofn.nMaxFile = (uint)buffer.Length; |
| 71 | + ofn.lpstrFilter = filterPtr; |
| 72 | + ofn.nFilterIndex = 1; // The first pair of strings has an index value of 1. |
| 73 | + ofn.Flags = OFN.OFN_PATHMUSTEXIST | OFN.OFN_FILEMUSTEXIST | OFN.OFN_ALLOWMULTISELECT | OFN.OFN_EXPLORER; |
| 74 | + if (Windows.GetOpenFileNameW(&ofn) && buffer[^1] == 0) |
| 75 | + { |
| 76 | + List<string> files = []; |
| 77 | + |
| 78 | + int directoryLength = Array.IndexOf(buffer, '\0'); |
| 79 | + string directory = new(buffer, 0, directoryLength); |
| 80 | + |
| 81 | + int startIndex = directoryLength + 1; |
| 82 | + while (startIndex < buffer.Length && buffer[startIndex] != '\0') |
| 83 | + { |
| 84 | + int endIndex = Array.IndexOf(buffer, '\0', startIndex); |
| 85 | + string fileName = new(buffer, startIndex, endIndex - startIndex); |
| 86 | + files.Add(Path.Combine(directory, fileName)); |
| 87 | + startIndex = endIndex + 1; // Move to the next file name |
| 88 | + } |
| 89 | + |
| 90 | + ArrayPool<char>.Shared.Return(buffer); |
| 91 | + if (files.Count > 0) |
| 92 | + { |
| 93 | + return Task.FromResult<string[]?>(files.ToArray()); |
| 94 | + } |
| 95 | + else |
| 96 | + { |
| 97 | + // If a single file was selected, the system appends it to the directory path. |
| 98 | + return Task.FromResult<string[]?>([directory]); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + ArrayPool<char>.Shared.Return(buffer); |
| 104 | + return Task.FromResult<string[]?>(null); |
| 105 | + } |
| 106 | + |
| 107 | + [SupportedOSPlatform("macos")] |
| 108 | + private static async Task<string[]?> OpenFilesAsyncMacOS(OpenFileDialogOptions options) |
| 109 | + { |
| 110 | + ReadOnlySpan<string> arguments = |
| 111 | + [ |
| 112 | + "-e", "set theFiles to choose file with multiple selections allowed", |
| 113 | + "-e", "set filePaths to {}", |
| 114 | + "-e", "repeat with aFile in theFiles", |
| 115 | + "-e", "set end of filePaths to POSIX path of aFile", |
| 116 | + "-e", "end repeat", |
| 117 | + "-e", "set text item delimiters to \":\"", |
| 118 | + "-e", "return filePaths as string", |
| 119 | + ]; |
| 120 | + string? output = await ProcessExecutor.TryRun("osascript", arguments); |
| 121 | + if (string.IsNullOrEmpty(output)) |
| 122 | + { |
| 123 | + return null; // User canceled the dialog |
| 124 | + } |
| 125 | + return output.Split(':'); |
| 126 | + } |
| 127 | + |
| 128 | + [SupportedOSPlatform("linux")] |
| 129 | + private static async Task<string[]?> OpenFilesAsyncLinux(OpenFileDialogOptions options) |
| 130 | + { |
| 131 | + // Todo: proper Linux implementation |
| 132 | + string? path = await OpenFileDialog.OpenFileAsync(); |
| 133 | + if (string.IsNullOrEmpty(path)) |
| 134 | + { |
| 135 | + return null; // User canceled the dialog |
| 136 | + } |
| 137 | + return [path]; |
| 138 | + } |
| 139 | +} |
0 commit comments