Expose a native filepicker dialog.

This commit is contained in:
Paul Chote
2011-01-19 19:27:36 +13:00
committed by Paul Chote
parent 108b90f192
commit 8d2a78abc6
4 changed files with 84 additions and 71 deletions

View File

@@ -10,13 +10,12 @@
@interface Controller : NSObject @interface Controller : NSObject
{ {
BOOL hasMono;
NSString *monoPath; NSString *monoPath;
NSString *gamePath; NSString *gamePath;
IBOutlet NSWindow *window; IBOutlet NSWindow *window;
} }
- (void)launchFilePicker:(NSArray *)args;
- (void)launchMod:(NSString *)mod; - (void)launchMod:(NSString *)mod;
- (BOOL)initMono; - (BOOL)initMono;
@end @end

View File

@@ -17,18 +17,18 @@
forKey:@"gamepath"]]; forKey:@"gamepath"]];
} }
- (void)awakeFromNib
{
gamePath = [[NSUserDefaults standardUserDefaults] stringForKey:@"gamepath"];
hasMono = [self initMono];
NSLog(@"%d, %@",hasMono, monoPath);
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{ {
if (!hasMono) gamePath = [[NSUserDefaults standardUserDefaults] stringForKey:@"gamepath"];
NSArray *args = [[NSProcessInfo processInfo] arguments];
// Ingame requests for native dialogs
if ([args containsObject:@"--filepicker"])
[self launchFilePicker:args];
// Try and launch the game
if (![self initMono])
{ {
NSAlert *alert = [NSAlert alertWithMessageText:@"Mono Framework" NSAlert *alert = [NSAlert alertWithMessageText:@"Mono Framework"
defaultButton:@"Download Mono" defaultButton:@"Download Mono"
@@ -42,72 +42,35 @@
[[NSApplication sharedApplication] terminate:self]; [[NSApplication sharedApplication] terminate:self];
} }
else
{ [self launchMod:@"cnc"];
[self launchMod:@"cnc"]; [NSApp terminate: nil];
[NSApp terminate: nil];
}
} }
- (BOOL)initMono - (void)launchFilePicker:(NSArray *)args
{ {
// Find the users mono NSOpenPanel *op = [NSOpenPanel openPanel];
NSPipe *outPipe = [NSPipe pipe]; [op setAllowsMultipleSelection:NO];
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/bin/which"];
[task setArguments:[NSMutableArray arrayWithObject:@"mono"]];
[task setStandardOutput:outPipe];
[task setStandardError:[task standardOutput]];
[task launch];
NSData *data = [[outPipe fileHandleForReading] readDataToEndOfFile]; NSUInteger a = [args indexOfObject:@"--title"];
[task waitUntilExit]; if (a != NSNotFound)
[task release]; [op setTitle:[args objectAtIndex:a+1]];
NSString *temp = [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] autorelease]; a = [args indexOfObject:@"--message"];
// Remove whitespace and resolve symlinks if (a != NSNotFound)
monoPath = [[[temp stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] [op setMessage:[args objectAtIndex:a+1]];
stringByResolvingSymlinksInPath] retain];
if (![monoPath length])
return NO;
// Find the mono version a = [args indexOfObject:@"--directory"];
outPipe = [NSPipe pipe]; if (a != NSNotFound)
task = [[NSTask alloc] init]; [op setDirectory:[[args objectAtIndex:a+1] stringByExpandingTildeInPath]];
[task setLaunchPath:monoPath];
[task setArguments:[NSMutableArray arrayWithObject:@"--version"]];
[task setStandardOutput:outPipe];
[task setStandardError:[task standardOutput]];
[task launch];
data = [[outPipe fileHandleForReading] readDataToEndOfFile]; if ([op runModal] == NSFileHandlingPanelOKButton)
[task waitUntilExit]; printf("%s\n", [[[op URL] path] UTF8String]);
[task release];
NSString *ret = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; [NSApp terminate: nil];
int major = 0;
int minor = 0;
int point = 0;
sscanf([ret UTF8String], "Mono JIT compiler version %d.%d.%d", &major, &minor, &point);
[ret release];
NSLog(@"mono %d.%d.%d: %@",major,minor,point,monoPath);
return (major > 2 ||
(major == 2 && minor > 6) ||
(major == 2 && minor == 6 && point >= 7));
} }
- (void)dealloc -(void) launchMod:(NSString *)mod
{
[monoPath release]; monoPath = nil;
[gamePath release]; gamePath = nil;
[super dealloc];
}
-(void)launchMod:(NSString *)mod
{ {
// Use LaunchServices because neither NSTask or NSWorkspace support Info.plist _and_ arguments pre-10.6 // Use LaunchServices because neither NSTask or NSWorkspace support Info.plist _and_ arguments pre-10.6
@@ -140,10 +103,61 @@
SetFrontProcess(&psn); SetFrontProcess(&psn);
} }
#pragma mark Application delegates
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender - (BOOL)initMono
{ {
return YES; // Find the users mono
NSPipe *outPipe = [NSPipe pipe];
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/bin/which"];
[task setArguments:[NSMutableArray arrayWithObject:@"mono"]];
[task setStandardOutput:outPipe];
[task setStandardError:[task standardOutput]];
[task launch];
NSData *data = [[outPipe fileHandleForReading] readDataToEndOfFile];
[task waitUntilExit];
[task release];
NSString *temp = [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] autorelease];
// Remove whitespace and resolve symlinks
monoPath = [[[temp stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
stringByResolvingSymlinksInPath] retain];
if (![monoPath length])
return NO;
// Find the mono version
outPipe = [NSPipe pipe];
task = [[NSTask alloc] init];
[task setLaunchPath:monoPath];
[task setArguments:[NSMutableArray arrayWithObject:@"--version"]];
[task setStandardOutput:outPipe];
[task setStandardError:[task standardOutput]];
[task launch];
data = [[outPipe fileHandleForReading] readDataToEndOfFile];
[task waitUntilExit];
[task release];
NSString *ret = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
int major = 0;
int minor = 0;
int point = 0;
sscanf([ret UTF8String], "Mono JIT compiler version %d.%d.%d", &major, &minor, &point);
[ret release];
return (major > 2 ||
(major == 2 && minor > 6) ||
(major == 2 && minor == 6 && point >= 7));
}
- (void)dealloc
{
[monoPath release]; monoPath = nil;
[gamePath release]; gamePath = nil;
[super dealloc];
} }
@end @end