Cleaning up inventory deserialize

This commit is contained in:
Garry Newman 2019-05-13 20:05:10 +01:00
parent f629b1c663
commit fff2fc6098
2 changed files with 58 additions and 13 deletions

View File

@ -146,6 +146,51 @@ namespace Steamworks
}
}
}
[TestMethod]
public async Task Serialize()
{
await SteamInventory.WaitForDefinitions();
var result = await SteamInventory.GetAllItemsAsync();
Assert.IsTrue( result.HasValue );
var data = result.Value.Serialize();
Assert.IsNotNull( data );
Console.WriteLine( string.Join( "", data.Select( x => x.ToString( "x" ) ) ) );
}
[TestMethod]
public async Task Deserialize()
{
await SteamInventory.WaitForDefinitions();
byte[] data = null;
int itemCount = 0;
// Serialize
{
var result = await SteamInventory.GetAllItemsAsync();
Assert.IsTrue( result.HasValue );
itemCount = result.Value.ItemCount;
data = result.Value.Serialize();
Assert.IsNotNull( data );
result.Value.Dispose();
}
await Task.Delay( 2000 );
// Deserialize
{
var result = await SteamInventory.DeserializeAsync( data );
Assert.IsTrue( result.HasValue );
Assert.AreEqual( itemCount, result.Value.ItemCount );
result.Value.Dispose();
}
}
}
}

View File

@ -280,28 +280,28 @@ namespace Steamworks
public static async Task<InventoryResult?> DeserializeAsync( byte[] data, int dataLength = -1 )
{
if ( data == null )
throw new ArgumentException( "data should nto be null" );
throw new ArgumentException( "data should not be null" );
if ( dataLength == -1 )
dataLength = data.Length;
var sresult = DeserializeResult( data, dataLength );
if ( !sresult.HasValue ) return null;
var ptr = Marshal.AllocHGlobal( dataLength );
return await InventoryResult.GetAsync( sresult.Value );
}
internal static unsafe SteamInventoryResult_t? DeserializeResult( byte[] data, int dataLength = -1 )
{
var sresult = default( SteamInventoryResult_t );
fixed ( byte* ptr = data )
try
{
Marshal.Copy( data, 0, ptr, dataLength );
var sresult = default( SteamInventoryResult_t );
if ( !Internal.DeserializeResult( ref sresult, (IntPtr)ptr, (uint)dataLength, false ) )
return null;
}
return sresult;
return await InventoryResult.GetAsync( sresult.Value );
}
finally
{
Marshal.FreeHGlobal( ptr );
}
}