Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
nativefiledialog.cs
Go to the documentation of this file.
1using System;
3using System.Text;
4
5public static class nativefiledialog
6{
7 public enum nfdresult_t
8 {
12 }
13
14 public struct nfdpathset_t
15 {
16 private IntPtr buf;
17
18 private IntPtr indices;
19
20 private IntPtr count;
21 }
22
23 private const string nativeLibName = "nfd";
24
25 private static int Utf8Size(string str)
26 {
27 return str.Length * 4 + 1;
28 }
29
30 private unsafe static byte* Utf8EncodeNullable(string str)
31 {
32 if (str == null)
33 {
34 return null;
35 }
36 int num = Utf8Size(str);
37 byte* ptr = (byte*)(void*)Marshal.AllocHGlobal(num);
38 fixed (char* chars = str)
39 {
40 Encoding.UTF8.GetBytes(chars, (str != null) ? (str.Length + 1) : 0, ptr, num);
41 }
42 return ptr;
43 }
44
45 private unsafe static string UTF8_ToManaged(IntPtr s, bool freePtr = false)
46 {
47 if (s == IntPtr.Zero)
48 {
49 return null;
50 }
51 byte* ptr;
52 for (ptr = (byte*)(void*)s; *ptr != 0; ptr++)
53 {
54 }
55 int num = (int)(ptr - (byte*)(void*)s);
56 if (num == 0)
57 {
58 return string.Empty;
59 }
60 char* ptr2 = stackalloc char[num];
61 int chars = Encoding.UTF8.GetChars((byte*)(void*)s, num, ptr2, num);
62 string result = new string(ptr2, 0, chars);
63 if (freePtr)
64 {
65 free(s);
66 }
67 return result;
68 }
69
70 [DllImport("msvcrt", CallingConvention = CallingConvention.Cdecl)]
71 private static extern void free(IntPtr ptr);
72
73 [DllImport("nfd", CallingConvention = CallingConvention.Cdecl, EntryPoint = "NFD_OpenDialog")]
74 private unsafe static extern nfdresult_t INTERNAL_NFD_OpenDialog(byte* filterList, byte* defaultPath, out IntPtr outPath);
75
76 public unsafe static nfdresult_t NFD_OpenDialog(string filterList, string defaultPath, out string outPath)
77 {
78 byte* intPtr = Utf8EncodeNullable(filterList);
79 byte* ptr = Utf8EncodeNullable(defaultPath);
80 IntPtr outPath2;
81 nfdresult_t result = INTERNAL_NFD_OpenDialog(intPtr, ptr, out outPath2);
82 Marshal.FreeHGlobal((IntPtr)intPtr);
84 outPath = UTF8_ToManaged(outPath2, freePtr: true);
85 return result;
86 }
87
88 [DllImport("nfd", CallingConvention = CallingConvention.Cdecl, EntryPoint = "NFD_OpenDialogMultiple")]
89 private unsafe static extern nfdresult_t INTERNAL_NFD_OpenDialogMultiple(byte* filterList, byte* defaultPath, out nfdpathset_t outPaths);
90
91 public unsafe static nfdresult_t NFD_OpenDialogMultiple(string filterList, string defaultPath, out nfdpathset_t outPaths)
92 {
93 byte* intPtr = Utf8EncodeNullable(filterList);
94 byte* ptr = Utf8EncodeNullable(defaultPath);
95 nfdresult_t result = INTERNAL_NFD_OpenDialogMultiple(intPtr, ptr, out outPaths);
96 Marshal.FreeHGlobal((IntPtr)intPtr);
98 return result;
99 }
100
101 [DllImport("nfd", CallingConvention = CallingConvention.Cdecl, EntryPoint = "NFD_SaveDialog")]
102 private unsafe static extern nfdresult_t INTERNAL_NFD_SaveDialog(byte* filterList, byte* defaultPath, out IntPtr outPath);
103
104 public unsafe static nfdresult_t NFD_SaveDialog(string filterList, string defaultPath, out string outPath)
105 {
106 byte* intPtr = Utf8EncodeNullable(filterList);
107 byte* ptr = Utf8EncodeNullable(defaultPath);
108 IntPtr outPath2;
109 nfdresult_t result = INTERNAL_NFD_SaveDialog(intPtr, ptr, out outPath2);
110 Marshal.FreeHGlobal((IntPtr)intPtr);
112 outPath = UTF8_ToManaged(outPath2, freePtr: true);
113 return result;
114 }
115
116 [DllImport("nfd", CallingConvention = CallingConvention.Cdecl, EntryPoint = "NFD_PickFolder")]
117 private unsafe static extern nfdresult_t INTERNAL_NFD_PickFolder(byte* defaultPath, out IntPtr outPath);
118
119 public unsafe static nfdresult_t NFD_PickFolder(string defaultPath, out string outPath)
120 {
121 byte* intPtr = Utf8EncodeNullable(defaultPath);
122 IntPtr outPath2;
123 nfdresult_t result = INTERNAL_NFD_PickFolder(intPtr, out outPath2);
124 Marshal.FreeHGlobal((IntPtr)intPtr);
125 outPath = UTF8_ToManaged(outPath2, freePtr: true);
126 return result;
127 }
128
129 [DllImport("nfd", CallingConvention = CallingConvention.Cdecl, EntryPoint = "NFD_GetError")]
130 private static extern IntPtr INTERNAL_NFD_GetError();
131
132 public static string NFD_GetError()
133 {
135 }
136
137 [DllImport("nfd", CallingConvention = CallingConvention.Cdecl)]
138 public static extern IntPtr NFD_PathSet_GetCount(ref nfdpathset_t pathset);
139
140 [DllImport("nfd", CallingConvention = CallingConvention.Cdecl, EntryPoint = "NFD_PathSet_GetPath")]
141 private static extern IntPtr INTERNAL_NFD_PathSet_GetPath(ref nfdpathset_t pathset, IntPtr index);
142
143 public static string NFD_PathSet_GetPath(ref nfdpathset_t pathset, IntPtr index)
144 {
146 }
147
148 [DllImport("nfd", CallingConvention = CallingConvention.Cdecl)]
149 public static extern void NFD_PathSet_Free(ref nfdpathset_t pathset);
150}
static void FreeHGlobal(IntPtr hglobal)
Definition Marshal.cs:1680
static IntPtr AllocHGlobal(int cb)
Definition Marshal.cs:625
static Encoding UTF8
Definition Encoding.cs:526
static unsafe nfdresult_t INTERNAL_NFD_OpenDialogMultiple(byte *filterList, byte *defaultPath, out nfdpathset_t outPaths)
static unsafe nfdresult_t INTERNAL_NFD_PickFolder(byte *defaultPath, out IntPtr outPath)
static unsafe nfdresult_t NFD_OpenDialogMultiple(string filterList, string defaultPath, out nfdpathset_t outPaths)
static unsafe nfdresult_t INTERNAL_NFD_OpenDialog(byte *filterList, byte *defaultPath, out IntPtr outPath)
static IntPtr NFD_PathSet_GetCount(ref nfdpathset_t pathset)
const string nativeLibName
static unsafe nfdresult_t INTERNAL_NFD_SaveDialog(byte *filterList, byte *defaultPath, out IntPtr outPath)
static void free(IntPtr ptr)
static IntPtr INTERNAL_NFD_PathSet_GetPath(ref nfdpathset_t pathset, IntPtr index)
static void NFD_PathSet_Free(ref nfdpathset_t pathset)
static unsafe nfdresult_t NFD_PickFolder(string defaultPath, out string outPath)
static string NFD_PathSet_GetPath(ref nfdpathset_t pathset, IntPtr index)
static unsafe nfdresult_t NFD_SaveDialog(string filterList, string defaultPath, out string outPath)
static unsafe string UTF8_ToManaged(IntPtr s, bool freePtr=false)
static unsafe nfdresult_t NFD_OpenDialog(string filterList, string defaultPath, out string outPath)
static IntPtr INTERNAL_NFD_GetError()
static string NFD_GetError()
static unsafe byte * Utf8EncodeNullable(string str)
static int Utf8Size(string str)
static readonly IntPtr Zero
Definition IntPtr.cs:18