Logos语法学习

3,232 阅读2分钟

Logos语法

它是在Theos开发中允许hook代码非常的简单明了,它能够替换、修改、新增方法或者类。

块等级

这种等级必须以%end结尾,并且也不存在函数

%group

    %group Groupname

如果为了支持适配不同系统的代码,可以通过以下方式

    %group iOS8
    %hook IOS8_SPECIFIC_CLASS
	  // your code here
    %end // end hook
    %end // end group ios8

    %group iOS9
    %hook IOS9_SPECIFIC_CLASS
    	// your code here
    %end // end hook
    %end // end group ios9

    %ctor {
    	if (kCFCoreFoundationVersionNumber >   1200) {
    		%init(iOS9);
    	} else {
    		%init(iOS8);
    	}
    }

%hook

所有的hook都有隐藏了一个“_ungrouped”的隐式分组;它用来修饰类名

%hook Classname

hook之间的代码是SBApplicationController具体的函数

%hook SBApplicationController
-(void)uninstallApplication:(SBApplication *)application {
	NSLog(@"Hey, we're hooking uninstallApplication:!");
	%orig; // Call the original implementation of this method
	return;
}
%end

%new

给hook的类添加新方法;signature是OC类型的新方法名,必须在%hook块之间

%new  // 修饰新的方法
%new(signature)  // 修饰新的方法signature
%hook ClassName
%new
- (id)someValue {
	return objc_getAssociatedObject(self, @selector(someValue));
}

%end

%subclass

通过运行时创建的子类,ivars不支持;使用%new修饰新增的方法,如果需要访问新类,必须通过%c操作符获取,可以在%group块中

%subclass MyObject : NSObject

- (id)init {
	self = %orig;
	[self setSomeValue:@"value"];
	return self;
}

//the following two new methods act as `@property (nonatomic, retain) id someValue;`
%new
- (id)someValue {
	return objc_getAssociatedObject(self, @selector(someValue));
}

%new
- (void)setSomeValue:(id)value {
	objc_setAssociatedObject(self, @selector(someValue), value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

%end

%ctor {
	MyObject *myObject = [[%c(MyObject) alloc] init];
	NSLog(@"myObject: %@", [myObject someValue]);
}

%property

%property (nonatomic|assign|retain|copy|weak|strong|getter|setter) Type name;

添加新的属性在类中,必须在%hook修饰或者%subclass修饰的内部。

%end

%end

用来修饰group/hook/subclass的块结束

顶部等级

这部分的指令不再group/hook/subclass的块内部。

%config

%config(Key=Value);

%hookf

%hookf(rtype, symbolName, args...) { … }
// Given the function prototype
FILE *fopen(const char *path, const char *mode);
// The hook is thus made
%hookf(FILE *, fopen, const char *path, const char *mode) {
	NSLog(@"Hey, we're hooking fopen to deny relative paths!");
	if (path[0] != '/') {
		return NULL;
	}
	return %orig; // Call the original implementation of this function
}
CFBooleanRef (*orig_MGGetBoolAnswer)(CFStringRef);
CFBooleanRef fixed_MGGetBoolAnswer(CFStringRef string)
{
	if (CFEqual(string, CFSTR("StarkCapability"))) {
		return kCFBooleanTrue;
	}
	return orig_MGGetBoolAnswer(string);
}

%hookf(CFBooleanRef, "_MGGetBoolAnswer", CFStringRef string)
{
	if (CFEqual(string, CFSTR("StarkCapability"))) {
		return kCFBooleanTrue;
	}
	return %orig;
}

%ctor

%ctor { … }

匿名构造方法

%dtor

%dtor { … }

匿名销毁构造方法

函数等级

只在函数block中

%init

%init;
%init([<class>=<expr>, …]);
%init(Group[, [+|-]<class>=<expr>, …]);

初始化分组或默认分组

%hook SomeClass
-(id)init {
    return %orig;
}
%end

%ctor {
    %init(SomeClass=objc_getClass("class with spaces in the name"));
}

%c

%c([+|-]Class)

等价于实例对象,或者类名

%orig

%orig
%orig(arg1, …)

调用原始方法,不能够在%new里面,

%log

%log;
%log([(<type>)<expr>, …]);

打印日志

Logos文件扩展格式

Extension Process order
.x will be processed by Logos, then preprocessed and compiled as objective-c.
.xm will be processed by Logos, then preprocessed and compiled as objective-c++.
.xi will be preprocessed as objective-c first, then Logos will process the result, and then it will be compiled.
.xmi will be preprocessed as objective-c++ first, then Logos will process the result, and then it will be compiled.

xi或者xmi文件可以使用#define 宏

logify.pl

自动动帮你生成对应类的所有hook方法

$THEOS/bin/logify.pl ./SSDownloadAsset.h