3 * The Cheat - The legendary universal game trainer for Mac OS X.
4 * http://www.brokenzipper.com/trac/wiki/TheCheat
6 * Copyright (c) 2003-2011, Charles McGarvey et al.
8 * Distributable under the terms and conditions of the 2-clause BSD
9 * license; see the file COPYING for the legal text of the license.
12 #import "ServerPrefs.h"
14 #include "cheat_global.h"
16 #import "AppController.h"
18 #import "CheatServer.h"
19 #import "ServerChild.h"
22 @interface ServerPrefs ( PrivateAPI
)
24 - (void)_serverStarted
:(NSNotification
*)note
;
25 - (void)_serverStopped
:(NSNotification
*)note
;
26 - (void)_childrenChanged
:(NSNotification
*)note
;
31 @implementation ServerPrefs
36 if ( self = [super init
] ) {
37 NSNotificationCenter
*nc
= [NSNotificationCenter defaultCenter
];
39 // register for server notifications
40 [nc addObserver
:self selector
:@selector(_serverStarted
:) name
:TCServerStartedNote object
:nil];
41 [nc addObserver
:self selector
:@selector(_serverStopped
:) name
:TCServerStoppedNote object
:nil];
42 [nc addObserver
:self selector
:@selector(_childrenChanged
:) name
:TCServerConnectionsChangedNote object
:nil];
49 [[NSNotificationCenter defaultCenter
] removeObserver
:self];
56 [ibDefaultPortText setStringValue
:[NSString stringWithFormat
:@
"Default cheat port is %i.", TCDefaultListenPort
]];
59 [ibNameField setStringValue
:[[NSUserDefaults standardUserDefaults
] objectForKey
:TCBroadcastNamePref
]];
60 [ibPortField setIntValue
:[[NSUserDefaults standardUserDefaults
] integerForKey
:TCListenPortPref
]];
62 if ( [[NSApp cheatServer
] isListening
] ) {
63 [self _serverStarted
:nil];
66 [self _serverStopped
:nil];
72 - (IBAction
)ibSetListenPort
:(id)sender
74 short unsigned port
= [ibPortField intValue
];
77 port
= TCDefaultListenPort
;
78 [sender setIntValue
:port
];
81 [[NSUserDefaults standardUserDefaults
] setInteger
:[ibPortField intValue
] forKey
:TCListenPortPref
];
84 - (IBAction
)ibSetBroadcast
:(id)sender
86 [[NSUserDefaults standardUserDefaults
] setObject
:[ibNameField stringValue
] forKey
:TCBroadcastNamePref
];
89 - (IBAction
)ibStartServer
:(id)sender
91 CheatServer
*server
= [NSApp cheatServer
];
93 [self ibSetListenPort
:nil];
94 [self ibSetBroadcast
:nil];
96 if ( [server isListening
] ) {
98 [[NSUserDefaults standardUserDefaults
] setBool
:NO forKey
:TCRunServerPref
];
99 [NSApp stopCheatServer
];
102 [[NSUserDefaults standardUserDefaults
] setBool
:YES forKey
:TCRunServerPref
];
103 if ( ![NSApp startCheatServer
] ) {
104 // cheat server failed to start
105 NSBeginAlertSheet( @
"The Cheat could not start the server.", @
"OK", nil, nil, [sender window
], nil, NULL
, NULL
, NULL
,
106 @
"The cheat server failed to start. Make sure the port is not in use by another program and try again." );
112 - (int)numberOfRowsInTableView
:(NSTableView
*)table
114 CheatServer
*server
= [NSApp cheatServer
];
115 if ( [server isListening
] ) {
116 return [server childCount
];
121 - (id)tableView
:(NSTableView
*)table objectValueForTableColumn
:(NSTableColumn
*)column row
:(int)row
123 NSString
*identifier
= [column identifier
];
126 child
= [[[NSApp cheatServer
] children
] objectAtIndex
:row
];
128 return [child valueForKey
:identifier
];
131 - (void)tableView
:(NSTableView
*)aTableView setObjectValue
:(id)object forTableColumn
:(NSTableColumn
*)column row
:(int)row
136 - (BOOL)tableView
:(NSTableView
*)aTableView shouldSelectTableColumn
:(NSTableColumn
*)aTableColumn
141 - (void)tableView
:(NSTableView
*)aTableView deleteRows
:(NSArray
*)rows
146 for ( i
= len
-1; i
>= 0; i
-- ) {
147 [[NSApp cheatServer
] removeChildAtIndex
:[[rows objectAtIndex
:i
] unsignedIntValue
]];
149 // reselect the last item if the selection is now invalid
150 len
= [[NSApp cheatServer
] childCount
] - 1;
151 if ( [aTableView selectedRow
] > len
) {
152 [aTableView selectRowIndexes
:[NSIndexSet indexSetWithIndex
:len
] byExtendingSelection
:NO
];
154 [aTableView reloadData
];
158 - (void)_serverStarted
:(NSNotification
*)note
160 CheatServer
*server
= [NSApp cheatServer
];
163 port
= [server port
];
164 if ( port
!= TCDefaultListenPort
) {
165 [ibStatusField setDefaultStatus
:[NSString stringWithFormat
:@
"cheat://%@:%i", [server host
], port
]];
168 [ibStatusField setDefaultStatus
:[NSString stringWithFormat
:@
"cheat://%@", [server host
]]];
170 [ibNameField setEnabled
:NO
];
171 [ibPortField setEnabled
:NO
];
172 [ibStartButton setTitle
:@
"Stop Server"];
173 [ibSessionTable reloadData
];
175 _tableTimer
= [[NSTimer scheduledTimerWithTimeInterval
:1.0 target
:self selector
:@selector(_childrenChanged
:)
176 userInfo
:nil repeats
:YES
] retain
];
179 - (void)_serverStopped
:(NSNotification
*)note
181 // server is not running
182 [ibStatusField setDefaultStatus
:@
"Not Running"];
183 [ibNameField setEnabled
:YES
];
184 [ibPortField setEnabled
:YES
];
185 [ibStartButton setTitle
:@
"Start Server"];
186 [ibSessionTable reloadData
];
188 [_tableTimer invalidate
];
189 [_tableTimer release
];
193 - (void)_childrenChanged
:(NSNotification
*)note
195 [ibSessionTable reloadData
];