-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFMNModuleLoader.m
54 lines (47 loc) · 1.56 KB
/
FMNModuleLoader.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
//
// FMNModuleLoader.m
// FMN
//
// Created by Nathaniel Gray on 8/22/06.
// Copyright 2006 __MyCompanyName__. All rights reserved.
//
#import "FMNModuleLoader.h"
@implementation FMNModuleLoader
+ (id) moduleAtPath:(NSString *)path withProtocol:(Protocol *)proto
{
NSBundle *bundle = [NSBundle bundleWithPath:path];
if (![bundle load]) {
NSLog(@"Couldn't load bundle: %@", path);
return nil;
}
Class pc = [bundle principalClass];
if (![pc conformsToProtocol:proto]) {
NSLog(@"Class %@ does not conform to protocol.", pc);
return nil;
}
NSLog(@"Loaded FMN module: %@", pc);
id instance = [pc alloc];
if ([instance respondsToSelector:@selector(initWithBundle:)]) {
return [[instance performSelector:@selector(initWithBundle:) withObject:bundle]
autorelease];
} else
return [[instance init] autorelease];
}
+ (NSArray *) allPluginsOfBundle:(NSBundle *)bundle
withProtocol:(Protocol *)proto
{
NSString *pluginDir = [bundle builtInPlugInsPath];
NSArray *bundlePaths = [NSBundle pathsForResourcesOfType:@"plugin"
inDirectory:pluginDir];
NSEnumerator *e = [bundlePaths objectEnumerator];
NSString *path;
NSMutableArray *modules = [NSMutableArray arrayWithCapacity:2];
while (path = [e nextObject]) {
id module = [FMNModuleLoader moduleAtPath:path withProtocol:proto];
if (module != nil) {
[modules addObject:module];
}
}
return modules;
}
@end