Facepunch.Steamworks
Another fucking c# Steamworks implementation
Why
The Steamworks C# implementations I found that were compatible with Unity have worked for a long time. But I hate them all. For a number of different reasons.
- They're not C#, they're just a collection of functions.
- They're not up to date.
- They require a 3rd party native dll.
- They can't be compiled into a standalone dll (in Unity).
- They have a license.
C# is meant to make things easier. So lets try to wrap it up in a way that makes it all easier.
What
Get your own information
SteamClient.SteamId // Your SteamId
SteamClient.Name // Your Name
View your friends list
foreach ( var friend in SteamFriends.GetFriends() )
{
Console.WriteLine( "{friend.Id}: {friend.Name}" );
Console.WriteLine( "{friend.IsOnline} / {friend.SteamLevel}" );
friend.SendMessage( "Hello Friend" );
}
But what if you want to get a list of friends playing the same game that we're playing?
foreach ( var friend in client.Friends.All.Where( x => x.IsPlayingThisGame ) )
{
//
}
App Info
Console.WriteLine( SteamApps.GameLanguage ); // Print the current game language
var installDir = SteamApps.AppInstallDir( 4000 ); // Get the path to the Garry's Mod install folder
var fileinfo = await SteamApps.GetFileDetailsAsync( "hl2.exe" ); // async get file details
DoSomething( fileinfo.SizeInBytes, fileinfo.Sha1 );
Get Avatars
var image = await SteamFriends.GetLargeAvatarAsync( steamid );
if ( !image.HasValue ) return DefaultImage;
return MakeTextureFromRGBA( image.Data, image.Width, image.Height );
Get a list of servers
using ( var list = new ServerList.Internet() )
{
list.AddFilter( "map", "de_dust" );
await list.RunQueryAsync();
foreach ( var server in list.Responsive )
{
Console.WriteLine( $"{server.Address} {server.Name}" );
}
}
Achievements
List them
foreach ( var a in SteamUserStats.Achievements )
{
Console.WriteLine( $"{a.Name} ({a.State}})" );
}
Unlock them
var ach = new Achievement( "GM_PLAYED_WITH_GARRY" );
ach.Trigger();
Voice
SteamUser.VoiceRecord = KeyDown( "V" );
if ( SteamUser.HasVoiceData )
{
var bytesrwritten = SteamUser.ReadVoiceData( stream );
// Send Stream Data To Server or Something
}
Auth
// Client sends ticket data to server somehow
var ticket = SteamUser.GetAuthSessionTicket();
// server listens to event
SteamServer.OnValidateAuthTicketResponse += ( steamid, ownerid, rsponse ) =>
{
if ( rsponse == AuthResponse.OK )
TellUserTheyCanBeOnServer( steamid );
else
KickUser( steamid );
};
// server gets ticket data from client, calls this function.. which either returns
// false straight away, or will issue a TicketResponse.
if ( !SteamServer.BeginAuthSession( ticketData, clientSteamId ) )
{
KickUser( clientSteamId );
}
//
// Client is leaving, cancels their ticket OnValidateAuth is called on the server again
// this time with AuthResponse.AuthTicketCanceled
//
ticket.Cancel();
Utils
SteamUtils.SecondsSinceAppActive;
SteamUtils.SecondsSinceComputerActive;
SteamUtils.IpCountry;
SteamUtils.UsingBatteryPower;
SteamUtils.CurrentBatteryPower;
SteamUtils.AppId;
SteamUtils.IsOverlayEnabled;
SteamUtils.IsSteamRunningInVR;
SteamUtils.IsSteamInBigPictureMode;
Usage
Client
Compile the Facepunch.Steamworks project and add the library to your Unity project. To create a client you can do this.
var client = new Facepunch.Steamworks.Client( 252490 );
Replace 252490 with the appid of your game. This should be a singleton - you should only create one client, right at the start of your game.
The client is disposable, so when you're closing your game should probably call..
client.Dispose();
Or use it in a using block if you can.
Server
To create a server do this.
ServerInit options = new ServerInit("GameDirectoryName", "GameDescription");
var server = new Facepunch.Steamworks.Server(252490, options);
This will register a secure server for game 252490, any ip, port 28015. Again, more usage in the Facepunch.Steamworks.Test project.
Lobby
To create a Lobby do this.
client.Lobby.Create(Steamworks.Lobby.Type.Public, 10);
Created lobbies are auto-joined, but if you want to find a friend's lobby, you'd call
client.LobbyList.Refresh();
//wait for the callback
client.LobbyList.OnLobbiesUpdated = () =>
{
if (client.LobbyList.Finished)
{
foreach (LobbyList.Lobby lobby in client.LobbyList.Lobbies)
{
Console.WriteLine($"Found Lobby: {lobby.Name}");
}
}
};
//join a lobby you found
client.Lobby.Join(LobbyList.Lobbies[0]);
Your can find more examples of Lobby functionality in the Lobby.cs file in the test project. Sending chat messages, assinging lobby data and member data, etc.
Unity
Yeah this works under Unity. That's half the point of it.
There's another repo with an example project with it working in Unity. You can find it here.
The TLDR is before you create the Client or the Server, call this to let Facepunch.Steamworks know which platform we're on - because it can't tell the difference between osx and linux by itself.
Facepunch.Steamworks.Config.ForUnity( Application.platform.ToString() );
You'll also want to put steam_api64.dll and steam_appid.txt (on windows 64) in your project root next to Assets, and use an editor script like this to copy them into standalone builds.
Help
Wanna help? Go for it, pull requests, bug reports, yes, do it.
You can also hit up the Steamworks Thread for help/discussion.
License
MIT - do whatever you want.