Fix 10.7 compatibility and add a window to show an exception log

This commit is contained in:
galad87 committed 2015-08-29 11:41:24 +02:00
1 parent 60b1c60155
commit d1360727ec
13 files changed
+452

No files matched your search

@@ -0,0 +1,4 @@
#include <Foundation/Foundation.h>
@interface NSArray (Subscripting)
@end
@@ -0,0 +1,26 @@
#import "NSArray+Subscripting.h"
#import <objc/runtime.h>
@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
+12
View File
@@ -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 <Foundation/Foundation.h>
@interface NSDictionary (Subscript)
@end
+34
View File
@@ -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 <objc/runtime.h>
@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
+12
View File
@@ -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 <Foundation/Foundation.h>
@interface NSMutableArray (Subscript)
@end
+50
View File
@@ -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
+12
View File
@@ -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 <Foundation/Foundation.h>
@interface NSMutableDictionary (Subscript)
@end
+34
View File
@@ -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 <objc/runtime.h>
@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
+11
View File
@@ -0,0 +1,11 @@
/* HBApplication.h $
This file is part of the HandBrake source code.
Homepage: <http://handbrake.fr/>.
It may be used under the terms of the GNU General Public License. */
#import <Cocoa/Cocoa.h>
@interface SBApplication : NSApplication
@end
+62
View File
@@ -0,0 +1,62 @@
/* HBApplication.m $
This file is part of the HandBrake source code.
Homepage: <http://handbrake.fr/>.
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
+25
View File
@@ -0,0 +1,25 @@
/* HBExceptionAlertController.h $
This file is part of the HandBrake source code.
Homepage: <http://handbrake.fr/>.
It may be used under the terms of the GNU General Public License. */
#import <Cocoa/Cocoa.h>
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
+33
View File
@@ -0,0 +1,33 @@
/* HBExceptionAlertController.m $
This file is part of the HandBrake source code.
Homepage: <http://handbrake.fr/>.
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
+137
View File
@@ -0,0 +1,137 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="8187.4" systemVersion="15A262e" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none">
<dependencies>
<deployment identifier="macosx"/>
<development version="6300" identifier="xcode"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="8187.4"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="SBExceptionAlertController">
<connections>
<outlet property="window" destination="1" id="17"/>
</connections>
</customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<window title="Internal Error" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" oneShot="NO" animationBehavior="default" id="1">
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/>
<rect key="contentRect" x="196" y="127" width="636" height="383"/>
<rect key="screenRect" x="0.0" y="0.0" width="2560" height="1417"/>
<value key="minSize" type="size" width="636" height="383"/>
<view key="contentView" id="2">
<rect key="frame" x="0.0" y="0.0" width="636" height="383"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<imageView id="3">
<rect key="frame" x="20" y="302" width="70" height="61"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<animations/>
<imageCell key="cell" refusesFirstResponder="YES" alignment="left" imageScaling="proportionallyDown" image="NSApplicationIcon" id="4"/>
</imageView>
<textField verticalHuggingPriority="750" id="9">
<rect key="frame" x="95" y="323" width="524" height="40"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
<animations/>
<textFieldCell key="cell" sendsActionOnEndEditing="YES" title="An internal error has occurred. You can choose to continue in an unstable state, or crash." id="10">
<font key="font" metaFont="systemBold"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField verticalHuggingPriority="750" id="11">
<rect key="frame" x="95" y="282" width="339" height="41"/>
<autoresizingMask key="autoresizingMask" flexibleMinY="YES"/>
<animations/>
<textFieldCell key="cell" sendsActionOnEndEditing="YES" title="Reason contents go here." id="12">
<font key="font" metaFont="smallSystem"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<connections>
<binding destination="-2" name="value" keyPath="exceptionMessage" id="27">
<dictionary key="options">
<bool key="NSAllowsEditingMultipleValuesSelection" value="NO"/>
</dictionary>
</binding>
</connections>
</textField>
<button verticalHuggingPriority="750" id="13">
<rect key="frame" x="418" y="12" width="96" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxY="YES"/>
<animations/>
<buttonCell key="cell" type="push" title="Crash" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="14">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
<string key="keyEquivalent" base64-UTF8="YES">
Gw
</string>
</buttonCell>
<connections>
<action selector="btnCrashClicked:" target="-2" id="24"/>
</connections>
</button>
<button verticalHuggingPriority="750" id="15">
<rect key="frame" x="526" y="12" width="97" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxY="YES"/>
<animations/>
<buttonCell key="cell" type="push" title="Continue" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="16">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
<string key="keyEquivalent" base64-UTF8="YES">
DQ
</string>
</buttonCell>
<connections>
<action selector="btnContinueClicked:" target="-2" id="23"/>
</connections>
</button>
<scrollView horizontalLineScroll="10" horizontalPageScroll="10" verticalLineScroll="10" verticalPageScroll="10" hasHorizontalScroller="NO" usesPredominantAxisScrolling="NO" id="19">
<rect key="frame" x="20" y="60" width="596" height="206"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<clipView key="contentView" id="RBQ-rK-0Rs">
<rect key="frame" x="1" y="1" width="594" height="204"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textView importsGraphics="NO" findStyle="panel" continuousSpellChecking="YES" allowsUndo="YES" usesRuler="YES" usesFontPanel="YES" verticallyResizable="YES" allowsNonContiguousLayout="YES" spellingCorrection="YES" smartInsertDelete="YES" id="22">
<rect key="frame" x="0.0" y="0.0" width="594" height="204"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<animations/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<size key="minSize" width="594" height="204"/>
<size key="maxSize" width="759" height="10000000"/>
<color key="insertionPointColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<size key="minSize" width="594" height="204"/>
<size key="maxSize" width="759" height="10000000"/>
<connections>
<binding destination="-2" name="attributedString" keyPath="exceptionBacktrace" id="28"/>
</connections>
</textView>
</subviews>
<animations/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</clipView>
<animations/>
<scroller key="horizontalScroller" hidden="YES" verticalHuggingPriority="750" doubleValue="1" horizontal="YES" id="21">
<rect key="frame" x="-100" y="-100" width="87" height="18"/>
<autoresizingMask key="autoresizingMask"/>
<animations/>
</scroller>
<scroller key="verticalScroller" verticalHuggingPriority="750" doubleValue="1" horizontal="NO" id="20">
<rect key="frame" x="579" y="1" width="16" height="204"/>
<autoresizingMask key="autoresizingMask"/>
<animations/>
</scroller>
</scrollView>
</subviews>
<animations/>
</view>
<connections>
<outlet property="initialFirstResponder" destination="13" id="29"/>
</connections>
<point key="canvasLocation" x="256" y="550.5"/>
</window>
</objects>
<resources>
<image name="NSApplicationIcon" width="128" height="128"/>
</resources>
</document>