Set working directory to Application Support on Mac OS, fixes #191

This commit is contained in:
Simon Robertshaw 2013-12-01 13:05:47 +00:00
parent c75a5a1f80
commit 58c713f7c6

View File

@ -84,17 +84,21 @@ static NSString *getApplicationName(void)
/* Set the working directory to the .app's parent directory */ /* Set the working directory to the .app's parent directory */
- (void) setupWorkingDirectory:(BOOL)shouldChdir - (void) setupWorkingDirectory:(BOOL)shouldChdir
{ {
if (shouldChdir) NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
{ if([paths count] < 1) return;
char parentdir[MAXPATHLEN];
CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle()); NSString *appSupportPath = [paths objectAtIndex:0];
CFURLRef url2 = CFURLCreateCopyDeletingLastPathComponent(0, url); BOOL isDir = NO;
if (CFURLGetFileSystemRepresentation(url2, 1, (UInt8 *)parentdir, MAXPATHLEN)) { NSError *error = nil;
chdir(parentdir); /* chdir to the binary app's parent */ 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])
{
NSLog(@"Could not set up working dir. Error: %@", error);
return;
} }
CFRelease(url);
CFRelease(url2);
} }
chdir([appPath UTF8String]);
} }
#if SDL_USE_NIB_FILE #if SDL_USE_NIB_FILE