diff --git a/xcode/SDLMain.h b/xcode/SDLMain.h index 3a447f788..fc8005420 100644 --- a/xcode/SDLMain.h +++ b/xcode/SDLMain.h @@ -9,12 +9,12 @@ @interface SDLMain: NSObject { - NSString* clonkDirectory; NSMutableArray* gatheredArguments; + NSString* clonkDirectory; NSString* addonSupplied; BOOL doNotLaunch; - BOOL hasFinished; BOOL terminateRequested; + BOOL gameLoopFinished; } - (NSString*) clonkDirectory; - (BOOL) argsLookLikeItShouldBeInstallation:(char**)argv argc:(int)argc; diff --git a/xcode/SDLMain.mm b/xcode/SDLMain.mm index c37547d02..19152f0b5 100644 --- a/xcode/SDLMain.mm +++ b/xcode/SDLMain.mm @@ -7,16 +7,24 @@ /* The main class of the application, the appl¤ication's delegate */ @implementation SDLMain -- (id) init { +- (id) init +{ if (self = [super init]) { - // first argument is path to executable (for authenticity) - gatheredArguments = [NSMutableArray arrayWithCapacity:10]; + gatheredArguments = [[NSMutableArray arrayWithCapacity:10] retain]; [gatheredArguments addObject:[[NSBundle mainBundle] executablePath]]; } return self; } -- (void) release { +- (void)awakeFromNib +{ + [[NSAppleEventManager sharedAppleEventManager] setEventHandler:self + andSelector:@selector(getUrl:withReplyEvent:) + forEventClass:kInternetEventClass andEventID:kAEGetURL]; +} + +- (void) release +{ if (terminateRequested) [NSApp replyToApplicationShouldTerminate:YES]; [super release]; @@ -28,17 +36,18 @@ NSArray* clonkFileNameExtensions = [NSArray arrayWithObjects:@"c4d", @"c4s", @"c4f", @"c4g", @"c4s", nil]; if ([clonkFileNameExtensions containsObject:pathExtension]) - { - // later decide whether to install or run - addonSupplied = filename; - if (hasFinished) { - // if application is already running install immediately - [self installAddOn]; - return YES; - } - // still add to gatheredArguments - // return YES; - } + { + // later decide whether to install or run + addonSupplied = filename; + if (YES) + { + // if application is already running install immediately + [self installAddOn]; + return YES; + } + // still add to gatheredArguments + // return YES; + } // Key/Update files or simply arguments: Just pass to engine, will install [gatheredArguments addObject:filename]; @@ -52,6 +61,30 @@ [gatheredArguments addObject:url]; } +- (void)gameLoop +{ + [[NSFileManager defaultManager] changeCurrentDirectoryPath:[self clonkDirectory]]; + + [NSApp activateIgnoringOtherApps:YES]; + + /*if ([self argsLookLikeItShouldBeInstallation:argv argc:argc]) { + if ([self installAddOn]) + return 0; + }*/ + + // Hand off to Clonk code + char** newArgv; + int newArgc; + [self makeFakeArgs:&newArgv argc:&newArgc]; + int status = SDL_main(newArgc, newArgv); + + for (int i = newArgc-1; i >= 0; i--) + { + free (newArgv[i]); + } + free(newArgv); +} + /* Called when the internal event loop has just started running */ - (void) applicationDidFinishLaunching: (NSNotification *) note { @@ -59,12 +92,19 @@ for (NSString* key in args) { [gatheredArguments addObject:[NSString stringWithFormat:@"/%@:%@", key, [args valueForKey:key]]]; }*/ - hasFinished = YES; + [self gameLoop]; + gameLoopFinished = YES; + [NSApp terminate:self]; } -- (NSApplicationTerminateReply) applicationShouldTerminate:(NSApplication*)application { - [self terminate:application]; - return NSTerminateCancel; // cancels logoff but it's the only way that does not interrupt the lifecycle of the application +- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)application +{ + if (!gameLoopFinished) + { + [self terminate:application]; + return NSTerminateCancel; // cancels logoff but it's the only way that does not interrupt the lifecycle of the application + } + return NSTerminateNow; } - (void)terminate:(NSApplication*)sender @@ -75,12 +115,9 @@ SDL_PushEvent(&event); } -- (BOOL)hasFinished { - return hasFinished; -} - // arguments that should be converted to a c char* array and then passed on to SDL_main -- (NSMutableArray*)gatheredArguments { +- (NSMutableArray*)gatheredArguments +{ return gatheredArguments; } @@ -94,8 +131,9 @@ } // Look for -psn argument which generally is a clue that the application should open a file (double-clicking, calling /usr/bin/open and such) -- (BOOL) argsLookLikeItShouldBeInstallation:(char**)argv argc:(int)argc { - // not having this check leads to deletion of Clonk foler -.- +- (BOOL) argsLookLikeItShouldBeInstallation:(char**)argv argc:(int)argc +{ + // not having this check leads to deletion of Clonk folder -.- if (!addonSupplied) return NO; for (int i = 0; i < argc; i++) { @@ -106,7 +144,8 @@ } // Copies the add-on to the clonk directory -- (BOOL) installAddOn { +- (BOOL) installAddOn +{ if (!addonSupplied) return NO; @@ -141,11 +180,13 @@ } // convert gatheredArguments to c array -- (void)makeFakeArgs:(char***)argv argc:(int*)argc { +- (void)makeFakeArgs:(char***)argv argc:(int*)argc +{ int argCount = [gatheredArguments count]; char** args = (char**)malloc(sizeof(char*) * argCount); - for (int i = 0; i < argCount; i++) { - args[i] = strdup([[gatheredArguments objectAtIndex:i] cStringUsingEncoding:NSASCIIStringEncoding]); + for (int i = 0; i < argCount; i++) + { + args[i] = strdup([[gatheredArguments objectAtIndex:i] cStringUsingEncoding:NSUTF8StringEncoding]); } *argv = args; *argc = argCount; @@ -158,56 +199,7 @@ #endif /* Main entry point to executable - should *not* be SDL_main! */ -int main (int argc, char **argv) +int main (int argc, const char **argv) { - // Catches whatever would be leaked otherwise. - // Gets rid of warnings from hax0red SDL framework. - [[NSAutoreleasePool alloc] init]; - - SDLMain* sdlMain = [[SDLMain alloc] init]; - [[NSFileManager defaultManager] changeCurrentDirectoryPath:[sdlMain clonkDirectory]]; - - // Set up application & delegate. - [NSApplication sharedApplication]; - [NSApp setDelegate:sdlMain]; - [[NSAppleEventManager sharedAppleEventManager] setEventHandler:sdlMain - andSelector:@selector(getUrl:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL]; - [NSBundle loadNibNamed:@"SDLMain" owner:NSApp]; - [NSApp finishLaunching]; - - [NSApp activateIgnoringOtherApps:YES]; - - // Now there's openFile calls and all that waiting for us in the event queue. - // Fetch events until applicationHasFinishedLaunching was called. - while (![sdlMain hasFinished]) { - NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask - untilDate:nil - inMode:NSDefaultRunLoopMode - dequeue:YES ]; - if ( event == nil ) { - break; - } - [NSApp sendEvent:event]; - } - - if ([sdlMain argsLookLikeItShouldBeInstallation:argv argc:argc]) { - if ([sdlMain installAddOn]) - return 0; - } - - // Hand off to Clonk code - char** newArgv; - int newArgc; - [sdlMain makeFakeArgs:&newArgv argc:&newArgc]; - int status = SDL_main(newArgc, newArgv); - - // I don't think this is even necessary since the OS will just wipe it all out when the process ends - for (int i = newArgc-1; i >= 0; i--) { - free (newArgv[i]); - } - free(newArgv); - - [sdlMain release]; - - return status; + return NSApplicationMain(argc, argv); } diff --git a/xcode/SDLMain.xib b/xcode/SDLMain.xib new file mode 100644 index 000000000..5e79c3947 --- /dev/null +++ b/xcode/SDLMain.xib @@ -0,0 +1,679 @@ + + + + 1060 + 10C540 + 740 + 1038.25 + 458.00 + + com.apple.InterfaceBuilder.CocoaPlugin + 740 + + + YES + + + + YES + com.apple.InterfaceBuilder.CocoaPlugin + + + YES + + YES + + + YES + + + + YES + + + NSApplication + + + + FirstResponder + + + NSApplication + + + SDLMain + + YES + + + Clonk + + 1048576 + 2147483647 + + NSImage + NSMenuCheckmark + + + NSImage + NSMenuMixedState + + submenuAction: + + Clonk + + YES + + + Hide Clonk + h + 1048576 + 2147483647 + + + + + + Hide Others + + 1048576 + 2147483647 + + + + + + Show All + + 1048576 + 2147483647 + + + + + + YES + YES + + + 1048576 + 2147483647 + + + + + + Quit Clonk + q + 1048576 + 2147483647 + + + + + _NSAppleMenu + + + + + Window + + 1048576 + 2147483647 + + + submenuAction: + + + Window + + + YES + + + Minimize + m + 1048576 + 2147483647 + + + + + _NSWindowsMenu + + + + _NSMainMenu + + + NSFontManager + + + SDLMain + + + + + YES + + + hideOtherApplications: + + + + 146 + + + + hide: + + + + 152 + + + + unhideAllApplications: + + + + 153 + + + + performMiniaturize: + + + + 328 + + + + delegate + + + + 338 + + + + terminate: + + + + 339 + + + + + YES + + 0 + + + + + + -2 + + + File's Owner + + + -1 + + + First Responder + + + 29 + + + YES + + + + + MainMenu + + + 56 + + + YES + + + + + + 57 + + + YES + + + + + + + + + + 134 + + + + + 136 + + + + + 145 + + + + + 149 + + + + + 150 + + + + + 284 + + + YES + + + + + + 285 + + + YES + + + + + + 283 + + + + + 253 + + + Font Manager + + + 331 + + + SDLMain + + + -3 + + + Application + + + + + YES + + YES + 134.IBPluginDependency + 134.ImportedFromIB2 + 136.IBPluginDependency + 136.ImportedFromIB2 + 145.IBPluginDependency + 145.ImportedFromIB2 + 149.IBPluginDependency + 149.ImportedFromIB2 + 150.IBPluginDependency + 150.ImportedFromIB2 + 253.ImportedFromIB2 + 283.IBPluginDependency + 283.ImportedFromIB2 + 284.IBPluginDependency + 284.ImportedFromIB2 + 285.IBEditorWindowLastContentRect + 285.IBPluginDependency + 285.ImportedFromIB2 + 29.IBEditorWindowLastContentRect + 29.IBPluginDependency + 29.ImportedFromIB2 + 331.ImportedFromIB2 + 56.IBPluginDependency + 56.ImportedFromIB2 + 57.IBEditorWindowLastContentRect + 57.IBPluginDependency + 57.ImportedFromIB2 + + + YES + com.apple.InterfaceBuilder.CocoaPlugin + + com.apple.InterfaceBuilder.CocoaPlugin + + com.apple.InterfaceBuilder.CocoaPlugin + + com.apple.InterfaceBuilder.CocoaPlugin + + com.apple.InterfaceBuilder.CocoaPlugin + + + com.apple.InterfaceBuilder.CocoaPlugin + + com.apple.InterfaceBuilder.CocoaPlugin + + {{124, 952}, {140, 23}} + com.apple.InterfaceBuilder.CocoaPlugin + + {{52, 975}, {155, 20}} + com.apple.InterfaceBuilder.CocoaPlugin + + + com.apple.InterfaceBuilder.CocoaPlugin + + {{64, 882}, {185, 93}} + com.apple.InterfaceBuilder.CocoaPlugin + + + + + YES + + + YES + + + + + YES + + + YES + + + + 339 + + + + YES + + FirstResponder + + IBUserSource + + + + + SDLMain + NSObject + + terminate: + NSApplication + + + IBProjectSource + SDLMain.h + + + + SDLMain + NSObject + + IBUserSource + + + + + + YES + + NSObject + + IBFrameworkSource + AddressBook.framework/Headers/ABActions.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSArchiver.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSClassDescription.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSConnection.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSError.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSFileManager.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSKeyValueCoding.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSKeyValueObserving.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSKeyedArchiver.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSMetadata.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSNetServices.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSObject.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSObjectScripting.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSPort.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSPortCoder.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSRunLoop.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSScriptClassDescription.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSScriptKeyValueCoding.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSScriptObjectSpecifiers.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSScriptWhoseTests.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSSpellServer.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSStream.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSThread.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSURL.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSURLConnection.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSURLDownload.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSXMLParser.h + + + + NSObject + + IBFrameworkSource + PrintCore.framework/Headers/PDEPluginInterface.h + + + + NSObject + + IBFrameworkSource + QuartzCore.framework/Headers/CAAnimation.h + + + + NSObject + + IBFrameworkSource + QuartzCore.framework/Headers/CALayer.h + + + + NSObject + + IBFrameworkSource + QuartzCore.framework/Headers/CIImageProvider.h + + + + + 0 + + com.apple.InterfaceBuilder.CocoaPlugin.macosx + + + + com.apple.InterfaceBuilder.CocoaPlugin.macosx + + + + com.apple.InterfaceBuilder.CocoaPlugin.InterfaceBuilder3 + + + YES + Clonk.xcodeproj + 3 + +