diff --git a/BeeHive/BHAnnotation.h b/BeeHive/BHAnnotation.h index c7438ba..205df2f 100644 --- a/BeeHive/BHAnnotation.h +++ b/BeeHive/BHAnnotation.h @@ -8,6 +8,7 @@ #import +#import "BeeHive.h" #ifndef BeehiveModSectName @@ -27,14 +28,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 ae477f8..9b0213e 100644 --- a/BeeHive/BHAnnotation.m +++ b/BeeHive/BHAnnotation.m @@ -15,27 +15,75 @@ #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); +} + +NSArray* BHReadConfiguration(char *sectionName,const struct mach_header *mhp) +{ + + NSMutableArray *configs = [NSMutableArray array]; + Dl_info info; + 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; @@ -44,28 +92,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 a314b39..b5e0d7b 100755 --- a/BeeHive/BHModuleManager.m +++ b/BeeHive/BHModuleManager.m @@ -117,24 +117,8 @@ - (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)triggerEvent:(BHModuleEventType)eventType +- (void)tiggerEvent:(BHModuleEventType)eventType { switch (eventType) { case BHMSetupEvent: diff --git a/BeeHive/BHServiceManager.m b/BeeHive/BHServiceManager.m index 817e8be..7075210 100644 --- a/BeeHive/BHServiceManager.m +++ b/BeeHive/BHServiceManager.m @@ -46,30 +46,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 2695ab4..07db8d1 100755 --- a/BeeHive/BeeHive.m +++ b/BeeHive/BeeHive.m @@ -67,8 +67,6 @@ - (void)loadStaticModules [[BHModuleManager sharedManager] loadLocalModules]; - [[BHModuleManager sharedManager] registedAnnotationModules]; - [[BHModuleManager sharedManager] registedAllModules]; } @@ -79,8 +77,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 265f01a..543fc78 100644 --- a/Example/BeeHive/TestAppDelegate.m +++ b/Example/BeeHive/TestAppDelegate.m @@ -11,6 +11,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