Logos语法学习

栏目: IOS · 发布时间: 7年前

内容简介:它是在Theos开发中允许hook代码非常的简单明了,它能够替换、修改、新增方法或者类。这种等级必须以%end结尾,并且也不存在函数如果为了支持适配不同系统的代码,可以通过以下方式

它是在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);
复制代码
Logos语法学习

%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>, …]);
复制代码

打印日志


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

51单片机应用从零开始

51单片机应用从零开始

杨欣、王玉凤、刘湘黔 / 清华大学 / 2008-1 / 39.80元

《51单片机应用与实践丛书•51单片机应用从零开始》在分析初学者认知规律的基础上,结合国内重点大学一线教师的教学经验以及借鉴国外经典教材的写作手法,对51单片机的应用基础知识进行系统而翔实的介绍。读者学习每一章之后,"实例点拨"环节除了可以巩固所学的内容外,还开辟了单片机应用的视野;再加上"器件介绍"环节,又充实了对单片机从基础到应用所需要的知识。8051单片机不仅是国内用得最多的单片机之一,同时......一起来看看 《51单片机应用从零开始》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

MD5 加密
MD5 加密

MD5 加密工具