2017-11-17 13:37:29 +00:00
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
|
|
|
|
@interface ServerView : NSTextView
|
|
|
|
{
|
2022-06-30 22:36:32 +00:00
|
|
|
NSTask *pTask;
|
|
|
|
NSFileHandle *pFile;
|
2017-11-17 13:37:29 +00:00
|
|
|
}
|
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
|
|
|
{
|
2022-06-30 22:36:32 +00:00
|
|
|
NSPipe *pPipe;
|
|
|
|
pTask = t;
|
|
|
|
pPipe = [NSPipe pipe];
|
|
|
|
[pTask setStandardOutput: pPipe];
|
|
|
|
pFile = [pPipe fileHandleForReading];
|
2017-11-17 13:37:29 +00:00
|
|
|
|
2022-06-30 22:36:32 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(outputNotification:) name: NSFileHandleReadCompletionNotification object: pFile];
|
2017-11-17 13:37:29 +00:00
|
|
|
|
2022-06-30 22:36:32 +00:00
|
|
|
[pFile readInBackgroundAndNotify];
|
2017-11-17 13:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) outputNotification: (NSNotification *) notification
|
|
|
|
{
|
2022-06-30 22:36:32 +00:00
|
|
|
NSData *pData = [[[notification userInfo] objectForKey: NSFileHandleNotificationDataItem] retain];
|
|
|
|
NSString *pString = [[NSString alloc] initWithData: pData encoding: NSASCIIStringEncoding];
|
|
|
|
NSAttributedString *pAttrStr = [[NSAttributedString alloc] initWithString: pString];
|
2017-11-17 13:37:29 +00:00
|
|
|
|
2022-06-30 22:36:32 +00:00
|
|
|
[[self textStorage] appendAttributedString: pAttrStr];
|
2017-11-17 13:37:29 +00:00
|
|
|
int length = [[self textStorage] length];
|
|
|
|
NSRange range = NSMakeRange(length, 0);
|
|
|
|
[self scrollRangeToVisible: range];
|
|
|
|
|
2022-06-30 22:36:32 +00:00
|
|
|
[pAttrStr release];
|
|
|
|
[pString release];
|
|
|
|
[pFile readInBackgroundAndNotify];
|
2017-11-17 13:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
-(void)windowWillClose:(NSNotification *)notification
|
|
|
|
{
|
2022-06-30 22:36:32 +00:00
|
|
|
[pTask terminate];
|
2017-11-17 13:37:29 +00:00
|
|
|
[NSApp terminate:self];
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
void runServer()
|
|
|
|
{
|
2022-06-30 22:36:32 +00:00
|
|
|
NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
|
2017-11-17 13:37:29 +00:00
|
|
|
NSApp = [NSApplication sharedApplication];
|
2022-06-30 22:36:32 +00:00
|
|
|
NSBundle *pMainBundle = [NSBundle mainBundle];
|
|
|
|
NSTask *pTask;
|
|
|
|
pTask = [[NSTask alloc] init];
|
|
|
|
[pTask setCurrentDirectoryPath: [pMainBundle resourcePath]]; // NOLINT(clang-analyzer-nullability.NullablePassedToNonnull)
|
|
|
|
|
|
|
|
NSArray *pArguments = [NSArray new];
|
|
|
|
|
|
|
|
NSAlert *pAlert = [[[NSAlert alloc] init] autorelease];
|
|
|
|
[pAlert setMessageText: @"Run DDNet Server"];
|
|
|
|
[pAlert addButtonWithTitle: @"Use default config"];
|
|
|
|
[pAlert addButtonWithTitle: @"Select config"];
|
|
|
|
[pAlert addButtonWithTitle: @"Cancel"];
|
|
|
|
switch([pAlert runModal])
|
2021-12-25 18:32:43 +00:00
|
|
|
{
|
|
|
|
case NSAlertFirstButtonReturn:
|
|
|
|
break;
|
|
|
|
case NSAlertThirdButtonReturn:
|
|
|
|
return;
|
|
|
|
case NSAlertSecondButtonReturn:
|
|
|
|
// get a server config
|
2022-06-30 22:36:32 +00:00
|
|
|
NSOpenPanel *pOpenDlg = [NSOpenPanel openPanel];
|
|
|
|
[pOpenDlg setCanChooseFiles:YES];
|
|
|
|
if([pOpenDlg runModal] != NSModalResponseOK)
|
2021-12-25 18:32:43 +00:00
|
|
|
return;
|
2022-06-30 22:36:32 +00:00
|
|
|
NSString *pFileName = [[pOpenDlg URL] path];
|
|
|
|
pArguments = [NSArray arrayWithObjects: @"-f", pFileName, nil];
|
2021-12-25 18:32:43 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-11-17 13:37:29 +00:00
|
|
|
|
|
|
|
// run server
|
2022-06-30 22:36:32 +00:00
|
|
|
NSWindow *pWindow;
|
|
|
|
ServerView *pView;
|
|
|
|
NSRect GraphicsRect;
|
2017-11-17 13:37:29 +00:00
|
|
|
|
2022-06-30 22:36:32 +00:00
|
|
|
GraphicsRect = NSMakeRect(100.0, 1000.0, 600.0, 400.0);
|
2017-11-17 13:37:29 +00:00
|
|
|
|
2022-06-30 22:36:32 +00:00
|
|
|
pWindow = [[NSWindow alloc]
|
|
|
|
initWithContentRect: GraphicsRect
|
2022-05-25 15:02:55 +00:00
|
|
|
styleMask: NSWindowStyleMaskTitled
|
|
|
|
| NSWindowStyleMaskClosable
|
|
|
|
| NSWindowStyleMaskMiniaturizable
|
2017-11-17 13:37:29 +00:00
|
|
|
backing: NSBackingStoreBuffered
|
|
|
|
defer: NO];
|
|
|
|
|
2022-06-30 22:36:32 +00:00
|
|
|
[pWindow setTitle: @"DDNet Server"];
|
2017-11-17 13:37:29 +00:00
|
|
|
|
2022-06-30 22:36:32 +00:00
|
|
|
pView = [[[ServerView alloc] initWithFrame: GraphicsRect] autorelease];
|
|
|
|
[pView setEditable: NO];
|
|
|
|
[pView setRulerVisible: YES];
|
2017-11-17 13:37:29 +00:00
|
|
|
|
2022-06-30 22:36:32 +00:00
|
|
|
[pWindow setContentView: pView];
|
|
|
|
[pWindow setDelegate: (id<NSWindowDelegate>)pView];
|
|
|
|
[pWindow makeKeyAndOrderFront: nil];
|
2017-11-17 13:37:29 +00:00
|
|
|
|
2022-06-30 22:36:32 +00:00
|
|
|
[pView listenTo: pTask];
|
|
|
|
[pTask setLaunchPath: [pMainBundle pathForAuxiliaryExecutable: @"DDNet-Server"]];
|
|
|
|
[pTask setArguments: pArguments];
|
|
|
|
[pTask launch];
|
2017-11-17 13:37:29 +00:00
|
|
|
[NSApp run];
|
2022-06-30 22:36:32 +00:00
|
|
|
[pTask terminate];
|
2017-11-17 13:37:29 +00:00
|
|
|
|
|
|
|
[NSApp release];
|
2022-06-30 22:36:32 +00:00
|
|
|
[pPool release];
|
2017-11-17 13:37:29 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|