Use Helper.TakeMemory instead of TakeBuffer where safe

This commit is contained in:
Garry Newman 2020-03-31 08:24:12 +01:00
parent ecdf217d08
commit a0974e9ba3
6 changed files with 22 additions and 27 deletions

View File

@ -149,7 +149,7 @@ namespace Steamworks.Data
{ {
var ptr = Helpers.TakeMemory(); var ptr = Helpers.TakeMemory();
var self = this; var self = this;
InternalToString( ref self, ptr, Helpers.MaxStringSize, true ); InternalToString( ref self, ptr, Helpers.MemoryBufferSize, true );
return Helpers.MemoryToString( ptr ); return Helpers.MemoryToString( ptr );
} }
} }

View File

@ -83,21 +83,18 @@ namespace Steamworks
var friend = new Friend( data.SteamIDUser ); var friend = new Friend( data.SteamIDUser );
var buffer = Helpers.TakeBuffer( 1024 * 32 ); var buffer = Helpers.TakeMemory();
var type = ChatEntryType.ChatMsg; var type = ChatEntryType.ChatMsg;
fixed ( byte* ptr = buffer ) var len = Internal.GetFriendMessage( data.SteamIDUser, data.MessageID, buffer, Helpers.MemoryBufferSize, ref type );
{
var len = Internal.GetFriendMessage( data.SteamIDUser, data.MessageID, (IntPtr)ptr, buffer.Length, ref type );
if ( len == 0 && type == ChatEntryType.Invalid ) if ( len == 0 && type == ChatEntryType.Invalid )
return; return;
var typeName = type.ToString(); var typeName = type.ToString();
var message = Encoding.UTF8.GetString( buffer, 0, len ); var message = Helpers.MemoryToString( buffer );
OnChatMessage( friend, typeName, message ); OnChatMessage( friend, typeName, message );
}
} }
private static IEnumerable<Friend> GetFriendsWithFlag(FriendFlags flag) private static IEnumerable<Friend> GetFriendsWithFlag(FriendFlags flag)

View File

@ -72,16 +72,13 @@ namespace Steamworks
{ {
SteamId steamid = default; SteamId steamid = default;
ChatEntryType chatEntryType = default; ChatEntryType chatEntryType = default;
var buffer = Helpers.TakeBuffer( 1024 * 4 ); var buffer = Helpers.TakeMemory();
fixed ( byte* p = buffer ) var readData = Internal.GetLobbyChatEntry( callback.SteamIDLobby, (int)callback.ChatID, ref steamid, buffer, Helpers.MemoryBufferSize, ref chatEntryType );
if ( readData > 0 )
{ {
var readData = Internal.GetLobbyChatEntry( callback.SteamIDLobby, (int)callback.ChatID, ref steamid, (IntPtr)p, buffer.Length, ref chatEntryType ); OnChatMessage?.Invoke( new Lobby( callback.SteamIDLobby ), new Friend( steamid ), Helpers.MemoryToString( buffer ) );
if ( readData > 0 )
{
OnChatMessage?.Invoke( new Lobby( callback.SteamIDLobby ), new Friend( steamid ), Encoding.UTF8.GetString( buffer, 0, readData ) );
}
} }
} }

View File

@ -98,7 +98,7 @@ namespace Steamworks
if ( _properties!= null && _properties.TryGetValue( name, out string val ) ) if ( _properties!= null && _properties.TryGetValue( name, out string val ) )
return val; return val;
uint _ = (uint)Helpers.MaxStringSize; uint _ = (uint)Helpers.MaxMemorySize;
if ( !SteamInventory.Internal.GetItemDefinitionProperty( Id, name, out var vl, ref _ ) ) if ( !SteamInventory.Internal.GetItemDefinitionProperty( Id, name, out var vl, ref _ ) )
return null; return null;

View File

@ -100,7 +100,7 @@ namespace Steamworks
internal static Dictionary<string, string> GetProperties( SteamInventoryResult_t result, int index ) internal static Dictionary<string, string> GetProperties( SteamInventoryResult_t result, int index )
{ {
var strlen = (uint) Helpers.MaxStringSize; var strlen = (uint) Helpers.MemoryBufferSize;
if ( !SteamInventory.Internal.GetResultItemProperty( result, (uint)index, null, out var propNames, ref strlen ) ) if ( !SteamInventory.Internal.GetResultItemProperty( result, (uint)index, null, out var propNames, ref strlen ) )
return null; return null;
@ -109,7 +109,7 @@ namespace Steamworks
foreach ( var propertyName in propNames.Split( ',' ) ) foreach ( var propertyName in propNames.Split( ',' ) )
{ {
strlen = (uint)Helpers.MaxStringSize; strlen = (uint)Helpers.MemoryBufferSize;
if ( SteamInventory.Internal.GetResultItemProperty( result, (uint)index, propertyName, out var strVal, ref strlen ) ) if ( SteamInventory.Internal.GetResultItemProperty( result, (uint)index, propertyName, out var strVal, ref strlen ) )
{ {

View File

@ -7,7 +7,7 @@ namespace Steamworks
{ {
internal static class Helpers internal static class Helpers
{ {
public const int MaxStringSize = 1024 * 32; public const int MemoryBufferSize = 1024 * 32;
[ThreadStatic] private static IntPtr[] MemoryPool; [ThreadStatic] private static IntPtr[] MemoryPool;
[ThreadStatic] private static int MemoryPoolIndex; [ThreadStatic] private static int MemoryPoolIndex;
@ -17,13 +17,13 @@ namespace Steamworks
if ( MemoryPool == null ) if ( MemoryPool == null )
{ {
// //
// The pool has 5 items. This should be safe because we shouldn't really // The pool has 4 items. This should be safe because we shouldn't really
// ever be using more than 2 memory pools // ever be using more than 2 memory pools
// //
MemoryPool = new IntPtr[5]; MemoryPool = new IntPtr[4];
for ( int i = 0; i < MemoryPool.Length; i++ ) for ( int i = 0; i < MemoryPool.Length; i++ )
MemoryPool[i] = Marshal.AllocHGlobal( MaxStringSize ); MemoryPool[i] = Marshal.AllocHGlobal( MemoryBufferSize );
} }
MemoryPoolIndex++; MemoryPoolIndex++;
@ -43,6 +43,7 @@ namespace Steamworks
/// <summary> /// <summary>
/// Returns a buffer. This will get returned and reused later on. /// Returns a buffer. This will get returned and reused later on.
/// We shouldn't really be using this anymore.
/// </summary> /// </summary>
public static byte[] TakeBuffer( int minSize ) public static byte[] TakeBuffer( int minSize )
{ {
@ -73,7 +74,7 @@ namespace Steamworks
{ {
var len = 0; var len = 0;
for( len = 0; len < MaxStringSize; len++ ) for( len = 0; len < MemoryBufferSize; len++ )
{ {
if ( ((byte*)ptr)[len] == 0 ) if ( ((byte*)ptr)[len] == 0 )
break; break;