Skip to content

Commit

Permalink
chore: add import sorting to clang format and reformat files
Browse files Browse the repository at this point in the history
  • Loading branch information
castdrian committed Feb 24, 2025
1 parent da0f8c6 commit 34e084d
Show file tree
Hide file tree
Showing 9 changed files with 313 additions and 171 deletions.
56 changes: 55 additions & 1 deletion .clang-format
Original file line number Diff line number Diff line change
@@ -1,14 +1,68 @@
# Base Style
BasedOnStyle: LLVM
Language: ObjC

# Indentation
IndentWidth: 4
ObjCBlockIndentWidth: 4
UseTab: Never
ColumnLimit: 100
AccessModifierOffset: -4
IndentCaseLabels: true
NamespaceIndentation: Inner

# Alignment
AlignConsecutiveAssignments: Consecutive
AlignConsecutiveDeclarations: Consecutive
AlignConsecutiveMacros: Consecutive
AlignTrailingComments: true
AlignOperands: true
AlignAfterOpenBracket: Align

# Objective-C specific
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
ObjCBreakBeforeNestedBlockParam: true
ObjCBinPackProtocolList: Never

# Breaking and wrapping
AllowShortBlocksOnASingleLine: Empty
AllowShortFunctionsOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: Empty
AllowShortLoopsOnASingleLine: false
AlignConsecutiveAssignments: Consecutive
BreakBeforeBraces: Custom
BraceWrapping:
AfterObjCDeclaration: true
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
BeforeCatch: true
BeforeElse: true
IndentBraces: false

# Spaces
SpaceAfterCStyleCast: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: true
SpacesInParentheses: false
SpacesInSquareBrackets: false

# Import ordering
SortIncludes: true
IncludeBlocks: Regroup
IncludeCategories:
# System headers with brackets
- Regex: "^<"
Priority: 1
# Main header (matching filename)
- Regex: '^"[^/]*\.h"$'
Priority: 2
# Local/Project headers
- Regex: '^"'
Priority: 3
6 changes: 3 additions & 3 deletions Headers/LoaderConfig.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#import <Foundation/Foundation.h>

@interface LoaderConfig : NSObject
@property(nonatomic, assign) BOOL customLoadUrlEnabled;
@property(nonatomic, strong) NSURL *customLoadUrl;
@interface LoaderConfig : NSObject
@property (nonatomic, assign) BOOL customLoadUrlEnabled;
@property (nonatomic, strong) NSURL *customLoadUrl;
+ (instancetype)defaultConfig;
+ (instancetype)getLoaderConfig;
- (instancetype)init;
Expand Down
2 changes: 1 addition & 1 deletion Headers/Logger.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#import <Foundation/Foundation.h>

#define LOG_PREFIX @"[Bunny]"
#define LOG_PREFIX @"[Bunny]"
#define BunnyLog(fmt, ...) NSLog((LOG_PREFIX @" " fmt), ##__VA_ARGS__)
2 changes: 1 addition & 1 deletion Headers/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
@end

extern id gBridge;
void showSettingsSheet(void);
void showSettingsSheet(void);
38 changes: 19 additions & 19 deletions Headers/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@
@class BunnySettingsViewController;

extern BOOL isJailbroken;
BOOL isSafeModeEnabled(void);
NSURL *getPyoncordDirectory(void);
UIColor *hexToUIColor(NSString *hex);
NSString *getDeviceIdentifier(void);
void showErrorAlert(NSString *title, NSString *message, void (^completion)(void));
void reloadApp(UIViewController *viewController);
void deletePlugins(void);
void deleteThemes(void);
void deleteAllData(UIViewController *presenter);
void refetchBundle(UIViewController *presenter);
void toggleSafeMode(void);
void setCustomBundleURL(NSURL *url, UIViewController *presenter);
void resetCustomBundleURL(UIViewController *presenter);
void showBundleSelector(UIViewController *presenter);
void removeCachedBundle(void);
void gracefulExit(UIViewController *presenter);
void deletePluginsAndReload(UIViewController *presenter);
void deleteThemesAndReload(UIViewController *presenter);
void handleBadBundleAttempt(UIViewController *presenter);
BOOL isSafeModeEnabled(void);
NSURL *getPyoncordDirectory(void);
UIColor *hexToUIColor(NSString *hex);
NSString *getDeviceIdentifier(void);
void showErrorAlert(NSString *title, NSString *message, void (^completion)(void));
void reloadApp(UIViewController *viewController);
void deletePlugins(void);
void deleteThemes(void);
void deleteAllData(UIViewController *presenter);
void refetchBundle(UIViewController *presenter);
void toggleSafeMode(void);
void setCustomBundleURL(NSURL *url, UIViewController *presenter);
void resetCustomBundleURL(UIViewController *presenter);
void showBundleSelector(UIViewController *presenter);
void removeCachedBundle(void);
void gracefulExit(UIViewController *presenter);
void deletePluginsAndReload(UIViewController *presenter);
void deleteThemesAndReload(UIViewController *presenter);
void handleBadBundleAttempt(UIViewController *presenter);

