Skip to content

Commit 1a0b770

Browse files
committed
refactor: dir enumerator
Signed-off-by: Lessica <82flex@gmail.com>
1 parent a9ec617 commit 1a0b770

1 file changed

Lines changed: 20 additions & 17 deletions

File tree

TrollFools/InjectorV3+Bundle.swift

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ extension InjectorV3 {
5050
let linkedDylibs = try linkedDylibsRecursivelyOfMachO(executableURL)
5151
DDLogInfo("Linked dylibs (\(linkedDylibs.count)): \(linkedDylibs.map { $0.lastPathComponent })", ddlog: logger)
5252

53-
var enumeratedURLs = OrderedSet<URL>()
54-
var allMachOsInFrameworks = OrderedSet<URL>()
55-
53+
var enumeratedMachOs = OrderedSet<URL>()
5654
if let enumerator = FileManager.default.enumerator(
5755
at: frameworksURL,
5856
includingPropertiesForKeys: [.fileSizeKey],
@@ -63,37 +61,40 @@ extension InjectorV3 {
6361
enumerator.skipDescendants()
6462
continue
6563
}
64+
6665
// Skip backup files created before injection
6766
if itemURL.path.hasSuffix(".\(Self.alternateSuffix)") {
6867
continue
6968
}
70-
if enumerator.level == 2 {
71-
enumeratedURLs.append(itemURL)
72-
if isMachO(itemURL) {
73-
allMachOsInFrameworks.append(itemURL)
74-
}
69+
70+
let itemExt = itemURL.pathExtension.lowercased()
71+
if enumerator.level == 2 && (itemExt.isEmpty || itemExt == "dylib") && isMachO(itemURL) {
72+
enumeratedMachOs.append(itemURL)
73+
continue
7574
}
75+
7676
// Scan bare dylibs at level 1 (directly in Frameworks/)
77-
if enumerator.level == 1 && itemURL.pathExtension.lowercased() == "dylib" && isMachO(itemURL) {
78-
allMachOsInFrameworks.append(itemURL)
79-
enumeratedURLs.append(itemURL)
77+
if enumerator.level == 1 && itemExt == "dylib" && isMachO(itemURL) {
78+
enumeratedMachOs.append(itemURL)
79+
continue
8080
}
8181
}
8282
}
8383

84-
DDLogInfo("Enumerated \(enumeratedURLs.count) items, \(allMachOsInFrameworks.count) Mach-Os in Frameworks/", ddlog: logger)
84+
DDLogInfo("Enumerated \(enumeratedMachOs.count) items", ddlog: logger)
8585

86-
var machOs = linkedDylibs.intersection(enumeratedURLs)
86+
var machOs = linkedDylibs.intersection(enumeratedMachOs)
8787
DDLogInfo("Intersection: \(machOs.count) linked Mach-Os in Frameworks/", ddlog: logger)
8888

8989
// Fallback: if none of the Mach-Os in Frameworks/ are statically linked
9090
// by the main binary (e.g. Unity apps use dlopen), use all available Mach-Os.
91-
if machOs.isEmpty && !allMachOsInFrameworks.isEmpty {
91+
if machOs.isEmpty && !enumeratedMachOs.isEmpty {
9292
if useFrameworkEnumerationFallback {
9393
didUseMachOEnumerationFallback = true
94+
9495
var excludedSwiftRuntimeCount = 0
9596
var excludedIgnoredNameCount = 0
96-
let filteredMachOs = allMachOsInFrameworks.filter { url in
97+
let filteredMachOs = enumeratedMachOs.filter { url in
9798
let nameLower = url.lastPathComponent.lowercased()
9899
if nameLower.hasPrefix("libswift") {
99100
excludedSwiftRuntimeCount += 1
@@ -105,11 +106,13 @@ extension InjectorV3 {
105106
}
106107
return true
107108
}
108-
let excludedCount = allMachOsInFrameworks.count - filteredMachOs.count
109+
110+
let excludedCount = enumeratedMachOs.count - filteredMachOs.count
109111
DDLogWarn(
110112
"No statically linked Mach-Os found, falling back to \(filteredMachOs.count) filtered Mach-Os in Frameworks/ (excluded \(excludedCount): \(excludedSwiftRuntimeCount) Swift runtime, \(excludedIgnoredNameCount) ignored by name)",
111113
ddlog: logger
112114
)
115+
113116
machOs = OrderedSet(filteredMachOs)
114117
} else {
115118
DDLogWarn("No statically linked Mach-Os found, fallback is disabled by settings", ddlog: logger)
@@ -119,7 +122,7 @@ extension InjectorV3 {
119122
// Filter out previously-injected Mach-Os by diffing current vs. backup load commands.
120123
// Any load command present in the current binary but absent from its backup was added by injection.
121124
var injectedAssetNames = Set<String>()
122-
for machO in (allMachOsInFrameworks.elements + [executableURL]) where hasAlternate(machO) {
125+
for machO in (enumeratedMachOs.elements + [executableURL]) where hasAlternate(machO) {
123126
if let current = try? loadedDylibsOfMachO(machO),
124127
let original = try? loadedDylibsOfMachO(Self.alternateURL(for: machO))
125128
{

0 commit comments

Comments
 (0)