]> Dogcows Code - chaz/thecheat/blob - AppController.m
The Cheat 1.2.4
[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 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
83 #pragma mark NSApplication Delegate
84 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
85
86 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
87 {
88 [NSApp activateIgnoringOtherApps:YES];
89
90 // check if this is the first launch
91 if ( ![[NSUserDefaults standardUserDefaults] boolForKey:TCFirstLaunchPref] ) {
92 // FIRST LAUNCH
93 [self showAboutBoxWindow:self];
94 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:TCFirstLaunchPref];
95 }
96
97 // if should check for updates on launch
98 if ( [[NSUserDefaults standardUserDefaults] boolForKey:TCUpdateCheckPref] ) {
99 ChazCheckForUpdate( TCUpdateCheckURL, NO );
100 }
101
102 // automatically start the cheat server if the pref is set
103 if ( [[NSUserDefaults standardUserDefaults] boolForKey:TCRunServerPref] ) {
104 if ( ![self startCheatServer] ) {
105 // inform the user that the server won't start
106 NSRunAlertPanel( @"The Cheat could not start the server.",
107 @"The cheat server failed to start. Check the server settings and start it manually.",
108 @"OK", nil, nil );
109 // open server prefs
110 [self showPreferenceWindow:self];
111 [_preferenceController chooseServer:self];
112 }
113 }
114 }
115
116
117 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
118 #pragma mark Interface Actions
119 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
120
121
122 - (IBAction)newSearchWindow:(id)sender
123 {
124 NSDocumentController *controller = [NSDocumentController sharedDocumentController];
125 CheatDocument *doc = [controller makeUntitledDocumentOfType:@"Cheat Document"];
126 if ( !doc ) {
127 ChazLog( @"nil document" );
128 }
129 [doc setMode:TCSearchMode];
130 [controller addDocument:doc];
131 [doc makeWindowControllers];
132 [doc showWindows];
133 }
134
135 - (IBAction)newBlankCheatWindow:(id)sender
136 {
137 NSDocumentController *controller = [NSDocumentController sharedDocumentController];
138 CheatDocument *doc = [controller makeUntitledDocumentOfType:@"Cheat Document"];
139 if ( !doc ) {
140 ChazLog( @"nil document" );
141 }
142 [doc setMode:TCCheatMode];
143 [controller addDocument:doc];
144 [doc makeWindowControllers];
145 [doc showWindows];
146 }
147
148 - (IBAction)showAboutBoxWindow:(id)sender
149 {
150 if ( !_aboutBoxController ) {
151 _aboutBoxController = [[AboutBoxController alloc] init];
152 }
153 [_aboutBoxController showWindow:self];
154 }
155
156 - (IBAction)showPreferenceWindow:(id)sender
157 {
158 if ( !_preferenceController ) {
159 _preferenceController = [[PreferenceController alloc] init];
160 }
161 [_preferenceController showWindow:self];
162 }
163
164
165 - (IBAction)launchHelpFile:(id)sender
166 {
167 if ( !_helpController ) {
168 _helpController = [[HelpController alloc] init];
169 }
170 [_helpController showWindow:self];
171 }
172
173 - (IBAction)launchEmailMenu:(id)sender
174 {
175 LaunchEmail();
176 }
177
178 - (IBAction)launchWebsiteMenu:(id)sender
179 {
180 LaunchWebsite();
181 }
182
183
184 - (IBAction)checkForUpdate:(id)sender
185 {
186 ChazCheckForUpdate( TCUpdateCheckURL, YES );
187 }
188
189
190 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
191 #pragma mark CheatServer Stuff
192 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
193
194 - (CheatServer *)cheatServer
195 {
196 if ( !_server ) {
197 _server = [[CheatServer alloc] initWithDelegate:self];
198 }
199 return _server;
200 }
201
202 - (BOOL)startCheatServer
203 {
204 ChazLog( @"cheat server starting..." );
205
206 // start the server with saved settings
207 int port = [[NSUserDefaults standardUserDefaults] integerForKey:TCListenPortPref];
208 NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:TCBroadcastNamePref];
209 if ( [name isEqualToString:@""] ) {
210 name = nil;
211 }
212
213 // stop the cheat server if it's running
214 [self stopCheatServer];
215
216 // start the server
217 if ( [[self cheatServer] listenOnPort:port broadcast:name] ) {
218 [[NSNotificationCenter defaultCenter] postNotificationName:TCServerStartedNote object:[self cheatServer]];
219 return YES;
220 }
221 return NO;
222 }
223
224 - (void)stopCheatServer
225 {
226 if ( _server ) {
227 [_server stop];
228 [[NSNotificationCenter defaultCenter] postNotificationName:TCServerStoppedNote object:[self cheatServer]];
229 }
230 }
231
232
233 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
234 #pragma mark CheatServerDelegate
235 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
236
237 - (void)serverDisconnectedUnexpectedly:(CheatServer *)theServer
238 {
239 ChazLog( @"server disconnected unexpectedly." );
240 [self stopCheatServer];
241 }
242
243 - (void)server:(CheatServer *)theServer failedToBroadcastName:(NSString *)theName
244 {
245 NSBeginInformationalAlertSheet( @"The cheat server can not broadcast.", @"OK", nil, nil, [_preferenceController window], nil, NULL, NULL, NULL,
246 @"The Cheat can't broadcast as \"%@\" because that name is in use by another server. The server will continue running with broadcasting disabled.", theName );
247 }
248
249 - (void)serverChildrenChanged:(CheatServer *)theServer
250 {
251 [[NSNotificationCenter defaultCenter] postNotificationName:TCServerConnectionsChangedNote object:theServer];
252 }
253
254
255 @end
This page took 0.041792 seconds and 4 git commands to generate.