Skip to content

Commit

Permalink
Remove manual Windows P/Invokes in favor of TerraFX.
Browse files Browse the repository at this point in the history
  • Loading branch information
PJB3005 committed Jul 15, 2023
1 parent c392d4f commit bce2901
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 64 deletions.
20 changes: 5 additions & 15 deletions Robust.Server/Utility/WindowsTickPeriod.cs
Original file line number Diff line number Diff line change
@@ -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}");
}

Expand All @@ -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);
}
}
33 changes: 9 additions & 24 deletions Robust.Shared/Log/ConsoleLogHandler.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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();
}

}
Expand Down
32 changes: 7 additions & 25 deletions Robust.Shared/Utility/ProcessExt.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
}
}

0 comments on commit bce2901

Please sign in to comment.