Skip to content

Commit

Permalink
tidied up api
Browse files Browse the repository at this point in the history
  • Loading branch information
samdeane committed Jan 25, 2025
1 parent 73e9de8 commit f107505
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 32 deletions.
56 changes: 30 additions & 26 deletions Sources/SwiftGodot/Core/Wrapped+Testing.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}
Expand All @@ -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.
"""
)
}

12 changes: 7 additions & 5 deletions Sources/SwiftGodot/EntryPoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftGodotTestability/GodotRuntime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit f107505

Please sign in to comment.