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 @@ public override string ToString()
{
var ptr = Helpers.TakeMemory();
var self = this;
InternalToString( ref self, ptr, Helpers.MaxStringSize, true );
InternalToString( ref self, ptr, Helpers.MemoryBufferSize, true );
return Helpers.MemoryToString( ptr );
}
}

View File

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

View File

@ -72,16 +72,13 @@ static private unsafe void OnLobbyChatMessageRecievedAPI( LobbyChatMsg_t callbac
{
SteamId steamid = 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, (IntPtr)p, buffer.Length, ref chatEntryType );
var readData = Internal.GetLobbyChatEntry( callback.SteamIDLobby, (int)callback.ChatID, ref steamid, buffer, Helpers.MemoryBufferSize, ref chatEntryType );
if ( readData > 0 )
{
OnChatMessage?.Invoke( new Lobby( callback.SteamIDLobby ), new Friend( steamid ), Encoding.UTF8.GetString( buffer, 0, readData ) );
}
OnChatMessage?.Invoke( new Lobby( callback.SteamIDLobby ), new Friend( steamid ), Helpers.MemoryToString( buffer ) );
}
}

View File

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

View File

@ -100,7 +100,7 @@ internal static InventoryItem From( SteamItemDetails_t details )
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 ) )
return null;
@ -109,7 +109,7 @@ internal static Dictionary<string, string> GetProperties( SteamInventoryResult_t
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 ) )
{

View File

@ -7,7 +7,7 @@ namespace Steamworks
{
internal static class Helpers
{
public const int MaxStringSize = 1024 * 32;
public const int MemoryBufferSize = 1024 * 32;
[ThreadStatic] private static IntPtr[] MemoryPool;
[ThreadStatic] private static int MemoryPoolIndex;
@ -17,13 +17,13 @@ public static unsafe IntPtr TakeMemory()
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
//
MemoryPool = new IntPtr[5];
MemoryPool = new IntPtr[4];
for ( int i = 0; i < MemoryPool.Length; i++ )
MemoryPool[i] = Marshal.AllocHGlobal( MaxStringSize );
MemoryPool[i] = Marshal.AllocHGlobal( MemoryBufferSize );
}
MemoryPoolIndex++;
@ -43,6 +43,7 @@ public static unsafe IntPtr TakeMemory()
/// <summary>
/// Returns a buffer. This will get returned and reused later on.
/// We shouldn't really be using this anymore.
/// </summary>
public static byte[] TakeBuffer( int minSize )
{
@ -73,7 +74,7 @@ internal unsafe static string MemoryToString( IntPtr ptr )
{
var len = 0;
for( len = 0; len < MaxStringSize; len++ )
for( len = 0; len < MemoryBufferSize; len++ )
{
if ( ((byte*)ptr)[len] == 0 )
break;