Skip to content

Commit

Permalink
Merge branch 'luoqisheng-feature/annotaion-dynamic'
Browse files Browse the repository at this point in the history
support dynamic annotation
  • Loading branch information
dazi.dp committed Mar 14, 2017
2 parents db4795f + 9dc201c commit 48ec680
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 97 deletions.
11 changes: 3 additions & 8 deletions BeeHive/BHAnnotation.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@


#import <Foundation/Foundation.h>
#import "BeeHive.h"

#ifndef BeehiveModSectName

Expand All @@ -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<NSString *> *)AnnotationModules;
+ (NSArray<NSString *> *)AnnotationServices;

@end
104 changes: 65 additions & 39 deletions BeeHive/BHAnnotation.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,75 @@
#include <dlfcn.h>
#import <objc/runtime.h>
#import <objc/message.h>
static NSArray<NSString *>* BHReadConfiguration(char *section)
#include <mach-o/ldsyms.h>




#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<NSString *>* 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<NSString *> *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<NSString *>* 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;

Expand All @@ -44,28 +92,6 @@
}

return configs;

}
@implementation BHAnnotation

+ (NSArray<NSString *> *)AnnotationModules
{
static NSArray<NSString *> *mods = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
mods = BHReadConfiguration(BeehiveModSectName);
});
return mods;
}

+ (NSArray<NSString *> *)AnnotationServices
{
static NSArray<NSString *> *services = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
services = BHReadConfiguration(BeehiveServiceSectName);
});
return services;

}

@end
18 changes: 1 addition & 17 deletions BeeHive/BHModuleManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,8 @@ - (void)registedAllModules

}

- (void)registedAnnotationModules
{

NSArray<NSString *>*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:
Expand Down
24 changes: 0 additions & 24 deletions BeeHive/BHServiceManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,6 @@ - (void)registerLocalServices
[self.lock unlock];
}

- (void)registerAnnotationServices
{
NSArray<NSString *>*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);
Expand Down
4 changes: 0 additions & 4 deletions BeeHive/BeeHive.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ - (void)loadStaticModules

[[BHModuleManager sharedManager] loadLocalModules];

[[BHModuleManager sharedManager] registedAnnotationModules];

[[BHModuleManager sharedManager] registedAllModules];

}
Expand All @@ -79,8 +77,6 @@ -(void)loadStaticServices

[[BHServiceManager sharedManager] registerLocalServices];

[[BHServiceManager sharedManager] registerAnnotationServices];

}

@end
2 changes: 1 addition & 1 deletion Example/BeeHive/BHUserTrackViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#import "BeeHive.h"

#import "BHService.h"

@BeeHiveService(UserTrackServiceProtocol,BHUserTrackViewController)
@interface BHUserTrackViewController()<UserTrackServiceProtocol>


Expand Down
2 changes: 1 addition & 1 deletion Example/BeeHive/BHViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#import "BeeHive.h"
#import "BHService.h"

BeeHiveService(HomeServiceProtocol,BHViewController)
@BeeHiveService(HomeServiceProtocol,BHViewController)
@interface BHViewController ()<HomeServiceProtocol>

@property(nonatomic,strong) NSMutableArray *registerViewControllers;
Expand Down
2 changes: 1 addition & 1 deletion Example/BeeHive/ShopModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#import "ShopModule.h"
#import "BeeHive.h"
BeeHiveMod(ShopModule)
@BeeHiveMod(ShopModule)
@interface ShopModule() <BHModuleProtocol>

@end
Expand Down
3 changes: 3 additions & 0 deletions Example/BeeHive/TestAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#import "BeeHive.h"
#import "BHService.h"
#import "BHTimeProfiler.h"
#import <mach-o/dyld.h>
#import "BHModuleManager.h"
#import "BHServiceManager.h"

@interface TestAppDelegate ()

Expand Down
6 changes: 4 additions & 2 deletions Example/Podfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 48ec680

Please sign in to comment.