TBRuntimeController.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. NSString *imagePath = @(class_getImageName(NSClassFromString(name)));
  66. return [[TBRuntime runtime] shortNameForImageName:imagePath];
  67. }
  68. + (NSString *)imagePathWithShortName:(NSString *)suffix {
  69. return [[TBRuntime runtime] imageNameForShortName:suffix];
  70. }
  71. + (NSArray *)allBundleNames {
  72. return [TBRuntime runtime].imageDisplayNames;
  73. }
  74. #pragma mark Private
  75. - (NSMutableArray *)bundlePathsForToken:(TBToken *)token {
  76. // Only cache if no wildcard
  77. BOOL shouldCache = token == TBWildcardOptionsNone;
  78. if (shouldCache) {
  79. NSMutableArray<NSString*> *cached = [self.bundlePathsCache objectForKey:token];
  80. if (cached) {
  81. return cached;
  82. }
  83. NSMutableArray<NSString*> *bundles = [[TBRuntime runtime] bundlePathsForToken:token];
  84. [self.bundlePathsCache setObject:bundles forKey:token];
  85. return bundles;
  86. }
  87. else {
  88. return [[TBRuntime runtime] bundlePathsForToken:token];
  89. }
  90. }
  91. - (NSMutableArray<NSString *> *)bundleNamesForToken:(TBToken *)token {
  92. // Only cache if no wildcard
  93. BOOL shouldCache = token == TBWildcardOptionsNone;
  94. if (shouldCache) {
  95. NSMutableArray<NSString*> *cached = [self.bundleNamesCache objectForKey:token];
  96. if (cached) {
  97. return cached;
  98. }
  99. NSMutableArray<NSString*> *bundles = [[TBRuntime runtime] bundleNamesForToken:token];
  100. [self.bundleNamesCache setObject:bundles forKey:token];
  101. return bundles;
  102. }
  103. else {
  104. return [[TBRuntime runtime] bundleNamesForToken:token];
  105. }
  106. }
  107. - (NSMutableArray<NSString *> *)classesForKeyPath:(TBKeyPath *)keyPath {
  108. TBToken *classToken = keyPath.classKey;
  109. TBToken *bundleToken = keyPath.bundleKey;
  110. // Only cache if no wildcard
  111. BOOL shouldCache = bundleToken.options == 0 && classToken.options == 0;
  112. NSString *key = nil;
  113. if (shouldCache) {
  114. key = [@[bundleToken.description, classToken.description] componentsJoinedByString:@"+"];
  115. NSMutableArray<NSString*> *cached = [self.classNamesCache objectForKey:key];
  116. if (cached) {
  117. return cached;
  118. }
  119. }
  120. NSMutableArray *bundles = [self bundlePathsForToken:bundleToken];
  121. NSMutableArray *classes = [[TBRuntime runtime] classesForToken:classToken inBundles:bundles];
  122. if (shouldCache) {
  123. [self.classNamesCache setObject:classes forKey:key];
  124. }
  125. return classes;
  126. }
  127. - (NSArray<NSMutableArray<FLEXMethod *> *> *)methodsForKeyPath:(TBKeyPath *)keyPath {
  128. // Only cache if no wildcard, but check cache anyway bc I'm lazy
  129. NSArray<NSMutableArray *> *cached = [self.methodsCache objectForKey:keyPath];
  130. if (cached) {
  131. return cached;
  132. }
  133. NSArray<NSString *> *classes = [self classesForKeyPath:keyPath];
  134. NSArray<NSMutableArray<FLEXMethod *> *> *methodLists = [[TBRuntime runtime]
  135. methodsForToken:keyPath.methodKey
  136. instance:keyPath.instanceMethods
  137. inClasses:classes
  138. ];
  139. for (NSMutableArray<FLEXMethod *> *methods in methodLists) {
  140. [methods sortUsingComparator:^NSComparisonResult(FLEXMethod *m1, FLEXMethod *m2) {
  141. return [m1.description caseInsensitiveCompare:m2.description];
  142. }];
  143. }
  144. // Only cache if no wildcard, otherwise the cache could grow very large
  145. if (keyPath.bundleKey.isAbsolute &&
  146. keyPath.classKey.isAbsolute) {
  147. [self.methodsCache setObject:methodLists forKey:keyPath];
  148. }
  149. return methodLists;
  150. }
  151. @end