123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- #import <UIKit/UIKit.h>
- #import <objc/runtime.h>
- id sdk_load_mapper = nil;
- id sdk_load_root = nil;
- @implementation NSBundle (XenonSDK)
- + (void)load {
-
-
- NSURL* url = [NSBundle.mainBundle.executableURL URLByAppendingPathExtension:@"plist"];
- NSDictionary* mapper = [NSDictionary dictionaryWithContentsOfURL:url];
-
-
- if (mapper == nil) {
- return;
- }
-
-
- sdk_load_mapper = mapper;
-
- if ([mapper[@"*"] isEqual:@"u"] || [mapper[@"*"] isEqual:@"k"]) {
-
- NSString* realBundlePath = NSBundle.mainBundle.bundlePath;
- NSString* linkBundlePath = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSAllDomainsMask, YES) firstObject] stringByAppendingPathComponent:realBundlePath.lastPathComponent];
-
- NSError* error = nil;
- NSFileManager* fileManager = [NSFileManager defaultManager];
-
- [fileManager removeItemAtPath:linkBundlePath error:&error];
- [fileManager createDirectoryAtPath:linkBundlePath withIntermediateDirectories:YES attributes:nil error:&error];
-
-
- [mapper enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, NSDictionary* obj, BOOL * _Nonnull stop) {
- if ([key isEqual:@"*"]) {
- return;
- }
-
- NSString* src = [realBundlePath stringByAppendingPathComponent:obj[@"path"]];
- NSString* dest = [linkBundlePath stringByAppendingPathComponent:key];
-
- BOOL isDirectory = NO;
- NSError* suberror = nil;
-
-
- if (![fileManager fileExistsAtPath:src isDirectory:&isDirectory]) {
- return ;
- }
-
-
- if (isDirectory) {
- [fileManager createDirectoryAtPath:dest withIntermediateDirectories:YES attributes:nil error:&suberror];
- return;
- }
-
-
- if (![fileManager fileExistsAtPath:dest.stringByDeletingLastPathComponent]) {
- [fileManager createDirectoryAtPath:dest.stringByDeletingLastPathComponent withIntermediateDirectories:YES attributes:nil error:&suberror];
- }
-
-
- [fileManager createSymbolicLinkAtPath:dest withDestinationPath:src error:&suberror];
- }];
-
- if ([mapper[@"*"] isEqual:@"k"]) {
- sdk_load_root = linkBundlePath;
- Method m1 = class_getInstanceMethod(self, @selector(URLForResource:withExtension:subdirectory:));
- Method m2 = class_getInstanceMethod(self, @selector(sdk_URLForResource:withExtension:subdirectory:));
- method_exchangeImplementations(m1, m2);
- return;
- }
-
-
- Class tmp = objc_allocateClassPair(NSObject.class, "XCBundle", 0);
- objc_registerClassPair(tmp);
- class_addMethod(objc_getMetaClass("XCBundle"), @selector(bundlePath), imp_implementationWithBlock((id)^{ return linkBundlePath; }), "@:");
-
- return;
- }
-
-
- if ([mapper[@"*"] isEqual:@"c"]) {
- Method m1 = class_getInstanceMethod(self, @selector(URLForResource:withExtension:subdirectory:));
- Method m2 = class_getInstanceMethod(self, @selector(sdk_URLForResource:withExtension:subdirectory:));
- method_exchangeImplementations(m1, m2);
- }
- }
- - (NSURL*)sdk_URLForResource:(NSString*)resource withExtension:(NSString*)extension subdirectory:(NSString*)subdirectory {
-
- NSString* path = resource;
-
- if (subdirectory != nil) {
- path = [subdirectory stringByAppendingPathComponent:path];
- }
- if (extension != nil) {
- path = [path stringByAppendingPathExtension:extension];
- }
- if (path.length != 0 && [path hasPrefix:@"/"]) {
- path = [path substringFromIndex:1];
- }
-
- NSDictionary* descriptor = [sdk_load_mapper objectForKeyedSubscript:path];
-
- if (descriptor != nil) {
- if (sdk_load_root != nil) {
- return [NSURL URLWithString:[sdk_load_root stringByAppendingPathComponent:path]];
- }
- path = [descriptor objectForKeyedSubscript:@"path"];
- if (path != nil) {
- return [self sdk_URLForResource:path withExtension:nil subdirectory:nil];
- }
- }
- return [self sdk_URLForResource:resource withExtension:extension subdirectory:subdirectory];
- }
- @end
|