2017-11-17 13:37:29 +00:00
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
|
|
|
|
@interface ServerView : NSTextView
|
|
|
|
{
|
|
|
|
NSTask *task;
|
|
|
|
NSFileHandle *file;
|
|
|
|
}
|
2017-11-17 13:57:42 +00:00
|
|
|
- (void)listenTo: (NSTask *)t;
|
2017-11-17 13:37:29 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation ServerView
|
2017-11-17 13:57:42 +00:00
|
|
|
- (void)listenTo: (NSTask *)t
|
2017-11-17 13:37:29 +00:00
|
|
|
{
|
|
|
|
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];
|
2017-11-17 13:57:42 +00:00
|
|
|
NSBundle *mainBundle = [NSBundle mainBundle];
|
2017-11-17 13:37:29 +00:00
|
|
|
NSTask *task;
|
|
|
|
task = [[NSTask alloc] init];
|
|
|
|
[task setCurrentDirectoryPath: [mainBundle resourcePath]];
|
|
|
|
|
|
|
|
// get a server config
|
2017-11-17 13:57:42 +00:00
|
|
|
NSOpenPanel *openDlg = [NSOpenPanel openPanel];
|
2017-11-17 13:37:29 +00:00
|
|
|
[openDlg setCanChooseFiles:YES];
|
|
|
|
|
2017-11-17 13:57:42 +00:00
|
|
|
if([openDlg runModal] != NSOKButton)
|
2017-11-17 13:37:29 +00:00
|
|
|
return;
|
|
|
|
|
2017-11-17 13:57:42 +00:00
|
|
|
NSString *filename = [[openDlg URL] path];
|
|
|
|
NSArray *arguments = [NSArray arrayWithObjects: @"-f", filename, nil];
|
2017-11-17 13:37:29 +00:00
|
|
|
|
|
|
|
// 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: (id<NSWindowDelegate>)view];
|
|
|
|
[window makeKeyAndOrderFront: nil];
|
|
|
|
|
|
|
|
[view listenTo: task];
|
|
|
|
[task setLaunchPath: [mainBundle pathForAuxiliaryExecutable: @"DDNet-Server"]];
|
|
|
|
[task setArguments: arguments];
|
|
|
|
[task launch];
|
|
|
|
[NSApp run];
|
|
|
|
[task terminate];
|
|
|
|
|
|
|
|
[NSApp release];
|
|
|
|
[pool release];
|
|
|
|
}
|
|
|
|
|
2017-11-17 13:57:42 +00:00
|
|
|
int main(int argc, char **argv)
|
2017-11-17 13:37:29 +00:00
|
|
|
{
|
|
|
|
runServer();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|