Skip to content

Commit

Permalink
Updates for Swift 6 / safe sendability
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfairh committed Jul 9, 2024
1 parent 2b38814 commit 6313962
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 42 deletions.
33 changes: 33 additions & 0 deletions .swiftpm/configuration/Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"originHash" : "3cc2a485937c52ec78310cc6e35ad84d961e1806b86b6b80241d994f379c382f",
"pins" : [
{
"identity" : "steamworks-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/johnfairh/steamworks-swift",
"state" : {
"revision" : "411f4848da65afb1b267f9472d44ac9fca7fb208",
"version" : "0.5.4"
}
},
{
"identity" : "swift-log",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-log.git",
"state" : {
"revision" : "9cb486020ebf03bfa5b5df985387a14a98744537",
"version" : "1.6.1"
}
},
{
"identity" : "tmlengines",
"kind" : "remoteSourceControl",
"location" : "https://github.com/johnfairh/TMLEngines",
"state" : {
"revision" : "de8ce9a42bd55394daf2d37b2c5767a0d0725e8c",
"version" : "1.3.4"
}
}
],
"version" : 3
}
6 changes: 3 additions & 3 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"originHash" : "a33c95d3128da4c89d3ec83b75e5b1df79f39a5b510fbf752156fc487d86a2fb",
"originHash" : "3cc2a485937c52ec78310cc6e35ad84d961e1806b86b6b80241d994f379c382f",
"pins" : [
{
"identity" : "steamworks-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/johnfairh/steamworks-swift",
"state" : {
"branch" : "main",
"revision" : "b8555dc8e017f9c2f48a8de78fae0c8efc47d28f"
"revision" : "411f4848da65afb1b267f9472d44ac9fca7fb208",
"version" : "0.5.4"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/johnfairh/steamworks-swift",
branch: "main"),
from: "0.5.4"),
.package(url: "https://github.com/johnfairh/TMLEngines",
from: "1.3.4")
],
Expand Down
6 changes: 4 additions & 2 deletions Sources/SpaceWar/SpaceWarClientConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ final class SpaceWarClientConnection {
self.connectionStartTime = 0
self.lastNetworkDataReceivedTime = 0

steam.onSteamNetConnectionStatusChangedCallback { [weak self] in
self?.onSteamNetConnectionStatusChanged(msg: $0)
Task { [weak self] in
for await msg in steam.steamNetConnectionStatusChangedCallback {
self?.onSteamNetConnectionStatusChanged(msg: msg)
}
}
}

Expand Down
62 changes: 36 additions & 26 deletions Sources/SpaceWar/SpaceWarMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,38 +128,48 @@ final class SpaceWarMain {

/// Connect to general Steam notifications, roughly all lifecycle-related
private func initSteamNotifications() {
steam.onIPCFailure { [weak self] msg in
// Some awful O/S or library error
self?.forceQuit(reason: "Steam IPC Failure (\(msg.failureType))")
Task { [weak self, steam] in
for await msg in steam.ipcFailure {
// Some awful O/S or library error
self?.forceQuit(reason: "Steam IPC Failure (\(msg.failureType))")
}
}

steam.onSteamShutdown { [weak self] _ in
// Steam shutdown request due to a user in a second concurrent session
// requesting to play this game
self?.forceQuit(reason: "Steam Shutdown")
Task { [weak self, steam] in
for await _ in steam.steamShutdown {
// Steam shutdown request due to a user in a second concurrent session
// requesting to play this game
self?.forceQuit(reason: "Steam Shutdown")
}
}

steam.onSteamServersDisconnected { [weak self] _ in
// Notification that we've been disconnected from Steam
guard let self else {
return
Task { [weak self, steam] in
for await _ in steam.steamServersDisconnected {
// Notification that we've been disconnected from Steam
guard let self else {
break
}
self.gameState.set(.connectingToSteam)
OutputDebugString("Got SteamServersDisconnected_t")
}
self.gameState.set(.connectingToSteam)
OutputDebugString("Got SteamServersDisconnected_t")
}

steam.onSteamServersConnected { [weak self] _ in
// Notification that we are reconnected to Steam
if let self, self.steam.user.loggedOn() {
self.gameState.set(.mainMenu)
} else {
OutputDebugString("Got SteamServersConnected, but not logged on?")
Task { [weak self, steam] in
for await _ in steam.steamServersConnected {
// Notification that we are reconnected to Steam
if let self, self.steam.user.loggedOn() {
self.gameState.set(.mainMenu)
} else {
OutputDebugString("Got SteamServersConnected, but not logged on?")
}
}
}

steam.onDurationControl { [weak self] msg in
// Notification that a Steam China duration control event has happened
self?.onDurationControl(msg: msg)
Task { [weak self, steam] in
for await msg in steam.durationControl {
// Notification that a Steam China duration control event has happened
self?.onDurationControl(msg: msg)
}
}
}

Expand Down Expand Up @@ -209,15 +219,15 @@ final class SpaceWarMain {
// 'join game' on a friend in their friends list
OutputDebugString("RichPresenceJoinRequested: \(msg.connect)")
if let self, let params = CmdLineParams(launchString: msg.connect) {
self.execCommandLineConnect(params: params)
Task { await self.execCommandLineConnect(params: params) }
}
}

steam.onNewUrlLaunchParameters { [weak self] msg in
// a Steam URL to launch this app was executed while the game is
// already running, eg steam://run/480//+connect%20127.0.0.1
if let self, let params = CmdLineParams(steam: self.steam) {
self.execCommandLineConnect(params: params)
Task { await self.execCommandLineConnect(params: params) }
}
}
}
Expand Down Expand Up @@ -270,8 +280,8 @@ final class SpaceWarMain {
func runOccasionally() {
// Update duration control
if steam.utils.isSteamChinaLauncher() {
steam.user.getDurationControl() { [weak self] msg in
if let msg, let self {
Task {
if let msg = await steam.user.getDurationControl() {
self.onDurationControl(msg: msg)
}
}
Expand Down
18 changes: 12 additions & 6 deletions Sources/SpaceWar/SpaceWarServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,23 +162,29 @@ final class SpaceWarServer {
// Tells us when we have successfully connected to Steam
steam.onSteamServersConnected { [weak self] msg in
OutputDebugString("SpaceWarServer connected to Steam successfully")
if let self {
self.isConnectedToSteam = true
self.serverConnection.steamID = self.steam.gameServer.getSteamID()
// log on is not finished until OnPolicyResponse() is called
MainActor.assumeIsolated {
if let self {
self.isConnectedToSteam = true
self.serverConnection.steamID = self.steam.gameServer.getSteamID()
// log on is not finished until OnPolicyResponse() is called
}
}
}

// Tells us when there was a failure to connect to Steam
steam.onSteamServerConnectFailure { [weak self] _ in
OutputDebugString("SpaceWarServer failed to connect to Steam")
self?.isConnectedToSteam = false
MainActor.assumeIsolated {
self?.isConnectedToSteam = false
}
}

// Tells us when we have been logged out of Steam
steam.onSteamServersDisconnected { [weak self] _ in
OutputDebugString("SpaceWarServer got logged out of Steam")
self?.isConnectedToSteam = false
MainActor.assumeIsolated {
self?.isConnectedToSteam = false
}
}

// Tells us that Steam has set our security policy (VAC on or off)
Expand Down
19 changes: 15 additions & 4 deletions Sources/SpaceWar/SpaceWarServerConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ final class SpaceWarServerConnection {
let serverName: String
let listenSocket: HSteamListenSocket?
let pollGroup: HSteamNetPollGroup?
private var netConnectionTask: Task<Void, Never>?
private var authResponseTask: Task<Void, Never>!

/// Server callback to check it's OK to allow a client to start the auth process
var callbackPermitAuth: (ClientToken) -> Bool = { _ in true }
Expand Down Expand Up @@ -61,16 +63,21 @@ final class SpaceWarServerConnection {
listenSocket = steam.networkingSockets.createListenSocketP2P(localVirtualPort: 0, options: [])
pollGroup = steam.networkingSockets.createPollGroup()

steam.onSteamNetConnectionStatusChangedCallback { [weak self] in
self?.onNetConnectionStatusChanged(msg: $0)
netConnectionTask = Task { [weak self] in
for await msg in steam.steamNetConnectionStatusChangedCallback {
self?.onNetConnectionStatusChanged(msg: msg)
}
}
} else {
listenSocket = nil
pollGroup = nil
netConnectionTask = nil
}

steam.onValidateAuthTicketResponse { [weak self] in
self?.onAuthSessionResponse(msg: $0)
authResponseTask = Task { [weak self] in
for await msg in steam.validateAuthTicketResponse {
self?.onAuthSessionResponse(msg: msg)
}
}
}

Expand All @@ -88,12 +95,16 @@ final class SpaceWarServerConnection {

deinit {
OutputDebugString("ServerConnection deinit")
authResponseTask.cancel()

if let listenSocket {
steam.networkingSockets.closeListenSocket(socket: listenSocket)
}
if let pollGroup {
steam.networkingSockets.destroyPollGroup(pollGroup: pollGroup)
}
netConnectionTask?.cancel()

let sID = steamID
if FAKE_NET_USE && sID.isValid {
MainActor.assumeIsolated {
Expand Down

0 comments on commit 6313962

Please sign in to comment.