From 4d93f97408a2ab36b45c1027f6230a941a7297ea Mon Sep 17 00:00:00 2001
From: mageven <62494521+mageven@users.noreply.github.com>
Date: Fri, 3 Apr 2020 12:23:06 +0530
Subject: [PATCH] Revert SwKbd Applet ReadStruct and fix IApplet's ReadStruct
 to catch (#1087)

error at compile time
---
 Ryujinx.HLE/HOS/Applets/IApplet.cs              |  2 +-
 .../SoftwareKeyboard/SoftwareKeyboardApplet.cs  | 17 ++++++++++++++++-
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/Ryujinx.HLE/HOS/Applets/IApplet.cs b/Ryujinx.HLE/HOS/Applets/IApplet.cs
index b10ede68f6..a29eeb843a 100644
--- a/Ryujinx.HLE/HOS/Applets/IApplet.cs
+++ b/Ryujinx.HLE/HOS/Applets/IApplet.cs
@@ -13,7 +13,7 @@ namespace Ryujinx.HLE.HOS.Applets
 
         ResultCode GetResult();
 
-        static T ReadStruct<T>(ReadOnlySpan<byte> data) where T : struct
+        static T ReadStruct<T>(ReadOnlySpan<byte> data) where T : unmanaged
         {
             return MemoryMarshal.Cast<byte, T>(data)[0];
         }
diff --git a/Ryujinx.HLE/HOS/Applets/SoftwareKeyboard/SoftwareKeyboardApplet.cs b/Ryujinx.HLE/HOS/Applets/SoftwareKeyboard/SoftwareKeyboardApplet.cs
index ed54eb98f4..e142838cab 100644
--- a/Ryujinx.HLE/HOS/Applets/SoftwareKeyboard/SoftwareKeyboardApplet.cs
+++ b/Ryujinx.HLE/HOS/Applets/SoftwareKeyboard/SoftwareKeyboardApplet.cs
@@ -41,7 +41,7 @@ namespace Ryujinx.HLE.HOS.Applets
             var keyboardConfig = _normalSession.Pop();
             var transferMemory = _normalSession.Pop();
 
-            _keyboardConfig = IApplet.ReadStruct<SoftwareKeyboardConfig>(keyboardConfig);
+            _keyboardConfig = ReadStruct<SoftwareKeyboardConfig>(keyboardConfig);
 
             if (_keyboardConfig.UseUtf8)
             {
@@ -176,5 +176,20 @@ namespace Ryujinx.HLE.HOS.Applets
                 return stream.ToArray();
             }
         }
+
+        private static T ReadStruct<T>(byte[] data)
+            where T : struct
+        {
+            GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
+
+            try
+            {    
+                return Marshal.PtrToStructure<T>(handle.AddrOfPinnedObject());
+            }
+            finally
+            {
+                handle.Free();
+            }
+        }
     }
 }