diff --git a/Facepunch.Steamworks.Test/Server/Inventory.cs b/Facepunch.Steamworks.Test/Server/Inventory.cs index fdc8503..fd71916 100644 --- a/Facepunch.Steamworks.Test/Server/Inventory.cs +++ b/Facepunch.Steamworks.Test/Server/Inventory.cs @@ -9,7 +9,7 @@ namespace Facepunch.Steamworks.Test public partial class Server { [TestMethod] - public void Invetory() + public void InventoryDeserialize() { using ( var client = new Facepunch.Steamworks.Client( 252490 ) ) { diff --git a/Facepunch.Steamworks/Client/Inventory.Result.cs b/Facepunch.Steamworks/Client/Inventory.Result.cs index e010ea0..eeef834 100644 --- a/Facepunch.Steamworks/Client/Inventory.Result.cs +++ b/Facepunch.Steamworks/Client/Inventory.Result.cs @@ -79,20 +79,18 @@ internal void Fill() } ).ToArray(); } - internal byte[] Serialize() + internal unsafe byte[] Serialize() { uint size = 0; inventory.inventory.SerializeResult( Handle, IntPtr.Zero, out size ); - IntPtr ptr = Marshal.AllocHGlobal((int) size); - - if ( !inventory.inventory.SerializeResult( Handle, ptr, out size ) ) - return null; - var data = new byte[size]; - Marshal.Copy( ptr, data, 0, (int)size ); - Marshal.FreeHGlobal( ptr ); + fixed ( byte* ptr = data ) + { + if ( !inventory.inventory.SerializeResult( Handle, (IntPtr) ptr, out size ) ) + return null; + } return data; } diff --git a/Facepunch.Steamworks/Client/Inventory.cs b/Facepunch.Steamworks/Client/Inventory.cs index d52197e..64d9c1c 100644 --- a/Facepunch.Steamworks/Client/Inventory.cs +++ b/Facepunch.Steamworks/Client/Inventory.cs @@ -206,22 +206,21 @@ private Definition FindDefinition( int def ) return Definitions.FirstOrDefault( x => x.Id == def ); } - public Result Deserialize( byte[] data, int dataLength = -1 ) + public unsafe Result Deserialize( byte[] data, int dataLength = -1 ) { if ( dataLength == -1 ) dataLength = data.Length; int resultHandle = -1; - IntPtr ptr = Marshal.AllocHGlobal( dataLength); - Marshal.Copy( data, 0, ptr, dataLength ); - var result = inventory.DeserializeResult( out resultHandle, ptr, (uint)dataLength, false ); - Marshal.FreeHGlobal( ptr ); + fixed ( byte* ptr = data ) + { + var result = inventory.DeserializeResult( out resultHandle, (IntPtr) ptr, (uint)dataLength, false ); + if ( !result || resultHandle == -1 ) + return null; - if ( !result || resultHandle == -1 ) - return null; - - return new Result( this, resultHandle ); + return new Result( this, resultHandle ); + } } }