From ff3b52e973d8fc5958fc9de2016b335de6049ad0 Mon Sep 17 00:00:00 2001 From: Finn Voorhees Date: Sat, 31 Jan 2026 14:57:09 +0000 Subject: [PATCH] Remove @PlaydateMain macro and SwiftSyntax dependency --- .../Sources/FlappySwift/Game.swift | 1 - .../Sources/FlappySwift/entry.swift | 18 ++++++ Examples/Pong/Sources/Pong/Game.swift | 1 - Examples/Pong/Sources/Pong/entry.swift | 18 ++++++ Examples/Xkpd/Sources/Xkpd/Game.swift | 1 - Examples/Xkpd/Sources/Xkpd/entry.swift | 18 ++++++ Package.resolved | 15 ----- Package.swift | 13 +---- README.md | 1 - Sources/PlaydateKit/PlaydateMain.swift | 7 --- .../PlaydateKitMacros/PlaydateMainMacro.swift | 58 ------------------- 11 files changed, 55 insertions(+), 96 deletions(-) create mode 100644 Examples/FlappySwift/Sources/FlappySwift/entry.swift create mode 100644 Examples/Pong/Sources/Pong/entry.swift create mode 100644 Examples/Xkpd/Sources/Xkpd/entry.swift delete mode 100644 Package.resolved delete mode 100644 Sources/PlaydateKit/PlaydateMain.swift delete mode 100644 Sources/PlaydateKitMacros/PlaydateMainMacro.swift diff --git a/Examples/FlappySwift/Sources/FlappySwift/Game.swift b/Examples/FlappySwift/Sources/FlappySwift/Game.swift index 5f612190..3827197a 100644 --- a/Examples/FlappySwift/Sources/FlappySwift/Game.swift +++ b/Examples/FlappySwift/Sources/FlappySwift/Game.swift @@ -6,7 +6,6 @@ final class TestScene: Scene {} // MARK: - Game -@PlaydateMain final class Game: PlaydateGame { var highScore = 0 diff --git a/Examples/FlappySwift/Sources/FlappySwift/entry.swift b/Examples/FlappySwift/Sources/FlappySwift/entry.swift new file mode 100644 index 00000000..3e040f02 --- /dev/null +++ b/Examples/FlappySwift/Sources/FlappySwift/entry.swift @@ -0,0 +1,18 @@ +import PlaydateKit + +/// Boilerplate entry code +nonisolated(unsafe) var game: Game! +@_cdecl("eventHandler") func eventHandler( + pointer: UnsafeMutablePointer!, + event: System.Event, + arg _: CUnsignedInt +) -> CInt { + switch event { + case .initialize: + Playdate.initialize(with: pointer) + game = Game() + System.updateCallback = game.update + default: game.handle(event) + } + return 0 +} diff --git a/Examples/Pong/Sources/Pong/Game.swift b/Examples/Pong/Sources/Pong/Game.swift index 5942100a..125c77a8 100644 --- a/Examples/Pong/Sources/Pong/Game.swift +++ b/Examples/Pong/Sources/Pong/Game.swift @@ -2,7 +2,6 @@ import PlaydateKit // MARK: - Game -@PlaydateMain final class Game: PlaydateGame { // MARK: Lifecycle diff --git a/Examples/Pong/Sources/Pong/entry.swift b/Examples/Pong/Sources/Pong/entry.swift new file mode 100644 index 00000000..3e040f02 --- /dev/null +++ b/Examples/Pong/Sources/Pong/entry.swift @@ -0,0 +1,18 @@ +import PlaydateKit + +/// Boilerplate entry code +nonisolated(unsafe) var game: Game! +@_cdecl("eventHandler") func eventHandler( + pointer: UnsafeMutablePointer!, + event: System.Event, + arg _: CUnsignedInt +) -> CInt { + switch event { + case .initialize: + Playdate.initialize(with: pointer) + game = Game() + System.updateCallback = game.update + default: game.handle(event) + } + return 0 +} diff --git a/Examples/Xkpd/Sources/Xkpd/Game.swift b/Examples/Xkpd/Sources/Xkpd/Game.swift index 803967ad..f60b8726 100644 --- a/Examples/Xkpd/Sources/Xkpd/Game.swift +++ b/Examples/Xkpd/Sources/Xkpd/Game.swift @@ -2,7 +2,6 @@ import PlaydateKit // MARK: - Game -@PlaydateMain final class Game: PlaydateGame { // MARK: Lifecycle diff --git a/Examples/Xkpd/Sources/Xkpd/entry.swift b/Examples/Xkpd/Sources/Xkpd/entry.swift new file mode 100644 index 00000000..f67bb411 --- /dev/null +++ b/Examples/Xkpd/Sources/Xkpd/entry.swift @@ -0,0 +1,18 @@ +import PlaydateKit + +/// Boilerplate entry code +nonisolated(unsafe) var game: Game! +@_cdecl("eventHandler") func eventHandler( + pointer: UnsafeMutablePointer!, + event: System.Event, + arg _: CUnsignedInt +) -> CInt { + switch event { + case .initialize: + Playdate.initialize(with: pointer) + game = Game.instance + System.updateCallback = game.update + default: game.handle(event) + } + return 0 +} diff --git a/Package.resolved b/Package.resolved deleted file mode 100644 index 3c97fdc6..00000000 --- a/Package.resolved +++ /dev/null @@ -1,15 +0,0 @@ -{ - "originHash" : "597266a278d9f7f988977f2dc78fe11a76884419c533b6a6f5ab7430b1b6851c", - "pins" : [ - { - "identity" : "swift-syntax", - "kind" : "remoteSourceControl", - "location" : "https://github.com/swiftlang/swift-syntax.git", - "state" : { - "revision" : "f99ae8aa18f0cf0d53481901f88a0991dc3bd4a2", - "version" : "601.0.1" - } - } - ], - "version" : 3 -} diff --git a/Package.swift b/Package.swift index a11e8f8b..97f11cde 100644 --- a/Package.swift +++ b/Package.swift @@ -1,7 +1,6 @@ // swift-tools-version: 6.1 import PackageDescription -import CompilerPluginSupport let armToolchainPath: String = if let path = Context.environment["ARM_NONE_EABI_GCC_PATH"] { path @@ -38,20 +37,10 @@ let package = Package( .plugin(name: "PDCPlugin", targets: ["PDCPlugin"]), .plugin(name: "RenamePlugin", targets: ["RenamePlugin"]) ], - dependencies: [ - .package(url: "https://github.com/swiftlang/swift-syntax.git", from: "601.0.1"), - ], targets: [ - .macro( - name: "PlaydateKitMacros", - dependencies: [ - .product(name: "SwiftSyntaxMacros", package: "swift-syntax"), - .product(name: "SwiftCompilerPlugin", package: "swift-syntax") - ] - ), .target( name: "PlaydateKit", - dependencies: ["CPlaydate", "PlaydateKitMacros"], + dependencies: ["CPlaydate"], swiftSettings: [ .enableExperimentalFeature("Embedded"), .unsafeFlags([ diff --git a/README.md b/README.md index 74ac94b0..e6255909 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,6 @@ Your `PlaydateGame` object manages the game lifecycle, receiving events such as ```swift import PlaydateKit -@PlaydateMain final class Game: PlaydateGame { init() { System.addCheckmarkMenuItem(title: "check me") { isChecked in diff --git a/Sources/PlaydateKit/PlaydateMain.swift b/Sources/PlaydateKit/PlaydateMain.swift deleted file mode 100644 index 80ab10f6..00000000 --- a/Sources/PlaydateKit/PlaydateMain.swift +++ /dev/null @@ -1,7 +0,0 @@ -/// Handles the entry point and setup code -/// -/// This macro should only be used exactly once, and the type it's used on -/// should conform to ``PlaydateGame``. A global variable `game` is -/// created to store the game object. -@attached(peer, names: named(game), named(eventHandler)) -public macro PlaydateMain() = #externalMacro(module: "PlaydateKitMacros", type: "PlaydateMainMacro") diff --git a/Sources/PlaydateKitMacros/PlaydateMainMacro.swift b/Sources/PlaydateKitMacros/PlaydateMainMacro.swift deleted file mode 100644 index 07d1fc51..00000000 --- a/Sources/PlaydateKitMacros/PlaydateMainMacro.swift +++ /dev/null @@ -1,58 +0,0 @@ -import SwiftCompilerPlugin -import SwiftSyntax -import SwiftSyntaxMacros - -struct InvalidDeclaration: Error, CustomStringConvertible { - var description: String { - "@PlaydateMain should only be used on a struct, class, or enum declaration" - } -} - -struct PlaydateMainMacro: PeerMacro { - static func expansion( - of attribute: AttributeSyntax, - providingPeersOf declaration: some DeclSyntaxProtocol, - in context: some MacroExpansionContext - ) throws -> [DeclSyntax] { - let gameType: TokenSyntax - if let classDecl = declaration.as(ClassDeclSyntax.self) { - gameType = classDecl.name.trimmed - } else if let structDecl = declaration.as(StructDeclSyntax.self) { - gameType = structDecl.name.trimmed - } else if let enumDecl = declaration.as(EnumDeclSyntax.self) { - gameType = enumDecl.name.trimmed - } else { - throw InvalidDeclaration() - } - - return [ - "nonisolated(unsafe) var game: \(gameType)!", - """ - @_cdecl("eventHandler") - func eventHandler( - pointer: UnsafeMutablePointer, - event: System.Event, - arg _: CUnsignedInt - ) -> CInt { - switch event { - case .initialize: - Playdate.initialize(with: pointer) - game = \(gameType)() - System.updateCallback = game.update - default: - game.handle(event) - } - - return 0 - } - """ - ] - } -} - -@main -struct PlaydateKitPlugin: CompilerPlugin { - let providingMacros: [Macro.Type] = [ - PlaydateMainMacro.self - ] -}