From d1360727ecfb286887a828b7cee9c3f0a0290a63 Mon Sep 17 00:00:00 2001 From: galad87 Date: Sat, 29 Aug 2015 11:41:24 +0200 Subject: [PATCH] Fix 10.7 compatibility and add a window to show an exception log --- Classes/Compatibility/NSArray+Subscripting.h | 4 + Classes/Compatibility/NSArray+Subscripting.m | 26 ++++ .../Compatibility/NSDictionary+Subscript.h | 12 ++ .../Compatibility/NSDictionary+Subscript.m | 34 +++++ .../Compatibility/NSMutableArray+Subscript.h | 12 ++ .../Compatibility/NSMutableArray+Subscript.m | 50 +++++++ .../NSMutableDictionary+Subscript.h | 12 ++ .../NSMutableDictionary+Subscript.m | 34 +++++ Classes/SBApplication.h | 11 ++ Classes/SBApplication.m | 62 ++++++++ Classes/SBExceptionAlertController.h | 25 ++++ Classes/SBExceptionAlertController.m | 33 +++++ English.lproj/ExceptionAlert.xib | 137 ++++++++++++++++++ 13 files changed, 452 insertions(+) create mode 100644 Classes/Compatibility/NSArray+Subscripting.h create mode 100644 Classes/Compatibility/NSArray+Subscripting.m create mode 100755 Classes/Compatibility/NSDictionary+Subscript.h create mode 100755 Classes/Compatibility/NSDictionary+Subscript.m create mode 100755 Classes/Compatibility/NSMutableArray+Subscript.h create mode 100755 Classes/Compatibility/NSMutableArray+Subscript.m create mode 100755 Classes/Compatibility/NSMutableDictionary+Subscript.h create mode 100755 Classes/Compatibility/NSMutableDictionary+Subscript.m create mode 100644 Classes/SBApplication.h create mode 100644 Classes/SBApplication.m create mode 100644 Classes/SBExceptionAlertController.h create mode 100644 Classes/SBExceptionAlertController.m create mode 100644 English.lproj/ExceptionAlert.xib diff --git a/Classes/Compatibility/NSArray+Subscripting.h b/Classes/Compatibility/NSArray+Subscripting.h new file mode 100644 index 0000000..519f24c --- /dev/null +++ b/Classes/Compatibility/NSArray+Subscripting.h @@ -0,0 +1,4 @@ +#include + +@interface NSArray (Subscripting) +@end diff --git a/Classes/Compatibility/NSArray+Subscripting.m b/Classes/Compatibility/NSArray+Subscripting.m new file mode 100644 index 0000000..0dcf587 --- /dev/null +++ b/Classes/Compatibility/NSArray+Subscripting.m @@ -0,0 +1,26 @@ +#import "NSArray+Subscripting.h" +#import + +@implementation NSArray (Subscripting) + ++ (void)load +{ + Class class = [self class]; + + SEL swizzledSelector = @selector(bnr_objectAtIndexedSubscript:); + + Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); + + class_addMethod([NSArray class], + @selector(objectAtIndexedSubscript:), + method_getImplementation(swizzledMethod), + method_getTypeEncoding(swizzledMethod) + ); +} + +- (id)bnr_objectAtIndexedSubscript:(NSInteger)index +{ + return [self objectAtIndex:index]; +} + +@end diff --git a/Classes/Compatibility/NSDictionary+Subscript.h b/Classes/Compatibility/NSDictionary+Subscript.h new file mode 100755 index 0000000..8dab9fe --- /dev/null +++ b/Classes/Compatibility/NSDictionary+Subscript.h @@ -0,0 +1,12 @@ +// +// NSDictionary+Subscript.h +// +// +// Created by Sernin van de Krol on 8/21/12. +// Copyright (c) 2012 Sernin van de Krol. All rights reserved. +// + +#import + +@interface NSDictionary (Subscript) +@end diff --git a/Classes/Compatibility/NSDictionary+Subscript.m b/Classes/Compatibility/NSDictionary+Subscript.m new file mode 100755 index 0000000..cd7aa0e --- /dev/null +++ b/Classes/Compatibility/NSDictionary+Subscript.m @@ -0,0 +1,34 @@ +// +// NSDictionary+Subscript.m +// +// +// Created by Sernin van de Krol on 8/21/12. +// Copyright (c) 2012 Sernin van de Krol. All rights reserved. +// + + +#import "NSDictionary+Subscript.h" +#import + +@implementation NSDictionary (Subscript) + ++ (void)load +{ + Class class = [self class]; + + SEL swizzledSelector = @selector(dg_objectForKeyedSubscript:); + Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); + + class_addMethod([NSDictionary class], + @selector(objectForKeyedSubscript:), + method_getImplementation(swizzledMethod), + method_getTypeEncoding(swizzledMethod) + ); +} + +-(id)dg_objectForKeyedSubscript:(id)key +{ + return [self objectForKey:key]; +} + +@end diff --git a/Classes/Compatibility/NSMutableArray+Subscript.h b/Classes/Compatibility/NSMutableArray+Subscript.h new file mode 100755 index 0000000..9e16900 --- /dev/null +++ b/Classes/Compatibility/NSMutableArray+Subscript.h @@ -0,0 +1,12 @@ +// +// NSMutableArray+Subscript.h +// +// +// Created by Sernin van de Krol on 8/21/12. +// Copyright (c) 2012 Sernin van de Krol. All rights reserved. +// + +#import + +@interface NSMutableArray (Subscript) +@end diff --git a/Classes/Compatibility/NSMutableArray+Subscript.m b/Classes/Compatibility/NSMutableArray+Subscript.m new file mode 100755 index 0000000..82b260e --- /dev/null +++ b/Classes/Compatibility/NSMutableArray+Subscript.m @@ -0,0 +1,50 @@ +// +// NSMutableArray+Subscript.m +// +// +// Created by Sernin van de Krol on 8/21/12. +// Copyright (c) 2012 Sernin van de Krol. All rights reserved. +// +#if __has_feature(objc_arc) + +#import "NSMutableArray+Subscript.h" + +@implementation NSMutableArray (Subscript) + ++ (void)load +{ + Class class = [self class]; + + SEL swizzledSelector = @selector(dg_setObject:atIndexedSubscript:); + Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); + + class_addMethod([NSMutableArray class], + @selector(setObject:atIndexedSubscript:), + method_getImplementation(swizzledMethod), + method_getTypeEncoding(swizzledMethod) + ); +} + +- (void)dg_setObject:(id)anObject atIndexedSubscript:(NSUInteger)index +{ + if(anObject == nil) + { + [NSException raise:NSInvalidArgumentException format:@"setObject:atIndexedSubscript does not allow objects to be nil"]; + } + if(index > self.count) + { + [NSException raise:NSRangeException format:@"setObject:atIndexedSubscript does not allow the index to be out of array bounds"]; + } + if(index == self.count) + { + [self addObject:anObject]; + } + else + { + [self replaceObjectAtIndex:index withObject:anObject]; + } +} + +@end + +#endif diff --git a/Classes/Compatibility/NSMutableDictionary+Subscript.h b/Classes/Compatibility/NSMutableDictionary+Subscript.h new file mode 100755 index 0000000..6bbeffa --- /dev/null +++ b/Classes/Compatibility/NSMutableDictionary+Subscript.h @@ -0,0 +1,12 @@ +// +// NSMutableDictionary+Subscript.h +// +// +// Created by Sernin van de Krol on 8/21/12. +// Copyright (c) 2012 Sernin van de Krol. All rights reserved. +// + +#import + +@interface NSMutableDictionary (Subscript) +@end diff --git a/Classes/Compatibility/NSMutableDictionary+Subscript.m b/Classes/Compatibility/NSMutableDictionary+Subscript.m new file mode 100755 index 0000000..d3de810 --- /dev/null +++ b/Classes/Compatibility/NSMutableDictionary+Subscript.m @@ -0,0 +1,34 @@ +// +// NSMutableDictionary+Subscript.m +// +// +// Created by Sernin van de Krol on 8/21/12. +// Copyright (c) 2012 Sernin van de Krol. All rights reserved. +// + + +#import "NSMutableDictionary+Subscript.h" +#import + +@implementation NSMutableDictionary (Subscript) + ++ (void)load +{ + Class class = [self class]; + + SEL swizzledSelector = @selector(dg_setObject:forKeyedSubscript:); + Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); + + class_addMethod([NSMutableDictionary class], + @selector(setObject:forKeyedSubscript:), + method_getImplementation(swizzledMethod), + method_getTypeEncoding(swizzledMethod) + ); +} + +- (void)dg_setObject:(id)object forKeyedSubscript:(id < NSCopying >)aKey +{ + [self setObject:object forKey:aKey]; +} + +@end diff --git a/Classes/SBApplication.h b/Classes/SBApplication.h new file mode 100644 index 0000000..6b82f49 --- /dev/null +++ b/Classes/SBApplication.h @@ -0,0 +1,11 @@ +/* HBApplication.h $ + + This file is part of the HandBrake source code. + Homepage: . + It may be used under the terms of the GNU General Public License. */ + +#import + +@interface SBApplication : NSApplication + +@end diff --git a/Classes/SBApplication.m b/Classes/SBApplication.m new file mode 100644 index 0000000..91c21f7 --- /dev/null +++ b/Classes/SBApplication.m @@ -0,0 +1,62 @@ +/* HBApplication.m $ + + This file is part of the HandBrake source code. + Homepage: . + It may be used under the terms of the GNU General Public License. */ + +#import "SBApplication.h" +#import "SBExceptionAlertController.h" + +@implementation SBApplication + +static void CrashMyApplication() +{ + *(char *)0x08 = 1; +} + +- (NSAttributedString *)_formattedExceptionBacktrace:(NSArray *)backtrace +{ + NSMutableAttributedString *result = [[NSMutableAttributedString alloc] init]; + for (__strong NSString *s in backtrace) + { + s = [s stringByAppendingString:@"\n"]; + NSAttributedString *attrS = [[NSAttributedString alloc] initWithString:s]; + [result appendAttributedString:attrS]; + } + [result addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Monaco" size:10] range:NSMakeRange(0, result.length)]; + return result; +} + +- (void)reportException:(NSException *)exception +{ + // NSApplication simply logs the exception to the console. We want to let the user know + // when it happens in order to possibly prevent subsequent random crashes that are difficult to debug + @try + { + @autoreleasepool + { + // Create a string based on the exception + NSString *exceptionMessage = [NSString stringWithFormat:@"%@\nReason: %@\nUser Info: %@", exception.name, exception.reason, exception.userInfo]; + // Always log to console for history + + //[HBUtilities writeToActivityLog:"Exception raised:\n%s", exceptionMessage.UTF8String]; + //[HBUtilities writeToActivityLog:"Backtrace:\n%s", exception.callStackSymbols.description.UTF8String]; + + SBExceptionAlertController *alertController = [[SBExceptionAlertController alloc] init]; + alertController.exceptionMessage = exceptionMessage; + alertController.exceptionBacktrace = [self _formattedExceptionBacktrace:exception.callStackSymbols]; + + NSInteger result = [alertController runModal]; + if (result == SBExceptionAlertControllerResultCrash) + { + CrashMyApplication(); + } + } + } + @catch (NSException *e) + { + // Suppress any exceptions raised in the handling + } +} + +@end diff --git a/Classes/SBExceptionAlertController.h b/Classes/SBExceptionAlertController.h new file mode 100644 index 0000000..1b215ea --- /dev/null +++ b/Classes/SBExceptionAlertController.h @@ -0,0 +1,25 @@ +/* HBExceptionAlertController.h $ + + This file is part of the HandBrake source code. + Homepage: . + It may be used under the terms of the GNU General Public License. */ + +#import + +typedef NS_ENUM(NSUInteger, SBExceptionAlertControllerResult) { + SBExceptionAlertControllerResultCrash, + SBExceptionAlertControllerResultContinue, +}; + +@interface SBExceptionAlertController : NSWindowController + +// Properties are used by bindings +@property (copy) NSString *exceptionMessage; +@property (copy) NSAttributedString *exceptionBacktrace; + +- (IBAction)btnCrashClicked:(id)sender; +- (IBAction)btnContinueClicked:(id)sender; + +- (NSInteger)runModal; + +@end diff --git a/Classes/SBExceptionAlertController.m b/Classes/SBExceptionAlertController.m new file mode 100644 index 0000000..4e550a8 --- /dev/null +++ b/Classes/SBExceptionAlertController.m @@ -0,0 +1,33 @@ +/* HBExceptionAlertController.m $ + + This file is part of the HandBrake source code. + Homepage: . + It may be used under the terms of the GNU General Public License. */ + +#import "SBExceptionAlertController.h" + +@implementation SBExceptionAlertController + +- (instancetype)init +{ + return [self initWithWindowNibName:@"ExceptionAlert"]; +} + +- (NSInteger)runModal +{ + return [NSApp runModalForWindow:self.window]; +} + +- (IBAction)btnCrashClicked:(id)sender +{ + [self.window orderOut:nil]; + [NSApp stopModalWithCode:SBExceptionAlertControllerResultCrash]; +} + +- (IBAction)btnContinueClicked:(id)sender +{ + [self.window orderOut:nil]; + [NSApp stopModalWithCode:SBExceptionAlertControllerResultContinue]; +} + +@end diff --git a/English.lproj/ExceptionAlert.xib b/English.lproj/ExceptionAlert.xib new file mode 100644 index 0000000..9fd0772 --- /dev/null +++ b/English.lproj/ExceptionAlert.xib @@ -0,0 +1,137 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +