LKS_AttrGroupsMaker.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. //
  2. // LKS_AttrGroupsMaker.m
  3. // LookinServer
  4. //
  5. // Created by Li Kai on 2019/6/6.
  6. // https://lookin.work
  7. //
  8. #import "LKS_AttrGroupsMaker.h"
  9. #import "LookinAttributesGroup.h"
  10. #import "LookinAttributesSection.h"
  11. #import "LookinAttribute.h"
  12. #import "LookinDashboardBlueprint.h"
  13. #import "LookinIvarTrace.h"
  14. #import "UIColor+LookinServer.h"
  15. #import "LookinServerDefines.h"
  16. @implementation LKS_AttrGroupsMaker
  17. + (NSArray<LookinAttributesGroup *> *)attrGroupsForLayer:(CALayer *)layer {
  18. if (!layer) {
  19. NSAssert(NO, @"");
  20. return nil;
  21. }
  22. NSArray<LookinAttributesGroup *> *groups = [[LookinDashboardBlueprint groupIDs] lookin_map:^id(NSUInteger idx, LookinAttrGroupIdentifier groupID) {
  23. LookinAttributesGroup *group = [LookinAttributesGroup new];
  24. group.identifier = groupID;
  25. NSArray<LookinAttrSectionIdentifier> *secIDs = [LookinDashboardBlueprint sectionIDsForGroupID:groupID];
  26. group.attrSections = [secIDs lookin_map:^id(NSUInteger idx, LookinAttrSectionIdentifier secID) {
  27. LookinAttributesSection *sec = [LookinAttributesSection new];
  28. sec.identifier = secID;
  29. NSArray<LookinAttrIdentifier> *attrIDs = [LookinDashboardBlueprint attrIDsForSectionID:secID];
  30. sec.attributes = [attrIDs lookin_map:^id(NSUInteger idx, LookinAttrIdentifier attrID) {
  31. NSInteger minAvailableVersion = [LookinDashboardBlueprint minAvailableOSVersionWithAttrID:attrID];
  32. if (minAvailableVersion > 0 && (NSProcessInfo.processInfo.operatingSystemVersion.majorVersion < minAvailableVersion)) {
  33. // iOS 版本过低不支持该属性
  34. return nil;
  35. }
  36. id targetObj = nil;
  37. if ([LookinDashboardBlueprint isUIViewPropertyWithAttrID:attrID]) {
  38. targetObj = layer.lks_hostView;
  39. } else {
  40. targetObj = layer;
  41. }
  42. if (targetObj) {
  43. Class targetClass = NSClassFromString([LookinDashboardBlueprint classNameWithAttrID:attrID]);
  44. if (![targetObj isKindOfClass:targetClass]) {
  45. return nil;
  46. }
  47. LookinAttribute *attr = [self _attributeWithIdentifer:attrID targetObject:targetObj];
  48. return attr;
  49. } else {
  50. return nil;
  51. }
  52. }];
  53. if (sec.attributes.count) {
  54. return sec;
  55. } else {
  56. return nil;
  57. }
  58. }];
  59. if ([groupID isEqualToString:LookinAttrGroup_AutoLayout]) {
  60. // 这里特殊处理一下,如果 AutoLayout 里面不包含 Constraints 的话(只有 Hugging 和 Resistance),就丢弃掉这整个 AutoLayout 不显示
  61. BOOL hasConstraits = [group.attrSections lookin_any:^BOOL(LookinAttributesSection *obj) {
  62. return [obj.identifier isEqualToString:LookinAttrSec_AutoLayout_Constraints];
  63. }];
  64. if (!hasConstraits) {
  65. return nil;
  66. }
  67. }
  68. if (group.attrSections.count) {
  69. return group;
  70. } else {
  71. return nil;
  72. }
  73. }];
  74. return groups;
  75. }
  76. + (LookinAttribute *)_attributeWithIdentifer:(LookinAttrIdentifier)identifier targetObject:(id)target {
  77. if (!target) {
  78. NSAssert(NO, @"");
  79. return nil;
  80. }
  81. LookinAttribute *attribute = [LookinAttribute new];
  82. attribute.identifier = identifier;
  83. SEL getter = [LookinDashboardBlueprint getterWithAttrID:identifier];
  84. if (!getter) {
  85. NSAssert(NO, @"");
  86. return nil;
  87. }
  88. if (![target respondsToSelector:getter]) {
  89. // 比如某些 QMUI 的属性,不引入 QMUI 就会走到这个分支里
  90. return nil;
  91. }
  92. NSMethodSignature *signature = [target methodSignatureForSelector:getter];
  93. if (signature.numberOfArguments > 2) {
  94. NSAssert(NO, @"getter 不可以有参数");
  95. return nil;
  96. }
  97. if (strcmp([signature methodReturnType], @encode(void)) == 0) {
  98. NSAssert(NO, @"getter 返回值不能为 void");
  99. return nil;
  100. }
  101. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
  102. invocation.target = target;
  103. invocation.selector = getter;
  104. [invocation invoke];
  105. const char *returnType = [signature methodReturnType];
  106. if (strcmp(returnType, @encode(char)) == 0) {
  107. char targetValue;
  108. [invocation getReturnValue:&targetValue];
  109. attribute.attrType = LookinAttrTypeChar;
  110. attribute.value = @(targetValue);
  111. } else if (strcmp(returnType, @encode(int)) == 0) {
  112. int targetValue;
  113. [invocation getReturnValue:&targetValue];
  114. attribute.value = @(targetValue);
  115. if ([LookinDashboardBlueprint enumListNameWithAttrID:identifier]) {
  116. attribute.attrType = LookinAttrTypeEnumInt;
  117. } else {
  118. attribute.attrType = LookinAttrTypeInt;
  119. }
  120. } else if (strcmp(returnType, @encode(short)) == 0) {
  121. short targetValue;
  122. [invocation getReturnValue:&targetValue];
  123. attribute.attrType = LookinAttrTypeShort;
  124. attribute.value = @(targetValue);
  125. } else if (strcmp(returnType, @encode(long)) == 0) {
  126. long targetValue;
  127. [invocation getReturnValue:&targetValue];
  128. attribute.value = @(targetValue);
  129. if ([LookinDashboardBlueprint enumListNameWithAttrID:identifier]) {
  130. attribute.attrType = LookinAttrTypeEnumLong;
  131. } else {
  132. attribute.attrType = LookinAttrTypeLong;
  133. }
  134. } else if (strcmp(returnType, @encode(long long)) == 0) {
  135. long long targetValue;
  136. [invocation getReturnValue:&targetValue];
  137. attribute.attrType = LookinAttrTypeLongLong;
  138. attribute.value = @(targetValue);
  139. } else if (strcmp(returnType, @encode(unsigned char)) == 0) {
  140. unsigned char targetValue;
  141. [invocation getReturnValue:&targetValue];
  142. attribute.attrType = LookinAttrTypeUnsignedChar;
  143. attribute.value = @(targetValue);
  144. } else if (strcmp(returnType, @encode(unsigned int)) == 0) {
  145. unsigned int targetValue;
  146. [invocation getReturnValue:&targetValue];
  147. attribute.attrType = LookinAttrTypeUnsignedInt;
  148. attribute.value = @(targetValue);
  149. } else if (strcmp(returnType, @encode(unsigned short)) == 0) {
  150. unsigned short targetValue;
  151. [invocation getReturnValue:&targetValue];
  152. attribute.attrType = LookinAttrTypeUnsignedShort;
  153. attribute.value = @(targetValue);
  154. } else if (strcmp(returnType, @encode(unsigned long)) == 0) {
  155. unsigned long targetValue;
  156. [invocation getReturnValue:&targetValue];
  157. attribute.attrType = LookinAttrTypeUnsignedLong;
  158. attribute.value = @(targetValue);
  159. } else if (strcmp(returnType, @encode(unsigned long long)) == 0) {
  160. unsigned long long targetValue;
  161. [invocation getReturnValue:&targetValue];
  162. attribute.attrType = LookinAttrTypeUnsignedLongLong;
  163. attribute.value = @(targetValue);
  164. } else if (strcmp(returnType, @encode(float)) == 0) {
  165. float targetValue;
  166. [invocation getReturnValue:&targetValue];
  167. attribute.attrType = LookinAttrTypeFloat;
  168. attribute.value = @(targetValue);
  169. } else if (strcmp(returnType, @encode(double)) == 0) {
  170. double targetValue;
  171. [invocation getReturnValue:&targetValue];
  172. attribute.attrType = LookinAttrTypeDouble;
  173. attribute.value = @(targetValue);
  174. } else if (strcmp(returnType, @encode(BOOL)) == 0) {
  175. BOOL targetValue;
  176. [invocation getReturnValue:&targetValue];
  177. attribute.attrType = LookinAttrTypeBOOL;
  178. attribute.value = @(targetValue);
  179. } else if (strcmp(returnType, @encode(SEL)) == 0) {
  180. SEL targetValue;
  181. [invocation getReturnValue:&targetValue];
  182. attribute.attrType = LookinAttrTypeSel;
  183. attribute.value = NSStringFromSelector(targetValue);
  184. } else if (strcmp(returnType, @encode(Class)) == 0) {
  185. Class targetValue;
  186. [invocation getReturnValue:&targetValue];
  187. attribute.attrType = LookinAttrTypeClass;
  188. attribute.value = NSStringFromClass(targetValue);
  189. } else if (strcmp(returnType, @encode(CGPoint)) == 0) {
  190. CGPoint targetValue;
  191. [invocation getReturnValue:&targetValue];
  192. attribute.attrType = LookinAttrTypeCGPoint;
  193. attribute.value = [NSValue valueWithCGPoint:targetValue];
  194. } else if (strcmp(returnType, @encode(CGVector)) == 0) {
  195. CGVector targetValue;
  196. [invocation getReturnValue:&targetValue];
  197. attribute.attrType = LookinAttrTypeCGVector;
  198. attribute.value = [NSValue valueWithCGVector:targetValue];
  199. } else if (strcmp(returnType, @encode(CGSize)) == 0) {
  200. CGSize targetValue;
  201. [invocation getReturnValue:&targetValue];
  202. attribute.attrType = LookinAttrTypeCGSize;
  203. attribute.value = [NSValue valueWithCGSize:targetValue];
  204. } else if (strcmp(returnType, @encode(CGRect)) == 0) {
  205. CGRect targetValue;
  206. [invocation getReturnValue:&targetValue];
  207. attribute.attrType = LookinAttrTypeCGRect;
  208. attribute.value = [NSValue valueWithCGRect:targetValue];
  209. } else if (strcmp(returnType, @encode(CGAffineTransform)) == 0) {
  210. CGAffineTransform targetValue;
  211. [invocation getReturnValue:&targetValue];
  212. attribute.attrType = LookinAttrTypeCGAffineTransform;
  213. attribute.value = [NSValue valueWithCGAffineTransform:targetValue];
  214. } else if (strcmp(returnType, @encode(UIEdgeInsets)) == 0) {
  215. UIEdgeInsets targetValue;
  216. [invocation getReturnValue:&targetValue];
  217. attribute.attrType = LookinAttrTypeUIEdgeInsets;
  218. attribute.value = [NSValue valueWithUIEdgeInsets:targetValue];
  219. } else if (strcmp(returnType, @encode(UIOffset)) == 0) {
  220. UIOffset targetValue;
  221. [invocation getReturnValue:&targetValue];
  222. attribute.attrType = LookinAttrTypeUIOffset;
  223. attribute.value = [NSValue valueWithUIOffset:targetValue];
  224. } else {
  225. NSString *argType_string = [[NSString alloc] lookin_safeInitWithUTF8String:returnType];
  226. if ([argType_string hasPrefix:@"@"]) {
  227. __unsafe_unretained id returnObjValue;
  228. [invocation getReturnValue:&returnObjValue];
  229. if (!returnObjValue && [LookinDashboardBlueprint hideIfNilWithAttrID:identifier]) {
  230. // 对于某些属性,若 value 为 nil 则不显示
  231. return nil;
  232. }
  233. attribute.attrType = [LookinDashboardBlueprint objectAttrTypeWithAttrID:identifier];
  234. if (attribute.attrType == LookinAttrTypeUIColor) {
  235. attribute.value = [returnObjValue lks_rgbaComponents];
  236. } else {
  237. attribute.value = returnObjValue;
  238. }
  239. } else {
  240. NSAssert(NO, @"不支持解析该类型的返回值");
  241. return nil;
  242. }
  243. }
  244. return attribute;
  245. }
  246. @end