diff --git a/Robust.Server/Utility/WindowsTickPeriod.cs b/Robust.Server/Utility/WindowsTickPeriod.cs index 2b94fad7d04..87298c379ee 100644 --- a/Robust.Server/Utility/WindowsTickPeriod.cs +++ b/Robust.Server/Utility/WindowsTickPeriod.cs @@ -1,21 +1,17 @@ using System; -using System.Runtime.InteropServices; +using TerraFX.Interop.Windows; namespace Robust.Server.Utility { internal static class WindowsTickPeriod { - private const uint TIMERR_NOERROR = 0; - // This is an actual error code my god. - private const uint TIMERR_NOCANDO = 97; - public static void TimeBeginPeriod(uint period) { if (!OperatingSystem.IsWindows()) throw new InvalidOperationException(); - var ret = timeBeginPeriod(period); - if (ret != TIMERR_NOERROR) + var ret = Windows.timeBeginPeriod(period); + if (ret != Windows.TIMERR_NOERROR) throw new InvalidOperationException($"timeBeginPeriod returned error: {ret}"); } @@ -24,15 +20,9 @@ public static void TimeEndPeriod(uint period) if (!OperatingSystem.IsWindows()) throw new InvalidOperationException(); - var ret = timeEndPeriod(period); - if (ret != TIMERR_NOERROR) + var ret = Windows.timeBeginPeriod(period); + if (ret != Windows.TIMERR_NOERROR) throw new InvalidOperationException($"timeEndPeriod returned error: {ret}"); } - - [DllImport("Winmm.dll")] - private static extern uint timeBeginPeriod(uint uPeriod); - - [DllImport("Winmm.dll")] - private static extern uint timeEndPeriod(uint uPeriod); } } diff --git a/Robust.Shared/Log/ConsoleLogHandler.cs b/Robust.Shared/Log/ConsoleLogHandler.cs index 4662b0f54c9..7b55455f477 100644 --- a/Robust.Shared/Log/ConsoleLogHandler.cs +++ b/Robust.Shared/Log/ConsoleLogHandler.cs @@ -1,12 +1,11 @@ using System; using System.Diagnostics; using System.IO; -using System.Runtime.InteropServices; using System.Text; using System.Text.Unicode; using System.Timers; -using JetBrains.Annotations; using Serilog.Events; +using TerraFX.Interop.Windows; namespace Robust.Shared.Log { @@ -200,14 +199,15 @@ public void Dispose() internal static class WindowsConsole { - public static bool TryEnableVirtualTerminalProcessing() + public static unsafe bool TryEnableVirtualTerminalProcessing() { try { - var stdHandle = NativeMethods.GetStdHandle(-11); - NativeMethods.GetConsoleMode(stdHandle, out var mode); - NativeMethods.SetConsoleMode(stdHandle, mode | 4); - NativeMethods.GetConsoleMode(stdHandle, out mode); + var stdHandle = Windows.GetStdHandle(unchecked((uint)-11)); + uint mode; + Windows.GetConsoleMode(stdHandle, &mode); + Windows.SetConsoleMode(stdHandle, mode | 4); + Windows.GetConsoleMode(stdHandle, &mode); return (mode & 4) == 4; } catch (DllNotFoundException) @@ -226,7 +226,7 @@ public static bool TryEnableVirtualTerminalProcessing() public static void TryDetachFromConsoleWindow() { - if (NativeMethods.GetConsoleWindow() == default + if (Windows.GetConsoleWindow() == default || Debugger.IsAttached || System.Console.IsOutputRedirected || System.Console.IsErrorRedirected @@ -235,27 +235,12 @@ public static void TryDetachFromConsoleWindow() return; } - _freedConsole = NativeMethods.FreeConsole(); + _freedConsole = Windows.FreeConsole(); } internal static class NativeMethods { public const int CodePageUtf8 = 65001; - - [DllImport("kernel32", SetLastError = true)] - internal static extern bool SetConsoleMode(IntPtr hConsoleHandle, int mode); - - [DllImport("kernel32", SetLastError = true)] - internal static extern bool GetConsoleMode(IntPtr handle, out int mode); - - [DllImport("kernel32", SetLastError = true)] - internal static extern IntPtr GetStdHandle(int handle); - - [DllImport("kernel32", SetLastError = true)] - internal static extern bool FreeConsole(); - - [DllImport("kernel32", SetLastError = true)] - internal static extern IntPtr GetConsoleWindow(); } } diff --git a/Robust.Shared/Utility/ProcessExt.cs b/Robust.Shared/Utility/ProcessExt.cs index 2db1b202fbd..c085510e0fd 100644 --- a/Robust.Shared/Utility/ProcessExt.cs +++ b/Robust.Shared/Utility/ProcessExt.cs @@ -1,7 +1,8 @@ using System; using System.Diagnostics; using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; +using TerraFX.Interop.Windows; + // ReSharper disable InconsistentNaming // ReSharper disable IdentifierTypo // ReSharper disable CommentTypo @@ -49,34 +50,15 @@ public static long GetPrivateMemorySize64NotSlowHolyFuckingShitMicrosoft(this Pr PROCESS_MEMORY_COUNTERS_EX counters; - if (GetProcessMemoryInfo(process.Handle, &counters, sizeof(PROCESS_MEMORY_COUNTERS_EX)) == 0) + if (Windows.GetProcessMemoryInfo( + (HANDLE)process.Handle, + (PROCESS_MEMORY_COUNTERS*)(&counters), + (uint)sizeof(PROCESS_MEMORY_COUNTERS_EX)) == 0) return 0; var count = counters.PrivateUsage; - return count; + return (long)count; } - - private struct PROCESS_MEMORY_COUNTERS_EX - { - public int cb; - public int PageFaultCount; - public nint PeakWorkingSetSize; - public nint WorkingSetSize; - public nint QuotaPeakPagedPoolUsage; - public nint QuotaPagedPoolUsage; - public nint QuotaPeakNonPagedPoolUsage; - public nint QuotaNonPagedPoolUsage; - public nint PagefileUsage; - public nint PeakPagefileUsage; - public nint PrivateUsage; - } - - // ReSharper disable once StringLiteralTypo - [DllImport("psapi.dll", SetLastError = true)] - private static extern int GetProcessMemoryInfo( - IntPtr Process, - PROCESS_MEMORY_COUNTERS_EX* ppsmemCounters, - int cb); } }