Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Interop.cs
Go to the documentation of this file.
1using System;
2using System.IO;
5
6internal static class Interop
7{
8 internal enum BOOL
9 {
10 FALSE,
11 TRUE
12 }
13
14 internal static class Kernel32
15 {
16 internal struct FILE_TIME
17 {
18 internal uint dwLowDateTime;
19
20 internal uint dwHighDateTime;
21 }
22
29
37
43
44 internal struct SECURITY_ATTRIBUTES
45 {
46 internal uint nLength;
47
49
50 internal BOOL bInheritHandle;
51 }
52
54 {
55 internal int dwFileAttributes;
56
58
60
62
63 internal uint nFileSizeHigh;
64
65 internal uint nFileSizeLow;
66
67 internal void PopulateFrom(ref WIN32_FIND_DATA findData)
68 {
69 dwFileAttributes = (int)findData.dwFileAttributes;
70 ftCreationTime = findData.ftCreationTime;
71 ftLastAccessTime = findData.ftLastAccessTime;
72 ftLastWriteTime = findData.ftLastWriteTime;
73 nFileSizeHigh = findData.nFileSizeHigh;
74 nFileSizeLow = findData.nFileSizeLow;
75 }
76 }
77
78 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
79 internal struct WIN32_FIND_DATA
80 {
81 internal uint dwFileAttributes;
82
84
86
88
89 internal uint nFileSizeHigh;
90
91 internal uint nFileSizeLow;
92
93 internal uint dwReserved0;
94
95 internal uint dwReserved1;
96
97 private unsafe fixed char _cFileName[260];
98
99 private unsafe fixed char _cAlternateFileName[14];
100 }
101
102 [DllImport("kernel32.dll", BestFitMapping = false, CharSet = CharSet.Unicode, EntryPoint = "CreateFileW", ExactSpelling = true, SetLastError = true)]
103 private unsafe static extern SafeFileHandle CreateFilePrivate(string lpFileName, int dwDesiredAccess, FileShare dwShareMode, SECURITY_ATTRIBUTES* lpSecurityAttributes, FileMode dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile);
104
105 internal unsafe static SafeFileHandle CreateFile(string lpFileName, int dwDesiredAccess, FileShare dwShareMode, SECURITY_ATTRIBUTES* lpSecurityAttributes, FileMode dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile)
106 {
107 lpFileName = System.IO.PathInternal.EnsureExtendedPrefixIfNeeded(lpFileName);
108 return CreateFilePrivate(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
109 }
110
111 [DllImport("kernel32.dll", SetLastError = true)]
112 internal static extern bool FindClose(IntPtr hFindFile);
113
114 [DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "FindFirstFileExW", ExactSpelling = true, SetLastError = true)]
115 private static extern Microsoft.Win32.SafeHandles.SafeFindHandle FindFirstFileExPrivate(string lpFileName, FINDEX_INFO_LEVELS fInfoLevelId, ref WIN32_FIND_DATA lpFindFileData, FINDEX_SEARCH_OPS fSearchOp, IntPtr lpSearchFilter, int dwAdditionalFlags);
116
118 {
120 return FindFirstFileExPrivate(fileName, FINDEX_INFO_LEVELS.FindExInfoBasic, ref data, FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero, 0);
121 }
122
123 [DllImport("kernel32.dll", BestFitMapping = true, CharSet = CharSet.Unicode, EntryPoint = "FormatMessageW", ExactSpelling = true, SetLastError = true)]
124 private unsafe static extern int FormatMessage(int dwFlags, IntPtr lpSource, uint dwMessageId, int dwLanguageId, void* lpBuffer, int nSize, IntPtr arguments);
125
126 internal static string GetMessage(int errorCode)
127 {
128 return GetMessage(errorCode, IntPtr.Zero);
129 }
130
131 internal unsafe static string GetMessage(int errorCode, IntPtr moduleHandle)
132 {
133 int num = 12800;
134 if (moduleHandle != IntPtr.Zero)
135 {
136 num |= 0x800;
137 }
138 Span<char> span = stackalloc char[256];
139 fixed (char* lpBuffer = span)
140 {
141 int num2 = FormatMessage(num, moduleHandle, (uint)errorCode, 0, lpBuffer, span.Length, IntPtr.Zero);
142 if (num2 > 0)
143 {
144 return GetAndTrimString(span.Slice(0, num2));
145 }
146 }
147 if (Marshal.GetLastWin32Error() == 122)
148 {
149 IntPtr intPtr = default(IntPtr);
150 try
151 {
152 int num3 = FormatMessage(num | 0x100, moduleHandle, (uint)errorCode, 0, &intPtr, 0, IntPtr.Zero);
153 if (num3 > 0)
154 {
155 return GetAndTrimString(new Span<char>((void*)intPtr, num3));
156 }
157 }
158 finally
159 {
160 Marshal.FreeHGlobal(intPtr);
161 }
162 }
163 return $"Unknown error (0x{errorCode:x})";
164 }
165
166 private static string GetAndTrimString(Span<char> buffer)
167 {
168 int num = buffer.Length;
169 while (num > 0 && buffer[num - 1] <= ' ')
170 {
171 num--;
172 }
173 return buffer.Slice(0, num).ToString();
174 }
175
176 [DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "GetFileAttributesExW", ExactSpelling = true, SetLastError = true)]
177 private static extern bool GetFileAttributesExPrivate(string name, GET_FILEEX_INFO_LEVELS fileInfoLevel, ref WIN32_FILE_ATTRIBUTE_DATA lpFileInformation);
178
179 internal static bool GetFileAttributesEx(string name, GET_FILEEX_INFO_LEVELS fileInfoLevel, ref WIN32_FILE_ATTRIBUTE_DATA lpFileInformation)
180 {
182 return GetFileAttributesExPrivate(name, fileInfoLevel, ref lpFileInformation);
183 }
184
185 [DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true)]
186 [SuppressGCTransition]
187 internal static extern bool SetThreadErrorMode(uint dwNewMode, out uint lpOldMode);
188
189 [DllImport("kernel32.dll", BestFitMapping = false, CharSet = CharSet.Unicode, EntryPoint = "CreateDirectoryW", SetLastError = true)]
190 private static extern bool CreateDirectoryPrivate(string path, ref SECURITY_ATTRIBUTES lpSecurityAttributes);
191
192 internal static bool CreateDirectory(string path, ref SECURITY_ATTRIBUTES lpSecurityAttributes)
193 {
195 return CreateDirectoryPrivate(path, ref lpSecurityAttributes);
196 }
197 }
198}
static bool FindClose(IntPtr hFindFile)
static Microsoft.Win32.SafeHandles.SafeFindHandle FindFirstFileExPrivate(string lpFileName, FINDEX_INFO_LEVELS fInfoLevelId, ref WIN32_FIND_DATA lpFindFileData, FINDEX_SEARCH_OPS fSearchOp, IntPtr lpSearchFilter, int dwAdditionalFlags)
static unsafe string GetMessage(int errorCode, IntPtr moduleHandle)
Definition Interop.cs:131
static void SetLastError(int errorCode)
static unsafe SafeFileHandle CreateFile(string lpFileName, int dwDesiredAccess, FileShare dwShareMode, SECURITY_ATTRIBUTES *lpSecurityAttributes, FileMode dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile)
Definition Interop.cs:105
static bool SetThreadErrorMode(uint dwNewMode, out uint lpOldMode)
static string GetMessage(int errorCode)
Definition Interop.cs:126
static bool GetFileAttributesEx(string name, GET_FILEEX_INFO_LEVELS fileInfoLevel, ref WIN32_FILE_ATTRIBUTE_DATA lpFileInformation)
Definition Interop.cs:179
static bool CreateDirectory(string path, ref SECURITY_ATTRIBUTES lpSecurityAttributes)
Definition Interop.cs:192
static bool CreateDirectoryPrivate(string path, ref SECURITY_ATTRIBUTES lpSecurityAttributes)
static unsafe SafeFileHandle CreateFilePrivate(string lpFileName, int dwDesiredAccess, FileShare dwShareMode, SECURITY_ATTRIBUTES *lpSecurityAttributes, FileMode dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile)
static bool GetFileAttributesExPrivate(string name, GET_FILEEX_INFO_LEVELS fileInfoLevel, ref WIN32_FILE_ATTRIBUTE_DATA lpFileInformation)
static unsafe int FormatMessage(int dwFlags, IntPtr lpSource, uint dwMessageId, int dwLanguageId, void *lpBuffer, int nSize, IntPtr arguments)
static Microsoft.Win32.SafeHandles.SafeFindHandle FindFirstFile(string fileName, ref WIN32_FIND_DATA data)
Definition Interop.cs:117
static string GetAndTrimString(Span< char > buffer)
Definition Interop.cs:153
static string EnsureExtendedPrefix(string path)
static string EnsureExtendedPrefixIfNeeded(string path)
static void FreeHGlobal(IntPtr hglobal)
Definition Marshal.cs:1680
void PopulateFrom(ref WIN32_FIND_DATA findData)
Definition Interop.cs:67
unsafe fixed char _cAlternateFileName[14]
Definition Interop.cs:99
unsafe fixed char _cFileName[260]
Definition Interop.cs:97
static readonly IntPtr Zero
Definition IntPtr.cs:18
Span< T > Slice(int start)
Definition Span.cs:271
int Length
Definition Span.cs:70