夜雨聆风学习资料网

ARTICLE · 1060359

插件使用Swift Orion来开发的见解与讲解

插件使用Swift Orion来开发的见解与讲解
swift自从到了swift5版本之后,就很稳定了。
而且swift代码相较于oc是比较清爽的,项目模块中的方法与属性是全局性的,不需要像oc那样需要引入才能使用。也不需要像oc那样需要声明“**.h”文件和“**.m”文件搭配使用。调用方法也是更为清爽。
所以我还是呼吁大家去拥抱现代高阶语言,学习起来也是更为简单,用起来也是更容易上手。
现在给一些theos中的代码对比吧,帮助大家能在orion中少走点弯路:
一、普通方法 hook
logos:
%hook SpringBoard -(void)applicationDidFinishLaunching: (id)arg {    %orig;  }%end
在swift orion中等同于:
class SpringBoardHook: ClassHook<SpringBoard> {  func applicationDidFinishLaunching(_ arg: AnyObject?) {    orig.applicationDidFinishLaunching(arg);  }}
二、类方法 hook
logos:
%hook UIApplication + (id)sharedApplication {    return %orig;  }%end
在swift orion中等同于:
classUIApplicationHook: ClassHook<UIApplication> {  classfuncsharedApplication() -> AnyObject? {      return orig.sharedApplication();    }}
三、新增方法
logos:
%hook SpringBoard  %new  -(void)someFunc {}%end
在swift orion中等同于:
classSpringBoardHook: ClassHook<SpringBoard> {  // orion:new  func someFunc() {}}
四、hook swift 模块所暴露的方法
logos:
@interface ViewController: UIViewController-(void) sendMsg;@end%hook ViewController  -(void) sendMsg {    // todo  }%end%ctor {  // 当然此处也可以用%c代替NSClassFromString  Class vcClass = NSClassFromString(@"TargetXxxxxxx.ViewController");  if (vcClass) {    %init(ViewController = vcClass);  }}
在swift orion中等同于:
class ViewControllerHook: ClassHook<NSObject> {  static let targetName = "TargetXxxxxxx.ViewController";  func sendMsg() {    // todo  }}
当然,可以可以用group更安全(此处演示Orion 的group方式, 后续就不演示它与logos的group区别了):
struct vcGroup: HookGroup {  let classString: String;}// 当然此处Hook对象并不一定都是指定NSObject,如果目标类最终是继承自UIView,那么就写UIViewclass ViewControllerHook: ClassHook<NSObject> {  typealias Group = vcGroup;  static let targetName = group.classString;  func sendMsg() {    // todo  }}class TweakInit: Tweak {  required init() {    if let _: AnyClass = NSClassFromString("TargetXxxxxxx.ViewController") {      vcGroup(classString: "TargetXxxxxxx.ViewController").activate();    }  }}
五、动态调用未声明的方法(普通方法)
logos:
@interface ViewControllerManager  -(void) sendMsg;@endid vc = [NSClassFromString(@"ViewControllerManager") new];[vc sendMsg];
在swift orion中等同于:
// 因为swift与oc的差异性,所以得需要一个协议来桥接@objc protocol MyViewControllerManager {  func sendMsg;}// 调用 ViewControllerManager sendMsg 方法let vcManager = Dynamic.ViewControllerManager.alloc(interface: MyViewControllerManager.self).init();vcManager.sendMsg();
六、动态调用未声明的方法(类方法)
logos:
@interfaceViewControllerMgr  +(void) sendMsg;@end// 应该是这样写,好久没用oc了,有点忘记了。[NSClassFromString(@"ViewControllerMgr") sendMsg];
在swift orion中等同于:
// 因为swift与oc的差异性,所以得需要一个协议来桥接@objc protocol MyViewControllerManager {  func sendMsg;}// 调用 ViewControllerManager sendMsg 方法// 和普通方法有点区别,请仔细对比let vcManager = Dynamic.ViewControllerManager.as(interface: MyViewControllerManager.self);vcManager.sendMsg();
七、funcation hook
logos:
%hookf(size_t, strlen, const char* val) {  return %orig;}
在swift orion中等同于:
class StrlenHook: FunctionHook {  static let target = Function.symbol("strlen", image: nil);  // swift中的数据类型,并不完全等同于c中的指针类型,具体转换成什么类型需要自己去科普  func function(_ val: UnsafePointer<Int8>) -> UInt {    return orig.function(val);  }}
二者在hook方式上有些区别,有时候logos繁琐,有时候orion繁琐,这个得适应。但是在业务逻辑上的实现,swift显然更舒服一些。
其他的我就不一一举例了,具体的可以看theos 官方文档:

https://orion.theos.dev/

动态语言一时爽,代码重构火葬场。

相关学习资料