Class SteamNetworkClient
- Namespace
- SteamNetworkLib
- Assembly
- SteamNetworkLib.dll
Main entry point for SteamNetworkLib - provides simplified access to all Steam networking features. Perfect for use in MelonLoader mods that need Steam lobby and P2P functionality.
public class SteamNetworkClient : IDisposable
- Inheritance
-
SteamNetworkClient
- Implements
- Inherited Members
Constructors
SteamNetworkClient()
Initializes a new instance of the SteamNetworkClient class. Call Initialize() before using any other methods.
public SteamNetworkClient()
Properties
CurrentLobby
Gets information about the current lobby, or null if not in a lobby.
public LobbyInfo? CurrentLobby { get; }
Property Value
IsHost
Gets whether the local player is the host of the current lobby.
public bool IsHost { get; }
Property Value
IsInLobby
Gets whether the local player is currently in a lobby.
public bool IsInLobby { get; }
Property Value
LibraryVersion
Gets the current version of SteamNetworkLib.
public static string LibraryVersion { get; }
Property Value
LobbyData
Gets the lobby data manager for handling lobby-wide data.
public SteamLobbyData LobbyData { get; }
Property Value
LobbyManager
Gets the lobby manager for handling Steam lobby operations.
public SteamLobbyManager LobbyManager { get; }
Property Value
LocalPlayerId
Gets the Steam ID of the local player.
public CSteamID LocalPlayerId { get; }
Property Value
- CSteamID
MemberData
Gets the member data manager for handling player-specific data.
public SteamMemberData MemberData { get; }
Property Value
P2PManager
Gets the P2P manager for handling peer-to-peer communication.
public SteamP2PManager P2PManager { get; }
Property Value
VersionCheckEnabled
Gets or sets whether automatic version checking is enabled. When enabled, the library will automatically check for version compatibility between players.
public bool VersionCheckEnabled { get; set; }
Property Value
Remarks
IMPORTANT: Version checking is crucial for ensuring proper data transfer and synchronization between players.
Disabling this feature or ignoring version mismatches may result in:
- Data serialization/deserialization failures
- Message format incompatibilities
- Synchronization errors
- Unexpected networking behavior or crashes
It is strongly recommended to keep this enabled and ensure all players use the same SteamNetworkLib version.
Methods
BroadcastMessage(P2PMessage)
Sends a message to all players in the lobby. This is a non-async wrapper around BroadcastMessageAsync.
public void BroadcastMessage(P2PMessage message)
Parameters
message
P2PMessageThe message to broadcast.
Remarks
This method is provided for backward compatibility. For new code, use BroadcastMessageAsync instead.
Exceptions
- InvalidOperationException
Thrown when the client is not initialized.
- P2PException
Thrown when the message cannot be sent.
BroadcastMessageAsync(P2PMessage)
Sends a message to all players in the lobby.
public Task BroadcastMessageAsync(P2PMessage message)
Parameters
message
P2PMessageThe message to broadcast.
Returns
Remarks
This method sends the message to each player individually.
Exceptions
- InvalidOperationException
Thrown when the client is not initialized.
- P2PException
Thrown when the message cannot be sent.
BroadcastTextMessage(string)
Broadcasts a simple text message to all players. This is a non-async wrapper around BroadcastTextMessageAsync.
public void BroadcastTextMessage(string text)
Parameters
text
stringThe text message to broadcast.
Remarks
This method is provided for backward compatibility. For new code, use BroadcastTextMessageAsync instead.
Exceptions
- InvalidOperationException
Thrown when the client is not initialized.
- P2PException
Thrown when the message cannot be sent.
BroadcastTextMessageAsync(string)
Broadcasts a simple text message to all players.
public Task BroadcastTextMessageAsync(string text)
Parameters
text
stringThe text message to broadcast.
Returns
- Task
A task that represents the asynchronous operation.
Remarks
This is a convenience method that creates a TextMessage internally.
Exceptions
- InvalidOperationException
Thrown when the client is not initialized.
- P2PException
Thrown when the message cannot be sent.
CheckLibraryVersionCompatibility()
Performs a comprehensive version check and fires the OnVersionMismatch event if incompatibilities are found.
public bool CheckLibraryVersionCompatibility()
Returns
- bool
True if all versions are compatible, false if mismatches were detected.
Exceptions
- InvalidOperationException
Thrown when the client is not initialized.
- LobbyException
Thrown when not in a lobby.
CreateLobbyAsync(ELobbyType, int)
Creates a new lobby with the specified settings.
public Task<LobbyInfo> CreateLobbyAsync(ELobbyType lobbyType = 1, int maxMembers = 4)
Parameters
lobbyType
ELobbyTypeThe type of lobby to create.
maxMembers
intThe maximum number of members allowed in the lobby.
Returns
- Task<LobbyInfo>
A task that represents the asynchronous operation. The task result contains the lobby information.
Exceptions
- InvalidOperationException
Thrown when the client is not initialized.
- LobbyException
Thrown when lobby creation fails.
Dispose()
Releases all resources used by the SteamNetworkClient.
public void Dispose()
Remarks
This method disposes all component managers and releases any unmanaged resources.
GetDataForAllPlayers(string)
Gets the same data key for all players in the lobby.
public Dictionary<CSteamID, string> GetDataForAllPlayers(string key)
Parameters
key
stringThe data key.
Returns
- Dictionary<CSteamID, string>
A dictionary mapping player Steam IDs to their data values.
Exceptions
- InvalidOperationException
Thrown when the client is not initialized.
- LobbyException
Thrown when not in a lobby.
GetLobbyData(string)
Gets lobby-wide data.
public string? GetLobbyData(string key)
Parameters
key
stringThe data key.
Returns
- string
The data value, or null if not found.
Exceptions
- InvalidOperationException
Thrown when the client is not initialized.
- LobbyException
Thrown when not in a lobby.
GetLobbyMembers()
Gets all members in the current lobby.
public List<MemberInfo> GetLobbyMembers()
Returns
- List<MemberInfo>
A list of MemberInfo objects for all players in the lobby.
Remarks
Returns an empty list if not currently in a lobby.
Exceptions
- InvalidOperationException
Thrown when the client is not initialized.
GetMyData(string)
Gets data for the local player.
public string? GetMyData(string key)
Parameters
key
stringThe data key.
Returns
- string
The data value, or null if not found.
Exceptions
- InvalidOperationException
Thrown when the client is not initialized.
- LobbyException
Thrown when not in a lobby.
GetPlayerData(CSteamID, string)
Gets data for a specific player.
public string? GetPlayerData(CSteamID playerId, string key)
Parameters
playerId
CSteamIDThe Steam ID of the player.
key
stringThe data key.
Returns
- string
The data value, or null if not found.
Exceptions
- InvalidOperationException
Thrown when the client is not initialized.
- LobbyException
Thrown when not in a lobby or the player is not in the lobby.
GetPlayerLibraryVersions()
Gets the SteamNetworkLib versions of all players in the lobby.
public Dictionary<CSteamID, string> GetPlayerLibraryVersions()
Returns
- Dictionary<CSteamID, string>
A dictionary mapping player Steam IDs to their SteamNetworkLib versions. Players without version data are excluded.
Remarks
Use this method to identify which players have different library versions that could cause data transfer issues.
Players with missing version data may be using older versions of SteamNetworkLib that don't support version checking,
which could result in unpredictable networking behavior and synchronization failures.
Exceptions
- InvalidOperationException
Thrown when the client is not initialized.
- LobbyException
Thrown when not in a lobby.
Initialize()
Initializes the Steam networking client. Must be called before using any other methods.
public bool Initialize()
Returns
- bool
True if initialization was successful.
Exceptions
- SteamNetworkException
Thrown when Steam is not available or when initialization fails.
InviteFriend(CSteamID)
Invites a friend to the current lobby.
public void InviteFriend(CSteamID friendId)
Parameters
friendId
CSteamIDThe Steam ID of the friend to invite.
Exceptions
- InvalidOperationException
Thrown when the client is not initialized.
- LobbyException
Thrown when not in a lobby or invitation fails.
IsModDataCompatible(string)
Checks if all players have compatible mod data for a given key.
public bool IsModDataCompatible(string dataKey)
Parameters
dataKey
stringThe data key to check for compatibility.
Returns
- bool
True if all players have the same data value, false otherwise.
Remarks
This method is useful for verifying that all players are using the same mod version.
Exceptions
- InvalidOperationException
Thrown when the client is not initialized.
- LobbyException
Thrown when not in a lobby.
JoinLobbyAsync(CSteamID)
Joins an existing lobby by ID.
public Task<LobbyInfo> JoinLobbyAsync(CSteamID lobbyId)
Parameters
lobbyId
CSteamIDThe Steam ID of the lobby to join.
Returns
- Task<LobbyInfo>
A task that represents the asynchronous operation. The task result contains the lobby information.
Exceptions
- InvalidOperationException
Thrown when the client is not initialized.
- LobbyException
Thrown when joining the lobby fails.
LeaveLobby()
Leaves the current lobby.
public void LeaveLobby()
Remarks
This method has no effect if the local player is not currently in a lobby.
Exceptions
- InvalidOperationException
Thrown when the client is not initialized.
OpenInviteDialog()
Opens the Steam overlay invite dialog.
public void OpenInviteDialog()
Exceptions
- InvalidOperationException
Thrown when the client is not initialized.
- LobbyException
Thrown when not in a lobby or the overlay fails to open.
ProcessIncomingMessages()
Processes incoming P2P packets. Call this regularly (e.g., in Update()).
public void ProcessIncomingMessages()
Remarks
This method should be called frequently to ensure timely processing of incoming messages. If not called regularly, messages may be delayed or dropped.
RegisterMessageHandler<T>(Action<T, CSteamID>)
Registers a handler for a specific message type.
public void RegisterMessageHandler<T>(Action<T, CSteamID> handler) where T : P2PMessage, new()
Parameters
handler
Action<T, CSteamID>The handler function that will be called when messages of this type are received.
Type Parameters
T
The type of message to handle.
Remarks
The handler will be called with the message and the sender's Steam ID.
Exceptions
- InvalidOperationException
Thrown when the client is not initialized.
SendDataSyncAsync(CSteamID, string, string, string)
Sends a data synchronization message to a player.
public Task<bool> SendDataSyncAsync(CSteamID playerId, string key, string value, string dataType = "string")
Parameters
playerId
CSteamIDThe Steam ID of the target player.
key
stringThe data key.
value
stringThe data value.
dataType
stringThe data type identifier.
Returns
- Task<bool>
A task that represents the asynchronous operation. The task result indicates whether the message was sent successfully.
Remarks
This is a convenience method that creates a DataSyncMessage internally.
Exceptions
- InvalidOperationException
Thrown when the client is not initialized.
- P2PException
Thrown when the message cannot be sent.
SendMessageToPlayerAsync(CSteamID, P2PMessage)
Sends a message to a specific player.
public Task<bool> SendMessageToPlayerAsync(CSteamID playerId, P2PMessage message)
Parameters
playerId
CSteamIDThe Steam ID of the target player.
message
P2PMessageThe message to send.
Returns
- Task<bool>
A task that represents the asynchronous operation. The task result indicates whether the message was sent successfully.
Exceptions
- InvalidOperationException
Thrown when the client is not initialized.
- P2PException
Thrown when the message cannot be sent.
SendTextMessageAsync(CSteamID, string)
Sends a simple text message to a player.
public Task<bool> SendTextMessageAsync(CSteamID playerId, string text)
Parameters
playerId
CSteamIDThe Steam ID of the target player.
text
stringThe text message to send.
Returns
- Task<bool>
A task that represents the asynchronous operation. The task result indicates whether the message was sent successfully.
Remarks
This is a convenience method that creates a TextMessage internally.
Exceptions
- InvalidOperationException
Thrown when the client is not initialized.
- P2PException
Thrown when the message cannot be sent.
SetLobbyData(string, string)
Sets lobby-wide data that is accessible to all players.
public void SetLobbyData(string key, string value)
Parameters
Remarks
Only the lobby owner can set lobby data. This method will fail silently if called by a non-owner.
Exceptions
- InvalidOperationException
Thrown when the client is not initialized.
- LobbyException
Thrown when not in a lobby or not the lobby owner.
SetMyData(string, string)
Sets data for the local player that is visible to all players.
public void SetMyData(string key, string value)
Parameters
Exceptions
- InvalidOperationException
Thrown when the client is not initialized.
- LobbyException
Thrown when not in a lobby.
SetMyDataBatch(Dictionary<string, string>)
Sets multiple data values at once for the local player.
public void SetMyDataBatch(Dictionary<string, string> data)
Parameters
data
Dictionary<string, string>A dictionary containing key-value pairs to set.
Exceptions
- InvalidOperationException
Thrown when the client is not initialized.
- LobbyException
Thrown when not in a lobby.
SyncModDataWithAllPlayers(string, string, string)
Synchronizes data with all players in the lobby. This is a non-async wrapper around SyncModDataWithAllPlayersAsync.
public void SyncModDataWithAllPlayers(string dataKey, string dataValue, string dataType = "string")
Parameters
dataKey
stringThe data key to synchronize.
dataValue
stringThe data value to synchronize.
dataType
stringThe data type identifier.
Remarks
This method is provided for backward compatibility. For new code, use SyncModDataWithAllPlayersAsync instead.
Exceptions
- InvalidOperationException
Thrown when the client is not initialized.
- LobbyException
Thrown when not in a lobby.
SyncModDataWithAllPlayersAsync(string, string, string)
Synchronizes data with all players in the lobby. Useful for mod compatibility checks and data synchronization.
public Task SyncModDataWithAllPlayersAsync(string dataKey, string dataValue, string dataType = "string")
Parameters
dataKey
stringThe data key to synchronize.
dataValue
stringThe data value to synchronize.
dataType
stringThe data type identifier.
Returns
- Task
A task that represents the asynchronous operation.
Remarks
This method both sets the local player's data and broadcasts it to all other players.
Exceptions
- InvalidOperationException
Thrown when the client is not initialized.
- LobbyException
Thrown when not in a lobby.
Events
OnLobbyCreated
Occurs when a new lobby is created.
public event EventHandler<LobbyCreatedEventArgs>? OnLobbyCreated
Event Type
OnLobbyDataChanged
Occurs when lobby data is changed.
public event EventHandler<LobbyDataChangedEventArgs>? OnLobbyDataChanged
Event Type
OnLobbyJoined
Occurs when the local player joins a lobby.
public event EventHandler<LobbyJoinedEventArgs>? OnLobbyJoined
Event Type
OnLobbyLeft
Occurs when the local player leaves a lobby.
public event EventHandler<LobbyLeftEventArgs>? OnLobbyLeft
Event Type
OnMemberDataChanged
Occurs when member data is changed.
public event EventHandler<MemberDataChangedEventArgs>? OnMemberDataChanged
Event Type
OnMemberJoined
Occurs when a new member joins the current lobby.
public event EventHandler<MemberJoinedEventArgs>? OnMemberJoined
Event Type
OnMemberLeft
Occurs when a member leaves the current lobby.
public event EventHandler<MemberLeftEventArgs>? OnMemberLeft
Event Type
OnP2PMessageReceived
Occurs when a P2P message is received from another player.
public event EventHandler<P2PMessageReceivedEventArgs>? OnP2PMessageReceived
Event Type
OnVersionMismatch
Occurs when a version mismatch is detected between players in the lobby.
public event EventHandler<VersionMismatchEventArgs>? OnVersionMismatch
Event Type
Remarks
CRITICAL: This event indicates that players are using incompatible versions of SteamNetworkLib.
Version mismatches can cause serious issues including data corruption, synchronization failures, and networking errors.