swiftlys2/docs/api/releases/features/ai/

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();
MemberSignatureReturns
APIGameServer.GetSteamIDstatic CSteamID GetSteamID()the Steam ID of the game server
APIGameServer.GetHSteamPipestatic HSteamPipe GetHSteamPipe()the Steam pipe handle of the game server
APIGameServer.GetHSteamUserstatic HSteamUser GetHSteamUser()the Steam user handle of the game server
APIGameServer.BSecurestatic 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

MemberSignatureNotes
APICallback`1.Createstatic Callback<T> Create(Action<T> callback)creates and registers the handler in one step
APICallback`1.Disposevoid Dispose()unregisters the handler
APICallResult`1.Createstatic 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.Setvoid 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.Disposevoid 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() throws InvalidOperationException when the game server API isn't initialized yet, see Waiting for the Steam API.
  • APICallbackIdentities - static int GetCallbackIdentity(Type callbackStruct) takes a callback struct Type and returns its numeric Steam callback ID (int), throwing when the struct carries no identity. Mostly useful for logging which callback fired.
  • APIPacksize - Packsize.value is the const int pack size (8 on Windows, 4 on other platforms) the generated Steam structs are declared with.

Quick Reference

ClassNamespacePurpose
SteamGameServerSwiftlyS2.Shared.SteamAPIIdentity, auth sessions, metadata, license checks
SteamGameServerUtilsSwiftlyS2.Shared.SteamAPIMisc utility queries (app ID, etc.)
SteamGameServerUGCSwiftlyS2.Shared.SteamAPIWorkshop item state and download
SteamGameServerStatsSwiftlyS2.Shared.SteamAPIServer-managed user stats
GameServerSwiftlyS2.Shared.SteamAPIProcess-level handles, identity and VAC status
CSteamID, AppId_t, PublishedFileId_tSwiftlyS2.Shared.SteamAPICore Steam identifier value types
Callback<T> / CallResult<T>SwiftlyS2.Shared.SteamAPIAsync callback / call-result handling
CallbackIdentitiesSwiftlyS2.Shared.SteamAPICallback ID lookup for callback structs
InteropHelpSwiftlyS2.Shared.SteamAPIAvailability guard for the game server API
PacksizeSwiftlyS2.Shared.SteamAPIPack size used by the generated Steam structs