Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions PlayTools/Utils/AKPluginLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,25 @@ class AKInterface {
}

private static func loadPlugin() -> Plugin? {
// 1. Form the plugin's bundle URL
guard let bundleURL = Bundle.main.builtInPlugInsURL?
.appendingPathComponent("AKInterface")
.appendingPathExtension("bundle") else { return nil }
let frameworkBundle = Bundle(for: AKInterface.self)
let candidates: [URL?] = [
frameworkBundle.builtInPlugInsURL?.appendingPathComponent("AKInterface.bundle"),
frameworkBundle.bundleURL.appendingPathComponent("PlugIns/AKInterface.bundle"),
frameworkBundle.bundleURL.appendingPathComponent("Versions/A/PlugIns/AKInterface.bundle"),
Bundle.main.builtInPlugInsURL?
.appendingPathComponent("AKInterface")
.appendingPathExtension("bundle")
]

// 2. Create a bundle instance with the plugin URL
guard let bundle = Bundle(url: bundleURL) else { return nil }

// 3. Load the bundle and our plugin class
guard let pluginClass = bundle.principalClass as? Plugin.Type else { return nil }

// 4. Create an instance of the plugin class
return pluginClass.init()
for candidate in candidates {
guard let bundleURL = candidate,
FileManager.default.fileExists(atPath: bundleURL.path),
let bundle = Bundle(url: bundleURL),
let pluginClass = bundle.principalClass as? Plugin.Type else {
continue
}
return pluginClass.init()
}
return nil
}
}