이 페이지는 아직 한국어로 번역되지 않아 영문 원문을 표시하고 있습니다.
멀티플레이 및 모딩steamworks
import "github.com/nsgo-dev/game/pkg/online/steamworks"
steamworks owns the one Steamworks client a process may have. It loads the Steamworks shared library through purego (no cgo or hand-written C), initializes Steamworks once, pumps Steam's callbacks once per display frame on the engine goroutine, and shuts Steamworks down when the last scope that opened it is disposed. presence.Steam and modding.SteamWorkshop attach to the same client instead of initializing Steamworks themselves. Achievements, stats and leaderboards go through steam, whose Driver can be implemented on this client.
Opening the client
client, err := steamworks.Open(cx, steamworks.Options{
AppID: 480, // your Steamworks App ID
RestartIfNecessary: true,
})
switch {
case errors.Is(err, steamworks.ErrRestartRequired):
os.Exit(0) // Steam is relaunching the game through the Steam client
case errors.Is(err, steamworks.ErrUnavailable):
// No library, no running Steam client, or a target without Steam: play without Steam features.
case err != nil:
return err
}
Openloadslibsteam_api.dylib(beside the executable or inContents/Frameworks),libsteam_api.so(beside the executable or inlib64) orsteam_api64.dll(beside the executable), or the file inOptions.LibraryPath. Bundle the library withnsgo-natives.yamlas Shipping on Steam describes, and setsteam.app_idinnsgo.yamlfor runs outside the Steam client.- With
RestartIfNecessaryand a non-zeroAppID,OpencallsSteamAPI_RestartAppIfNecessaryfirst and returnsErrRestartRequiredwhen Steam relaunches the game. - Initialization goes through
SteamAPI_InitFlat. A failure returns an*InitErrorcarrying theESteamAPIInitResult(InitNoSteamClient,InitVersionMismatch, ...) and Steam's message; it matchesErrUnavailable. - A second
Openfrom another scope returns the same*Clientwithout initializing again; it refuses a different non-zeroAppID(ErrAppIDMismatch) or a differentOptions.Driver(ErrDriverConflict).Currentreturns the open client, or nil. - Callbacks run through
SteamAPI_ManualDispatch_*once per display frame. Every event handler below runs inside that frame, on the engine goroutine. When the scope that owns the pump is disposed, the pump moves to another attached scope; when the last one is disposed,SteamAPI_Shutdownruns, pending waits fail withErrClosedand the client's methods return zero values. - On web, iOS, Android and other targets without Steam,
OpenreturnsErrUnavailableunlessOptions.Driversupplies a driver.
Events
| Method | Steamworks callback | Event |
|---|---|---|
OnOverlay |
GameOverlayActivated_t |
OverlayEvent{Active, UserInitiated, AppID}; OverlayActive reports the last state |
OnJoin |
GameRichPresenceJoinRequested_t, or the +connect launch argument |
JoinEvent{FriendID, Connect, FromLaunch} |
OnLobbyJoin |
GameLobbyJoinRequested_t, or the +connect_lobby launch argument |
LobbyJoinEvent{LobbyID, FriendID, FromLaunch} |
OnShutdown |
SteamShutdown_t |
ShutdownEvent{} |
OnGamepadTextInput |
GamepadTextInputDismissed_t |
GamepadTextInputEvent{Submitted, Text, AppID} after ShowGamepadTextInput |
OnCallback |
any callback ID | a copy of the raw callback struct, for packages that decode their own callbacks (Workshop uses it for ItemInstalled_t) |
Each returns a function that unregisters the handler. A join that started the game (+connect or +connect_lobby in os.Args, or in Options.Args) is delivered once, with FromLaunch set, on the first frame after a handler is registered, so a handler registered after Open does not miss it. LaunchConnect returns the captured +connect value.
Pausing while the overlay is open
stop := client.PauseOnOverlay(loop)
defer stop()
PauseOnOverlay pauses a game.Loop while the Steam Overlay is open and resumes it when the overlay closes. A pause that was already set when the overlay opened, such as an open pause menu, stays set; stop unregisters the handler and resumes the loop only if the helper paused it.
Identity, overlay and auth tickets
SteamID,PersonaName,AppID,RunningOnSteamDeckandOverlayEnabledread the signed-in user and session.OpenOverlay(dialog)andOpenOverlayURL(url)open the overlay to a dialog ("friends","achievements", ...) or a web page.SetRichPresenceandClearRichPresencewriteISteamFriendsrich presence;presence.SteamwithSteamConfig.Clientcalls them for you.AuthTicketForWebAPI(ctx, identity)requests a session ticket withGetAuthTicketForWebApiand waits forGetTicketForWebApiResponse_t; sendAuthTicket.Hex()to your server, which checks it with theISteamUserAuth/AuthenticateUserTicketWeb API using the same identity. Cancelingctxcancels the ticket.AwaitCall(ctx, call, callbackID)waits for aSteamAPICall_tresult delivered by the pump;ResolveSymbolandCallbackPacklet packages bind further flat API functions from the same library.
Testing
NewFakeDriver returns an in-process *FakeDriver. Pass it in Options.Driver, queue callbacks with EmitOverlay, EmitJoin, EmitLobbyJoin, EmitShutdown, EmitGamepadTextInput, EmitAuthTicket, CompleteAPICall or EmitCallback, run a display frame, and inspect InitCount, ShutdownCount, RichPresence and the other counters.