]> Dogcows Code - chaz/thecheat/blob - AppController.m
The Cheat 1.2.3
[chaz/thecheat] / AppController.m
1
2 // **********************************************************************
3 // The Cheat - A universal game cheater for Mac OS X
4 // (C) 2003-2005 Chaz McGarvey (BrokenZipper)
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 1, or (at your option)
9 // any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 //
20
21 #import "AppController.h"
22
23 #import "CheatDocument.h"
24 #import "AboutBoxController.h"
25 #import "HelpController.h"
26 #import "PreferenceController.h"
27
28 @implementation AppController
29
30
31 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
32 #pragma mark Initialization
33 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
34
35
36 + (void)initialize
37 {
38 NSMutableDictionary *defaults = [NSMutableDictionary dictionary];
39
40 TCFirstLaunchPref = [[NSString stringWithFormat:@"TC%@%@Pref", ChazAppName(), ChazAppVersion()] retain];
41 NSString *broadcastName = [NSString stringWithFormat:@"%@'s Computer", NSFullUserName()];
42
43 // register user defaults
44 [defaults setObject:[NSNumber numberWithBool:NO] forKey:TCFirstLaunchPref];
45 [defaults setObject:[NSNumber numberWithBool:NO] forKey:TCWindowsOnTopPref];
46 [defaults setObject:[NSNumber numberWithBool:YES] forKey:TCUpdateCheckPref];
47 [defaults setObject:[NSNumber numberWithBool:YES] forKey:TCDisplayValuesPref];
48 [defaults setObject:[NSNumber numberWithFloat:1.0] forKey:TCValueUpdatePref];
49 [defaults setObject:[NSNumber numberWithInt:1000] forKey:TCHitsDisplayedPref];
50 [defaults setObject:[NSNumber numberWithBool:NO] forKey:TCRunServerPref];
51 [defaults setObject:broadcastName forKey:TCBroadcastNamePref];
52 [defaults setObject:[NSNumber numberWithInt:TCDefaultListenPort] forKey:TCListenPortPref];
53 [defaults setObject:[NSNumber numberWithFloat:gFadeAnimationDuration] forKey:TCFadeAnimationPref];
54 [defaults setObject:[NSNumber numberWithBool:YES] forKey:TCAskForSavePref];
55 [defaults setObject:[NSNumber numberWithBool:YES] forKey:TCSwitchVariablesPref];
56 [defaults setObject:[NSNumber numberWithBool:YES] forKey:TCAutoStartEditingVarsPref];
57
58 // register it
59 [[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
60
61 // set globals
62 gFadeAnimationDuration = [[NSUserDefaults standardUserDefaults] floatForKey:TCFadeAnimationPref];
63 }
64
65 - (id)init
66 {
67 if ( self = [super init] ) {
68 [self setDelegate:self];
69 }
70
71 return self;
72 }
73
74
75 - (void)dealloc
76 {
77 ChazLog( @"AppController deallocated!!" );
78 [self stopCheatServer];
79 [super dealloc];
80 }
81
82 // http://vgable.com/blog/2008/10/05/restarting-your-cocoa-application/
83 - (void)restartOurselves
84 {
85 NSString *killArg1AndOpenArg2Script = @"kill -9 $1 \n open \"$2\"";
86 NSString *ourPID = [NSString stringWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]];
87 NSString *pathToUs = [[NSBundle mainBundle] bundlePath];
88
89 NSArray *shArgs = [NSArray arrayWithObjects:@"-c", killArg1AndOpenArg2Script, @"", ourPID, pathToUs, nil];
90 NSTask *restartTask = [NSTask launchedTaskWithLaunchPath:@"/bin/sh" arguments:shArgs];
91 [restartTask waitUntilExit];
92 NSLog(@"*** ERROR: %@ should have been terminated, but we are still running", pathToUs);
93 assert(!"We should not be running!");
94 }
95
96 - (BOOL) checkExecutablePermissions {
97 NSDictionary *applicationAttributes = [[NSFileManager defaultManager] fileAttributesAtPath:[[NSBundle mainBundle] executablePath] traverseLink: YES];
98
99 // We expect 2755 as octal (1517 as decimal, -rwxr-sr-x as extended notation)
100 return ([applicationAttributes filePosixPermissions] == 1517 && [[applicationAttributes fileGroupOwnerAccountName] isEqualToString: @"procmod"]);
101 }
102
103 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
104 #pragma mark NSApplication Delegate
105 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
106
107 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
108 {
109 [NSApp activateIgnoringOtherApps:YES];
110
111 // check if this is the first launch
112 if ( ![[NSUserDefaults standardUserDefaults] boolForKey:TCFirstLaunchPref] ) {
113 // FIRST LAUNCH
114 [self showAboutBoxWindow:self];
115 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:TCFirstLaunchPref];
116 }
117
118 // if should check for updates on launch
119 if ( [[NSUserDefaults standardUserDefaults] boolForKey:TCUpdateCheckPref] ) {
120 ChazCheckForUpdate( TCUpdateCheckURL, NO );
121 }
122
123 // automatically start the cheat server if the pref is set
124 if ( [[NSUserDefaults standardUserDefaults] boolForKey:TCRunServerPref] ) {
125 if ( ![self startCheatServer] ) {
126 // inform the user that the server won't start
127 NSRunAlertPanel( @"The Cheat could not start the server.",
128 @"The cheat server failed to start. Check the server settings and start it manually.",
129 @"OK", nil, nil );
130 // open server prefs
131 [self showPreferenceWindow:self];
132 [_preferenceController chooseServer:self];
133 }
134 }
135 }
136
137
138 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
139 #pragma mark Interface Actions
140 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
141
142
143 - (IBAction)newSearchWindow:(id)sender
144 {
145 NSDocumentController *controller = [NSDocumentController sharedDocumentController];
146 CheatDocument *doc = [controller makeUntitledDocumentOfType:@"Cheat Document"];
147 if ( !doc ) {
148 ChazLog( @"nil document" );
149 }
150 [doc setMode:TCSearchMode];
151 [controller addDocument:doc];
152 [doc makeWindowControllers];
153 [doc showWindows];
154 }
155
156 - (IBAction)newBlankCheatWindow:(id)sender
157 {
158 NSDocumentController *controller = [NSDocumentController sharedDocumentController];
159 CheatDocument *doc = [controller makeUntitledDocumentOfType:@"Cheat Document"];
160 if ( !doc ) {
161 ChazLog( @"nil document" );
162 }
163 [doc setMode:TCCheatMode];
164 [controller addDocument:doc];
165 [doc makeWindowControllers];
166 [doc showWindows];
167 }
168
169 - (IBAction)showAboutBoxWindow:(id)sender
170 {
171 if ( !_aboutBoxController ) {
172 _aboutBoxController = [[AboutBoxController alloc] init];
173 }
174 [_aboutBoxController showWindow:self];
175 }
176
177 - (IBAction)showPreferenceWindow:(id)sender
178 {
179 if ( !_preferenceController ) {
180 _preferenceController = [[PreferenceController alloc] init];
181 }
182 [_preferenceController showWindow:self];
183 }
184
185
186 - (IBAction)launchHelpFile:(id)sender
187 {
188 if ( !_helpController ) {
189 _helpController = [[HelpController alloc] init];
190 }
191 [_helpController showWindow:self];
192 }
193
194 - (IBAction)launchEmailMenu:(id)sender
195 {
196 LaunchEmail();
197 }
198
199 - (IBAction)launchWebsiteMenu:(id)sender
200 {
201 LaunchWebsite();
202 }
203
204
205 - (IBAction)checkForUpdate:(id)sender
206 {
207 ChazCheckForUpdate( TCUpdateCheckURL, YES );
208 }
209
210
211 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
212 #pragma mark CheatServer Stuff
213 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
214
215 - (CheatServer *)cheatServer
216 {
217 if ( !_server ) {
218 _server = [[CheatServer alloc] initWithDelegate:self];
219 }
220 return _server;
221 }
222
223 - (BOOL)startCheatServer
224 {
225 ChazLog( @"cheat server starting..." );
226
227 // start the server with saved settings
228 int port = [[NSUserDefaults standardUserDefaults] integerForKey:TCListenPortPref];
229 NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:TCBroadcastNamePref];
230 if ( [name isEqualToString:@""] ) {
231 name = nil;
232 }
233
234 // stop the cheat server if it's running
235 [self stopCheatServer];
236
237 // start the server
238 if ( [[self cheatServer] listenOnPort:port broadcast:name] ) {
239 [[NSNotificationCenter defaultCenter] postNotificationName:TCServerStartedNote object:[self cheatServer]];
240 return YES;
241 }
242 return NO;
243 }
244
245 - (void)stopCheatServer
246 {
247 if ( _server ) {
248 [_server stop];
249 [[NSNotificationCenter defaultCenter] postNotificationName:TCServerStoppedNote object:[self cheatServer]];
250 }
251 }
252
253
254 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
255 #pragma mark CheatServerDelegate
256 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
257
258 - (void)serverDisconnectedUnexpectedly:(CheatServer *)theServer
259 {
260 ChazLog( @"server disconnected unexpectedly." );
261 [self stopCheatServer];
262 }
263
264 - (void)server:(CheatServer *)theServer failedToBroadcastName:(NSString *)theName
265 {
266 NSBeginInformationalAlertSheet( @"The cheat server can not broadcast.", @"OK", nil, nil, [_preferenceController window], nil, NULL, NULL, NULL,
267 @"The Cheat can't broadcast as \"%@\" because that name is in use by another server. The server will continue running with broadcasting disabled.", theName );
268 }
269
270 - (void)serverChildrenChanged:(CheatServer *)theServer
271 {
272 [[NSNotificationCenter defaultCenter] postNotificationName:TCServerConnectionsChangedNote object:theServer];
273 }
274
275
276 @end
This page took 0.045762 seconds and 4 git commands to generate.