From 6313962105ae545d4c347dc9610a688428cee172 Mon Sep 17 00:00:00 2001 From: John Fairhurst Date: Tue, 9 Jul 2024 17:26:46 +0100 Subject: [PATCH] Updates for Swift 6 / safe sendability --- .swiftpm/configuration/Package.resolved | 33 ++++++++++ Package.resolved | 6 +- Package.swift | 2 +- .../SpaceWar/SpaceWarClientConnection.swift | 6 +- Sources/SpaceWar/SpaceWarMain.swift | 62 +++++++++++-------- Sources/SpaceWar/SpaceWarServer.swift | 18 ++++-- .../SpaceWar/SpaceWarServerConnection.swift | 19 ++++-- 7 files changed, 104 insertions(+), 42 deletions(-) create mode 100644 .swiftpm/configuration/Package.resolved diff --git a/.swiftpm/configuration/Package.resolved b/.swiftpm/configuration/Package.resolved new file mode 100644 index 0000000..5d36eff --- /dev/null +++ b/.swiftpm/configuration/Package.resolved @@ -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 +} diff --git a/Package.resolved b/Package.resolved index 405e650..5d36eff 100644 --- a/Package.resolved +++ b/Package.resolved @@ -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" } }, { diff --git a/Package.swift b/Package.swift index 3d70b6a..9f35506 100644 --- a/Package.swift +++ b/Package.swift @@ -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") ], diff --git a/Sources/SpaceWar/SpaceWarClientConnection.swift b/Sources/SpaceWar/SpaceWarClientConnection.swift index b37968b..ba821bf 100644 --- a/Sources/SpaceWar/SpaceWarClientConnection.swift +++ b/Sources/SpaceWar/SpaceWarClientConnection.swift @@ -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) + } } } diff --git a/Sources/SpaceWar/SpaceWarMain.swift b/Sources/SpaceWar/SpaceWarMain.swift index 7e3d836..4a073b1 100644 --- a/Sources/SpaceWar/SpaceWarMain.swift +++ b/Sources/SpaceWar/SpaceWarMain.swift @@ -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) + } } } @@ -209,7 +219,7 @@ 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) } } } @@ -217,7 +227,7 @@ final class SpaceWarMain { // 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) } } } } @@ -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) } } diff --git a/Sources/SpaceWar/SpaceWarServer.swift b/Sources/SpaceWar/SpaceWarServer.swift index 70207a1..3f34df6 100644 --- a/Sources/SpaceWar/SpaceWarServer.swift +++ b/Sources/SpaceWar/SpaceWarServer.swift @@ -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) diff --git a/Sources/SpaceWar/SpaceWarServerConnection.swift b/Sources/SpaceWar/SpaceWarServerConnection.swift index e2777e8..8aaa959 100644 --- a/Sources/SpaceWar/SpaceWarServerConnection.swift +++ b/Sources/SpaceWar/SpaceWarServerConnection.swift @@ -17,6 +17,8 @@ final class SpaceWarServerConnection { let serverName: String let listenSocket: HSteamListenSocket? let pollGroup: HSteamNetPollGroup? + private var netConnectionTask: Task? + private var authResponseTask: Task! /// Server callback to check it's OK to allow a client to start the auth process var callbackPermitAuth: (ClientToken) -> Bool = { _ in true } @@ -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) + } } } @@ -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 {