-
Notifications
You must be signed in to change notification settings - Fork 0
/
SWPluginLoader.m
142 lines (101 loc) · 3.95 KB
/
SWPluginLoader.m
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
//
// SWPlugin.m
// This file is part of the "SWApplicationSupport" project, and is distributed under the MIT License.
//
// Created by Samuel Williams on 3/08/06.
// Copyright 2006 Samuel Williams. All rights reserved.
//
#import "SWPluginLoader.h"
@implementation SWPluginLoader
- (instancetype) initWithBundleType: (NSString*)newBundleType applicationName: (NSString*)appName {
if (self = [super init]) {
bundleType = [newBundleType copy];
applicationName = [appName copy];
}
return self;
}
- (void) setDelegate: (id)newDelegate {
delegate = newDelegate;
}
- (id) delegate {
return delegate;
}
/* This will find all the paths where possible plugins could be located */
- (NSSet*) applicationPluginSearchPaths {
if (!applicationPluginSearchPaths) {
//NSBundle * appBundle = [NSBundle mainBundle];
NSString *appSupportSubpath = [NSString stringWithFormat:@"/Application Support/%@/PlugIns", applicationName];
NSString *builtInPath = [NSBundle mainBundle].builtInPlugInsPath;
NSArray *librarySearchPaths;
NSEnumerator *searchPathEnum;
NSMutableArray *bundleSearchPaths = [NSMutableArray array];
// Find Library directories in all domains except /System
librarySearchPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSAllDomainsMask - NSSystemDomainMask, YES);
// Copy each discovered path into an array after adding
// the Application Support/KillerApp/PlugIns subpath
searchPathEnum = [librarySearchPaths objectEnumerator];
NSString *currPath;
while(currPath = [searchPathEnum nextObject]) {
[bundleSearchPaths addObject:[currPath stringByAppendingPathComponent:appSupportSubpath]];
}
[bundleSearchPaths addObject:builtInPath];
applicationPluginSearchPaths = [NSSet setWithArray:bundleSearchPaths];
}
return applicationPluginSearchPaths;
}
/* This will find paths to all plugins available */
- (NSSet*) pluginPaths {
if (!pluginPaths) {
NSSet *paths = self.applicationPluginSearchPaths;
NSMutableArray *bundlePaths = [NSMutableArray new];
NSRange progress = NSMakeRange(0, paths.count);
NSEnumerator *searchPathEnum = [paths objectEnumerator];
NSString *currPath;
while (currPath = [searchPathEnum nextObject]) {
if (delegate && [delegate respondsToSelector:@selector(pluginLoader:willScan:progress:)])
[delegate pluginLoader:self willScan:currPath progress:progress];
[bundlePaths addObjectsFromArray:[NSBundle pathsForResourcesOfType:bundleType inDirectory:currPath]];
progress.location += 1;
if (delegate && [delegate respondsToSelector:@selector(pluginLoader:didScan:progress:)])
[delegate pluginLoader:self didScan:currPath progress:progress];
}
pluginPaths = [NSSet setWithArray:bundlePaths];
}
return pluginPaths;
}
/* Load all plugins */
- (NSSet*) loadAllPlugins {
if (!loadedPlugins) {
NSSet *bundlePaths = self.pluginPaths;
NSMutableArray *allBundles = [NSMutableArray new];
NSRange progress = NSMakeRange(0, bundlePaths.count);
NSEnumerator *bundlePathEnum = [bundlePaths objectEnumerator];
NSString *currPath;
while (currPath = [bundlePathEnum nextObject]) {
if (delegate && [delegate respondsToSelector:@selector(pluginLoader:willLoad:progress:)])
[delegate pluginLoader:self willLoad:currPath progress:progress];
NSBundle *bundle = [NSBundle bundleWithPath:currPath];
if (bundle == nil) {
NSLog (@"Error loading bundle: %@", currPath);
continue;
}
[allBundles addObject:bundle];
progress.location += 1;
if (delegate && [delegate respondsToSelector:@selector(pluginLoader:didLoad:result:progress:)])
[delegate pluginLoader:self didLoad:currPath result:bundle progress:progress];
}
loadedPlugins = [NSSet setWithArray:allBundles];
}
return loadedPlugins;
}
- (void) invalidateAllPlugins {
applicationPluginSearchPaths = nil;
pluginPaths = nil;
loadedPlugins = nil;
}
- (void) dealloc {
[self invalidateAllPlugins];
bundleType = nil;
applicationName = nil;
}
@end