Steamworks
SwiftlyS2 ships generated server-side Steamworks bindings under SwiftlyS2.Shared.SteamAPI (using SwiftlyS2.Shared.SteamAPI;), covering identity, authorization, server metadata, license/Workshop checks, and Steam's async callback patterns, as static classes mirroring the native ISteamGameServer* interfaces - see APISteamGameServer, APISteamGameServerUtils, APISteamGameServerUGC and APISteamGameServerStats for the full member lists (or the Quick Reference table at the end for the class overview). The same namespace also exposes the game server handles and the callback registration types described below.
Waiting for the Steam API
Steam callbacks aren't usable until the Steam API finishes activating - subscribe to Core.Event.OnSteamAPIActivated before touching any class above:
public override void Load(bool hotReload)
{
Core.Event.OnSteamAPIActivated += OnSteamApiActivated;
}
private void OnSteamApiActivated()
{
var appId = SteamGameServerUtils.GetAppID();
Core.Logger.LogInformation("Steam API ready, AppId={AppId}", appId.m_AppId);
}APIInteropHelp.TestIfAvailableGameServer is the synchronous equivalent: it throws an InvalidOperationException when the game server API isn't initialized yet, so it can be used as a guard from code that doesn't run off OnSteamAPIActivated.
Converting Player IDs to Steam IDs
IPlayer.SteamID is a plain ulong - wrap it in APICSteamID before passing it to a Steam API call:
IPlayer? player = Core.PlayerManager.GetPlayer(playerId);
if (player == null) return;
var steamId = new CSteamID(player.SteamID);
if (!steamId.IsValid() || !steamId.BIndividualAccount())
{
Core.Logger.LogWarning("Invalid SteamID for slot {PlayerId}", playerId);
return;
}Authorization Signals
Core.Event.OnClientSteamAuthorize/OnClientSteamAuthorizeFail fire once Steam confirms or rejects a connecting client:
Core.Event.OnClientSteamAuthorize += @event =>
Core.Logger.LogInformation("Steam auth OK for slot {PlayerId}", @event.PlayerId);
Core.Event.OnClientSteamAuthorizeFail += @event =>
Core.Logger.LogWarning("Steam auth failed for slot {PlayerId}", @event.PlayerId);Both events carry a single int PlayerId (APIIOnClientSteamAuthorizeEvent, APIIOnClientSteamAuthorizeFailEvent). The outcome is also readable from the player object afterwards: IPlayer.IsAuthorized tells you whether the player is authorized, and IPlayer.UnauthorizedSteamID holds the Steam ID that wasn't verified yet.
Server Metadata
SteamGameServer.SetServerName("My Swiftly Server");
SteamGameServer.SetMapName("de_dust2");
SteamGameServer.SetMaxPlayerCount(32);
SteamGameServer.SetPasswordProtected(false);
bool loggedOn = SteamGameServer.BLoggedOn();
bool secure = SteamGameServer.BSecure();
CSteamID serverSteamId = SteamGameServer.GetSteamID();Game Server Handles
The classes above go through the ISteamGameServer* interfaces - APIGameServer holds the process-level accessors that don't need an interface instance:
CSteamID serverSteamId = GameServer.GetSteamID();
HSteamPipe pipe = GameServer.GetHSteamPipe();
HSteamUser user = GameServer.GetHSteamUser();
bool secure = GameServer.BSecure();| Member | Signature | Returns |
|---|---|---|
| APIGameServer.GetSteamID | static CSteamID GetSteamID() | the Steam ID of the game server |
| APIGameServer.GetHSteamPipe | static HSteamPipe GetHSteamPipe() | the Steam pipe handle of the game server |
| APIGameServer.GetHSteamUser | static HSteamUser GetHSteamUser() | the Steam user handle of the game server |
| APIGameServer.BSecure | static bool BSecure() | whether the game server is VAC-secured |
License / Ownership Checks
var appId = new AppId_t(730); // see ApiRef: AppId_t
var result = SteamGameServer.UserHasLicenseForApp(steamId, appId);
if (result == EUserHasLicenseForAppResult.k_EUserHasLicenseResultHasLicense)
{
Core.Logger.LogInformation("Player owns app {AppId}", appId.m_AppId);
}Workshop Items
Use SteamGameServerUGC - not the client-side SteamUGC - for server-side Workshop queries:
var fileId = new PublishedFileId_t(3070212801);
var state = (EItemState)SteamGameServerUGC.GetItemState(fileId);
if ((state & EItemState.k_EItemStateInstalled) == 0)
{
SteamGameServerUGC.DownloadItem(fileId, bHighPriority: true);
}Downloads complete asynchronously through a Steam callback struct - see below.
Steam Callbacks and Call Results
Steam surfaces two async patterns: APICallback`1 fires whenever a matching struct is posted; APICallResult`1 is a one-shot result tied to a SteamAPICall_t from a request method. Both are sealed IDisposable classes constrained to a callback struct (where T : struct) with a private constructor, so Create is the only way to obtain an instance - dispose them, typically in Unload.
private Callback<DownloadItemResult_t>? _downloadResult;
public override void Load(bool hotReload)
{
_downloadResult = Callback<DownloadItemResult_t>.Create(OnDownloadItemResult);
}
public override void Unload()
{
_downloadResult?.Dispose();
}
private void OnDownloadItemResult(DownloadItemResult_t result)
{
if (result.m_eResult != EResult.k_EResultOK) return;
SteamGameServerUGC.GetItemInstallInfo(
result.m_nPublishedFileId, out ulong size, out string folder, 1024, out uint timestamp);
}Callback<T> stays registered until you dispose it, so keep the instance in a field and dispose it in Unload; disposing twice is a no-op, but a disposed instance can't be registered again.
CallResult<T> disposes itself right after its result fires, so keeping the instance in a field is only useful to cancel the pending call early. A disposed instance ignores further Set calls, so each request needs its own CallResult<T> instead of a re-pointed one.
T has to be one of the generated Steam callback structs - it must carry a callback identity, otherwise Callback<T>.Create (through APICallbackIdentities.GetCallbackIdentity) throws.
Registration Members
| Member | Signature | Notes |
|---|---|---|
| APICallback`1.Create | static Callback<T> Create(Action<T> callback) | creates and registers the handler in one step |
| APICallback`1.Dispose | void Dispose() | unregisters the handler |
| APICallResult`1.Create | static CallResult<T> Create(ulong hAPICall, Action<T, bool> callback) | hAPICall is the SteamAPICall_t handle, the callback receives the result struct and ioFailure |
| APICallResult`1.Set | void Set(ulong hAPICall) | re-points an existing instance at another call handle, unregistering the previous one; pass 0 to only unregister, and a no-op once the instance is disposed |
| APICallResult`1.Dispose | void Dispose() | cancels the pending call |
Set is for re-pointing a result that hasn't fired yet - e.g. dropping a pending RequestUserStats call and waiting on a newer handle with the same callback.
Auth Sessions
To validate a client-provided auth ticket manually, use BeginAuthSession/EndAuthSession rather than the deprecated connect/disconnect variants:
var authResult = SteamGameServer.BeginAuthSession(ticket, ticket.Length, steamId);
if (authResult != EBeginAuthSessionResult.k_EBeginAuthSessionResultOK)
{
return;
}
// ... later, once the session ends ...
SteamGameServer.EndAuthSession(steamId);Helpers
- APIInteropHelp -
static void TestIfAvailableGameServer()throwsInvalidOperationExceptionwhen the game server API isn't initialized yet, see Waiting for the Steam API. - APICallbackIdentities -
static int GetCallbackIdentity(Type callbackStruct)takes a callback structTypeand returns its numeric Steam callback ID (int), throwing when the struct carries no identity. Mostly useful for logging which callback fired. - APIPacksize -
Packsize.valueis theconst intpack size (8on Windows,4on other platforms) the generated Steam structs are declared with.
Quick Reference
| Class | Namespace | Purpose |
|---|---|---|
SteamGameServer | SwiftlyS2.Shared.SteamAPI | Identity, auth sessions, metadata, license checks |
SteamGameServerUtils | SwiftlyS2.Shared.SteamAPI | Misc utility queries (app ID, etc.) |
SteamGameServerUGC | SwiftlyS2.Shared.SteamAPI | Workshop item state and download |
SteamGameServerStats | SwiftlyS2.Shared.SteamAPI | Server-managed user stats |
GameServer | SwiftlyS2.Shared.SteamAPI | Process-level handles, identity and VAC status |
CSteamID, AppId_t, PublishedFileId_t | SwiftlyS2.Shared.SteamAPI | Core Steam identifier value types |
Callback<T> / CallResult<T> | SwiftlyS2.Shared.SteamAPI | Async callback / call-result handling |
CallbackIdentities | SwiftlyS2.Shared.SteamAPI | Callback ID lookup for callback structs |
InteropHelp | SwiftlyS2.Shared.SteamAPI | Availability guard for the game server API |
Packsize | SwiftlyS2.Shared.SteamAPI | Pack size used by the generated Steam structs |