Skip to content
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
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so
*.pyc

# Logs and databases #
######################
*.log

# OS generated files #
######################
.DS_Store*
ehthumbs.db
Icon?
Thumbs.db
2 changes: 1 addition & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ SwizzleKit is intended to be a collection of macros, functions, categories and c

Rationale

Writing plugins for closed sourced applications is difficult and has become more difficult with 10.6 in 64bit mode.
Writing plugins for closed sourced applications is difficult and has become more difficult with 10.6 in 64bit mode.

Writing stable and successful plugins require a good degree of methodical work to do trivial task such as writing categories, accessing ivars in methods. SwizzleKit provides a formal way to do this so that theses tasks are bug free.

Expand Down
16 changes: 8 additions & 8 deletions SwizzleKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
// ------------------------------------------------------------------------


// Underimplemented!!!!
// Underimplemented!!!!
//
// This is a work in progress. somethings probaby won't work.
// This is a work in progress. somethings probaby won't work.
// have fun!


Expand Down Expand Up @@ -45,10 +45,10 @@
#define SUPER(...) objc_msgSendSuper(&(struct objc_super){self, class_getSuperclass([self class])},_cmd, ##__VA_ARGS__)


//respondsDirectlyToSelector method returns YES if this object implements the selector directly.
//respondsDirectlyToSelector method returns YES if this object implements the selector directly.
// Returns NO if any superclass implements the selector or no superclass implemention.

@interface NSObject (UNIQUE_PREFIXswizzleKit)
@interface NSObject (UNIQUE_PREFIXswizzleKit)
-(BOOL)UNIQUE_PREFIXrespondsDirectlyToSelector:(SEL)aSelector;
@end

Expand All @@ -74,8 +74,8 @@ void UNIQUE_PREFIXdescribeClass(const char * clsName);
// It will also create a class Method +mapTable that will create the maptable on the first call (access though the accessors function above.

// The mapTableVariables should be accessed ONLY through the use of the functions declared above
//
//

#define IMPLEMENT_MAPTABLE_VARIABLES_USING_PREFIX(prefix) \
static NSMapTable *_mappedViewerIVars = NULL; \
static NSLock * _mapTableLock = nil; \
Expand Down Expand Up @@ -184,8 +184,8 @@ void UNIQUE_PREFIXdescribeClass(const char * clsName);
} \


id UNIQUE_PREFIXobject_getMapTableVariable(id anObject, const char* variableName);
void UNIQUE_PREFIXobject_setMapTableVariable(id anObject, const char* variableName,id value);
id UNIQUE_PREFIXobject_getMapTableVariable(id anObject, const char* variableName);
void UNIQUE_PREFIXobject_setMapTableVariable(id anObject, const char* variableName,id value);



Expand Down
78 changes: 39 additions & 39 deletions SwizzleKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ -(BOOL)UNIQUE_PREFIXrespondsDirectlyToSelector:(SEL)aSelector{
BOOL responds = NO;
unsigned int methodCount = 0;
Method * methods = nil;

// extend instance Methods
methods = class_copyMethodList([self class], &methodCount);
int ci= methodCount;
Expand Down Expand Up @@ -79,7 +79,7 @@ void UNIQUE_PREFIXobject_setMapTableVariable(id anObject, const char* variableNa
}
}
}

}

