TBRuntimeController.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // FLEXRuntimeController.m
  3. // FLEX
  4. //
  5. // Created by Tanner on 3/23/17.
  6. //
  7. #import "TBRuntimeController.h"
  8. #import "TBRuntime.h"
  9. #import "FLEXMethod.h"
  10. @interface TBRuntimeController ()
  11. @property (nonatomic, readonly) NSCache *bundlePathsCache;
  12. @property (nonatomic, readonly) NSCache *bundleNamesCache;
  13. @property (nonatomic, readonly) NSCache *classNamesCache;
  14. @property (nonatomic, readonly) NSCache *methodsCache;
  15. @end
  16. @implementation TBRuntimeController
  17. #pragma mark Initialization
  18. static TBRuntimeController *controller = nil;
  19. + (instancetype)shared {
  20. static dispatch_once_t onceToken;
  21. dispatch_once(&onceToken, ^{
  22. controller = [self new];
  23. });
  24. return controller;
  25. }
  26. - (id)init {
  27. self = [super init];
  28. if (self) {
  29. _bundlePathsCache = [NSCache new];
  30. _bundleNamesCache = [NSCache new];
  31. _classNamesCache = [NSCache new];
  32. _methodsCache = [NSCache new];
  33. }
  34. return self;
  35. }
  36. #pragma mark Public
  37. + (NSArray *)dataForKeyPath:(TBKeyPath *)keyPath {
  38. if (keyPath.bundleKey) {
  39. if (keyPath.classKey) {
  40. if (keyPath.methodKey) {
  41. return [[self shared] methodsForKeyPath:keyPath];
  42. } else {
  43. return [[self shared] classesForKeyPath:keyPath];
  44. }
  45. } else {
  46. return [[self shared] bundleNamesForToken:keyPath.bundleKey];
  47. }
  48. } else {
  49. return @[];
  50. }
  51. }
  52. + (NSArray<NSArray<FLEXMethod *> *> *)methodsForToken:(TBToken *)token
  53. instance:(NSNumber *)inst
  54. inClasses:(NSArray<NSString*> *)classes {
  55. return [[TBRuntime runtime]
  56. methodsForToken:token
  57. instance:inst
  58. inClasses:classes
  59. ];
  60. }
  61. + (NSMutableArray<NSString *> *)classesForKeyPath:(TBKeyPath *)keyPath {
  62. return [[self shared] classesForKeyPath:keyPath];
  63. }
  64. + (NSString *)shortBundleNameForClass:(NSString *)name {
  65. const char *imageName = class_getImageName(NSClassFromString(name));
  66. if (!imageName) {
  67. return @"(unspecified)";
  68. }
  69. return [[TBRuntime runtime] shortNameForImageName:@(imageName)];
  70. }
  71. + (NSString *)imagePathWithShortName:(NSString *)suffix {
  72. return [[TBRuntime runtime] imageNameForShortName:suffix];
  73. }
  74. + (NSArray *)allBundleNames {
  75. return [TBRuntime runtime].imageDisplayNames;
  76. }
  77. #pragma mark Private
  78. - (NSMutableArray *)bundlePathsForToken:(TBToken *)token {
  79. // Only cache if no wildcard
  80. BOOL shouldCache = token == TBWildcardOptionsNone;
  81. if (shouldCache) {
  82. NSMutableArray<NSString*> *cached = [self.bundlePathsCache objectForKey:token];
  83. if (cached) {
  84. return cached;
  85. }
  86. NSMutableArray<NSString*> *bundles = [[TBRuntime runtime] bundlePathsForToken:token];
  87. [self.bundlePathsCache setObject:bundles forKey:token];
  88. return bundles;
  89. }
  90. else {
  91. return [[TBRuntime runtime] bundlePathsForToken:token];
  92. }
  93. }
  94. - (NSMutableArray<NSString *> *)bundleNamesForToken:(TBToken *)token {
  95. // Only cache if no wildcard
  96. BOOL shouldCache = token == TBWildcardOptionsNone;
  97. if (shouldCache) {
  98. NSMutableArray<NSString*> *cached = [self.bundleNamesCache objectForKey:token];
  99. if (cached) {
  100. return cached;
  101. }
  102. NSMutableArray<NSString*> *bundles = [[TBRuntime runtime] bundleNamesForToken:token];
  103. [self.bundleNamesCache setObject:bundles forKey:token];
  104. return bundles;
  105. }
  106. else {
  107. return [[TBRuntime runtime] bundleNamesForToken:token];
  108. }
  109. }
  110. - (NSMutableArray<NSString *> *)classesForKeyPath:(TBKeyPath *)keyPath {
  111. TBToken *classToken = keyPath.classKey;
  112. TBToken *bundleToken = keyPath.bundleKey;
  113. // Only cache if no wildcard
  114. BOOL shouldCache = bundleToken.options == 0 && classToken.options == 0;
  115. NSString *key = nil;
  116. if (shouldCache) {
  117. key = [@[bundleToken.description, classToken.description] componentsJoinedByString:@"+"];
  118. NSMutableArray<NSString*> *cached = [self.classNamesCache objectForKey:key];
  119. if (cached) {
  120. return cached;
  121. }
  122. }
  123. NSMutableArray *bundles = [self bundlePathsForToken:bundleToken];
  124. NSMutableArray *classes = [[TBRuntime runtime] classesForToken:classToken inBundles:bundles];
  125. if (shouldCache) {
  126. [self.classNamesCache setObject:classes forKey:key];
  127. }
  128. return classes;
  129. }
  130. - (NSArray<NSMutableArray<FLEXMethod *> *> *)methodsForKeyPath:(TBKeyPath *)keyPath {
  131. // Only cache if no wildcard, but check cache anyway bc I'm lazy
  132. NSArray<NSMutableArray *> *cached = [self.methodsCache objectForKey:keyPath];
  133. if (cached) {
  134. return cached;
  135. }
  136. NSArray<NSString *> *classes = [self classesForKeyPath:keyPath];
  137. NSArray<NSMutableArray<FLEXMethod *> *> *methodLists = [[TBRuntime runtime]
  138. methodsForToken:keyPath.methodKey
  139. instance:keyPath.instanceMethods
  140. inClasses:classes
  141. ];
  142. for (NSMutableArray<FLEXMethod *> *methods in methodLists) {
  143. [methods sortUsingComparator:^NSComparisonResult(FLEXMethod *m1, FLEXMethod *m2) {
  144. return [m1.description caseInsensitiveCompare:m2.description];
  145. }];
  146. }
  147. // Only cache if no wildcard, otherwise the cache could grow very large
  148. if (keyPath.bundleKey.isAbsolute &&
  149. keyPath.classKey.isAbsolute) {
  150. [self.methodsCache setObject:methodLists forKey:keyPath];
  151. }
  152. return methodLists;
  153. }
  154. @end