-
Notifications
You must be signed in to change notification settings - Fork 7
/
Sharing.swift
153 lines (122 loc) · 7.83 KB
/
Sharing.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import Foundation
enum TDKSFOperationEvent: CFIndex {
case unknown = 0
case newOperation
case askUser
case waitForAnswer
case canceled
case started
case preprocess
case progress
case postprocess
case finished
case errorOccurred
case connecting
case information
case conflict
case blocked
}
typealias TDKSFBrowserClientContext = (
version: CFIndex,
info: UnsafeMutableRawPointer?,
retain: CFAllocatorRetainCallBack?,
release: CFAllocatorReleaseCallBack?,
copyDescription: CFAllocatorCopyDescriptionCallBack?
);
typealias TDKSFOperationClientContext = (
version: CFIndex,
info: UnsafeMutableRawPointer?,
retain: CFAllocatorRetainCallBack?,
release: CFAllocatorReleaseCallBack?,
copyDescription: CFAllocatorCopyDescriptionCallBack?
);
typealias TDKSFBrowser = OpaquePointer
typealias TDKSFNode = OpaquePointer
typealias TDKSFOperation = OpaquePointer
private(set) var kTDKSFBrowserKindAirDrop: CFString!
private(set) var kTDKSFOperationKindSender: CFString!
private(set) var kTDKSFOperationFileIconKey: CFString!
private(set) var kTDKSFOperationItemsKey: CFString!
private(set) var kTDKSFOperationNodeKey: CFString!
typealias TDKSFBrowserCreateFunction = @convention(c) (_ alloc: CFAllocator?, _ kind: CFString?) -> TDKSFBrowser
private(set) var TDKSFBrowserCreate: TDKSFBrowserCreateFunction!
typealias TDKSFBrowserCallbackFunction = @convention(c) (_ browser: TDKSFBrowser, _ node: TDKSFNode, _ children: CFArray, _: UnsafeMutableRawPointer?, _: UnsafeMutableRawPointer?, _ context: UnsafeMutableRawPointer?) -> Void
typealias TDKSFBrowserSetClientFunction = @convention(c) (_ browser: TDKSFBrowser, _ callback: TDKSFBrowserCallbackFunction, _ clientContext: UnsafeMutableRawPointer/*<TDKSFBrowserClientContext>*/) -> Void
private(set) var TDKSFBrowserSetClient: TDKSFBrowserSetClientFunction!
typealias TDKSFBrowserSetDispatchQueueFunction = @convention(c) (_ browser: TDKSFBrowser, _ queue: DispatchQueue) -> Void
private(set) var TDKSFBrowserSetDispatchQueue: TDKSFBrowserSetDispatchQueueFunction!
typealias TDKSFBrowserOpenNodeFunction = @convention(c) (_ browser: TDKSFBrowser, _ node: TDKSFNode?, _ protocol: UnsafeMutableRawPointer?, _ flags: CFOptionFlags) -> Void
private(set) var TDKSFBrowserOpenNode: TDKSFBrowserOpenNodeFunction!
typealias TDKSFBrowserCopyChildrenFunction = @convention(c) (_ browser: TDKSFBrowser, _ node: TDKSFNode?) -> CFArray
private(set) var TDKSFBrowserCopyChildren: TDKSFBrowserCopyChildrenFunction!
typealias TDKSFBrowserInvalidateFunction = @convention(c) (_ browser: TDKSFBrowser) -> Void
private(set) var TDKSFBrowserInvalidate: TDKSFBrowserInvalidateFunction!
typealias TDKSFBrowserGetRootNodeFunction = @convention(c) (_ browser: TDKSFBrowser) -> TDKSFNode
private(set) var TDKSFBrowserGetRootNode: TDKSFBrowserGetRootNodeFunction!
typealias TDKSFNodeCopyDisplayNameFunction = @convention(c) (_ node: TDKSFNode) -> CFString?
private(set) var TDKSFNodeCopyDisplayName: TDKSFNodeCopyDisplayNameFunction!
typealias TDKSFNodeCopyComputerNameFunction = @convention(c) (_ node: TDKSFNode) -> CFString?
private(set) var TDKSFNodeCopyComputerName: TDKSFNodeCopyComputerNameFunction!
typealias TDKSFNodeCopySecondaryNameFunction = @convention(c) (_ node: TDKSFNode) -> CFString?
private(set) var TDKSFNodeCopySecondaryName: TDKSFNodeCopySecondaryNameFunction!
typealias TDKSFOperationCreateFunction = @convention(c) (_ alloc: CFAllocator?, _ kind: CFString, _: UnsafeMutableRawPointer?, _: UnsafeMutableRawPointer?) -> TDKSFOperation
private(set) var TDKSFOperationCreate: TDKSFOperationCreateFunction!
typealias TDKSFOperationCallbackFunction = @convention(c) (_ operation: TDKSFOperation, _ event: TDKSFOperationEvent.RawValue, _ results: AnyObject, _ context: UnsafeMutableRawPointer?) -> Void
typealias TDKSFOperationSetClientFunction = @convention(c) (_ operation: TDKSFOperation, _ callback: TDKSFOperationCallbackFunction, _ context: UnsafeMutableRawPointer/*<TDKSFOperationClientContext>*/) -> Void
private(set) var TDKSFOperationSetClient: TDKSFOperationSetClientFunction!
typealias TDKSFOperationSetDispatchQueueFunction = @convention(c) (_ operation: TDKSFOperation, _ queue: DispatchQueue) -> Void
private(set) var TDKSFOperationSetDispatchQueue: TDKSFOperationSetDispatchQueueFunction!
typealias TDKSFOperationCopyPropertyFunction = @convention(c) (_ operation: TDKSFOperation, _ name: CFString) -> AnyObject
private(set) var TDKSFOperationCopyProperty: TDKSFOperationCopyPropertyFunction!
typealias TDKSFOperationSetPropertyFunction = @convention(c) (_ operation: TDKSFOperation, _ name: CFString, _ value: AnyObject) -> Void
private(set) var TDKSFOperationSetProperty: TDKSFOperationSetPropertyFunction!
typealias TDKSFOperationResumeFunction = @convention(c) (_ operation: TDKSFOperation) -> Void
private(set) var TDKSFOperationResume: TDKSFOperationResumeFunction!
typealias TDKSFOperationCancelFunction = @convention(c) (_ operation: TDKSFOperation) -> Void
private(set) var TDKSFOperationCancel: TDKSFOperationCancelFunction!
var TDKIsInitialized = false
func TDKInitialize() {
guard !TDKIsInitialized else { return }
TDKIsInitialized = true
let bundleURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, "/System/Library/PrivateFrameworks/Sharing.framework" as CFString, .cfurlposixPathStyle, false)
let bundle = CFBundleCreate(kCFAllocatorDefault, bundleURL)
assert(CFBundleLoadExecutable(bundle))
let stringSymbolNames = ["kSFBrowserKindAirDrop", "kSFOperationKindSender", "kSFOperationFileIconKey", "kSFOperationItemsKey", "kSFOperationNodeKey"]
var stringSymbolDestinations = Array<UnsafeMutableRawPointer?>(repeating: nil, count: stringSymbolNames.count)
stringSymbolDestinations.withUnsafeMutableBufferPointer { bufferPointer in
CFBundleGetDataPointersForNames(bundle, stringSymbolNames as CFArray, bufferPointer.baseAddress!)
}
func stringCast<T>(_ index: Int) -> T {
return stringSymbolDestinations[index]!.assumingMemoryBound(to: T.self).pointee
}
kTDKSFBrowserKindAirDrop = stringCast(0)
kTDKSFOperationKindSender = stringCast(1)
kTDKSFOperationFileIconKey = stringCast(2)
kTDKSFOperationItemsKey = stringCast(3)
kTDKSFOperationNodeKey = stringCast(4)
let functionSymbolNames = ["SFBrowserCreate", "SFBrowserSetClient", "SFBrowserSetDispatchQueue", "SFBrowserOpenNode", "SFBrowserCopyChildren", "SFBrowserInvalidate", "SFBrowserGetRootNode", "SFNodeCopyDisplayName", "SFNodeCopyComputerName", "SFNodeCopySecondaryName", "SFOperationCreate", "SFOperationSetClient", "SFOperationSetDispatchQueue", "SFOperationCopyProperty", "SFOperationSetProperty", "SFOperationResume", "SFOperationCancel"]
var functionSymbolDestinations = Array<UnsafeMutableRawPointer?>(repeating: nil, count: functionSymbolNames.count)
functionSymbolDestinations.withUnsafeMutableBufferPointer { bufferPointer in
CFBundleGetFunctionPointersForNames(bundle, functionSymbolNames as CFArray, bufferPointer.baseAddress!)
}
func functionCast<T>(_ index: Int) -> T! {
return unsafeBitCast(functionSymbolDestinations[index]!, to: T.self)
}
TDKSFBrowserCreate = functionCast(0)
TDKSFBrowserSetClient = functionCast(1)
TDKSFBrowserSetDispatchQueue = functionCast(2)
TDKSFBrowserOpenNode = functionCast(3)
TDKSFBrowserCopyChildren = functionCast(4)
TDKSFBrowserInvalidate = functionCast(5)
TDKSFBrowserGetRootNode = functionCast(6)
TDKSFNodeCopyDisplayName = functionCast(7)
TDKSFNodeCopyComputerName = functionCast(8)
TDKSFNodeCopySecondaryName = functionCast(9)
TDKSFOperationCreate = functionCast(10)
TDKSFOperationSetClient = functionCast(11)
TDKSFOperationSetDispatchQueue = functionCast(12)
TDKSFOperationCopyProperty = functionCast(13)
TDKSFOperationSetProperty = functionCast(14)
TDKSFOperationResume = functionCast(15)
TDKSFOperationCancel = functionCast(16)
}