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,13 +42,68 @@
[[NSApplication sharedApplication] terminate:self]; [[NSApplication sharedApplication] terminate:self];
} }
else
{
[self launchMod:@"cnc"]; [self launchMod:@"cnc"];
[NSApp terminate: nil]; [NSApp terminate: nil];
}
} }
- (void)launchFilePicker:(NSArray *)args
{
NSOpenPanel *op = [NSOpenPanel openPanel];
[op setAllowsMultipleSelection:NO];
NSUInteger a = [args indexOfObject:@"--title"];
if (a != NSNotFound)
[op setTitle:[args objectAtIndex:a+1]];
a = [args indexOfObject:@"--message"];
if (a != NSNotFound)
[op setMessage:[args objectAtIndex:a+1]];
a = [args indexOfObject:@"--directory"];
if (a != NSNotFound)
[op setDirectory:[[args objectAtIndex:a+1] stringByExpandingTildeInPath]];
if ([op runModal] == NSFileHandlingPanelOKButton)
printf("%s\n", [[[op URL] path] UTF8String]);
[NSApp terminate: nil];
}
-(void) launchMod:(NSString *)mod
{
// Use LaunchServices because neither NSTask or NSWorkspace support Info.plist _and_ arguments pre-10.6
// First argument is the directory to run in
// Second...Nth arguments are passed to OpenRA.Game.exe
// Launcher wrapper sets mono --debug, gl renderer and support dir.
NSArray *args = [NSArray arrayWithObjects:@"--launch", gamePath, monoPath,
[NSString stringWithFormat:@"SupportDir=%@",[@"~/Library/Application Support/OpenRA" stringByExpandingTildeInPath]],
[NSString stringWithFormat:@"Game.Mods=%@",mod],
nil];
FSRef appRef;
CFURLGetFSRef((CFURLRef)[NSURL URLWithString:[[[NSBundle mainBundle] executablePath] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]], &appRef);
// Set the launch parameters
LSApplicationParameters params;
params.version = 0;
params.flags = kLSLaunchDefaults | kLSLaunchNewInstance;
params.application = &appRef;
params.asyncLaunchRefCon = NULL;
params.environment = NULL; // CFDictionaryRef of environment variables; could be useful
params.argv = (CFArrayRef)args;
params.initialEvent = NULL;
ProcessSerialNumber psn;
OSStatus err = LSOpenApplication(&params, &psn);
// Bring the game window to the front
if (err == noErr)
SetFrontProcess(&psn);
}
- (BOOL)initMono - (BOOL)initMono
{ {
// Find the users mono // Find the users mono
@@ -92,7 +147,6 @@
int point = 0; int point = 0;
sscanf([ret UTF8String], "Mono JIT compiler version %d.%d.%d", &major, &minor, &point); sscanf([ret UTF8String], "Mono JIT compiler version %d.%d.%d", &major, &minor, &point);
[ret release]; [ret release];
NSLog(@"mono %d.%d.%d: %@",major,minor,point,monoPath);
return (major > 2 || return (major > 2 ||
(major == 2 && minor > 6) || (major == 2 && minor > 6) ||
@@ -106,44 +160,4 @@
[super dealloc]; [super dealloc];
} }
-(void)launchMod:(NSString *)mod
{
// Use LaunchServices because neither NSTask or NSWorkspace support Info.plist _and_ arguments pre-10.6
// First argument is the directory to run in
// Second...Nth arguments are passed to OpenRA.Game.exe
// Launcher wrapper sets mono --debug, gl renderer and support dir.
NSArray *args = [NSArray arrayWithObjects:@"--launch", gamePath, monoPath,
[NSString stringWithFormat:@"SupportDir=%@",[@"~/Library/Application Support/OpenRA" stringByExpandingTildeInPath]],
[NSString stringWithFormat:@"Game.Mods=%@",mod],
nil];
FSRef appRef;
CFURLGetFSRef((CFURLRef)[NSURL URLWithString:[[[NSBundle mainBundle] executablePath] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]], &appRef);
// Set the launch parameters
LSApplicationParameters params;
params.version = 0;
params.flags = kLSLaunchDefaults | kLSLaunchNewInstance;
params.application = &appRef;
params.asyncLaunchRefCon = NULL;
params.environment = NULL; // CFDictionaryRef of environment variables; could be useful
params.argv = (CFArrayRef)args;
params.initialEvent = NULL;
ProcessSerialNumber psn;
OSStatus err = LSOpenApplication(&params, &psn);
// Bring the game window to the front
if (err == noErr)
SetFrontProcess(&psn);
}
#pragma mark Application delegates
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
{
return YES;
}
@end @end