Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Interop.cs
Go to the documentation of this file.
1using System;
4
5internal static class Interop
6{
7 internal static class Kernel32
8 {
9 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
10 private struct CPINFOEXW
11 {
12 internal uint MaxCharSize;
13
14 internal unsafe fixed byte DefaultChar[2];
15
16 internal unsafe fixed byte LeadByte[12];
17
18 internal char UnicodeDefaultChar;
19
20 internal uint CodePage;
21
22 internal unsafe fixed char CodePageName[260];
23 }
24
25 internal struct CONSOLE_CURSOR_INFO
26 {
27 internal int dwSize;
28
29 internal bool bVisible;
30 }
31
33 {
34 internal COORD dwSize;
35
37
38 internal short wAttributes;
39
41
43 }
44
45 internal struct COORD
46 {
47 internal short X;
48
49 internal short Y;
50 }
51
52 internal struct SMALL_RECT
53 {
54 internal short Left;
55
56 internal short Top;
57
58 internal short Right;
59
60 internal short Bottom;
61 }
62
63 internal enum Color : short
64 {
65 Black = 0,
68 ForegroundRed = 4,
71 BackgroundBlue = 16,
72 BackgroundGreen = 32,
73 BackgroundRed = 64,
76 ForegroundMask = 15,
77 BackgroundMask = 240,
78 ColorMask = 255
79 }
80
81 internal struct CHAR_INFO
82 {
83 private ushort charData;
84
85 private short attributes;
86 }
87
88 [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
89 private unsafe static extern BOOL GetCPInfoExW(uint CodePage, uint dwFlags, CPINFOEXW* lpCPInfoEx);
90
91 internal unsafe static int GetLeadByteRanges(int codePage, byte[] leadByteRanges)
92 {
93 int num = 0;
94 Unsafe.SkipInit(out CPINFOEXW cPINFOEXW);
95 if (GetCPInfoExW((uint)codePage, 0u, &cPINFOEXW) != 0)
96 {
97 for (int i = 0; i < 10 && leadByteRanges[i] != 0; i += 2)
98 {
99 leadByteRanges[i] = cPINFOEXW.LeadByte[i];
100 leadByteRanges[i + 1] = cPINFOEXW.LeadByte[i + 1];
101 num++;
102 }
103 }
104 return num;
105 }
106
107 [DllImport("kernel32.dll", SetLastError = true)]
108 internal static extern bool Beep(int frequency, int duration);
109
110 [DllImport("kernel32.dll", BestFitMapping = true, CharSet = CharSet.Unicode, EntryPoint = "FormatMessageW", ExactSpelling = true, SetLastError = true)]
111 private unsafe static extern int FormatMessage(int dwFlags, IntPtr lpSource, uint dwMessageId, int dwLanguageId, void* lpBuffer, int nSize, IntPtr arguments);
112
113 internal static string GetMessage(int errorCode)
114 {
115 return GetMessage(errorCode, IntPtr.Zero);
116 }
117
118 internal unsafe static string GetMessage(int errorCode, IntPtr moduleHandle)
119 {
120 int num = 12800;
121 if (moduleHandle != IntPtr.Zero)
122 {
123 num |= 0x800;
124 }
125 Span<char> span = stackalloc char[256];
126 fixed (char* lpBuffer = span)
127 {
128 int num2 = FormatMessage(num, moduleHandle, (uint)errorCode, 0, lpBuffer, span.Length, IntPtr.Zero);
129 if (num2 > 0)
130 {
131 return GetAndTrimString(span.Slice(0, num2));
132 }
133 }
134 if (Marshal.GetLastWin32Error() == 122)
135 {
136 IntPtr intPtr = default(IntPtr);
137 try
138 {
139 int num3 = FormatMessage(num | 0x100, moduleHandle, (uint)errorCode, 0, &intPtr, 0, IntPtr.Zero);
140 if (num3 > 0)
141 {
142 return GetAndTrimString(new Span<char>((void*)intPtr, num3));
143 }
144 }
145 finally
146 {
147 Marshal.FreeHGlobal(intPtr);
148 }
149 }
150 return $"Unknown error (0x{errorCode:x})";
151 }
152
153 private static string GetAndTrimString(Span<char> buffer)
154 {
155 int num = buffer.Length;
156 while (num > 0 && buffer[num - 1] <= ' ')
157 {
158 num--;
159 }
160 return buffer.Slice(0, num).ToString();
161 }
162
163 [DllImport("kernel32.dll", SetLastError = true)]
164 internal static extern bool GetConsoleCursorInfo(IntPtr hConsoleOutput, out CONSOLE_CURSOR_INFO cci);
165
166 [DllImport("kernel32.dll", SetLastError = true)]
167 internal static extern bool SetConsoleCursorInfo(IntPtr hConsoleOutput, ref CONSOLE_CURSOR_INFO cci);
168
169 [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
170 internal static extern bool FillConsoleOutputAttribute(IntPtr hConsoleOutput, short wColorAttribute, int numCells, COORD startCoord, out int pNumBytesWritten);
171
172 [DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "FillConsoleOutputCharacterW", SetLastError = true)]
173 internal static extern bool FillConsoleOutputCharacter(IntPtr hConsoleOutput, char character, int nLength, COORD dwWriteCoord, out int pNumCharsWritten);
174
175 [DllImport("kernel32.dll", SetLastError = true)]
176 internal static extern bool GetConsoleScreenBufferInfo(IntPtr hConsoleOutput, out CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);
177
178 [DllImport("kernel32.dll")]
179 internal static extern uint GetConsoleCP();
180
181 [DllImport("kernel32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
182 internal unsafe static extern uint GetConsoleTitleW(char* title, uint nSize);
183
184 [DllImport("kernel32.dll", SetLastError = true)]
185 internal static extern bool GetConsoleMode(IntPtr handle, out int mode);
186
188 {
189 int mode;
190 return GetConsoleMode(handle, out mode);
191 }
192
193 [DllImport("kernel32.dll", SetLastError = true)]
194 internal static extern bool SetConsoleMode(IntPtr handle, int mode);
195
196 [DllImport("kernel32.dll")]
197 internal static extern uint GetConsoleOutputCP();
198
199 [DllImport("kernel32.dll", SetLastError = true)]
200 internal static extern COORD GetLargestConsoleWindowSize(IntPtr hConsoleOutput);
201
202 [DllImport("kernel32.dll", SetLastError = true)]
203 internal static extern uint GetFileType(IntPtr hFile);
204
205 [DllImport("kernel32.dll")]
206 [SuppressGCTransition]
207 internal static extern IntPtr GetStdHandle(int nStdHandle);
208
209 [DllImport("kernel32.dll")]
210 internal unsafe static extern int MultiByteToWideChar(uint CodePage, uint dwFlags, byte* lpMultiByteStr, int cbMultiByte, char* lpWideCharStr, int cchWideChar);
211
212 [DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "PeekConsoleInputW", SetLastError = true)]
213 internal static extern bool PeekConsoleInput(IntPtr hConsoleInput, out InputRecord buffer, int numInputRecords_UseOne, out int numEventsRead);
214
215 [DllImport("kernel32.dll", SetLastError = true)]
216 internal unsafe static extern int ReadFile(IntPtr handle, byte* bytes, int numBytesToRead, out int numBytesRead, IntPtr mustBeZero);
217
218 [DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "ReadConsoleW", SetLastError = true)]
219 internal unsafe static extern bool ReadConsole(IntPtr hConsoleInput, byte* lpBuffer, int nNumberOfCharsToRead, out int lpNumberOfCharsRead, IntPtr pInputControl);
220
221 [DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "ReadConsoleInputW", SetLastError = true)]
222 internal static extern bool ReadConsoleInput(IntPtr hConsoleInput, out InputRecord buffer, int numInputRecords_UseOne, out int numEventsRead);
223
224 [DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "ReadConsoleOutputW", SetLastError = true)]
225 internal unsafe static extern bool ReadConsoleOutput(IntPtr hConsoleOutput, CHAR_INFO* pBuffer, COORD bufferSize, COORD bufferCoord, ref SMALL_RECT readRegion);
226
227 [DllImport("kernel32.dll", SetLastError = true)]
228 internal static extern bool SetConsoleCP(int codePage);
229
230 [DllImport("kernel32.dll", SetLastError = true)]
231 internal static extern bool SetConsoleCursorPosition(IntPtr hConsoleOutput, COORD cursorPosition);
232
233 [DllImport("kernel32.dll", SetLastError = true)]
234 internal static extern bool SetConsoleOutputCP(int codePage);
235
236 [DllImport("kernel32.dll", SetLastError = true)]
237 internal static extern bool SetConsoleScreenBufferSize(IntPtr hConsoleOutput, COORD size);
238
239 [DllImport("kernel32.dll", SetLastError = true)]
240 internal static extern int SetConsoleTextAttribute(IntPtr hConsoleOutput, short wAttributes);
241
242 [DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "SetConsoleTitleW", SetLastError = true)]
243 internal static extern bool SetConsoleTitle(string title);
244
245 [DllImport("kernel32.dll", SetLastError = true)]
246 internal unsafe static extern bool SetConsoleWindowInfo(IntPtr hConsoleOutput, bool absolute, SMALL_RECT* consoleWindow);
247
248 [DllImport("kernel32.dll")]
249 internal unsafe static extern int WideCharToMultiByte(uint CodePage, uint dwFlags, char* lpWideCharStr, int cchWideChar, byte* lpMultiByteStr, int cbMultiByte, IntPtr lpDefaultChar, IntPtr lpUsedDefaultChar);
250
251 [DllImport("kernel32.dll", SetLastError = true)]
252 internal unsafe static extern int WriteFile(IntPtr handle, byte* bytes, int numBytesToWrite, out int numBytesWritten, IntPtr mustBeZero);
253
254 [DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "WriteConsoleW", SetLastError = true)]
255 internal unsafe static extern bool WriteConsole(IntPtr hConsoleOutput, byte* lpBuffer, int nNumberOfCharsToWrite, out int lpNumberOfCharsWritten, IntPtr lpReservedMustBeNull);
256
257 [DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "WriteConsoleOutputW", SetLastError = true)]
258 internal unsafe static extern bool WriteConsoleOutput(IntPtr hConsoleOutput, CHAR_INFO* buffer, COORD bufferSize, COORD bufferCoord, ref SMALL_RECT writeRegion);
259 }
260
261 internal enum BOOL
262 {
263 FALSE,
264 TRUE
265 }
266
267 internal static class User32
268 {
269 [DllImport("user32.dll")]
270 internal static extern short GetKeyState(int virtualKeyCode);
271 }
272
273 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
274 internal struct KeyEventRecord
275 {
276 internal BOOL keyDown;
277
278 internal short repeatCount;
279
280 internal short virtualKeyCode;
281
282 internal short virtualScanCode;
283
284 internal char uChar;
285
286 internal int controlKeyState;
287 }
288
289 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
290 internal struct InputRecord
291 {
292 internal short eventType;
293
295 }
296}
static uint GetConsoleCP()
static bool SetConsoleOutputCP(int codePage)
static uint GetConsoleOutputCP()
static unsafe int ReadFile(IntPtr handle, byte *bytes, int numBytesToRead, out int numBytesRead, IntPtr mustBeZero)
static bool SetConsoleMode(IntPtr handle, int mode)
static unsafe bool ReadConsole(IntPtr hConsoleInput, byte *lpBuffer, int nNumberOfCharsToRead, out int lpNumberOfCharsRead, IntPtr pInputControl)
static IntPtr GetStdHandle(int nStdHandle)
static bool FillConsoleOutputAttribute(IntPtr hConsoleOutput, short wColorAttribute, int numCells, COORD startCoord, out int pNumBytesWritten)
static unsafe bool ReadConsoleOutput(IntPtr hConsoleOutput, CHAR_INFO *pBuffer, COORD bufferSize, COORD bufferCoord, ref SMALL_RECT readRegion)
static unsafe int WriteFile(IntPtr handle, byte *bytes, int numBytesToWrite, out int numBytesWritten, IntPtr mustBeZero)
static unsafe string GetMessage(int errorCode, IntPtr moduleHandle)
Definition Interop.cs:118
static bool SetConsoleTitle(string title)
static bool PeekConsoleInput(IntPtr hConsoleInput, out InputRecord buffer, int numInputRecords_UseOne, out int numEventsRead)
static void SetLastError(int errorCode)
static bool SetConsoleCP(int codePage)
static bool SetConsoleScreenBufferSize(IntPtr hConsoleOutput, COORD size)
static bool GetConsoleCursorInfo(IntPtr hConsoleOutput, out CONSOLE_CURSOR_INFO cci)
static unsafe bool WriteConsole(IntPtr hConsoleOutput, byte *lpBuffer, int nNumberOfCharsToWrite, out int lpNumberOfCharsWritten, IntPtr lpReservedMustBeNull)
static unsafe int WideCharToMultiByte(uint CodePage, uint dwFlags, char *lpWideCharStr, int cchWideChar, byte *lpMultiByteStr, int cbMultiByte, IntPtr lpDefaultChar, IntPtr lpUsedDefaultChar)
static unsafe int MultiByteToWideChar(uint CodePage, uint dwFlags, byte *lpMultiByteStr, int cbMultiByte, char *lpWideCharStr, int cchWideChar)
static bool IsGetConsoleModeCallSuccessful(IntPtr handle)
Definition Interop.cs:187
static bool SetConsoleCursorInfo(IntPtr hConsoleOutput, ref CONSOLE_CURSOR_INFO cci)
static bool GetConsoleMode(IntPtr handle, out int mode)
static string GetMessage(int errorCode)
Definition Interop.cs:113
static bool FillConsoleOutputCharacter(IntPtr hConsoleOutput, char character, int nLength, COORD dwWriteCoord, out int pNumCharsWritten)
static int SetConsoleTextAttribute(IntPtr hConsoleOutput, short wAttributes)
static bool Beep(int frequency, int duration)
static unsafe int GetLeadByteRanges(int codePage, byte[] leadByteRanges)
Definition Interop.cs:91
static COORD GetLargestConsoleWindowSize(IntPtr hConsoleOutput)
static unsafe int FormatMessage(int dwFlags, IntPtr lpSource, uint dwMessageId, int dwLanguageId, void *lpBuffer, int nSize, IntPtr arguments)
static unsafe BOOL GetCPInfoExW(uint CodePage, uint dwFlags, CPINFOEXW *lpCPInfoEx)
static bool SetConsoleCursorPosition(IntPtr hConsoleOutput, COORD cursorPosition)
static bool GetConsoleScreenBufferInfo(IntPtr hConsoleOutput, out CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo)
static unsafe bool SetConsoleWindowInfo(IntPtr hConsoleOutput, bool absolute, SMALL_RECT *consoleWindow)
static unsafe bool WriteConsoleOutput(IntPtr hConsoleOutput, CHAR_INFO *buffer, COORD bufferSize, COORD bufferCoord, ref SMALL_RECT writeRegion)
static bool ReadConsoleInput(IntPtr hConsoleInput, out InputRecord buffer, int numInputRecords_UseOne, out int numEventsRead)
static uint GetFileType(IntPtr hFile)
static unsafe uint GetConsoleTitleW(char *title, uint nSize)
static string GetAndTrimString(Span< char > buffer)
Definition Interop.cs:153
static short GetKeyState(int virtualKeyCode)
static void FreeHGlobal(IntPtr hglobal)
Definition Marshal.cs:1680
KeyEventRecord keyEvent
Definition Interop.cs:294
unsafe fixed char CodePageName[260]
Definition Interop.cs:22
unsafe fixed byte LeadByte[12]
Definition Interop.cs:16
unsafe fixed byte DefaultChar[2]
Definition Interop.cs:14
static readonly IntPtr Zero
Definition IntPtr.cs:18
Span< T > Slice(int start)
Definition Span.cs:271
int Length
Definition Span.cs:70