From 90de1e0fab685b13647dcab1b1ae6fea30e8c83f Mon Sep 17 00:00:00 2001 From: Emma Alyx Wunder Date: Thu, 27 Jun 2024 23:46:27 +0200 Subject: [PATCH] Fixed WindowsNative.GetShortPathName to use LibraryImport Changed comparison method in FormatMessage to use string.IsNullOrEmpty --- src/Ryujinx.Common/Logging/Logger.cs | 2 +- src/Ryujinx.Common/Utilities/WindowsNative.cs | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Ryujinx.Common/Logging/Logger.cs b/src/Ryujinx.Common/Logging/Logger.cs index 2f1e842f5..166a572ab 100644 --- a/src/Ryujinx.Common/Logging/Logger.cs +++ b/src/Ryujinx.Common/Logging/Logger.cs @@ -108,7 +108,7 @@ namespace Ryujinx.Common.Logging [MethodImpl(MethodImplOptions.AggressiveInlining)] private static string FormatMessage(LogClass logClass, string caller, string message) { - if (_homeDirShort != "") + if (!string.IsNullOrEmpty(_homeDirShort)) { message = message.Replace(_homeDirShort, _homeDirRedacted); } diff --git a/src/Ryujinx.Common/Utilities/WindowsNative.cs b/src/Ryujinx.Common/Utilities/WindowsNative.cs index 98e6d0434..125d5e258 100644 --- a/src/Ryujinx.Common/Utilities/WindowsNative.cs +++ b/src/Ryujinx.Common/Utilities/WindowsNative.cs @@ -1,15 +1,14 @@ using System; using System.Runtime.InteropServices; using System.Runtime.Versioning; -using System.Text; namespace Ryujinx.Common.Utilities { public static partial class WindowsNative { [SupportedOSPlatform("windows")] - [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] - static extern int GetShortPathName(string longPath, StringBuilder shortPath, int bufferSize); + [LibraryImport("kernel32.dll", StringMarshalling = StringMarshalling.Utf16, SetLastError = true)] + internal static partial int GetShortPathNameW(string longPath, char[] shortPath, int bufferSize); private const int ShortPathBufferLength = 256; @@ -20,8 +19,8 @@ namespace Ryujinx.Common.Utilities return ""; } - StringBuilder shortPathBuffer = new StringBuilder(ShortPathBufferLength); - int result = GetShortPathName(longPath, shortPathBuffer, ShortPathBufferLength); + char[] shortPathBuffer = new char[ShortPathBufferLength]; + int result = GetShortPathNameW(longPath, shortPathBuffer, shortPathBuffer.Length); if (result == 0) { int errCode = Marshal.GetLastWin32Error(); @@ -29,7 +28,7 @@ namespace Ryujinx.Common.Utilities return ""; } - return shortPathBuffer.ToString(); + return new string(shortPathBuffer[..result]); } } }