Skip to content

Commit

Permalink
Automatically get the field offset of gameserveritem_t.HasSuccessfulR…
Browse files Browse the repository at this point in the history
…esponse instead of hardcoding it
  • Loading branch information
Jake-Rich committed Aug 13, 2024
1 parent a80e859 commit 7f747b0
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions Facepunch.Steamworks/Interfaces/ISteamMatchmakingServers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ namespace Steamworks
{
internal partial class ISteamMatchmakingServers
{
// Cached offset of gameserveritem_t.m_bHadSuccessfulResponse
private static int hasSuccessfulResponseOffset;

/// <summary>
/// Read gameserveritem_t.m_bHadSuccessfulResponse without allocating the struct on the heap
/// </summary>
Expand All @@ -21,8 +24,19 @@ internal bool HasServerResponded( HServerListRequest hRequest, int iServer )
// Return false if steam returned null
if ( returnValue == IntPtr.Zero ) return false;

// first 8 bytes is IPAddress, next 4 bytes is ping, next 1 byte is m_bHadSuccessfulResponse
return Marshal.ReadByte( IntPtr.Add( returnValue, 12 ) ) == 1;
// Cache the offset of m_bHadSuccessfulResponse
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;
}
}
}

0 comments on commit 7f747b0

Please sign in to comment.