QMUIRuntime.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**
  2. * Tencent is pleased to support the open source community by making QMUI_iOS available.
  3. * Copyright (C) 2016-2021 THL A29 Limited, a Tencent company. All rights reserved.
  4. * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
  5. * http://opensource.org/licenses/MIT
  6. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  7. */
  8. //
  9. // QMUIRuntime.m
  10. // QMUIKit
  11. //
  12. // Created by QMUI Team on 2018/9/5.
  13. //
  14. #import "QMUIRuntime.h"
  15. #import "QMUICommonDefines.h"
  16. #import "QMUIHelper.h"
  17. #include <mach-o/getsect.h>
  18. #include <mach-o/dyld.h>
  19. @implementation QMUIPropertyDescriptor
  20. + (instancetype)descriptorWithProperty:(objc_property_t)property {
  21. QMUIPropertyDescriptor *descriptor = [[self alloc] init];
  22. NSString *propertyName = [NSString stringWithUTF8String:property_getName(property)];
  23. descriptor.name = propertyName;
  24. // getter
  25. char *getterChar = property_copyAttributeValue(property, "G");
  26. descriptor.getter = NSSelectorFromString(getterChar != NULL ? [NSString stringWithUTF8String:getterChar] : propertyName);
  27. if (getterChar != NULL) {
  28. free(getterChar);
  29. }
  30. // setter
  31. char *setterChar = property_copyAttributeValue(property, "S");
  32. NSString *setterString = setterChar != NULL ? [NSString stringWithUTF8String:setterChar] : NSStringFromSelector(setterWithGetter(NSSelectorFromString(propertyName)));
  33. descriptor.setter = NSSelectorFromString(setterString);
  34. if (setterChar != NULL) {
  35. free(setterChar);
  36. }
  37. // atomic/nonatomic
  38. char *attrValue_N = property_copyAttributeValue(property, "N");
  39. BOOL isAtomic = (attrValue_N == NULL);
  40. descriptor.isAtomic = isAtomic;
  41. descriptor.isNonatomic = !isAtomic;
  42. if (attrValue_N != NULL) {
  43. free(attrValue_N);
  44. }
  45. // assign/weak/strong/copy
  46. char *attrValue_isCopy = property_copyAttributeValue(property, "C");
  47. char *attrValue_isStrong = property_copyAttributeValue(property, "&");
  48. char *attrValue_isWeak = property_copyAttributeValue(property, "W");
  49. BOOL isCopy = attrValue_isCopy != NULL;
  50. BOOL isStrong = attrValue_isStrong != NULL;
  51. BOOL isWeak = attrValue_isWeak != NULL;
  52. if (attrValue_isCopy != NULL) {
  53. free(attrValue_isCopy);
  54. }
  55. if (attrValue_isStrong != NULL) {
  56. free(attrValue_isStrong);
  57. }
  58. if (attrValue_isWeak != NULL) {
  59. free(attrValue_isWeak);
  60. }
  61. descriptor.isCopy = isCopy;
  62. descriptor.isStrong = isStrong;
  63. descriptor.isWeak = isWeak;
  64. descriptor.isAssign = !isCopy && !isStrong && !isWeak;
  65. // readonly/readwrite
  66. char *attrValue_isReadonly = property_copyAttributeValue(property, "R");
  67. BOOL isReadonly = (attrValue_isReadonly != NULL);
  68. if (attrValue_isReadonly != NULL) {
  69. free(attrValue_isReadonly);
  70. }
  71. descriptor.isReadonly = isReadonly;
  72. descriptor.isReadwrite = !isReadonly;
  73. // type
  74. char *type = property_copyAttributeValue(property, "T");
  75. descriptor.type = [QMUIPropertyDescriptor typeWithEncodeString:[NSString stringWithUTF8String:type]];
  76. if (type != NULL) {
  77. free(type);
  78. }
  79. return descriptor;
  80. }
  81. - (NSString *)description {
  82. NSMutableString *result = [[NSMutableString alloc] init];
  83. [result appendString:@"@property("];
  84. if (self.isNonatomic) [result appendString:@"nonatomic, "];
  85. [result appendString:self.isAssign ? @"assign" : (self.isWeak ? @"weak" : (self.isStrong ? @"strong" : @"copy"))];
  86. if (self.isReadonly) [result appendString:@", readonly"];
  87. if (![NSStringFromSelector(self.getter) isEqualToString:self.name]) [result appendFormat:@", getter=%@", NSStringFromSelector(self.getter)];
  88. if (self.setter != setterWithGetter(NSSelectorFromString(self.name))) [result appendFormat:@", setter=%@", NSStringFromSelector(self.setter)];
  89. [result appendString:@") "];
  90. [result appendString:self.type];
  91. [result appendString:@" "];
  92. [result appendString:self.name];
  93. [result appendString:@";"];
  94. return result.copy;
  95. }
  96. #define _DetectTypeAndReturn(_type) if (strncmp(@encode(_type), typeEncoding, strlen(@encode(_type))) == 0) return @#_type;
  97. + (NSString *)typeWithEncodeString:(NSString *)encodeString {
  98. if ([encodeString containsString:@"@\""]) {
  99. NSString *result = [encodeString substringWithRange:NSMakeRange(2, encodeString.length - 2 - 1)];
  100. if ([result containsString:@"<"] && [result containsString:@">"]) {
  101. // protocol
  102. if ([result hasPrefix:@"<"]) {
  103. // id pointer
  104. return [NSString stringWithFormat:@"id%@", result];
  105. }
  106. }
  107. // class
  108. return [NSString stringWithFormat:@"%@ *", result];
  109. }
  110. const char *typeEncoding = encodeString.UTF8String;
  111. _DetectTypeAndReturn(NSInteger)
  112. _DetectTypeAndReturn(NSUInteger)
  113. _DetectTypeAndReturn(int)
  114. _DetectTypeAndReturn(short)
  115. _DetectTypeAndReturn(long)
  116. _DetectTypeAndReturn(long long)
  117. _DetectTypeAndReturn(char)
  118. _DetectTypeAndReturn(unsigned char)
  119. _DetectTypeAndReturn(unsigned int)
  120. _DetectTypeAndReturn(unsigned short)
  121. _DetectTypeAndReturn(unsigned long)
  122. _DetectTypeAndReturn(unsigned long long)
  123. _DetectTypeAndReturn(CGFloat)
  124. _DetectTypeAndReturn(float)
  125. _DetectTypeAndReturn(double)
  126. _DetectTypeAndReturn(void)
  127. _DetectTypeAndReturn(char *)
  128. _DetectTypeAndReturn(id)
  129. _DetectTypeAndReturn(Class)
  130. _DetectTypeAndReturn(SEL)
  131. _DetectTypeAndReturn(BOOL)
  132. return encodeString;
  133. }
  134. @end
  135. #ifndef __LP64__
  136. typedef struct mach_header headerType;
  137. #else
  138. typedef struct mach_header_64 headerType;
  139. #endif
  140. static BOOL strendswith(const char *str, const char *suffix) {
  141. if (!str || !suffix) return NO;
  142. size_t lenstr = strlen(str);
  143. size_t lensuffix = strlen(suffix);
  144. if (lensuffix > lenstr) return NO;
  145. return strncmp(str + lenstr - lensuffix, suffix, lensuffix) == 0;
  146. }
  147. static const headerType *getProjectImageHeader() {
  148. const uint32_t imageCount = _dyld_image_count();
  149. NSString *executablePath = NSBundle.mainBundle.executablePath;
  150. if (!executablePath) return nil;
  151. const headerType *target_image_header = 0;
  152. for (uint32_t i = 0; i < imageCount; i++) {
  153. const char *image_name = _dyld_get_image_name(i);// name 是一串完整的文件路径,以 image 名结尾
  154. NSString *imagePath = [NSString stringWithUTF8String:image_name];
  155. if ([imagePath isEqualToString:executablePath]) {
  156. target_image_header = (headerType *)_dyld_get_image_header(i);
  157. break;
  158. }
  159. }
  160. return target_image_header;
  161. }
  162. // from https://github.com/opensource-apple/objc4/blob/master/runtime/objc-file.mm
  163. static classref_t *getDataSection(const headerType *machHeader, const char *sectname, size_t *outCount) {
  164. if (!machHeader) return nil;
  165. unsigned long byteCount = 0;
  166. classref_t *data = (classref_t *)getsectiondata(machHeader, "__DATA", sectname, &byteCount);
  167. if (!data) {
  168. data = (classref_t *)getsectiondata(machHeader, "__DATA_CONST", sectname, &byteCount);
  169. }
  170. if (!data) {
  171. data = (classref_t *)getsectiondata(machHeader, "__DATA_DIRTY", sectname, &byteCount);
  172. }
  173. if (outCount) *outCount = byteCount / sizeof(classref_t);
  174. return data;
  175. }
  176. int qmui_getProjectClassList(classref_t **classes) {
  177. size_t count = 0;
  178. if (!!classes) {
  179. *classes = getDataSection(getProjectImageHeader(), "__objc_classlist", &count);
  180. } else {
  181. getDataSection(getProjectImageHeader(), "__objc_classlist", &count);
  182. }
  183. return (int)count;
  184. }
  185. BOOL qmui_exists_dyld_image(const char *target_image_name) {
  186. const uint32_t imageCount = _dyld_image_count();
  187. for (uint32_t i = 0; i < imageCount; i++) {
  188. const char *image_name = _dyld_get_image_name(i);
  189. if (strendswith(image_name, target_image_name)) {
  190. return true;
  191. }
  192. }
  193. return false;
  194. }