Permalink
Browse files

fix crash and re-enable Application Support directory on OS X version…

…s < 10.9
  • Loading branch information...
jacob1 committed Aug 4, 2014
1 parent 92237ca commit ab2dd1ce617c1daa1cd9070ca8d562297413e69d
Showing with 22 additions and 44 deletions.
  1. +22 −44 src/SDLMain.m
@@ -85,50 +85,26 @@ - (void)terminate:(id)sender
/* The main class of the application, the application's delegate */
@implementation SDLMain
/* Set the working directory to the .app's parent directory */
/* Set the working directory to Application Support */
- (void) setupWorkingDirectory:(BOOL)shouldChdir
{
SInt32 versionMajor = 0, versionMinor = 0;
Gestalt(gestaltSystemVersionMajor, &versionMajor);
Gestalt(gestaltSystemVersionMinor, &versionMinor);
/* Set the working directory to Application Support on Mavericks and above */
if (versionMajor > 10 || versionMinor >= 9)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
if ([paths count] < 1)
return;
NSString *appSupportPath = [paths objectAtIndex:0];
BOOL isDir = NO;
NSError *error = nil;
NSString *appPath = [appSupportPath stringByAppendingPathComponent:@"The Powder Toy"];
if (![[NSFileManager defaultManager] fileExistsAtPath:appPath isDirectory:&isDir] && isDir == NO)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
if ([paths count] < 1)
return;
NSString *appSupportPath = [paths objectAtIndex:0];
BOOL isDir = NO;
NSError *error = nil;
NSString *appPath = [appSupportPath stringByAppendingPathComponent:@"The Powder Toy"];
if (![[NSFileManager defaultManager] fileExistsAtPath:appPath isDirectory:&isDir] && isDir == NO)
if (![[NSFileManager defaultManager] createDirectoryAtPath:appPath withIntermediateDirectories:YES attributes:nil error:&error])
{
if (![[NSFileManager defaultManager] createDirectoryAtPath:appPath withIntermediateDirectories:YES attributes:nil error:&error])
{
NSLog(@"Could not set up working dir. Error: %@", error);
return;
}
}
chdir([appPath UTF8String]);
}
/* Set the working directory to the .app's parent directory, because the code above breaks anything below Mavericks? (just a guess) */
else
{
if (shouldChdir)
{
char parentdir[MAXPATHLEN];
CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle());
CFURLRef url2 = CFURLCreateCopyDeletingLastPathComponent(0, url);
if (CFURLGetFileSystemRepresentation(url2, 1, (UInt8 *)parentdir, MAXPATHLEN))
{
chdir(parentdir); /* chdir to the binary app's parent */
}
CFRelease(url);
CFRelease(url2);
NSLog(@"Could not set up working dir. Error: %@", error);
return;
}
}
chdir([appPath UTF8String]);
}
#if SDL_USE_NIB_FILE
@@ -391,7 +367,8 @@ - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString
@end
char * readUserPreferences() {
char * readUserPreferences()
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *prefDataNSString = [prefs stringForKey:@"powder.pref"];
@@ -403,23 +380,23 @@ - (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString
SDL_strlcpy(prefDataCopy, prefData, [prefDataNSString length]+1);
[prefDataNSString release];
[prefs release];
return prefDataCopy;
}
void writeUserPreferences(const char * prefData) {
void writeUserPreferences(const char * prefData)
{
NSString *prefDataNSString = [NSString stringWithUTF8String:prefData];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:prefDataNSString forKey:@"powder.pref"];
[prefs synchronize];
[prefDataNSString release];
[prefs release];
}
char * readClipboard() {
char * readClipboard()
{
NSPasteboard *clipboard = [NSPasteboard generalPasteboard];
NSArray *classes = [[NSArray alloc] initWithObjects:[NSString class], nil];
@@ -439,7 +416,8 @@ void writeUserPreferences(const char * prefData) {
return clipboardDataCopy;
}
void writeClipboard(const char * clipboardData) {
void writeClipboard(const char * clipboardData)
{
NSPasteboard *clipboard = [NSPasteboard generalPasteboard];
NSString *newString = [NSString stringWithUTF8String: clipboardData];

0 comments on commit ab2dd1c

Please sign in to comment.