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

通过实例方法 hook instance 后,支持对其 isaClass 进行 instance hook #20

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
1 change: 1 addition & 0 deletions Stinger/Classes/STDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ typedef NS_ENUM(NSInteger, STHookResult) {
@property (nonatomic, weak) Class hookedCls;
@property (nonatomic, weak) Class statedCls;
@property (nonatomic, assign) BOOL isInstanceHook;
@property (nonatomic, assign) BOOL isInstanceIsaHook;

+ (instancetype)poolWithTypeEncoding:(NSString *)typeEncoding originalIMP:(IMP)imp selector:(SEL)sel;
@end
Expand Down
5 changes: 4 additions & 1 deletion Stinger/Classes/STHookInfoPool.m
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ @implementation STHookInfoPool {
@synthesize hookedCls = _hookedCls;
@synthesize statedCls = _statedCls;
@synthesize isInstanceHook = _isInstanceHook;
@synthesize isInstanceIsaHook = _isInstanceIsaHook;
@synthesize semaphore = _semaphore;


Expand Down Expand Up @@ -408,7 +409,9 @@ NS_INLINE void _st_ffi_function(ffi_cif *cif, void *ret, void **args, void *user
void **slf = args[0];

if (hookedClassInfoPool->_isInstanceHook) {
statedClassInfoPool = _st_fast_get_HookInfoPool(hookedClassInfoPool->_statedCls, hookedClassInfoPool->_uniqueKey);
if (!hookedClassInfoPool->_isInstanceIsaHook) {
statedClassInfoPool = _st_fast_get_HookInfoPool(hookedClassInfoPool->_statedCls, hookedClassInfoPool->_uniqueKey);
}
instanceInfoPool = _st_fast_get_HookInfoPool((__bridge id)(*slf), hookedClassInfoPool->_uniqueKey);
}

Expand Down
18 changes: 10 additions & 8 deletions Stinger/Classes/Stinger.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ @implementation NSObject (Stinger)
#pragma mark - For specific class

+ (STHookResult)st_hookInstanceMethod:(SEL)sel option:(STOption)option usingIdentifier:(STIdentifier)identifier withBlock:(id)block {
return hookMethod(self, sel, option, identifier, block);
return hookMethod(self, sel, option, identifier, block, NO);
}

+ (STHookResult)st_hookClassMethod:(SEL)sel option:(STOption)option usingIdentifier:(STIdentifier)identifier withBlock:(id)block {
return hookMethod(object_getClass(self), sel, option, identifier, block);
return hookMethod(object_getClass(self), sel, option, identifier, block, NO);
}

+ (NSArray<STIdentifier> *)st_allIdentifiersForKey:(SEL)key {
Expand Down Expand Up @@ -56,7 +56,7 @@ - (STHookResult)st_hookInstanceMethod:(SEL)sel option:(STOption)option usingIden
Class stSubClass = getSTSubClass(self);
if (!stSubClass) return STHookResultOther;

STHookResult hookMethodResult = hookMethod(stSubClass, sel, option, identifier, block);
STHookResult hookMethodResult = hookMethod(stSubClass, sel, option, identifier, block, YES);
if (hookMethodResult != STHookResultSuccess) return hookMethodResult;
if (!objc_getAssociatedObject(self, STSubClassKey)) {
object_setClass(self, stSubClass);
Expand Down Expand Up @@ -91,7 +91,7 @@ - (BOOL)st_removeHookWithIdentifier:(STIdentifier)identifier forKey:(SEL)key {

#pragma mark - inline functions

NS_INLINE STHookResult hookMethod(Class hookedCls, SEL sel, STOption option, STIdentifier identifier, id block) {
NS_INLINE STHookResult hookMethod(Class hookedCls, SEL sel, STOption option, STIdentifier identifier, id block, BOOL byInstanceHook) {
NSCParameterAssert(hookedCls);
NSCParameterAssert(sel);
NSCParameterAssert(identifier);
Expand Down Expand Up @@ -124,11 +124,13 @@ NS_INLINE STHookResult hookMethod(Class hookedCls, SEL sel, STOption option, STI
st_setHookInfoPool(hookedCls, sel, hookInfoPool);
}
if (st_isIntanceHookCls(hookedCls)) {
return STHookResultSuccess;
} else {
STHookInfo *hookInfo = [STHookInfo infoWithOption:option withIdentifier:identifier withBlock:block];
return [hookInfoPool addInfo:hookInfo] ? STHookResultSuccess : STHookResultErrorIDExisted;
if (byInstanceHook) {
return STHookResultSuccess;
}
hookInfoPool.isInstanceIsaHook = YES;
}
STHookInfo *hookInfo = [STHookInfo infoWithOption:option withIdentifier:identifier withBlock:block];
return [hookInfoPool addInfo:hookInfo] ? STHookResultSuccess : STHookResultErrorIDExisted;
}
}

Expand Down