LKS_RequestHandler.m 24 KB

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