From a3d01a768f0e7f9d5a36b4bcea04458634818863 Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Thu, 16 Feb 2017 01:30:53 -0800 Subject: [PATCH] Move a bunch of clearly shared logic into CyteKit. --- CyteKit/Application.mm | 7 + CyteKit/CyteKit.h | 38 ++++++ CyteKit/WebView.mm | 26 ++++ CyteKit/WebViewController.mm | 28 ++++ ...t-Cyte.h => countByEnumeratingWithState.h} | 12 +- ...Cyte.mm => countByEnumeratingWithState.mm} | 21 ++- CyteKit/webScriptObjectInContext.h | 31 +++++ CyteKit/webScriptObjectInContext.mm | 56 ++++++++ MobileCydia.mm | 127 +----------------- 9 files changed, 216 insertions(+), 130 deletions(-) create mode 100644 CyteKit/CyteKit.h rename CyteKit/{WebScriptObject-Cyte.h => countByEnumeratingWithState.h} (73%) rename CyteKit/{WebScriptObject-Cyte.mm => countByEnumeratingWithState.mm} (63%) create mode 100644 CyteKit/webScriptObjectInContext.h create mode 100644 CyteKit/webScriptObjectInContext.mm diff --git a/CyteKit/Application.mm b/CyteKit/Application.mm index 9bd42ffc..77b1260c 100644 --- a/CyteKit/Application.mm +++ b/CyteKit/Application.mm @@ -25,6 +25,7 @@ #include #include "CyteKit/Application.h" +#include "CyteKit/URLCache.h" #include "iPhonePrivate.h" #include @@ -57,6 +58,12 @@ if ([self respondsToSelector:@selector(setApplicationSupportsShakeToEdit:)]) [self setApplicationSupportsShakeToEdit:NO]; + + [NSURLCache setSharedURLCache:[[[CyteURLCache alloc] + initWithMemoryCapacity:524288 + diskCapacity:10485760 + diskPath:[NSString stringWithFormat:@"%@/%@/%@", NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject, NSBundle.mainBundle.bundleIdentifier, @"SDURLCache"] + ] autorelease]]; } @end diff --git a/CyteKit/CyteKit.h b/CyteKit/CyteKit.h new file mode 100644 index 00000000..3edcc889 --- /dev/null +++ b/CyteKit/CyteKit.h @@ -0,0 +1,38 @@ +/* Cydia - iPhone UIKit Front-End for Debian APT + * Copyright (C) 2008-2015 Jay Freeman (saurik) +*/ + +/* GNU General Public License, Version 3 {{{ */ +/* + * Cydia is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * Cydia is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Cydia. If not, see . +**/ +/* }}} */ + +#ifndef CyteKit_CyteKit_H +#define CyteKit_CyteKit_H + +#include "CyteKit/Application.h" +#include "CyteKit/NavigationController.h" +#include "CyteKit/RegEx.hpp" +#include "CyteKit/TableViewCell.h" +#include "CyteKit/TabBarController.h" +#include "CyteKit/URLCache.h" +#include "CyteKit/WebViewController.h" +#include "CyteKit/WebViewTableViewCell.h" + +#include "CyteKit/countByEnumeratingWithState.h" +#include "CyteKit/stringWithUTF8Bytes.h" +#include "CyteKit/webScriptObjectInContext.h" + +#endif//CyteKit_CyteKit_H diff --git a/CyteKit/WebView.mm b/CyteKit/WebView.mm index 37115874..9e0cbea8 100644 --- a/CyteKit/WebView.mm +++ b/CyteKit/WebView.mm @@ -417,3 +417,29 @@ __attribute__((__constructor__)) static void $() { class_addMethod($UIWebViewWebViewDelegate, @selector(_clearUIWebView), (IMP) &$UIWebViewWebViewDelegate$_clearUIWebView, "v8@0:4"); } } + +@implementation UIWebDocumentView (Cydia) + +- (void) _setScrollerOffset:(CGPoint)offset { + UIScroller *scroller([self _scroller]); + + CGSize size([scroller contentSize]); + CGSize bounds([scroller bounds].size); + + CGPoint max; + max.x = size.width - bounds.width; + max.y = size.height - bounds.height; + + // wtf Apple?! + if (max.x < 0) + max.x = 0; + if (max.y < 0) + max.y = 0; + + offset.x = offset.x < 0 ? 0 : offset.x > max.x ? max.x : offset.x; + offset.y = offset.y < 0 ? 0 : offset.y > max.y ? max.y : offset.y; + + [scroller setOffset:offset]; +} + +@end diff --git a/CyteKit/WebViewController.mm b/CyteKit/WebViewController.mm index 20838bf9..40b12001 100644 --- a/CyteKit/WebViewController.mm +++ b/CyteKit/WebViewController.mm @@ -19,6 +19,8 @@ extern NSString * const kCAFilterNearest; #include #include +#include "Substrate.hpp" + #define ForSaurik 0 #define DefaultTimeout_ 120.0 @@ -1297,3 +1299,29 @@ float CYScrollViewDecelerationRateNormal; } @end + +MSClassHook(WAKWindow) + +static CGSize $WAKWindow$screenSize(WAKWindow *self, SEL _cmd) { + CGSize size([[UIScreen mainScreen] bounds].size); + /*if ([$WAKWindow respondsToSelector:@selector(hasLandscapeOrientation)]) + if ([$WAKWindow hasLandscapeOrientation]) + std::swap(size.width, size.height);*/ + return size; +} + +static struct WAKWindow$screenSize { WAKWindow$screenSize() { + if ($WAKWindow != NULL) + if (Method method = class_getInstanceMethod($WAKWindow, @selector(screenSize))) + method_setImplementation(method, (IMP) &$WAKWindow$screenSize); +} } WAKWindow$screenSize;; + +MSClassHook(NSUserDefaults) + +MSHook(id, NSUserDefaults$objectForKey$, NSUserDefaults *self, SEL _cmd, NSString *key) { + if ([key respondsToSelector:@selector(isEqualToString:)] && [key isEqualToString:@"WebKitLocalStorageDatabasePathPreferenceKey"]) + return [NSString stringWithFormat:@"%@/%@/%@", NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject, NSBundle.mainBundle.bundleIdentifier, @"LocalStorage"]; + return _NSUserDefaults$objectForKey$(self, _cmd, key); +} + +CYHook(NSUserDefaults, objectForKey$, objectForKey:) diff --git a/CyteKit/WebScriptObject-Cyte.h b/CyteKit/countByEnumeratingWithState.h similarity index 73% rename from CyteKit/WebScriptObject-Cyte.h rename to CyteKit/countByEnumeratingWithState.h index 6a394bcd..32106fa1 100644 --- a/CyteKit/WebScriptObject-Cyte.h +++ b/CyteKit/countByEnumeratingWithState.h @@ -19,15 +19,21 @@ **/ /* }}} */ -#ifndef CyteKit_WebScriptObject_Cyte_H -#define CyteKit_WebScriptObject_Cyte_H +#ifndef CyteKit_countByEnumeratingWithState_H +#define CyteKit_countByEnumeratingWithState_H #include "iPhonePrivate.h" +@interface DOMNodeList (Cyte) +// XXX? - (NSUInteger) count; +- (NSUInteger) countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)objects count:(NSUInteger)count; +// XXX? - (id) objectAtIndex:(unsigned)index; +@end + @interface WebScriptObject (Cyte) - (NSUInteger) count; - (NSUInteger) countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)objects count:(NSUInteger)count; - (id) objectAtIndex:(unsigned)index; @end -#endif//CyteKit_WebScriptObject_Cyte_H +#endif//CyteKit_countByEnumeratingWithState_H diff --git a/CyteKit/WebScriptObject-Cyte.mm b/CyteKit/countByEnumeratingWithState.mm similarity index 63% rename from CyteKit/WebScriptObject-Cyte.mm rename to CyteKit/countByEnumeratingWithState.mm index 7fa851d2..ca5e5777 100644 --- a/CyteKit/WebScriptObject-Cyte.mm +++ b/CyteKit/countByEnumeratingWithState.mm @@ -21,10 +21,29 @@ #include "CyteKit/UCPlatform.h" -#include "CyteKit/WebScriptObject-Cyte.h" +#include "CyteKit/countByEnumeratingWithState.h" + +#include #include "iPhonePrivate.h" +static NSUInteger DOMNodeList$countByEnumeratingWithState$objects$count$(DOMNodeList *self, SEL sel, NSFastEnumerationState *state, id *objects, NSUInteger count) { + size_t length([self length] - state->state); + if (length <= 0) + return 0; + else if (length > count) + length = count; + for (size_t i(0); i != length; ++i) + objects[i] = [self item:state->state++]; + state->itemsPtr = objects; + state->mutationsPtr = (unsigned long *) self; + return length; +} + +static struct DOMNodeList$countByEnumeratingWithState { DOMNodeList$countByEnumeratingWithState() { + class_addMethod(objc_getClass("DOMNodeList"), @selector(countByEnumeratingWithState:objects:count:), (IMP) &DOMNodeList$countByEnumeratingWithState$objects$count$, "I20@0:4^{NSFastEnumerationState}8^@12I16"); +} } DOMNodeList$countByEnumeratingWithState; + @implementation WebScriptObject (Cyte) - (NSUInteger) count { diff --git a/CyteKit/webScriptObjectInContext.h b/CyteKit/webScriptObjectInContext.h new file mode 100644 index 00000000..a73c8ce6 --- /dev/null +++ b/CyteKit/webScriptObjectInContext.h @@ -0,0 +1,31 @@ +/* Cydia - iPhone UIKit Front-End for Debian APT + * Copyright (C) 2008-2015 Jay Freeman (saurik) +*/ + +/* GNU General Public License, Version 3 {{{ */ +/* + * Cydia is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * Cydia is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Cydia. If not, see . +**/ +/* }}} */ + +#ifndef CyteKit_webScriptObjectInContext_H +#define CyteKit_webScriptObjectInContext_H + +#include "iPhonePrivate.h" + +@interface NSObject (CydiaScript) +- (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context; +@end + +#endif//CyteKit_webScriptObjectInContext_H diff --git a/CyteKit/webScriptObjectInContext.mm b/CyteKit/webScriptObjectInContext.mm new file mode 100644 index 00000000..6f053360 --- /dev/null +++ b/CyteKit/webScriptObjectInContext.mm @@ -0,0 +1,56 @@ +/* Cydia - iPhone UIKit Front-End for Debian APT + * Copyright (C) 2008-2015 Jay Freeman (saurik) +*/ + +/* GNU General Public License, Version 3 {{{ */ +/* + * Cydia is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * Cydia is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Cydia. If not, see . +**/ +/* }}} */ + +#include "CyteKit/UCPlatform.h" + +#include "CyteKit/webScriptObjectInContext.h" + +#include "iPhonePrivate.h" + +@implementation NSObject (CydiaScript) + +- (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context { + return self; +} + +@end + +@implementation NSArray (CydiaScript) + +- (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context { + WebScriptObject *object([context evaluateWebScript:@"[]"]); + for (size_t i(0), e([self count]); i != e; ++i) + [object setWebScriptValueAtIndex:i value:[[self objectAtIndex:i] Cydia$webScriptObjectInContext:context]]; + return object; +} + +@end + +@implementation NSDictionary (CydiaScript) + +- (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context { + WebScriptObject *object([context evaluateWebScript:@"({})"]); + for (id i in self) + [object setValue:[[self objectForKey:i] Cydia$webScriptObjectInContext:context] forKey:i]; + return object; +} + +@end diff --git a/MobileCydia.mm b/MobileCydia.mm index d52a38ee..64bc7ab9 100644 --- a/MobileCydia.mm +++ b/MobileCydia.mm @@ -112,16 +112,7 @@ extern "C" { #include "Substrate.hpp" #include "Menes/Menes.h" -#include "CyteKit/Application.h" -#include "CyteKit/NavigationController.h" -#include "CyteKit/RegEx.hpp" -#include "CyteKit/TableViewCell.h" -#include "CyteKit/TabBarController.h" -#include "CyteKit/URLCache.h" -#include "CyteKit/WebScriptObject-Cyte.h" -#include "CyteKit/WebViewController.h" -#include "CyteKit/WebViewTableViewCell.h" -#include "CyteKit/stringWithUTF8Bytes.h" +#include "CyteKit/CyteKit.h" #include "Cydia/MIMEAddress.h" #include "Cydia/LoadingViewController.h" @@ -389,47 +380,6 @@ void CYArrayInsertionSortValues(Type_ *values, size_t length, CFComparisonResult /* }}} */ -/* Apple Bug Fixes {{{ */ -@implementation UIWebDocumentView (Cydia) - -- (void) _setScrollerOffset:(CGPoint)offset { - UIScroller *scroller([self _scroller]); - - CGSize size([scroller contentSize]); - CGSize bounds([scroller bounds].size); - - CGPoint max; - max.x = size.width - bounds.width; - max.y = size.height - bounds.height; - - // wtf Apple?! - if (max.x < 0) - max.x = 0; - if (max.y < 0) - max.y = 0; - - offset.x = offset.x < 0 ? 0 : offset.x > max.x ? max.x : offset.x; - offset.y = offset.y < 0 ? 0 : offset.y > max.y ? max.y : offset.y; - - [scroller setOffset:offset]; -} - -@end -/* }}} */ - -NSUInteger DOMNodeList$countByEnumeratingWithState$objects$count$(DOMNodeList *self, SEL sel, NSFastEnumerationState *state, id *objects, NSUInteger count) { - size_t length([self length] - state->state); - if (length <= 0) - return 0; - else if (length > count) - length = count; - for (size_t i(0); i != length; ++i) - objects[i] = [self item:state->state++]; - state->itemsPtr = objects; - state->mutationsPtr = (unsigned long *) self; - return length; -} - /* Cydia NSString Additions {{{ */ @interface NSString (Cydia) - (NSComparisonResult) compareByPath:(NSString *)other; @@ -5127,42 +5077,6 @@ static _H Diversions_; @end /* }}} */ -// CydiaScript {{{ -@interface NSObject (CydiaScript) -- (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context; -@end - -@implementation NSObject (CydiaScript) - -- (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context { - return self; -} - -@end - -@implementation NSArray (CydiaScript) - -- (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context { - WebScriptObject *object([context evaluateWebScript:@"[]"]); - for (size_t i(0), e([self count]); i != e; ++i) - [object setWebScriptValueAtIndex:i value:[[self objectAtIndex:i] Cydia$webScriptObjectInContext:context]]; - return object; -} - -@end - -@implementation NSDictionary (CydiaScript) - -- (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context { - WebScriptObject *object([context evaluateWebScript:@"({})"]); - for (id i in self) - [object setValue:[[self objectForKey:i] Cydia$webScriptObjectInContext:context] forKey:i]; - return object; -} - -@end -// }}} - /* Confirmation Controller {{{ */ bool DepSubstrate(const pkgCache::VerIterator &iterator) { if (!iterator.end()) @@ -9811,12 +9725,6 @@ _trace(); [BridgedHosts_ addObject:[[NSURL URLWithString:CydiaURL(@"")] host]]; } - [NSURLCache setSharedURLCache:[[[CyteURLCache alloc] - initWithMemoryCapacity:524288 - diskCapacity:10485760 - diskPath:Cache("SDURLCache") - ] autorelease]]; - [CydiaWebViewController _initialize]; [NSURLProtocol registerClass:[CydiaURLProtocol class]]; @@ -10038,24 +9946,6 @@ id Dealloc_(id self, SEL selector) { return object; }*/ -Class $WAKWindow; - -static CGSize $WAKWindow$screenSize(WAKWindow *self, SEL _cmd) { - CGSize size([[UIScreen mainScreen] bounds].size); - /*if ([$WAKWindow respondsToSelector:@selector(hasLandscapeOrientation)]) - if ([$WAKWindow hasLandscapeOrientation]) - std::swap(size.width, size.height);*/ - return size; -} - -Class $NSUserDefaults; - -MSHook(id, NSUserDefaults$objectForKey$, NSUserDefaults *self, SEL _cmd, NSString *key) { - if ([key respondsToSelector:@selector(isEqualToString:)] && [key isEqualToString:@"WebKitLocalStorageDatabasePathPreferenceKey"]) - return Cache("LocalStorage"); - return _NSUserDefaults$objectForKey$(self, _cmd, key); -} - static NSMutableDictionary *AutoreleaseDeepMutableCopyOfDictionary(CFTypeRef type) { if (type == NULL) return nil; @@ -10126,21 +10016,6 @@ int main(int argc, char *argv[]) { PackageName = reinterpret_cast(method_getImplementation(class_getInstanceMethod([Package class], @selector(cyname)))); - /* Library Hacks {{{ */ - class_addMethod(objc_getClass("DOMNodeList"), @selector(countByEnumeratingWithState:objects:count:), (IMP) &DOMNodeList$countByEnumeratingWithState$objects$count$, "I20@0:4^{NSFastEnumerationState}8^@12I16"); - - $WAKWindow = objc_getClass("WAKWindow"); - if ($WAKWindow != NULL) - if (Method method = class_getInstanceMethod($WAKWindow, @selector(screenSize))) - method_setImplementation(method, (IMP) &$WAKWindow$screenSize); - - $NSUserDefaults = objc_getClass("NSUserDefaults"); - Method NSUserDefaults$objectForKey$(class_getInstanceMethod($NSUserDefaults, @selector(objectForKey:))); - if (NSUserDefaults$objectForKey$ != NULL) { - _NSUserDefaults$objectForKey$ = reinterpret_cast(method_getImplementation(NSUserDefaults$objectForKey$)); - method_setImplementation(NSUserDefaults$objectForKey$, reinterpret_cast(&$NSUserDefaults$objectForKey$)); - } - /* }}} */ /* Set Locale {{{ */ Locale_ = CFLocaleCopyCurrent(); Languages_ = [NSLocale preferredLanguages]; -- 2.45.2