LKS_RequestHandler.m 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. #ifdef SHOULD_COMPILE_LOOKIN_SERVER
  2. //
  3. // LKS_RequestHandler.m
  4. // LookinServer
  5. //
  6. // Created by Li Kai on 2019/1/15.
  7. // https://lookin.work
  8. //
  9. #import "LKS_RequestHandler.h"
  10. #import "NSObject+LookinServer.h"
  11. #import "UIImage+LookinServer.h"
  12. #import "LKS_ConnectionManager.h"
  13. #import "LookinConnectionResponseAttachment.h"
  14. #import "LookinAttributeModification.h"
  15. #import "LookinDisplayItemDetail.h"
  16. #import "LookinHierarchyInfo.h"
  17. #import "LookinServerDefines.h"
  18. #import <objc/runtime.h>
  19. #import "LookinObject.h"
  20. #import "LKS_LocalInspectManager.h"
  21. #import "LookinAppInfo.h"
  22. #import "LKS_MethodTraceManager.h"
  23. #import "LKS_AttrGroupsMaker.h"
  24. #import "LKS_AttrModificationHandler.h"
  25. #import "LKS_AttrModificationPatchHandler.h"
  26. #import "LKS_HierarchyDetailsHandler.h"
  27. #import "LookinStaticAsyncUpdateTask.h"
  28. @interface LKS_RequestHandler ()
  29. @end
  30. @implementation LKS_RequestHandler {
  31. NSSet *_validRequestTypes;
  32. }
  33. - (instancetype)init {
  34. if (self = [super init]) {
  35. _validRequestTypes = [NSSet setWithObjects:@(LookinRequestTypePing),
  36. @(LookinRequestTypeApp),
  37. @(LookinRequestTypeHierarchy),
  38. @(LookinRequestTypeModification),
  39. @(LookinRequestTypeAttrModificationPatch),
  40. @(LookinRequestTypeHierarchyDetails),
  41. @(LookinRequestTypeFetchObject),
  42. @(LookinRequestTypeAllAttrGroups),
  43. @(LookinRequestTypeAllSelectorNames),
  44. @(LookinRequestTypeAddMethodTrace),
  45. @(LookinRequestTypeDeleteMethodTrace),
  46. @(LookinRequestTypeClassesAndMethodTraceLit),
  47. @(LookinRequestTypeInvokeMethod),
  48. @(LookinRequestTypeFetchImageViewImage),
  49. @(LookinRequestTypeModifyRecognizerEnable),
  50. @(LookinPush_BringForwardScreenshotTask),
  51. @(LookinPush_CanceHierarchyDetails),
  52. nil];
  53. }
  54. return self;
  55. }
  56. - (BOOL)canHandleRequestType:(uint32_t)requestType {
  57. if ([_validRequestTypes containsObject:@(requestType)]) {
  58. return YES;
  59. }
  60. NSAssert(NO, @"");
  61. return NO;
  62. }
  63. - (void)handleRequestType:(uint32_t)requestType tag:(uint32_t)tag object:(id)object {
  64. if (requestType == LookinRequestTypePing) {
  65. LookinConnectionResponseAttachment *responseAttachment = [LookinConnectionResponseAttachment new];
  66. // 当 app 处于后台时,可能可以执行代码也可能不能执行代码,如果运气好了可以执行代码,则这里直接主动使用 appIsInBackground 标识 app 处于后台,不要让 Lookin 客户端傻傻地等待超时了
  67. if (![LKS_ConnectionManager sharedInstance].applicationIsActive) {
  68. responseAttachment.appIsInBackground = YES;
  69. }
  70. [[LKS_ConnectionManager sharedInstance] respond:responseAttachment requestType:requestType tag:tag];
  71. } else if (requestType == LookinRequestTypeApp) {
  72. // 请求可用设备信息
  73. NSDictionary<NSString *, id> *params = object;
  74. BOOL needImages = ((NSNumber *)params[@"needImages"]).boolValue;
  75. NSArray<NSNumber *> *localIdentifiers = params[@"local"];
  76. LookinAppInfo *appInfo = [LookinAppInfo currentInfoWithScreenshot:needImages icon:needImages localIdentifiers:localIdentifiers];
  77. LookinConnectionResponseAttachment *responseAttachment = [LookinConnectionResponseAttachment new];
  78. responseAttachment.data = appInfo;
  79. [[LKS_ConnectionManager sharedInstance] respond:responseAttachment requestType:requestType tag:tag];
  80. } else if (requestType == LookinRequestTypeHierarchy) {
  81. LookinConnectionResponseAttachment *responseAttachment = [LookinConnectionResponseAttachment new];
  82. responseAttachment.data = [LookinHierarchyInfo staticInfo];
  83. [[LKS_ConnectionManager sharedInstance] respond:responseAttachment requestType:requestType tag:tag];
  84. } else if (requestType == LookinRequestTypeModification) {
  85. // 请求修改某个属性
  86. [LKS_AttrModificationHandler handleModification:object completion:^(LookinDisplayItemDetail *data, NSError *error) {
  87. LookinConnectionResponseAttachment *attachment = [LookinConnectionResponseAttachment new];
  88. if (error) {
  89. attachment.error = error;
  90. } else {
  91. attachment.data = data;
  92. }
  93. [[LKS_ConnectionManager sharedInstance] respond:attachment requestType:requestType tag:tag];
  94. }];
  95. } else if (requestType == LookinRequestTypeAttrModificationPatch) {
  96. NSArray<LookinStaticAsyncUpdateTask *> *tasks = object;
  97. NSUInteger dataTotalCount = tasks.count;
  98. [LKS_AttrModificationHandler handlePatchWithTasks:tasks block:^(LookinDisplayItemDetail *data) {
  99. LookinConnectionResponseAttachment *attrAttachment = [LookinConnectionResponseAttachment new];
  100. attrAttachment.data = data;
  101. attrAttachment.dataTotalCount = dataTotalCount;
  102. attrAttachment.currentDataCount = 1;
  103. [[LKS_ConnectionManager sharedInstance] respond:attrAttachment requestType:LookinRequestTypeAttrModificationPatch tag:tag];
  104. }];
  105. } else if (requestType == LookinRequestTypeHierarchyDetails) {
  106. NSArray<LookinStaticAsyncUpdateTasksPackage *> *packages = object;
  107. NSUInteger responsesDataTotalCount = [packages lookin_reduceInteger:^NSInteger(NSInteger accumulator, NSUInteger idx, LookinStaticAsyncUpdateTasksPackage *package) {
  108. accumulator += package.tasks.count;
  109. return accumulator;
  110. } initialAccumlator:0];
  111. [[LKS_HierarchyDetailsHandler sharedInstance] startWithPackages:packages block:^(NSArray<LookinDisplayItemDetail *> *details, NSError *error) {
  112. LookinConnectionResponseAttachment *attachment = [LookinConnectionResponseAttachment new];
  113. attachment.error = error;
  114. attachment.data = details;
  115. attachment.dataTotalCount = responsesDataTotalCount;
  116. attachment.currentDataCount = details.count;
  117. [[LKS_ConnectionManager sharedInstance] respond:attachment requestType:LookinRequestTypeHierarchyDetails tag:tag];
  118. }];
  119. } else if (requestType == LookinRequestTypeFetchObject) {
  120. unsigned long oid = ((NSNumber *)object).unsignedLongValue;
  121. NSObject *object = [NSObject lks_objectWithOid:oid];
  122. LookinObject *lookinObj = [LookinObject instanceWithObject:object];
  123. LookinConnectionResponseAttachment *attach = [LookinConnectionResponseAttachment new];
  124. attach.data = lookinObj;
  125. [[LKS_ConnectionManager sharedInstance] respond:attach requestType:requestType tag:tag];
  126. } else if (requestType == LookinRequestTypeAllAttrGroups) {
  127. unsigned long oid = ((NSNumber *)object).unsignedLongValue;
  128. CALayer *layer = (CALayer *)[NSObject lks_objectWithOid:oid];
  129. if (![layer isKindOfClass:[CALayer class]]) {
  130. [self _submitResponseWithError:LookinErr_ObjNotFound requestType:LookinRequestTypeAllAttrGroups tag:tag];
  131. return;
  132. }
  133. NSArray<LookinAttributesGroup *> *list = [LKS_AttrGroupsMaker attrGroupsForLayer:layer];
  134. [self _submitResponseWithData:list requestType:LookinRequestTypeAllAttrGroups tag:tag];
  135. } else if (requestType == LookinRequestTypeAllSelectorNames) {
  136. NSDictionary *params = object;
  137. Class targetClass = NSClassFromString(params[@"className"]);
  138. BOOL hasArg = [(NSNumber *)params[@"hasArg"] boolValue];
  139. if (!targetClass) {
  140. NSString *errorMsg = [NSString stringWithFormat:LKS_Localized(@"Didn't find the class named \"%@\". Please input another class and try again."), object];
  141. [self _submitResponseWithError:LookinErrorMake(errorMsg, @"") requestType:requestType tag:tag];
  142. return;
  143. }
  144. NSArray<NSString *> *selNames = [self _methodNameListForClass:targetClass hasArg:hasArg];
  145. [self _submitResponseWithData:selNames requestType:requestType tag:tag];
  146. } else if (requestType == LookinRequestTypeAddMethodTrace) {
  147. if (![object isKindOfClass:[NSDictionary class]]) {
  148. [self _submitResponseWithError:LookinErr_Inner requestType:requestType tag:tag];
  149. return;
  150. }
  151. NSDictionary *dict = object;
  152. NSString *className = dict[@"className"];
  153. NSString *selName = dict[@"selName"];
  154. Class targetClass = NSClassFromString(className);
  155. if (!targetClass) {
  156. NSString *errorMsg = [NSString stringWithFormat:LKS_Localized(@"Didn't find the class named \"%@\". Please input another class and try again."), object];
  157. [self _submitResponseWithError:LookinErrorMake(errorMsg, @"") requestType:requestType tag:tag];
  158. return;
  159. }
  160. SEL targetSelector = NSSelectorFromString(selName);
  161. if (class_getInstanceMethod(targetClass, targetSelector) == NULL) {
  162. NSString *errorMsg = [NSString stringWithFormat:LKS_Localized(@"%@ doesn't have a method called \"%@\". Please input another method name and try again."), className, selName];
  163. [self _submitResponseWithError:LookinErrorMake(errorMsg, @"") requestType:requestType tag:tag];
  164. return;
  165. }
  166. [[LKS_MethodTraceManager sharedInstance] addWithClassName:className selName:selName];
  167. [self _submitResponseWithData:[LKS_MethodTraceManager sharedInstance].currentActiveTraceList requestType:requestType tag:tag];
  168. } else if (requestType == LookinRequestTypeDeleteMethodTrace) {
  169. if (![object isKindOfClass:[NSDictionary class]]) {
  170. [self _submitResponseWithError:LookinErr_Inner requestType:requestType tag:tag];
  171. return;
  172. }
  173. NSDictionary *dict = object;
  174. NSString *className = dict[@"className"];
  175. NSString *selName = dict[@"selName"];
  176. [[LKS_MethodTraceManager sharedInstance] removeWithClassName:className selName:selName];
  177. [self _submitResponseWithData:[LKS_MethodTraceManager sharedInstance].currentActiveTraceList requestType:requestType tag:tag];
  178. } else if (requestType == LookinRequestTypeClassesAndMethodTraceLit) {
  179. LKS_MethodTraceManager *mng = [LKS_MethodTraceManager sharedInstance];
  180. NSDictionary *dict = @{@"classes":mng.allClassesListInApp, @"activeList":mng.currentActiveTraceList};
  181. [self _submitResponseWithData:dict requestType:requestType tag:tag];
  182. } else if (requestType == LookinRequestTypeInvokeMethod) {
  183. NSDictionary *param = object;
  184. unsigned long oid = [param[@"oid"] unsignedLongValue];
  185. NSString *text = param[@"text"];
  186. if (!text.length) {
  187. [self _submitResponseWithError:LookinErr_Inner requestType:requestType tag:tag];
  188. return;
  189. }
  190. NSObject *targerObj = [NSObject lks_objectWithOid:oid];
  191. if (!targerObj) {
  192. [self _submitResponseWithError:LookinErr_ObjNotFound requestType:requestType tag:tag];
  193. return;
  194. }
  195. SEL targetSelector = NSSelectorFromString(text);
  196. if (targetSelector && [targerObj respondsToSelector:targetSelector]) {
  197. NSString *resultDescription;
  198. NSObject *resultObject;
  199. NSError *error;
  200. [self _handleInvokeWithObject:targerObj selector:targetSelector resultDescription:&resultDescription resultObject:&resultObject error:&error];
  201. if (error) {
  202. [self _submitResponseWithError:error requestType:requestType tag:tag];
  203. return;
  204. }
  205. NSMutableDictionary *responseData = [NSMutableDictionary dictionaryWithCapacity:2];
  206. if (resultDescription) {
  207. responseData[@"description"] = resultDescription;
  208. }
  209. if (resultObject) {
  210. responseData[@"object"] = resultObject;
  211. }
  212. [self _submitResponseWithData:responseData requestType:requestType tag:tag];
  213. } else {
  214. NSString *errMsg = [NSString stringWithFormat:LKS_Localized(@"%@ doesn't have an instance method called \"%@\"."), NSStringFromClass(targerObj.class), text];
  215. [self _submitResponseWithError:LookinErrorMake(errMsg, @"") requestType:requestType tag:tag];
  216. }
  217. } else if (requestType == LookinPush_BringForwardScreenshotTask) {
  218. [[LKS_HierarchyDetailsHandler sharedInstance] bringForwardWithPackages:object];
  219. } else if (requestType == LookinPush_CanceHierarchyDetails) {
  220. [[LKS_HierarchyDetailsHandler sharedInstance] cancel];
  221. } else if (requestType == LookinRequestTypeFetchImageViewImage) {
  222. if (![object isKindOfClass:[NSNumber class]]) {
  223. [self _submitResponseWithError:LookinErr_Inner requestType:requestType tag:tag];
  224. return;
  225. }
  226. unsigned long imageViewOid = [(NSNumber *)object unsignedLongValue];
  227. UIImageView *imageView = (UIImageView *)[NSObject lks_objectWithOid:imageViewOid];
  228. if (!imageView) {
  229. [self _submitResponseWithError:LookinErr_ObjNotFound requestType:requestType tag:tag];
  230. return;
  231. }
  232. if (![imageView isKindOfClass:[UIImageView class]]) {
  233. [self _submitResponseWithError:LookinErr_Inner requestType:requestType tag:tag];
  234. return;
  235. }
  236. UIImage *image = imageView.image;
  237. NSData *imageData = [image lookin_data];
  238. [self _submitResponseWithData:imageData requestType:requestType tag:tag];
  239. } else if (requestType == LookinRequestTypeModifyRecognizerEnable) {
  240. NSDictionary<NSString *, NSNumber *> *params = object;
  241. unsigned long recognizerOid = ((NSNumber *)params[@"oid"]).unsignedLongValue;
  242. BOOL shouldBeEnabled = ((NSNumber *)params[@"enable"]).boolValue;
  243. UIGestureRecognizer *recognizer = (UIGestureRecognizer *)[NSObject lks_objectWithOid:recognizerOid];
  244. if (!recognizer) {
  245. [self _submitResponseWithError:LookinErr_ObjNotFound requestType:requestType tag:tag];
  246. return;
  247. }
  248. if (![recognizer isKindOfClass:[UIGestureRecognizer class]]) {
  249. [self _submitResponseWithError:LookinErr_Inner requestType:requestType tag:tag];
  250. return;
  251. }
  252. recognizer.enabled = shouldBeEnabled;
  253. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  254. // dispatch 以确保拿到的 enabled 是比较新的
  255. [self _submitResponseWithData:@(recognizer.enabled) requestType:requestType tag:tag];
  256. });
  257. }
  258. }
  259. - (NSArray<NSString *> *)_methodNameListForClass:(Class)aClass hasArg:(BOOL)hasArg {
  260. NSSet<NSString *> *prefixesToVoid = [NSSet setWithObjects:@"_", @"CA_", @"cpl", @"mf_", @"vs_", @"pep_", @"isNS", @"avkit_", @"PG_", @"px_", @"pl_", @"nsli_", @"pu_", @"pxg_", nil];
  261. NSMutableArray<NSString *> *array = [NSMutableArray array];
  262. Class currentClass = aClass;
  263. while (currentClass) {
  264. NSString *className = NSStringFromClass(currentClass);
  265. BOOL isSystemClass = ([className hasPrefix:@"UI"] || [className hasPrefix:@"CA"] || [className hasPrefix:@"NS"]);
  266. unsigned int methodCount = 0;
  267. Method *methods = class_copyMethodList(currentClass, &methodCount);
  268. for (unsigned int i = 0; i < methodCount; i++) {
  269. NSString *selName = NSStringFromSelector(method_getName(methods[i]));
  270. if (!hasArg && [selName containsString:@":"]) {
  271. continue;
  272. }
  273. if (isSystemClass) {
  274. BOOL invalid = [prefixesToVoid lookin_any:^BOOL(NSString *prefix) {
  275. return [selName hasPrefix:prefix];
  276. }];
  277. if (invalid) {
  278. continue;
  279. }
  280. }
  281. if (selName.length && ![array containsObject:selName]) {
  282. [array addObject:selName];
  283. }
  284. }
  285. if (methods) free(methods);
  286. currentClass = [currentClass superclass];
  287. }
  288. return [array lookin_sortedArrayByStringLength];
  289. }
  290. - (void)_handleInvokeWithObject:(NSObject *)obj selector:(SEL)selector resultDescription:(NSString **)description resultObject:(LookinObject **)resultObject error:(NSError **)error {
  291. NSMethodSignature *signature = [obj methodSignatureForSelector:selector];
  292. if (signature.numberOfArguments > 2) {
  293. *error = LookinErrorMake(LKS_Localized(@"Lookin doesn't support invoking methods with arguments yet."), @"");
  294. return;
  295. }
  296. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
  297. [invocation setTarget:obj];
  298. [invocation setSelector:selector];
  299. [invocation invoke];
  300. const char *returnType = [signature methodReturnType];
  301. if (strcmp(returnType, @encode(void)) == 0) {
  302. //void, do nothing
  303. *description = LookinStringFlag_VoidReturn;
  304. } else if (strcmp(returnType, @encode(char)) == 0) {
  305. char charValue;
  306. [invocation getReturnValue:&charValue];
  307. *description = [NSString stringWithFormat:@"%@", @(charValue)];
  308. } else if (strcmp(returnType, @encode(int)) == 0) {
  309. int intValue;
  310. [invocation getReturnValue:&intValue];
  311. if (intValue == INT_MAX) {
  312. *description = @"INT_MAX";
  313. } else if (intValue == INT_MIN) {
  314. *description = @"INT_MIN";
  315. } else {
  316. *description = [NSString stringWithFormat:@"%@", @(intValue)];
  317. }
  318. } else if (strcmp(returnType, @encode(short)) == 0) {
  319. short shortValue;
  320. [invocation getReturnValue:&shortValue];
  321. if (shortValue == SHRT_MAX) {
  322. *description = @"SHRT_MAX";
  323. } else if (shortValue == SHRT_MIN) {
  324. *description = @"SHRT_MIN";
  325. } else {
  326. *description = [NSString stringWithFormat:@"%@", @(shortValue)];
  327. }
  328. } else if (strcmp(returnType, @encode(long)) == 0) {
  329. long longValue;
  330. [invocation getReturnValue:&longValue];
  331. if (longValue == NSNotFound) {
  332. *description = @"NSNotFound";
  333. } else if (longValue == LONG_MAX) {
  334. *description = @"LONG_MAX";
  335. } else if (longValue == LONG_MIN) {
  336. *description = @"LONG_MAX";
  337. } else {
  338. *description = [NSString stringWithFormat:@"%@", @(longValue)];
  339. }
  340. } else if (strcmp(returnType, @encode(long long)) == 0) {
  341. long long longLongValue;
  342. [invocation getReturnValue:&longLongValue];
  343. if (longLongValue == LLONG_MAX) {
  344. *description = @"LLONG_MAX";
  345. } else if (longLongValue == LLONG_MIN) {
  346. *description = @"LLONG_MIN";
  347. } else {
  348. *description = [NSString stringWithFormat:@"%@", @(longLongValue)];
  349. }
  350. } else if (strcmp(returnType, @encode(unsigned char)) == 0) {
  351. unsigned char ucharValue;
  352. [invocation getReturnValue:&ucharValue];
  353. if (ucharValue == UCHAR_MAX) {
  354. *description = @"UCHAR_MAX";
  355. } else {
  356. *description = [NSString stringWithFormat:@"%@", @(ucharValue)];
  357. }
  358. } else if (strcmp(returnType, @encode(unsigned int)) == 0) {
  359. unsigned int uintValue;
  360. [invocation getReturnValue:&uintValue];
  361. if (uintValue == UINT_MAX) {
  362. *description = @"UINT_MAX";
  363. } else {
  364. *description = [NSString stringWithFormat:@"%@", @(uintValue)];
  365. }
  366. } else if (strcmp(returnType, @encode(unsigned short)) == 0) {
  367. unsigned short ushortValue;
  368. [invocation getReturnValue:&ushortValue];
  369. if (ushortValue == USHRT_MAX) {
  370. *description = @"USHRT_MAX";
  371. } else {
  372. *description = [NSString stringWithFormat:@"%@", @(ushortValue)];
  373. }
  374. } else if (strcmp(returnType, @encode(unsigned long)) == 0) {
  375. unsigned long ulongValue;
  376. [invocation getReturnValue:&ulongValue];
  377. if (ulongValue == ULONG_MAX) {
  378. *description = @"ULONG_MAX";
  379. } else {
  380. *description = [NSString stringWithFormat:@"%@", @(ulongValue)];
  381. }
  382. } else if (strcmp(returnType, @encode(unsigned long long)) == 0) {
  383. unsigned long long ulongLongValue;
  384. [invocation getReturnValue:&ulongLongValue];
  385. if (ulongLongValue == ULONG_LONG_MAX) {
  386. *description = @"ULONG_LONG_MAX";
  387. } else {
  388. *description = [NSString stringWithFormat:@"%@", @(ulongLongValue)];
  389. }
  390. } else if (strcmp(returnType, @encode(float)) == 0) {
  391. float floatValue;
  392. [invocation getReturnValue:&floatValue];
  393. if (floatValue == FLT_MAX) {
  394. *description = @"FLT_MAX";
  395. } else if (floatValue == FLT_MIN) {
  396. *description = @"FLT_MIN";
  397. } else {
  398. *description = [NSString stringWithFormat:@"%@", @(floatValue)];
  399. }
  400. } else if (strcmp(returnType, @encode(double)) == 0) {
  401. double doubleValue;
  402. [invocation getReturnValue:&doubleValue];
  403. if (doubleValue == DBL_MAX) {
  404. *description = @"DBL_MAX";
  405. } else if (doubleValue == DBL_MIN) {
  406. *description = @"DBL_MIN";
  407. } else {
  408. *description = [NSString stringWithFormat:@"%@", @(doubleValue)];
  409. }
  410. } else if (strcmp(returnType, @encode(BOOL)) == 0) {
  411. BOOL boolValue;
  412. [invocation getReturnValue:&boolValue];
  413. *description = boolValue ? @"YES" : @"NO";
  414. } else if (strcmp(returnType, @encode(SEL)) == 0) {
  415. SEL selValue;
  416. [invocation getReturnValue:&selValue];
  417. *description = [NSString stringWithFormat:@"SEL(%@)", NSStringFromSelector(selValue)];
  418. } else if (strcmp(returnType, @encode(Class)) == 0) {
  419. Class classValue;
  420. [invocation getReturnValue:&classValue];
  421. *description = [NSString stringWithFormat:@"<%@>", NSStringFromClass(classValue)];
  422. } else if (strcmp(returnType, @encode(CGPoint)) == 0) {
  423. CGPoint targetValue;
  424. [invocation getReturnValue:&targetValue];
  425. *description = NSStringFromCGPoint(targetValue);
  426. } else if (strcmp(returnType, @encode(CGVector)) == 0) {
  427. CGVector targetValue;
  428. [invocation getReturnValue:&targetValue];
  429. *description = NSStringFromCGVector(targetValue);
  430. } else if (strcmp(returnType, @encode(CGSize)) == 0) {
  431. CGSize targetValue;
  432. [invocation getReturnValue:&targetValue];
  433. *description = NSStringFromCGSize(targetValue);
  434. } else if (strcmp(returnType, @encode(CGRect)) == 0) {
  435. CGRect rectValue;
  436. [invocation getReturnValue:&rectValue];
  437. *description = NSStringFromCGRect(rectValue);
  438. } else if (strcmp(returnType, @encode(CGAffineTransform)) == 0) {
  439. CGAffineTransform rectValue;
  440. [invocation getReturnValue:&rectValue];
  441. *description = NSStringFromCGAffineTransform(rectValue);
  442. } else if (strcmp(returnType, @encode(UIEdgeInsets)) == 0) {
  443. UIEdgeInsets targetValue;
  444. [invocation getReturnValue:&targetValue];
  445. *description = NSStringFromUIEdgeInsets(targetValue);
  446. } else if (strcmp(returnType, @encode(UIOffset)) == 0) {
  447. UIOffset targetValue;
  448. [invocation getReturnValue:&targetValue];
  449. *description = NSStringFromUIOffset(targetValue);
  450. } else {
  451. if (@available(iOS 11.0, tvOS 11.0, *)) {
  452. if (strcmp(returnType, @encode(NSDirectionalEdgeInsets)) == 0) {
  453. NSDirectionalEdgeInsets targetValue;
  454. [invocation getReturnValue:&targetValue];
  455. *description = NSStringFromDirectionalEdgeInsets(targetValue);
  456. return;
  457. }
  458. }
  459. NSString *argType_string = [[NSString alloc] lookin_safeInitWithUTF8String:returnType];
  460. if ([argType_string hasPrefix:@"@"] || [argType_string hasPrefix:@"^{"]) {
  461. __unsafe_unretained id returnObjValue;
  462. [invocation getReturnValue:&returnObjValue];
  463. if (returnObjValue) {
  464. *description = [NSString stringWithFormat:@"%@", returnObjValue];
  465. LookinObject *parsedLookinObj = [LookinObject instanceWithObject:returnObjValue];
  466. *resultObject = parsedLookinObj;
  467. } else {
  468. *description = @"nil";
  469. }
  470. } else {
  471. *description = [NSString stringWithFormat:LKS_Localized(@"%@ was invoked successfully, but Lookin can't parse the return value:%@"), NSStringFromSelector(selector), argType_string];
  472. }
  473. }
  474. }
  475. - (void)_submitResponseWithError:(NSError *)error requestType:(uint32_t)requestType tag:(uint32_t)tag {
  476. LookinConnectionResponseAttachment *attachment = [LookinConnectionResponseAttachment new];
  477. attachment.error = error;
  478. [[LKS_ConnectionManager sharedInstance] respond:attachment requestType:requestType tag:tag];
  479. }
  480. - (void)_submitResponseWithData:(NSObject *)data requestType:(uint32_t)requestType tag:(uint32_t)tag {
  481. LookinConnectionResponseAttachment *attachment = [LookinConnectionResponseAttachment new];
  482. attachment.data = data;
  483. [[LKS_ConnectionManager sharedInstance] respond:attachment requestType:requestType tag:tag];
  484. }
  485. @end
  486. #endif /* SHOULD_COMPILE_LOOKIN_SERVER */