Skip to content

Commit 7f747b0

Browse files
committed
Automatically get the field offset of gameserveritem_t.HasSuccessfulResponse instead of hardcoding it
1 parent a80e859 commit 7f747b0

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

Facepunch.Steamworks/Interfaces/ISteamMatchmakingServers.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ namespace Steamworks
88
{
99
internal partial class ISteamMatchmakingServers
1010
{
11+
// Cached offset of gameserveritem_t.m_bHadSuccessfulResponse
12+
private static int hasSuccessfulResponseOffset;
13+
1114
/// <summary>
1215
/// Read gameserveritem_t.m_bHadSuccessfulResponse without allocating the struct on the heap
1316
/// </summary>
@@ -21,8 +24,19 @@ internal bool HasServerResponded( HServerListRequest hRequest, int iServer )
2124
// Return false if steam returned null
2225
if ( returnValue == IntPtr.Zero ) return false;
2326

24-
// first 8 bytes is IPAddress, next 4 bytes is ping, next 1 byte is m_bHadSuccessfulResponse
25-
return Marshal.ReadByte( IntPtr.Add( returnValue, 12 ) ) == 1;
27+
// Cache the offset of m_bHadSuccessfulResponse
28+
if ( hasSuccessfulResponseOffset == 0 )
29+
{
30+
hasSuccessfulResponseOffset = Marshal.OffsetOf<gameserveritem_t>( nameof( gameserveritem_t.HadSuccessfulResponse ) ).ToInt32();
31+
32+
if ( hasSuccessfulResponseOffset == 0 )
33+
{
34+
throw new Exception( "Failed to get offset of gameserveritem_t.HadSuccessfulResponse" );
35+
}
36+
}
37+
38+
// Read byte m_bHadSuccessfulResponse
39+
return Marshal.ReadByte( IntPtr.Add( returnValue, hasSuccessfulResponseOffset ) ) == 1;
2640
}
2741
}
2842
}

0 commit comments

Comments
 (0)