From f107505d6cd3b5155cf67a7b299d62e7ea7f4ead Mon Sep 17 00:00:00 2001 From: Sam Deane Date: Sat, 25 Jan 2025 14:50:04 +0000 Subject: [PATCH] tidied up api --- Sources/SwiftGodot/Core/Wrapped+Testing.swift | 56 ++++++++++--------- Sources/SwiftGodot/EntryPoint.swift | 12 ++-- .../SwiftGodotTestability/GodotRuntime.swift | 2 +- 3 files changed, 38 insertions(+), 32 deletions(-) diff --git a/Sources/SwiftGodot/Core/Wrapped+Testing.swift b/Sources/SwiftGodot/Core/Wrapped+Testing.swift index c666cc98e..4ecb52d7f 100644 --- a/Sources/SwiftGodot/Core/Wrapped+Testing.swift +++ b/Sources/SwiftGodot/Core/Wrapped+Testing.swift @@ -1,27 +1,32 @@ -public struct Testing { - /// Public API for tracking live objects - public struct LiveObjects { - /// All framework objects - public static var framework: [Wrapped] { Array(liveFrameworkObjects.values) } +public extension Wrapped { + struct Testing { + /// Public API for tracking live objects + public struct LiveObjects { + /// All framework objects + public static var framework: [Wrapped] { Array(liveFrameworkObjects.values) } - /// All user-defined objects - public static var subtyped: [Wrapped] { Array(liveSubtypedObjects.values) } + /// All user-defined objects + public static var subtyped: [Wrapped] { Array(liveSubtypedObjects.values) } - /// All objects - public static var all: [Wrapped] { framework + subtyped } + /// All objects + public static var all: [Wrapped] { framework + subtyped } - /// Reset all existing tracked objects - public static func reset() { - liveFrameworkObjects.removeAll() - liveSubtypedObjects.removeAll() + /// Reset all existing tracked objects + public static func reset() { + liveFrameworkObjects.removeAll() + liveSubtypedObjects.removeAll() + } } - } - - public struct ClassNames { - public static func setDuplicateNameCallback(_ callback: @escaping (_ name: StringName, _ type: Wrapped.Type) -> Void) -> DuplicateNameCallback { - let old = duplicateClassNameDetected - duplicateClassNameDetected = callback - return old + + /// Public API for monitoring class names. + public struct ClassNames { + public typealias DuplicateNameCallback = (StringName, Wrapped.Type) -> Void + /// Set a callback to be called when a duplicate class name is detected. + public static func setDuplicateNameCallback(_ callback: @escaping DuplicateNameCallback) -> DuplicateNameCallback { + let old = duplicateClassNameDetected + duplicateClassNameDetected = callback + return old + } } } } @@ -31,12 +36,11 @@ public struct Testing { internal var liveFrameworkObjects: [UnsafeRawPointer: Wrapped] = [:] internal var liveSubtypedObjects: [UnsafeRawPointer: Wrapped] = [:] -public typealias DuplicateNameCallback = (StringName, Wrapped.Type) -> Void -var duplicateClassNameDetected: DuplicateNameCallback = { name, type in +/// Callback to be called when a duplicate class name is detected. +internal var duplicateClassNameDetected: Wrapped.Testing.ClassNames.DuplicateNameCallback = { name, type in preconditionFailure( - """ - Godot already has a class named \(name), so I cannot register \(type) using that name. This is a fatal error because the only way I can tell whether Godot is handing me a pointer to a class I'm responsible for is by checking the class name. - """ + """ + Godot already has a class named \(name), so I cannot register \(type) using that name. This is a fatal error because the only way I can tell whether Godot is handing me a pointer to a class I'm responsible for is by checking the class name. + """ ) } - diff --git a/Sources/SwiftGodot/EntryPoint.swift b/Sources/SwiftGodot/EntryPoint.swift index f6e5beab0..5d55c342c 100644 --- a/Sources/SwiftGodot/EntryPoint.swift +++ b/Sources/SwiftGodot/EntryPoint.swift @@ -454,13 +454,15 @@ func withArgPointers(_ _args: UnsafeMutableRawPointer?..., body: ([UnsafeRawPoin body(unsafeBitCast(_args, to: [UnsafeRawPointer?].self)) } -public func setExtensionInterfaceOpaque(library libraryPtr: UnsafeMutableRawPointer, getProcAddrFun godotGetProcAddr: Any) { - let interface = LibGodotExtensionInterface(library: libraryPtr, getProcAddrFun: godotGetProcAddr as! GDExtensionInterfaceGetProcAddress) - setExtensionInterface(interface: interface) +public extension ExtensionInterface { + /// Create an interface instance from the raw pointers provided by Godot. + static func rawInterace(library libraryPtr: UnsafeMutableRawPointer, getProcAddrFun godotGetProcAddr: Any) -> ExtensionInterface { + let procAddrTyped = unsafeBitCast(godotGetProcAddr, to: GDExtensionInterfaceGetProcAddress.self) + let libraryTyped = GDExtensionClassLibraryPtr(libraryPtr) + return LibGodotExtensionInterface(library: libraryTyped, getProcAddrFun: procAddrTyped) + } } - - #if os(Windows) typealias RawType = Int32 #else diff --git a/Sources/SwiftGodotTestability/GodotRuntime.swift b/Sources/SwiftGodotTestability/GodotRuntime.swift index f43d56fcb..1a60f6bb8 100644 --- a/Sources/SwiftGodotTestability/GodotRuntime.swift +++ b/Sources/SwiftGodotTestability/GodotRuntime.swift @@ -59,7 +59,7 @@ private extension GodotRuntime { guard let godotGetProcAddr, let libraryPtr else { return 0 } - setExtensionInterfaceOpaque(library: libraryPtr, getProcAddrFun: godotGetProcAddr) + setExtensionInterface(.rawInterface(library: libraryPtr, getProcAddrFun: godotGetProcAddr)) godotLibrary = OpaquePointer (libraryPtr) extensionInit?.pointee = GDExtensionInitialization ( minimum_initialization_level: GDEXTENSION_INITIALIZATION_CORE,