This commit is contained in:
Garry Newman 2016-10-05 09:44:35 +01:00
parent 1b2463cc0b
commit e6126a0e40
3 changed files with 15 additions and 18 deletions

View File

@ -9,7 +9,7 @@ namespace Facepunch.Steamworks.Test
public partial class Server public partial class Server
{ {
[TestMethod] [TestMethod]
public void Invetory() public void InventoryDeserialize()
{ {
using ( var client = new Facepunch.Steamworks.Client( 252490 ) ) using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{ {

View File

@ -79,20 +79,18 @@ internal void Fill()
} ).ToArray(); } ).ToArray();
} }
internal byte[] Serialize() internal unsafe byte[] Serialize()
{ {
uint size = 0; uint size = 0;
inventory.inventory.SerializeResult( Handle, IntPtr.Zero, out size ); 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]; var data = new byte[size];
Marshal.Copy( ptr, data, 0, (int)size ); fixed ( byte* ptr = data )
Marshal.FreeHGlobal( ptr ); {
if ( !inventory.inventory.SerializeResult( Handle, (IntPtr) ptr, out size ) )
return null;
}
return data; return data;
} }

View File

@ -206,22 +206,21 @@ private Definition FindDefinition( int def )
return Definitions.FirstOrDefault( x => x.Id == 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 ) if ( dataLength == -1 )
dataLength = data.Length; dataLength = data.Length;
int resultHandle = -1; int resultHandle = -1;
IntPtr ptr = Marshal.AllocHGlobal( dataLength); fixed ( byte* ptr = data )
Marshal.Copy( data, 0, ptr, dataLength ); {
var result = inventory.DeserializeResult( out resultHandle, ptr, (uint)dataLength, false ); var result = inventory.DeserializeResult( out resultHandle, (IntPtr) ptr, (uint)dataLength, false );
Marshal.FreeHGlobal( ptr ); if ( !result || resultHandle == -1 )
return null;
if ( !result || resultHandle == -1 ) return new Result( this, resultHandle );
return null; }
return new Result( this, resultHandle );
} }
} }