revenge-tweak/Sources/Theme.m

83 lines
2.8 KiB
Mathematica
Raw Permalink Normal View History

2024-11-14 11:15:18 +01:00
#import "Theme.h"
#import "Utils.h"
#import "Logger.h"
#import <objc/runtime.h>
void swizzleDCDThemeColor(NSDictionary<NSString *, NSArray<NSString *> *> *semanticColors) {
2024-11-15 11:45:12 +01:00
RevengeLog(@"Swizzling DCDThemeColor");
2024-11-14 11:15:18 +01:00
Class DCDTheme = NSClassFromString(@"DCDTheme");
Class dcdThemeTarget = object_getClass(DCDTheme);
2024-11-15 11:45:12 +01:00
2024-11-14 11:15:18 +01:00
SEL themeIndexSelector = NSSelectorFromString(@"themeIndex");
Method themeIndexMethod = class_getClassMethod(dcdThemeTarget, themeIndexSelector);
IMP themeIndexImpl = method_getImplementation(themeIndexMethod);
int (*themeIndex)(id, SEL) = (int (*)(id, SEL))themeIndexImpl;
2024-11-15 11:45:12 +01:00
2024-11-14 11:15:18 +01:00
Class DCDThemeColor = NSClassFromString(@"DCDThemeColor");
Class target = object_getClass(DCDThemeColor);
2024-11-15 11:45:12 +01:00
2024-11-14 11:15:18 +01:00
unsigned int methodCount;
Method *methods = class_copyMethodList(target, &methodCount);
2024-11-15 11:45:12 +01:00
2024-11-14 11:15:18 +01:00
for (unsigned int i = 0; i < methodCount; i++) {
Method method = methods[i];
SEL selector = method_getName(method);
NSString *methodName = NSStringFromSelector(selector);
2024-11-15 11:45:12 +01:00
2024-11-14 11:15:18 +01:00
NSArray<NSString *> *semanticColor = semanticColors[methodName];
if (semanticColor) {
2024-11-15 11:45:12 +01:00
RevengeLog(@"Swizzling %@", methodName);
2024-11-14 11:15:18 +01:00
IMP originalImpl = method_getImplementation(method);
UIColor *(*original)(id, SEL) = (UIColor *(*)(id, SEL))originalImpl;
2024-11-15 11:45:12 +01:00
2024-11-14 11:15:18 +01:00
id block = ^UIColor *(id self) {
int themeIndexVal = themeIndex(dcdThemeTarget, themeIndexSelector);
if (semanticColor.count - 1 >= themeIndexVal) {
UIColor *semanticUIColor = hexToUIColor(semanticColor[themeIndexVal]);
if (semanticUIColor) {
return semanticUIColor;
}
}
return original(target, selector);
};
2024-11-15 11:45:12 +01:00
2024-11-14 11:15:18 +01:00
IMP newImpl = imp_implementationWithBlock(block);
method_setImplementation(method, newImpl);
}
}
2024-11-15 11:45:12 +01:00
2024-11-14 11:15:18 +01:00
free(methods);
}
void swizzleUIColor(NSDictionary<NSString *, NSString *> *rawColors) {
2024-11-15 11:45:12 +01:00
RevengeLog(@"Swizzling UIColor");
2024-11-14 11:15:18 +01:00
Class UIColorClass = NSClassFromString(@"UIColor");
Class target = object_getClass(UIColorClass);
2024-11-15 11:45:12 +01:00
2024-11-14 11:15:18 +01:00
unsigned int methodCount;
Method *methods = class_copyMethodList(target, &methodCount);
2024-11-15 11:45:12 +01:00
2024-11-14 11:15:18 +01:00
for (unsigned int i = 0; i < methodCount; i++) {
Method method = methods[i];
SEL selector = method_getName(method);
NSString *methodName = NSStringFromSelector(selector);
2024-11-15 11:45:12 +01:00
2024-11-14 11:15:18 +01:00
NSString *rawColor = rawColors[methodName];
if (rawColor) {
2024-11-15 11:45:12 +01:00
RevengeLog(@"Swizzling %@", methodName);
2024-11-14 11:15:18 +01:00
id block = ^UIColor *(id self) {
return hexToUIColor(rawColor);
};
2024-11-15 11:45:12 +01:00
2024-11-14 11:15:18 +01:00
IMP newImpl = imp_implementationWithBlock(block);
method_setImplementation(method, newImpl);
}
}
2024-11-15 11:45:12 +01:00
2024-11-14 11:15:18 +01:00
free(methods);
2024-11-15 11:45:12 +01:00
}