From 21594a04051c02b5a3c9b38f67b383e1f2c9b56a Mon Sep 17 00:00:00 2001 From: def Date: Sat, 25 Dec 2021 19:32:43 +0100 Subject: [PATCH] DDNet-Server-Launcher (for macOS): Support running without config --- ddnet-libs | 2 +- src/macoslaunch/server.mm | 30 ++++++--- src/macoslaunch/server_mysql.m | 112 --------------------------------- 3 files changed, 23 insertions(+), 121 deletions(-) delete mode 100644 src/macoslaunch/server_mysql.m diff --git a/ddnet-libs b/ddnet-libs index fb6f77066..2b721d453 160000 --- a/ddnet-libs +++ b/ddnet-libs @@ -1 +1 @@ -Subproject commit fb6f77066f108262d285bbca95ffdeea80c27515 +Subproject commit 2b721d4531b4ae18904b4159f42aeda8950f77e2 diff --git a/src/macoslaunch/server.mm b/src/macoslaunch/server.mm index 97c8301f9..4b03ba208 100644 --- a/src/macoslaunch/server.mm +++ b/src/macoslaunch/server.mm @@ -54,15 +54,29 @@ void runServer() task = [[NSTask alloc] init]; [task setCurrentDirectoryPath: [mainBundle resourcePath]]; - // get a server config - NSOpenPanel *openDlg = [NSOpenPanel openPanel]; - [openDlg setCanChooseFiles:YES]; + NSArray *arguments = [NSArray new]; - if([openDlg runModal] != NSOKButton) - return; - - NSString *filename = [[openDlg URL] path]; - NSArray *arguments = [NSArray arrayWithObjects: @"-f", filename, nil]; + NSAlert *alert = [[[NSAlert alloc] init] autorelease]; + [alert setMessageText: @"Run DDNet Server"]; + [alert addButtonWithTitle: @"Run with default config"]; + [alert addButtonWithTitle: @"Select config"]; + [alert addButtonWithTitle: @"Cancel"]; + switch([alert runModal]) + { + case NSAlertFirstButtonReturn: + break; + case NSAlertThirdButtonReturn: + return; + case NSAlertSecondButtonReturn: + // get a server config + NSOpenPanel *openDlg = [NSOpenPanel openPanel]; + [openDlg setCanChooseFiles:YES]; + if([openDlg runModal] != NSOKButton) + return; + NSString *filename = [[openDlg URL] path]; + arguments = [NSArray arrayWithObjects: @"-f", filename, nil]; + break; + } // run server NSWindow *window; diff --git a/src/macoslaunch/server_mysql.m b/src/macoslaunch/server_mysql.m deleted file mode 100644 index 1a553d7d6..000000000 --- a/src/macoslaunch/server_mysql.m +++ /dev/null @@ -1,112 +0,0 @@ -#import - -@interface ServerView : NSTextView -{ - NSTask *task; - NSFileHandle *file; -} -- (void)listenTo: (NSTask*)t; -@end - -@implementation ServerView -- (void)listenTo: (NSTask*)t; -{ - NSPipe *pipe; - task = t; - pipe = [NSPipe pipe]; - [task setStandardOutput: pipe]; - file = [pipe fileHandleForReading]; - - [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(outputNotification:) name: NSFileHandleReadCompletionNotification object: file]; - - [file readInBackgroundAndNotify]; -} - -- (void) outputNotification: (NSNotification *) notification -{ - NSData *data = [[[notification userInfo] objectForKey: NSFileHandleNotificationDataItem] retain]; - NSString *string = [[NSString alloc] initWithData: data encoding: NSASCIIStringEncoding]; - NSAttributedString *attrstr = [[NSAttributedString alloc] initWithString: string]; - - [[self textStorage] appendAttributedString: attrstr]; - int length = [[self textStorage] length]; - NSRange range = NSMakeRange(length, 0); - [self scrollRangeToVisible: range]; - - [attrstr release]; - [string release]; - [file readInBackgroundAndNotify]; -} - --(void)windowWillClose:(NSNotification *)notification -{ - [task terminate]; - [NSApp terminate:self]; -} -@end - -void runServer() -{ - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - NSApp = [NSApplication sharedApplication]; - NSBundle* mainBundle = [NSBundle mainBundle]; - NSTask *task; - task = [[NSTask alloc] init]; - [task setCurrentDirectoryPath: [mainBundle resourcePath]]; - - // get a server config - NSOpenPanel* openDlg = [NSOpenPanel openPanel]; - [openDlg setCanChooseFiles:YES]; - - if([openDlg runModalForDirectory:nil file:nil] != NSOKButton) - return; - - NSArray* filenames = [openDlg filenames]; - if([filenames count] != 1) - return; - - NSString* filename = [filenames objectAtIndex: 0]; - NSArray* arguments = [NSArray arrayWithObjects: @"-f", filename, nil]; - - // run server - NSWindow *window; - ServerView *view; - NSRect graphicsRect; - - graphicsRect = NSMakeRect(100.0, 1000.0, 600.0, 400.0); - - window = [[NSWindow alloc] - initWithContentRect: graphicsRect - styleMask: NSTitledWindowMask - | NSClosableWindowMask - | NSMiniaturizableWindowMask - backing: NSBackingStoreBuffered - defer: NO]; - - [window setTitle: @"DDNet Server"]; - - view = [[[ServerView alloc] initWithFrame: graphicsRect] autorelease]; - [view setEditable: NO]; - [view setRulerVisible: YES]; - - [window setContentView: view]; - [window setDelegate: view]; - [window makeKeyAndOrderFront: nil]; - - [view listenTo: task]; - [task setLaunchPath: [mainBundle pathForAuxiliaryExecutable: @"DDNet-Server_sql"]]; - [task setArguments: arguments]; - [task launch]; - [NSApp run]; - [task terminate]; - - [NSApp release]; - [pool release]; -} - -int main (int argc, char **argv) -{ - runServer(); - - return 0; -}