-
Notifications
You must be signed in to change notification settings - Fork 3
/
Loader.m
103 lines (78 loc) · 2.93 KB
/
Loader.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
#import <Modulous/Loader.h>
#import <Modulous/Module.h>
#import <dlfcn.h>
@implementation ModulousLoader
@synthesize _modules;
+ (instancetype)loaderWithURL:(NSURL *)url {
ModulousLoader* loader = [self new];
[loader _loadBundlesFromURL:url];
return loader;
}
+ (instancetype)loaderWithPath:(NSString *)path {
NSURL* file_url = [NSURL fileURLWithPath:path isDirectory:YES];
return [self loaderWithURL:file_url];
}
- (void)_loadBundlesFromURL:(NSURL *)url {
NSArray<NSURL *>* bundle_urls = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:url includingPropertiesForKeys:@[] options:0 error:nil];
if(bundle_urls) {
NSMutableDictionary<NSString *, ModulousModule *>* modules = [NSMutableDictionary new];
for(NSURL* bundle_url in bundle_urls) {
ModulousModule* module = [ModulousModule bundleWithURL:bundle_url];
if(module && [module bundleIdentifier]) {
if([modules objectForKey:[module bundleIdentifier]]) {
NSLog(@"[ModulousLoader] warning: skipping duplicate bundle identifier %@", [module bundleIdentifier]);
continue;
}
// if(!dlopen_preflight([[module executablePath] fileSystemRepresentation])) {
// NSLog(@"[ModulousLoader] warning: dlopen_preflight failed on bundle identifier %@", [module bundleIdentifier]);
// continue;
// }
[modules setObject:module forKey:[module bundleIdentifier]];
}
}
_modules = [modules copy];
}
}
- (NSArray<NSDictionary *> *)getModuleInfo {
NSMutableArray<NSDictionary *>* infos = [NSMutableArray new];
[_modules enumerateKeysAndObjectsUsingBlock:^(NSString* key, ModulousModule* module, BOOL* stop) {
NSDictionary* info = [module infoDictionary];
if(info) {
[infos addObject:info];
}
}];
return [infos copy];
}
- (NSArray<NSDictionary *> *)getModuleInfoWithIdentifers:(NSArray<NSString *> *)identifiers {
NSMutableArray<NSDictionary *>* infos = [NSMutableArray new];
for(NSString* identifier in identifiers) {
ModulousModule* module = [_modules objectForKey:identifier];
if(module) {
NSDictionary* info = [module infoDictionary];
if(info) {
[infos addObject:info];
}
}
}
return [infos copy];
}
- (void)loadModules {
[_modules enumerateKeysAndObjectsUsingBlock:^(NSString* key, ModulousModule* module, BOOL* stop) {
[module loadModule];
}];
}
- (void)loadModulesWithIdentifiers:(NSArray<NSString *> *)identifiers {
for(NSString* identifier in identifiers) {
ModulousModule* module = [_modules objectForKey:identifier];
if(module) {
[module loadModule];
}
}
}
- (instancetype)init {
if((self = [super init])) {
_modules = @{};
}
return self;
}
@end