void UNIQUE_PREFIXdescribeClass(const char * clsName){
Expand All @@ -94,26 +94,26 @@ void UNIQUE_PREFIXdescribeClass(const char * clsName){
Ivar * ivars = class_copyIvarList(aClass, &ivarCount);
for (ci=0;ci<ivarCount;ci++){
[logString appendFormat:@" %s %s; //%ld\n",ivar_getTypeEncoding(ivars[ci]), ivar_getName(ivars[ci]), ivar_getOffset(ivars[ci])];

}
[logString appendString:@"}\n"];
free(ivars);

unsigned int classMethodCount =0;
Method *classMethods = class_copyMethodList(object_getClass(aClass), &classMethodCount);
for(ci=0;ci<classMethodCount;ci++){
[logString appendFormat:@"+[%s %@]\n",class_getName(aClass),NSStringFromSelector(method_getName(classMethods[ci]))];
}
free(classMethods);

unsigned int instanceMethodCount =0;
Method *instanceMethods = class_copyMethodList(aClass, &instanceMethodCount);
for(ci=0;ci<instanceMethodCount;ci++){
[logString appendFormat:@"-[%s %@]\n",class_getName(aClass),NSStringFromSelector(method_getName(instanceMethods[ci]))];
}



NSLog(@"%@",logString);
}
}
Expand All @@ -122,13 +122,13 @@ @implementation UNIQUE_PREFIXSwizzler
+(Class)subclass:(Class)baseClass usingClassName:(NSString*)subclassName providerClass:(Class)providerClass{
Class subclass = objc_allocateClassPair(baseClass, [subclassName UTF8String], 0);
if (!subclass) return nil;

unsigned int ivarCount =0;
Ivar * ivars = class_copyIvarList(providerClass, &ivarCount);
int ci = 0;
for (ci=0 ;ci < ivarCount; ci++){
Ivar anIvar = ivars[ci];

NSUInteger ivarSize = 0;
NSUInteger ivarAlignment = 0;
const char * typeEncoding = ivar_getTypeEncoding(anIvar);
Expand All @@ -140,18 +140,18 @@ +(Class)subclass:(Class)baseClass usingClassName:(NSString*)subclassName provide
NSLog(@"could not add iVar %s", ivar_getName(anIvar));
return nil;
}

}
free(ivars);
objc_registerClassPair(subclass);

[self extendClass:subclass withMethodsFromClass:providerClass];
return subclass;
}
+(void)extendClass:(Class) targetClass withMethodsFromClass:(Class)providerClass{
unsigned int methodCount = 0;
Method * methods = nil;

// extend instance Methods
methods = class_copyMethodList(providerClass, &methodCount);
int ci= methodCount;
Expand All @@ -161,7 +161,7 @@ +(void)extendClass:(Class) targetClass withMethodsFromClass:(Class)providerClass
//NSLog(@"extending -[%s %@]",class_getName(targetClass),methodName);
}
free(methods);

// extend Class Methods
methods = class_copyMethodList(object_getClass(providerClass), &methodCount);
ci= methodCount;
Expand All @@ -171,7 +171,7 @@ +(void)extendClass:(Class) targetClass withMethodsFromClass:(Class)providerClass
//NSLog(@"extending +[%s %@]",class_getName(targetClass),methodName);
}
free(methods);

methods = 0;
}
+(BOOL)addClassMethodName:(NSString *)methodName fromProviderClass:(Class)providerClass toClass:(Class)targetClass{
Expand All @@ -181,18 +181,18 @@ +(BOOL)addClassMethodName:(NSString *)methodName fromProviderClass:(Class)provid
}
SEL selector = NSSelectorFromString(methodName);
Method originalMethod = class_getClassMethod(providerClass,selector);

if (!originalMethod) {
return NO;
}

IMP originalImplementation = method_getImplementation(originalMethod);
if (!originalImplementation){
return NO;
}

class_addMethod(metaClass, selector ,originalImplementation, method_getTypeEncoding(originalMethod));

return YES;
}

Expand All @@ -202,18 +202,18 @@ +(BOOL)addInstanceMethodName:(NSString *)methodName fromProviderClass:(Class)pro
}
SEL selector = NSSelectorFromString(methodName);
Method originalMethod = class_getInstanceMethod(providerClass,selector);

if (!originalMethod) {
return NO;
}

IMP originalImplementation = method_getImplementation(originalMethod);
if (!originalImplementation){
return NO;
}

class_addMethod(targetClass, selector ,originalImplementation, method_getTypeEncoding(originalMethod));

return YES;
}

