From 31e0d041976a37266864e4ecaf5bb6982babeb76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AF=BB=E5=B3=B0?= Date: Tue, 14 Mar 2017 19:10:37 +0800 Subject: [PATCH 1/2] support dynamic lib --- BeeHive/BHAnnotation.h | 11 +- BeeHive/BHAnnotation.m | 109 +++++++++++++------- BeeHive/BHModuleManager.m | 17 --- BeeHive/BHServiceManager.m | 24 ----- BeeHive/BeeHive.m | 4 - Example/BeeHive/BHUserTrackViewController.m | 2 +- Example/BeeHive/BHViewController.m | 2 +- Example/BeeHive/ShopModule.m | 2 +- Example/BeeHive/TestAppDelegate.m | 3 + Example/Podfile | 6 +- 10 files changed, 83 insertions(+), 97 deletions(-) diff --git a/BeeHive/BHAnnotation.h b/BeeHive/BHAnnotation.h index 413c888..b8ec89e 100644 --- a/BeeHive/BHAnnotation.h +++ b/BeeHive/BHAnnotation.h @@ -7,6 +7,7 @@ // #import +#import "BeeHive.h" #ifndef BeehiveModSectName @@ -26,14 +27,8 @@ #define BeeHiveMod(name) \ -char * k##name##_mod BeeHiveDATA(BeehiveMods) = ""#name""; +class BeeHive; char * k##name##_mod BeeHiveDATA(BeehiveMods) = ""#name""; #define BeeHiveService(servicename,impl) \ -char * k##servicename##_service BeeHiveDATA(BeehiveServices) = "{ \""#servicename"\" : \""#impl"\"}"; +class BeeHive;char * k##servicename##_service BeeHiveDATA(BeehiveServices) = "{ \""#servicename"\" : \""#impl"\"}"; -@interface BHAnnotation : NSObject - -+ (NSArray *)AnnotationModules; -+ (NSArray *)AnnotationServices; - -@end diff --git a/BeeHive/BHAnnotation.m b/BeeHive/BHAnnotation.m index c6964ca..b94acd9 100644 --- a/BeeHive/BHAnnotation.m +++ b/BeeHive/BHAnnotation.m @@ -13,27 +13,80 @@ #include #import #import -static NSArray* BHReadConfiguration(char *section) +#include + + + + +#ifndef __LP64__ + #define mach_header mach_header + #define section section + #define getsectbynamefromheader getsectbynamefromheader +#else + #define mach_header mach_header_64 + #define section section_64 + #define getsectbynamefromheader getsectbynamefromheader_64 +#endif + +NSArray* BHReadConfiguration(char *sectionName,const struct mach_header *mhp); +static void dyld_callback(const struct mach_header *mhp, intptr_t vmaddr_slide) { - NSMutableArray *configs = [NSMutableArray array]; + NSArray *mods = BHReadConfiguration(BeehiveModSectName, mhp); + for (NSString *modName in mods) { + Class cls; + if (modName) { + cls = NSClassFromString(modName); + + if (cls) { + [[BHModuleManager sharedManager] registerDynamicModule:cls]; + } + } + } - Dl_info info; - dladdr(BHReadConfiguration, &info); -#ifndef __LP64__ - // const struct mach_header *mhp = _dyld_get_image_header(0); // both works as below line - const struct mach_header *mhp = (struct mach_header*)info.dli_fbase; - unsigned long size = 0; - uint32_t *memory = (uint32_t*)getsectiondata(mhp, "__DATA", section, & size); -#else /* defined(__LP64__) */ - const struct mach_header_64 *mhp = (struct mach_header_64*)info.dli_fbase; - unsigned long size = 0; - uint64_t *memory = (uint64_t*)getsectiondata(mhp, "__DATA", section, & size); -#endif /* defined(__LP64__) */ - for(int idx = 0; idx < size/sizeof(void*); ++idx){ + //register services + NSArray *services = BHReadConfiguration(BeehiveServiceSectName,mhp); + for (NSString *map in services) { + NSData *jsonData = [map dataUsingEncoding:NSUTF8StringEncoding]; + NSError *error = nil; + id json = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error]; + if (!error) { + if ([json isKindOfClass:[NSDictionary class]] && [json allKeys].count) { + + NSString *protocol = [json allKeys][0]; + NSString *clsName = [json allValues][0]; + + if (protocol && clsName) { + [[BHServiceManager sharedManager] registerService:NSProtocolFromString(protocol) implClass:NSClassFromString(clsName)]; + } + + } + } + } + + +} +__attribute__((constructor)) +void initProphet() { + _dyld_register_func_for_add_image(dyld_callback); +} + +//const struct macho_header *mhp = NULL; +NSArray* BHReadConfiguration(char *sectionName,const struct mach_header *mhp) +{ + + NSMutableArray *configs = [NSMutableArray array]; + Dl_info info; + if (mhp == NULL) { + dladdr(BHReadConfiguration, &info); + mhp = (struct mach_header*)info.dli_fbase; + } + unsigned long size = 0; + uintptr_t *memory = (uintptr_t*)getsectiondata(mhp, SEG_DATA, sectionName, &size); + unsigned long counter = size/sizeof(void*); + for(int idx = 0; idx < counter; ++idx){ char *string = (char*)memory[idx]; - NSString *str = [NSString stringWithUTF8String:string]; if(!str)continue; @@ -42,28 +95,6 @@ } return configs; - -} -@implementation BHAnnotation - -+ (NSArray *)AnnotationModules -{ - static NSArray *mods = nil; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - mods = BHReadConfiguration(BeehiveModSectName); - }); - return mods; -} -+ (NSArray *)AnnotationServices -{ - static NSArray *services = nil; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - services = BHReadConfiguration(BeehiveServiceSectName); - }); - return services; + } - -@end diff --git a/BeeHive/BHModuleManager.m b/BeeHive/BHModuleManager.m index 55e5d23..5c9b971 100644 --- a/BeeHive/BHModuleManager.m +++ b/BeeHive/BHModuleManager.m @@ -118,23 +118,6 @@ - (void)registedAllModules } -- (void)registedAnnotationModules -{ - - NSArray*mods = [BHAnnotation AnnotationModules]; - for (NSString *modName in mods) { - Class cls; - if (modName) { - cls = NSClassFromString(modName); - - if (cls) { - [self registerDynamicModule:cls]; - } - } - } -} - - - (void)tiggerEvent:(BHModuleEventType)eventType { switch (eventType) { diff --git a/BeeHive/BHServiceManager.m b/BeeHive/BHServiceManager.m index c2a1986..4395edb 100644 --- a/BeeHive/BHServiceManager.m +++ b/BeeHive/BHServiceManager.m @@ -54,30 +54,6 @@ - (void)registerLocalServices [self.lock unlock]; } -- (void)registerAnnotationServices -{ - NSArray*services = [BHAnnotation AnnotationServices]; - - for (NSString *map in services) { - NSData *jsonData = [map dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error = nil; - id json = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error]; - if (!error) { - if ([json isKindOfClass:[NSDictionary class]] && [json allKeys].count) { - - NSString *protocol = [json allKeys][0]; - NSString *clsName = [json allValues][0]; - - if (protocol && clsName) { - [self registerService:NSProtocolFromString(protocol) implClass:NSClassFromString(clsName)]; - } - - } - } - } - -} - - (void)registerService:(Protocol *)service implClass:(Class)implClass { NSParameterAssert(service != nil); diff --git a/BeeHive/BeeHive.m b/BeeHive/BeeHive.m index 06dfd09..24a4316 100644 --- a/BeeHive/BeeHive.m +++ b/BeeHive/BeeHive.m @@ -68,8 +68,6 @@ - (void)loadStaticModules [[BHModuleManager sharedManager] loadLocalModules]; - [[BHModuleManager sharedManager] registedAnnotationModules]; - [[BHModuleManager sharedManager] registedAllModules]; } @@ -82,8 +80,6 @@ -(void)loadStaticServices [[BHServiceManager sharedManager] registerLocalServices]; - [[BHServiceManager sharedManager] registerAnnotationServices]; - } @end diff --git a/Example/BeeHive/BHUserTrackViewController.m b/Example/BeeHive/BHUserTrackViewController.m index 7e9564a..1d3281f 100644 --- a/Example/BeeHive/BHUserTrackViewController.m +++ b/Example/BeeHive/BHUserTrackViewController.m @@ -11,7 +11,7 @@ #import "BeeHive.h" #import "BHService.h" - +@BeeHiveService(UserTrackServiceProtocol,BHUserTrackViewController) @interface BHUserTrackViewController() diff --git a/Example/BeeHive/BHViewController.m b/Example/BeeHive/BHViewController.m index f8ef525..64f418d 100644 --- a/Example/BeeHive/BHViewController.m +++ b/Example/BeeHive/BHViewController.m @@ -11,7 +11,7 @@ #import "BeeHive.h" #import "BHService.h" -BeeHiveService(HomeServiceProtocol,BHViewController) +@BeeHiveService(HomeServiceProtocol,BHViewController) @interface BHViewController () @property(nonatomic,strong) NSMutableArray *registerViewControllers; diff --git a/Example/BeeHive/ShopModule.m b/Example/BeeHive/ShopModule.m index 50dbb70..4841c37 100644 --- a/Example/BeeHive/ShopModule.m +++ b/Example/BeeHive/ShopModule.m @@ -8,7 +8,7 @@ #import "ShopModule.h" #import "BeeHive.h" -BeeHiveMod(ShopModule) +@BeeHiveMod(ShopModule) @interface ShopModule() @end diff --git a/Example/BeeHive/TestAppDelegate.m b/Example/BeeHive/TestAppDelegate.m index b138c22..d382202 100644 --- a/Example/BeeHive/TestAppDelegate.m +++ b/Example/BeeHive/TestAppDelegate.m @@ -10,6 +10,9 @@ #import "BeeHive.h" #import "BHService.h" #import "BHTimeProfiler.h" +#import +#import "BHModuleManager.h" +#import "BHServiceManager.h" @interface TestAppDelegate () diff --git a/Example/Podfile b/Example/Podfile index 492029f..9e94b79 100644 --- a/Example/Podfile +++ b/Example/Podfile @@ -1,11 +1,13 @@ -source 'https://github.com/CocoaPods/Specs.git' +#source 'https://github.com/CocoaPods/Specs.git' -platform :ios, '7.0' +platform :ios, ‘8.0’ +use_frameworks! def pods pod "BeeHive", :path => "../" end + target 'BeeHive_Example' do pods end From c208a3d0d9f9ec1f9dccde3f0be3c701f9a07fe9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AF=BB=E5=B3=B0?= Date: Tue, 14 Mar 2017 19:12:00 +0800 Subject: [PATCH 2/2] do not need dladdr --- BeeHive/BHAnnotation.m | 5 ----- 1 file changed, 5 deletions(-) diff --git a/BeeHive/BHAnnotation.m b/BeeHive/BHAnnotation.m index b94acd9..8f7b4cc 100644 --- a/BeeHive/BHAnnotation.m +++ b/BeeHive/BHAnnotation.m @@ -72,16 +72,11 @@ void initProphet() { _dyld_register_func_for_add_image(dyld_callback); } -//const struct macho_header *mhp = NULL; NSArray* BHReadConfiguration(char *sectionName,const struct mach_header *mhp) { NSMutableArray *configs = [NSMutableArray array]; Dl_info info; - if (mhp == NULL) { - dladdr(BHReadConfiguration, &info); - mhp = (struct mach_header*)info.dli_fbase; - } unsigned long size = 0; uintptr_t *memory = (uintptr_t*)getsectiondata(mhp, SEG_DATA, sectionName, &size); unsigned long counter = size/sizeof(void*);