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;
6
7internal static class Interop
8{
9 internal static class Kernel32
10 {
11 internal struct SECURITY_ATTRIBUTES
12 {
13 internal uint nLength;
14
16
17 internal BOOL bInheritHandle;
18 }
19
20 internal struct SYSTEM_INFO
21 {
22 internal ushort wProcessorArchitecture;
23
24 internal ushort wReserved;
25
26 internal int dwPageSize;
27
29
31
33
35
36 internal int dwProcessorType;
37
39
40 internal short wProcessorLevel;
41
42 internal short wProcessorRevision;
43 }
44
46 {
47 internal unsafe void* BaseAddress;
48
49 internal unsafe void* AllocationBase;
50
51 internal uint AllocationProtect;
52
54
55 internal uint State;
56
57 internal uint Protect;
58
59 internal uint Type;
60 }
61
62 internal struct MEMORYSTATUSEX
63 {
64 internal uint dwLength;
65
66 internal uint dwMemoryLoad;
67
68 internal ulong ullTotalPhys;
69
70 internal ulong ullAvailPhys;
71
72 internal ulong ullTotalPageFile;
73
74 internal ulong ullAvailPageFile;
75
76 internal ulong ullTotalVirtual;
77
78 internal ulong ullAvailVirtual;
79
81 }
82
83 [DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "CreateFileMappingW", SetLastError = true)]
84 internal static extern SafeMemoryMappedFileHandle CreateFileMapping(SafeFileHandle hFile, ref SECURITY_ATTRIBUTES lpFileMappingAttributes, int flProtect, int dwMaximumSizeHigh, int dwMaximumSizeLow, string lpName);
85
86 [DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "CreateFileMappingW", SetLastError = true)]
87 internal static extern SafeMemoryMappedFileHandle CreateFileMapping(IntPtr hFile, ref SECURITY_ATTRIBUTES lpFileMappingAttributes, int flProtect, int dwMaximumSizeHigh, int dwMaximumSizeLow, string lpName);
88
89 [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
90 internal static extern SafeMemoryMappedViewHandle MapViewOfFile(SafeMemoryMappedFileHandle hFileMappingObject, int dwDesiredAccess, int dwFileOffsetHigh, int dwFileOffsetLow, UIntPtr dwNumberOfBytesToMap);
91
92 [DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "OpenFileMappingW", SetLastError = true)]
93 internal static extern SafeMemoryMappedFileHandle OpenFileMapping(int dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, string lpName);
94
95 [DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true)]
96 internal static extern IntPtr VirtualAlloc(SafeHandle lpAddress, UIntPtr dwSize, int flAllocationType, int flProtect);
97
98 [DllImport("kernel32.dll")]
99 internal unsafe static extern BOOL GlobalMemoryStatusEx(MEMORYSTATUSEX* lpBuffer);
100
101 [DllImport("kernel32.dll", SetLastError = true)]
102 internal static extern bool CloseHandle(IntPtr handle);
103
104 [DllImport("kernel32.dll", BestFitMapping = true, CharSet = CharSet.Unicode, EntryPoint = "FormatMessageW", ExactSpelling = true, SetLastError = true)]
105 private unsafe static extern int FormatMessage(int dwFlags, IntPtr lpSource, uint dwMessageId, int dwLanguageId, void* lpBuffer, int nSize, IntPtr arguments);
106
107 internal static string GetMessage(int errorCode)
108 {
109 return GetMessage(errorCode, IntPtr.Zero);
110 }
111
112 internal unsafe static string GetMessage(int errorCode, IntPtr moduleHandle)
113 {
114 int num = 12800;
115 if (moduleHandle != IntPtr.Zero)
116 {
117 num |= 0x800;
118 }
119 Span<char> span = stackalloc char[256];
120 fixed (char* lpBuffer = span)
121 {
122 int num2 = FormatMessage(num, moduleHandle, (uint)errorCode, 0, lpBuffer, span.Length, IntPtr.Zero);
123 if (num2 > 0)
124 {
125 return GetAndTrimString(span.Slice(0, num2));
126 }
127 }
128 if (Marshal.GetLastWin32Error() == 122)
129 {
130 IntPtr intPtr = default(IntPtr);
131 try
132 {
133 int num3 = FormatMessage(num | 0x100, moduleHandle, (uint)errorCode, 0, &intPtr, 0, IntPtr.Zero);
134 if (num3 > 0)
135 {
136 return GetAndTrimString(new Span<char>((void*)intPtr, num3));
137 }
138 }
139 finally
140 {
141 Marshal.FreeHGlobal(intPtr);
142 }
143 }
144 return $"Unknown error (0x{errorCode:x})";
145 }
146
147 private static string GetAndTrimString(Span<char> buffer)
148 {
149 int num = buffer.Length;
150 while (num > 0 && buffer[num - 1] <= ' ')
151 {
152 num--;
153 }
154 return buffer.Slice(0, num).ToString();
155 }
156
157 [DllImport("kernel32.dll", SetLastError = true)]
158 internal static extern bool FlushViewOfFile(IntPtr lpBaseAddress, UIntPtr dwNumberOfBytesToFlush);
159
160 [DllImport("kernel32.dll")]
161 internal static extern void GetSystemInfo(out SYSTEM_INFO lpSystemInfo);
162
163 [DllImport("kernel32.dll", SetLastError = true)]
164 internal static extern bool UnmapViewOfFile(IntPtr lpBaseAddress);
165
166 [DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true)]
167 internal static extern UIntPtr VirtualQuery(SafeHandle lpAddress, ref MEMORY_BASIC_INFORMATION lpBuffer, UIntPtr dwLength);
168 }
169
170 internal enum BOOL
171 {
172 FALSE,
173 TRUE
174 }
175
176 public unsafe static void CheckForAvailableVirtualMemory(ulong nativeSize)
177 {
178 Kernel32.MEMORYSTATUSEX mEMORYSTATUSEX = default(Kernel32.MEMORYSTATUSEX);
179 mEMORYSTATUSEX.dwLength = (uint)sizeof(Kernel32.MEMORYSTATUSEX);
180 if (Kernel32.GlobalMemoryStatusEx(&mEMORYSTATUSEX) != 0)
181 {
182 ulong ullTotalVirtual = mEMORYSTATUSEX.ullTotalVirtual;
183 if (nativeSize >= ullTotalVirtual)
184 {
186 }
187 }
188 }
189
190 public static SafeMemoryMappedFileHandle CreateFileMapping(SafeFileHandle hFile, ref Kernel32.SECURITY_ATTRIBUTES securityAttributes, int pageProtection, long maximumSize, string name)
191 {
192 SplitLong(maximumSize, out var high, out var low);
193 return Kernel32.CreateFileMapping(hFile, ref securityAttributes, pageProtection, high, low, name);
194 }
195
196 public static SafeMemoryMappedFileHandle CreateFileMapping(IntPtr hFile, ref Kernel32.SECURITY_ATTRIBUTES securityAttributes, int pageProtection, long maximumSize, string name)
197 {
198 SplitLong(maximumSize, out var high, out var low);
199 return Kernel32.CreateFileMapping(hFile, ref securityAttributes, pageProtection, high, low, name);
200 }
201
202 public static SafeMemoryMappedViewHandle MapViewOfFile(SafeMemoryMappedFileHandle hFileMappingObject, int desiredAccess, long fileOffset, UIntPtr numberOfBytesToMap)
203 {
204 SplitLong(fileOffset, out var high, out var low);
205 return Kernel32.MapViewOfFile(hFileMappingObject, desiredAccess, high, low, numberOfBytesToMap);
206 }
207
208 public static SafeMemoryMappedFileHandle OpenFileMapping(int desiredAccess, bool inheritHandle, string name)
209 {
210 return Kernel32.OpenFileMapping(desiredAccess, inheritHandle, name);
211 }
212
213 public static IntPtr VirtualAlloc(SafeHandle baseAddress, UIntPtr size, int allocationType, int protection)
214 {
215 return Kernel32.VirtualAlloc(baseAddress, size, allocationType, protection);
216 }
217
218 [MethodImpl(MethodImplOptions.AggressiveInlining)]
219 private static void SplitLong(long number, out int high, out int low)
220 {
221 high = (int)(number >> 32);
222 low = (int)(number & 0xFFFFFFFFu);
223 }
224}
static UIntPtr VirtualQuery(SafeHandle lpAddress, ref MEMORY_BASIC_INFORMATION lpBuffer, UIntPtr dwLength)
static SafeMemoryMappedFileHandle CreateFileMapping(SafeFileHandle hFile, ref SECURITY_ATTRIBUTES lpFileMappingAttributes, int flProtect, int dwMaximumSizeHigh, int dwMaximumSizeLow, string lpName)
static IntPtr VirtualAlloc(SafeHandle lpAddress, UIntPtr dwSize, int flAllocationType, int flProtect)
static unsafe string GetMessage(int errorCode, IntPtr moduleHandle)
Definition Interop.cs:112
static void SetLastError(int errorCode)
static bool UnmapViewOfFile(IntPtr lpBaseAddress)
static bool FlushViewOfFile(IntPtr lpBaseAddress, UIntPtr dwNumberOfBytesToFlush)
static void GetSystemInfo(out SYSTEM_INFO lpSystemInfo)
static unsafe BOOL GlobalMemoryStatusEx(MEMORYSTATUSEX *lpBuffer)
static bool CloseHandle(IntPtr handle)
static SafeMemoryMappedViewHandle MapViewOfFile(SafeMemoryMappedFileHandle hFileMappingObject, int dwDesiredAccess, int dwFileOffsetHigh, int dwFileOffsetLow, UIntPtr dwNumberOfBytesToMap)
static string GetMessage(int errorCode)
Definition Interop.cs:107
static SafeMemoryMappedFileHandle OpenFileMapping(int dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, string lpName)
static unsafe int FormatMessage(int dwFlags, IntPtr lpSource, uint dwMessageId, int dwLanguageId, void *lpBuffer, int nSize, IntPtr arguments)
static SafeMemoryMappedFileHandle CreateFileMapping(IntPtr hFile, ref SECURITY_ATTRIBUTES lpFileMappingAttributes, int flProtect, int dwMaximumSizeHigh, int dwMaximumSizeLow, string lpName)
static string GetAndTrimString(Span< char > buffer)
Definition Interop.cs:153
static SafeMemoryMappedFileHandle CreateFileMapping(IntPtr hFile, ref Kernel32.SECURITY_ATTRIBUTES securityAttributes, int pageProtection, long maximumSize, string name)
Definition Interop.cs:196
static SafeMemoryMappedViewHandle MapViewOfFile(SafeMemoryMappedFileHandle hFileMappingObject, int desiredAccess, long fileOffset, UIntPtr numberOfBytesToMap)
Definition Interop.cs:202
static void SplitLong(long number, out int high, out int low)
Definition Interop.cs:219
static SafeMemoryMappedFileHandle CreateFileMapping(SafeFileHandle hFile, ref Kernel32.SECURITY_ATTRIBUTES securityAttributes, int pageProtection, long maximumSize, string name)
Definition Interop.cs:190
static SafeMemoryMappedFileHandle OpenFileMapping(int desiredAccess, bool inheritHandle, string name)
Definition Interop.cs:208
static IntPtr VirtualAlloc(SafeHandle baseAddress, UIntPtr size, int allocationType, int protection)
Definition Interop.cs:213
static unsafe void CheckForAvailableVirtualMemory(ulong nativeSize)
Definition Interop.cs:176
static void FreeHGlobal(IntPtr hglobal)
Definition Marshal.cs:1680
static string IO_NotEnoughMemory
Definition SR.cs:64
Definition SR.cs:7
static readonly IntPtr Zero
Definition IntPtr.cs:18
Span< T > Slice(int start)
Definition Span.cs:271
int Length
Definition Span.cs:70