Automatically get the field offset of gameserveritem_t.HasSuccessfulResponse instead of hardcoding it

This commit is contained in:
Jake Rich 2024-08-12 21:44:23 -04:00
parent a80e8590d5
commit 7f747b0468

View File

@ -8,6 +8,9 @@ namespace Steamworks
{ {
internal partial class ISteamMatchmakingServers internal partial class ISteamMatchmakingServers
{ {
// Cached offset of gameserveritem_t.m_bHadSuccessfulResponse
private static int hasSuccessfulResponseOffset;
/// <summary> /// <summary>
/// Read gameserveritem_t.m_bHadSuccessfulResponse without allocating the struct on the heap /// Read gameserveritem_t.m_bHadSuccessfulResponse without allocating the struct on the heap
/// </summary> /// </summary>
@ -21,8 +24,19 @@ internal bool HasServerResponded( HServerListRequest hRequest, int iServer )
// Return false if steam returned null // Return false if steam returned null
if ( returnValue == IntPtr.Zero ) return false; if ( returnValue == IntPtr.Zero ) return false;
// first 8 bytes is IPAddress, next 4 bytes is ping, next 1 byte is m_bHadSuccessfulResponse // Cache the offset of m_bHadSuccessfulResponse
return Marshal.ReadByte( IntPtr.Add( returnValue, 12 ) ) == 1; if ( hasSuccessfulResponseOffset == 0 )
{
hasSuccessfulResponseOffset = Marshal.OffsetOf<gameserveritem_t>( nameof( gameserveritem_t.HadSuccessfulResponse ) ).ToInt32();
if ( hasSuccessfulResponseOffset == 0 )
{
throw new Exception( "Failed to get offset of gameserveritem_t.HadSuccessfulResponse" );
}
}
// Read byte m_bHadSuccessfulResponse
return Marshal.ReadByte( IntPtr.Add( returnValue, hasSuccessfulResponseOffset ) ) == 1;
} }
} }
} }