Expand All @@ -223,7 +223,7 @@ +(IMP)swizzleClassMethod:(NSString*)methodName forClass:(Class)targetClass{
SEL oldSelector = NSSelectorFromString(methodName);
NSString * newMethodName = [SWIZZLE_PREFIX stringByAppendingString:methodName];
SEL newSelector = NSSelectorFromString(newMethodName);

oldMethod = class_getClassMethod(targetClass, oldSelector);
if (oldMethod==NULL) {
NSLog(@"SWIZZLE Error - Can't find existing method for +[%@ %@]",NSStringFromClass(targetClass),NSStringFromSelector(oldSelector));
Expand All @@ -247,19 +247,19 @@ +(IMP)swizzleClassMethod:(NSString*)methodName forClass:(Class)targetClass{
NSLog(@"SWIZZLE Error - Can't find existing method for +[%@ %@]",NSStringFromClass(targetClass),NSStringFromSelector(oldSelector));
return NULL;
}

//Debugger();
}
if (NULL != oldMethod && NULL != newMethod) {
//newIMP =
//newIMP =
newIMP= method_getImplementation(oldMethod);
method_exchangeImplementations(oldMethod, newMethod);

//newIMP = method_setImplementation(oldMethod,method_getImplementation(newMethod));
//method_setImplementation(newMethod, newIMP);
return newIMP;
}

return NULL;
}
+(IMP)swizzleInstanceMethod:(NSString*)methodName forClass:(Class)targetClass{
Expand All @@ -269,7 +269,7 @@ +(IMP)swizzleInstanceMethod:(NSString*)methodName forClass:(Class)targetClass{
SEL oldSelector = NSSelectorFromString(methodName);
NSString * newMethodName = [SWIZZLE_PREFIX stringByAppendingString:methodName];
SEL newSelector = NSSelectorFromString(newMethodName);

oldMethod = class_getInstanceMethod(targetClass, oldSelector);
if (oldMethod==NULL) {
NSLog(@"SWIZZLE Error - Can't find existing method for -[%@ %@]",NSStringFromClass(targetClass),NSStringFromSelector(oldSelector));
Expand All @@ -293,17 +293,17 @@ +(IMP)swizzleInstanceMethod:(NSString*)methodName forClass:(Class)targetClass{
NSLog(@"SWIZZLE Error - Can't find existing method for -[%@ %@]",NSStringFromClass(targetClass),NSStringFromSelector(oldSelector));
return NULL;
}

//Debugger();
}
if (NULL != oldMethod && NULL != newMethod) {
//newIMP = method_exchangeImplementations(, )
oldIMP = method_setImplementation(oldMethod,method_getImplementation(newMethod));
method_setImplementation(newMethod, oldIMP);

return oldIMP;
}

return NULL;
}
@end
Expand All @@ -318,7 +318,7 @@ +(NSArray*)abbreviatedCallStackSymbols{
int i, frames = backtrace(callstack, 128);
char** strs = backtrace_symbols(callstack, frames);
NSMutableArray *callStack = [[NSMutableArray alloc] initWithCapacity:frames];

for (i = 1; i < frames; ++i) {
NSString * frameString = [[NSString alloc] initWithUTF8String:strs[i]];
NSScanner * scanner = [NSScanner scannerWithString:frameString];
Expand All @@ -336,10 +336,10 @@ +(NSArray*)abbreviatedCallStackSymbols{
NSString * method = nil;
[scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&method];
[callStack addObject:[NSString stringWithFormat:@"%3-s %18-s %@",[frameNumber UTF8String],[module UTF8String],method]];

}
free(strs);

return [callStack autorelease];
}

Expand All @@ -350,7 +350,7 @@ +(NSArray *)callStackSymbolsForFrameCount:(NSInteger) frameCount{
char** strs = backtrace_symbols(callstack, frames);
NSMutableArray *callStack = [[NSMutableArray alloc] initWithCapacity:frames];
int maxFrame = MIN(frames,frameCount+1);

for (i = 1; i < maxFrame; ++i) {
NSString * frameString = [[NSString alloc] initWithUTF8String:strs[i]];
NSScanner * scanner = [NSScanner scannerWithString:frameString];
Expand All @@ -368,12 +368,12 @@ +(NSArray *)callStackSymbolsForFrameCount:(NSInteger) frameCount{
NSString * method = nil;
[scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&method];
[callStack addObject:[NSString stringWithFormat:@"%3-s %18-s %@",[frameNumber UTF8String],[module UTF8String],method]];

}
free(strs);

return [callStack autorelease];

}
@end