From f4e879a1e6ad810aa38c1c020467a2589441871b Mon Sep 17 00:00:00 2001 From: Mary-nyan Date: Thu, 24 Nov 2022 15:26:29 +0100 Subject: Reduce usage of Marshal.PtrToStructure and Marshal.StructureToPtr (#3805) * common: Make BinaryReaderExtensions Read & Write take unamanged types This allows us to not rely on Marshal.PtrToStructure and Marshal.StructureToPtr for those. * common: Make MemoryHelper Read & Write takes unamanged types * Update Marshal.SizeOf => Unsafe.SizeOf when appropriate and start moving software applet to unmanaged types --- .../SoftwareKeyboard/SoftwareKeyboardApplet.cs | 23 +++++++++++----------- .../SoftwareKeyboardCustomizeDic.cs | 5 ++--- .../SoftwareKeyboard/SoftwareKeyboardDictSet.cs | 6 +++--- .../SoftwareKeyboard/SoftwareKeyboardUserWord.cs | 5 ++--- 4 files changed, 18 insertions(+), 21 deletions(-) (limited to 'Ryujinx.HLE/HOS/Applets/SoftwareKeyboard') diff --git a/Ryujinx.HLE/HOS/Applets/SoftwareKeyboard/SoftwareKeyboardApplet.cs b/Ryujinx.HLE/HOS/Applets/SoftwareKeyboard/SoftwareKeyboardApplet.cs index e287318a..74073420 100644 --- a/Ryujinx.HLE/HOS/Applets/SoftwareKeyboard/SoftwareKeyboardApplet.cs +++ b/Ryujinx.HLE/HOS/Applets/SoftwareKeyboard/SoftwareKeyboardApplet.cs @@ -1,4 +1,5 @@ -using Ryujinx.Common.Configuration.Hid; +using Ryujinx.Common; +using Ryujinx.Common.Configuration.Hid; using Ryujinx.Common.Logging; using Ryujinx.HLE.HOS.Applets.SoftwareKeyboard; using Ryujinx.HLE.HOS.Services.Am.AppletAE; @@ -9,6 +10,7 @@ using Ryujinx.Memory; using System; using System.Diagnostics; using System.IO; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; @@ -78,13 +80,13 @@ namespace Ryujinx.HLE.HOS.Applets var launchParams = _normalSession.Pop(); var keyboardConfig = _normalSession.Pop(); - _isBackground = keyboardConfig.Length == Marshal.SizeOf(); + _isBackground = keyboardConfig.Length == Unsafe.SizeOf(); if (_isBackground) { // Initialize the keyboard applet in background mode. - _keyboardBackgroundInitialize = ReadStruct(keyboardConfig); + _keyboardBackgroundInitialize = MemoryMarshal.Read(keyboardConfig); _backgroundState = InlineKeyboardState.Uninitialized; if (_device.UiHandler == null) @@ -342,7 +344,7 @@ namespace Ryujinx.HLE.HOS.Applets else { int wordsCount = reader.ReadInt32(); - int wordSize = Marshal.SizeOf(); + int wordSize = Unsafe.SizeOf(); remaining = stream.Length - stream.Position; if (wordsCount > MaxUserWords) @@ -359,8 +361,7 @@ namespace Ryujinx.HLE.HOS.Applets for (int word = 0; word < wordsCount; word++) { - byte[] wordData = reader.ReadBytes(wordSize); - _keyboardBackgroundUserWords[word] = ReadStruct(wordData); + _keyboardBackgroundUserWords[word] = reader.ReadStruct(); } } } @@ -369,27 +370,25 @@ namespace Ryujinx.HLE.HOS.Applets case InlineKeyboardRequest.SetCustomizeDic: // Read the custom dic data. remaining = stream.Length - stream.Position; - if (remaining != Marshal.SizeOf()) + if (remaining != Unsafe.SizeOf()) { Logger.Warning?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard Customize Dic of {remaining} bytes"); } else { - var keyboardDicData = reader.ReadBytes((int)remaining); - _keyboardBackgroundDic = ReadStruct(keyboardDicData); + _keyboardBackgroundDic = reader.ReadStruct(); } break; case InlineKeyboardRequest.SetCustomizedDictionaries: // Read the custom dictionaries data. remaining = stream.Length - stream.Position; - if (remaining != Marshal.SizeOf()) + if (remaining != Unsafe.SizeOf()) { Logger.Warning?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard DictSet of {remaining} bytes"); } else { - var keyboardDictData = reader.ReadBytes((int)remaining); - _keyboardBackgroundDictSet = ReadStruct(keyboardDictData); + _keyboardBackgroundDictSet = reader.ReadStruct(); } break; case InlineKeyboardRequest.Calc: diff --git a/Ryujinx.HLE/HOS/Applets/SoftwareKeyboard/SoftwareKeyboardCustomizeDic.cs b/Ryujinx.HLE/HOS/Applets/SoftwareKeyboard/SoftwareKeyboardCustomizeDic.cs index 538fb927..53c8c895 100644 --- a/Ryujinx.HLE/HOS/Applets/SoftwareKeyboard/SoftwareKeyboardCustomizeDic.cs +++ b/Ryujinx.HLE/HOS/Applets/SoftwareKeyboard/SoftwareKeyboardCustomizeDic.cs @@ -5,10 +5,9 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard /// /// A structure used by SetCustomizeDic request to software keyboard. /// - [StructLayout(LayoutKind.Sequential, Pack = 4)] + [StructLayout(LayoutKind.Sequential, Size = 0x70)] struct SoftwareKeyboardCustomizeDic { - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 112)] - public byte[] Unknown; + // Unknown } } diff --git a/Ryujinx.HLE/HOS/Applets/SoftwareKeyboard/SoftwareKeyboardDictSet.cs b/Ryujinx.HLE/HOS/Applets/SoftwareKeyboard/SoftwareKeyboardDictSet.cs index b4ffdb90..38554881 100644 --- a/Ryujinx.HLE/HOS/Applets/SoftwareKeyboard/SoftwareKeyboardDictSet.cs +++ b/Ryujinx.HLE/HOS/Applets/SoftwareKeyboard/SoftwareKeyboardDictSet.cs @@ -1,4 +1,5 @@ -using System.Runtime.InteropServices; +using Ryujinx.Common.Memory; +using System.Runtime.InteropServices; namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard { @@ -21,8 +22,7 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard /// /// Array of word entries in the buffer. /// - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 24)] - public ulong[] Entries; + public Array24 Entries; /// /// Number of used entries in the Entries field. diff --git a/Ryujinx.HLE/HOS/Applets/SoftwareKeyboard/SoftwareKeyboardUserWord.cs b/Ryujinx.HLE/HOS/Applets/SoftwareKeyboard/SoftwareKeyboardUserWord.cs index 08f1c3d3..f1bfec2b 100644 --- a/Ryujinx.HLE/HOS/Applets/SoftwareKeyboard/SoftwareKeyboardUserWord.cs +++ b/Ryujinx.HLE/HOS/Applets/SoftwareKeyboard/SoftwareKeyboardUserWord.cs @@ -5,10 +5,9 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard /// /// A structure used by SetUserWordInfo request to the software keyboard. /// - [StructLayout(LayoutKind.Sequential, Pack = 4)] + [StructLayout(LayoutKind.Sequential, Size = 0x64)] struct SoftwareKeyboardUserWord { - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 100)] - public byte[] Unknown; + // Unknown } } -- cgit v1.2.3