-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInterpose.swift
More file actions
113 lines (100 loc) · 3.68 KB
/
Interpose.swift
File metadata and controls
113 lines (100 loc) · 3.68 KB
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
#if !(arch(arm64) || arch(x86_64))
#error("[InterposeKit] This code only supports arm64 and x86_64 architectures.")
#endif
import ObjectiveC
/// Interpose is a modern library to swizzle elegantly in Swift.
public enum Interpose {
// ============================================================================ //
// MARK: Class Hooks
// ============================================================================ //
public static func prepareHook<MethodSignature, HookSignature>(
on `class`: AnyClass,
for selector: Selector,
methodSignature: MethodSignature.Type,
hookSignature: HookSignature.Type,
build: @escaping HookBuilder<MethodSignature, HookSignature>
) throws -> Hook {
try Hook(
target: .class(`class`),
selector: selector,
build: build
)
}
@discardableResult
public static func applyHook<MethodSignature, HookSignature>(
on `class`: AnyClass,
for selector: Selector,
methodSignature: MethodSignature.Type,
hookSignature: HookSignature.Type,
build: @escaping HookBuilder<MethodSignature, HookSignature>
) throws -> Hook {
let hook = try prepareHook(
on: `class`,
for: selector,
methodSignature: methodSignature,
hookSignature: hookSignature,
build: build
)
try hook.apply()
return hook
}
// ============================================================================ //
// MARK: Object Hooks
// ============================================================================ //
public static func prepareHook<MethodSignature, HookSignature>(
on object: NSObject,
for selector: Selector,
methodSignature: MethodSignature.Type,
hookSignature: HookSignature.Type,
build: @escaping HookBuilder<MethodSignature, HookSignature>
) throws -> Hook {
try Hook(
target: .object(object),
selector: selector,
build: build
)
}
@discardableResult
public static func applyHook<MethodSignature, HookSignature>(
on object: NSObject,
for selector: Selector,
methodSignature: MethodSignature.Type,
hookSignature: HookSignature.Type,
build: @escaping HookBuilder<MethodSignature, HookSignature>
) throws -> Hook {
let hook = try prepareHook(
on: object,
for: selector,
methodSignature: methodSignature,
hookSignature: hookSignature,
build: build
)
try hook.apply()
return hook
}
// ============================================================================ //
// MARK: Logging
// ============================================================================ //
/// The flag that enables logging of InterposeKit internal operations to standard output
/// using the `print(…)` function. Defaults to `false`.
///
/// It is recommended to set this flag only once early in your application lifecycle,
/// e.g. at app startup or in test setup.
#if compiler(>=5.10)
public nonisolated(unsafe) static var isLoggingEnabled = false
#else
public static var isLoggingEnabled = false
#endif
internal nonisolated static func log(
_ message: @autoclosure () -> String
) {
if self.isLoggingEnabled {
print("[InterposeKit] \(message())")
}
}
internal nonisolated static func fail(
_ message: @autoclosure () -> String
) -> Never {
fatalError("[InterposeKit] \(message())")
}
}