Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support reset arguments #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion Example/Stinger/ASViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ - (void)setFrame:(CGRect)rect {

}

- (BOOL)setFrame:(CGRect)rect object:(NSString *)obj num:(NSInteger)num {
NSLog(@"origin fun: %@, %@, %ld", @(rect), obj, num);
return YES;
}

- (void)viewDidLoad {
[super viewDidLoad];
[self.class st_hookInstanceMethod:@selector(methodA) option:STOptionBefore usingIdentifier:@"hook methodA before" withBlock:^(id<StingerParams> params) {
Expand All @@ -34,7 +39,18 @@ - (void)viewDidLoad {
[self.class st_hookInstanceMethod:@selector(methodA) option:STOptionAfter usingIdentifier:@"hook methodA after" withBlock:^(id<StingerParams> params) {

}];


[self.class st_hookInstanceMethod:@selector(setFrame:object:num:) option:STOptionAfter usingIdentifier:@"change arg" withBlock:^BOOL(id<StingerParams> params, CGRect rect, NSString *obj, NSInteger num) {
[params setArgument:@(CGRectMake(5, 6, 7, 8)) atIndex:0];
[params setArgument:@"hook success" atIndex:1];
[params setArgument:@(num + 1) atIndex:2];
BOOL returnValue;
[params invokeAndGetOriginalRetValue:&returnValue];
return returnValue;
}];
[self setFrame:CGRectMake(1, 2, 3, 4) object:@"not hook" num:111];


// [self.class aspect_hookSelector:@selector(methodA) withOptions:AspectPositionBefore usingBlock:^(id<AspectInfo> params) {
//
// } error:nil];
Expand Down
1 change: 1 addition & 0 deletions Stinger/Classes/STDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ typedef NS_ENUM(NSInteger, STHookResult) {
- (SEL)sel;
- (NSArray *)arguments;
- (NSString *)typeEncoding;
- (void)setArgument:(id)arg atIndex:(NSInteger)idx;
- (void)invokeAndGetOriginalRetValue:(void *)retLoc;
@end

Expand Down
33 changes: 28 additions & 5 deletions Stinger/Classes/StingerParams.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ @interface StingerParams ()
@property (nonatomic) IMP originalIMP;
@property (nonatomic) void **args;
@property (nonatomic) NSArray *argumentTypes;
@property (nonatomic) NSArray *arguments;
@property (nonatomic) NSMutableArray *arguments;
@end

@implementation StingerParams
Expand All @@ -46,13 +46,37 @@ - (SEL)sel {
}

- (NSArray *)arguments {
return _arguments;
return [_arguments copy];
}

- (NSString *)typeEncoding {
return _types;
}

/// 修改函数参数
/// - Parameters:
/// - arg: 对应的参数,值类型请转换为NSValue传递
/// - idx: 序号,默认从0开始
- (void)setArgument:(id)arg atIndex:(NSInteger)idx {
_arguments[idx]= arg;
NSString *argTypeStr = _argumentTypes[idx + 2];
const char *argType = argTypeStr.UTF8String;
if (strcmp(argType, @encode(id)) == 0 || strcmp(argType, @encode(Class)) == 0) {
void **objPointer = _args[idx + 2];
*objPointer = (__bridge void *)(arg);
return;
}
if ([arg isKindOfClass:NSValue.class]) {
if (@available(iOS 11.0, *)) {
NSUInteger valueSize = 0;
NSGetSizeAndAlignment(argType, &valueSize, NULL);
[(NSValue *)arg getValue:_args[idx + 2] size:valueSize];
} else {
[(NSValue *)arg getValue:_args[idx + 2]];
}
}
}

- (void)invokeAndGetOriginalRetValue:(void *)retLoc {
NSMethodSignature *signature = [NSMethodSignature signatureWithObjCTypes:_types.UTF8String];
NSInteger count = signature.numberOfArguments;
Expand All @@ -69,12 +93,11 @@ - (void)invokeAndGetOriginalRetValue:(void *)retLoc {
#pragma - mark Private

- (void)st_genarateArguments {
NSMutableArray *args = [[NSMutableArray alloc] initWithCapacity:_argumentTypes.count];
_arguments = [NSMutableArray array];
for (NSUInteger i = 2; i < _argumentTypes.count; i++) {
id argument = [self st_argumentWithType:_argumentTypes[i] index:i];
[args addObject:argument ?: NSNull.null];
[_arguments addObject:argument ?: NSNull.null];
}
_arguments = [args copy];
}

- (id)st_argumentWithType:(NSString *)type index:(NSUInteger)index {
Expand Down