NSString *decryptString(NSString *encrypted);
99 changes: 63 additions & 36 deletions Sources/Fonts.x
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
#import "Fonts.h"
#import "Logger.h"
#import "Utils.h"
#import <CoreFoundation/CoreFoundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <CoreText/CoreText.h>

#import "Fonts.h"
#import "Logger.h"
#import "Utils.h"

NSMutableDictionary<NSString *, NSString *> *fontMap;

%hook UIFont

+ (UIFont *)fontWithName:(NSString *)name size:(CGFloat)size {
+ (UIFont *)fontWithName:(NSString *)name size:(CGFloat)size
{
NSString *replacementName = fontMap[name];
if (replacementName) {
if (replacementName)
{
UIFontDescriptor *replacementDescriptor =
[UIFontDescriptor fontDescriptorWithName:replacementName size:size];
UIFontDescriptor *fallbackDescriptor =
Expand All @@ -28,9 +31,11 @@ NSMutableDictionary<NSString *, NSString *> *fontMap;
return %orig;
}

+ (UIFont *)fontWithDescriptor:(UIFontDescriptor *)descriptor size:(CGFloat)size {
+ (UIFont *)fontWithDescriptor:(UIFontDescriptor *)descriptor size:(CGFloat)size
{
NSString *replacementName = fontMap[descriptor.postscriptName];
if (replacementName) {
if (replacementName)
{
UIFontDescriptor *replacementDescriptor =
[UIFontDescriptor fontDescriptorWithName:replacementName size:size];
UIFontDescriptor *finalDescriptor =
Expand All @@ -43,28 +48,34 @@ NSMutableDictionary<NSString *, NSString *> *fontMap;
return %orig;
}

+ (UIFont *)systemFontOfSize:(CGFloat)size {
+ (UIFont *)systemFontOfSize:(CGFloat)size
{
NSString *replacementName = fontMap[@"systemFont"];
if (replacementName) {
if (replacementName)
{
return [UIFont fontWithName:replacementName size:size];
}
return %orig;
}

+ (UIFont *)preferredFontForTextStyle:(UIFontTextStyle)style {
+ (UIFont *)preferredFontForTextStyle:(UIFontTextStyle)style
{
NSString *replacementName = fontMap[@"systemFont"];
if (replacementName) {
if (replacementName)
{
return [UIFont fontWithName:replacementName size:[UIFont systemFontSize]];
}
return %orig;
}

%end

void patchFonts(NSDictionary<NSString *, NSString *> *mainFonts, NSString *fontDefName) {
void patchFonts(NSDictionary<NSString *, NSString *> *mainFonts, NSString *fontDefName)
{
BunnyLog(@"patchFonts called with fonts: %@ and def name: %@", mainFonts, fontDefName);

if (!fontMap) {
if (!fontMap)
{
BunnyLog(@"Creating new fontMap");
fontMap = [NSMutableDictionary dictionary];
}
Expand All @@ -73,15 +84,17 @@ void patchFonts(NSDictionary<NSString *, NSString *> *mainFonts, NSString *fontD
stringWithContentsOfURL:[getPyoncordDirectory() URLByAppendingPathComponent:@"fonts.json"]
encoding:NSUTF8StringEncoding
error:nil];
if (fontJson) {
if (fontJson)
{
BunnyLog(@"Found existing fonts.json: %@", fontJson);
}

for (NSString *fontName in mainFonts) {
for (NSString *fontName in mainFonts)
{
NSString *url = mainFonts[fontName];
BunnyLog(@"Replacing font %@ with URL: %@", fontName, url);

NSURL *fontURL = [NSURL URLWithString:url];
NSURL *fontURL = [NSURL URLWithString:url];
NSString *fontExtension = fontURL.pathExtension;

NSURL *fontCachePath = [[[getPyoncordDirectory() URLByAppendingPathComponent:@"downloads"
Expand All @@ -95,41 +108,48 @@ void patchFonts(NSDictionary<NSString *, NSString *> *mainFonts, NSString *fontD
stringWithFormat:@"%@.%@", fontName, fontExtension]];

NSURL *parentDir = [fontCachePath URLByDeletingLastPathComponent];
if (![[NSFileManager defaultManager] fileExistsAtPath:parentDir.path]) {
if (![[NSFileManager defaultManager] fileExistsAtPath:parentDir.path])
{
BunnyLog(@"Creating parent directory: %@", parentDir.path);
[[NSFileManager defaultManager] createDirectoryAtURL:parentDir
withIntermediateDirectories:YES
attributes:nil
error:nil];
}

if (![[NSFileManager defaultManager] fileExistsAtPath:fontCachePath.path]) {
if (![[NSFileManager defaultManager] fileExistsAtPath:fontCachePath.path])
{
BunnyLog(@"Downloading font %@ from %@", fontName, url);
NSData *data = [NSData dataWithContentsOfURL:fontURL];
if (data) {
if (data)
{
BunnyLog(@"Writing font data to: %@", fontCachePath.path);
[data writeToURL:fontCachePath atomically:YES];
}
}

NSData *fontData = [NSData dataWithContentsOfURL:fontCachePath];
if (fontData) {
if (fontData)
{
BunnyLog(@"Registering font %@ with provider", fontName);
CGDataProviderRef provider =
CGDataProviderCreateWithCFData((__bridge CFDataRef)fontData);
CGDataProviderCreateWithCFData((__bridge CFDataRef) fontData);
CGFontRef font = CGFontCreateWithDataProvider(provider);

if (font) {
if (font)
{
CFStringRef postScriptName = CGFontCopyPostScriptName(font);

CTFontRef existingFont = CTFontCreateWithName(postScriptName, 0, NULL);
if (existingFont) {
if (existingFont)
{
CFErrorRef unregisterError = NULL;
if (!CTFontManagerUnregisterGraphicsFont(font, &unregisterError)) {
if (!CTFontManagerUnregisterGraphicsFont(font, &unregisterError))
{
BunnyLog(@"Failed to deregister font %@: %@",
(__bridge NSString *)postScriptName,
(__bridge NSString *) postScriptName,
unregisterError
? (__bridge NSString *)CFErrorCopyDescription(unregisterError)
? (__bridge NSString *) CFErrorCopyDescription(unregisterError)
: @"Unknown error");
if (unregisterError)
CFRelease(unregisterError);
Expand All @@ -138,23 +158,28 @@ void patchFonts(NSDictionary<NSString *, NSString *> *mainFonts, NSString *fontD
}

CFErrorRef error = NULL;
if (CTFontManagerRegisterGraphicsFont(font, &error)) {
fontMap[fontName] = (__bridge NSString *)postScriptName;
if (CTFontManagerRegisterGraphicsFont(font, &error))
{
fontMap[fontName] = (__bridge NSString *) postScriptName;
BunnyLog(@"Successfully registered font %@ to %@", fontName,
(__bridge NSString *)postScriptName);
(__bridge NSString *) postScriptName);

NSError *jsonError;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:fontMap
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:fontMap
options:0
error:&jsonError];
if (!jsonError) {
if (!jsonError)
{
[jsonData writeToURL:[getPyoncordDirectory()
URLByAppendingPathComponent:@"fontMap.json"]
atomically:YES];
}
} else {
NSString *errorDesc = error ? (__bridge NSString *)CFErrorCopyDescription(error)
: @"Unknown error";
}
else
{
NSString *errorDesc = error
? (__bridge NSString *) CFErrorCopyDescription(error)
: @"Unknown error";
BunnyLog(@"Failed to register font %@: %@", fontName, errorDesc);
if (error)
CFRelease(error);
Expand All @@ -168,8 +193,10 @@ void patchFonts(NSDictionary<NSString *, NSString *> *mainFonts, NSString *fontD
}
}

%ctor {
@autoreleasepool {
%ctor
{
@autoreleasepool
{
fontMap = [NSMutableDictionary dictionary];
BunnyLog(@"Font hooks initialized");
%init;
Expand Down
Loading

0 comments on commit 34e084d

Please sign in to comment.