]> Dogcows Code - chaz/thecheat/blob - AppController.m
The Cheat 1.2.1
[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
29 @implementation AppController
30
31
32 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
33 #pragma mark Initialization
34 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
35
36
37 + (void)initialize
38 {
39 NSMutableDictionary *defaults = [NSMutableDictionary dictionary];
40
41 TCFirstLaunchPref = [[NSString stringWithFormat:@"TC%@%@Pref", ChazAppName(), ChazAppVersion()] retain];
42 NSString *broadcastName = [NSString stringWithFormat:@"%@'s Computer", NSFullUserName()];
43
44 // register user defaults
45 [defaults setObject:[NSNumber numberWithBool:NO] forKey:TCFirstLaunchPref];
46 [defaults setObject:[NSNumber numberWithBool:NO] forKey:TCWindowsOnTopPref];
47 [defaults setObject:[NSNumber numberWithBool:YES] forKey:TCUpdateCheckPref];
48 [defaults setObject:[NSNumber numberWithBool:YES] forKey:TCDisplayValuesPref];
49 [defaults setObject:[NSNumber numberWithFloat:1.0] forKey:TCValueUpdatePref];
50 [defaults setObject:[NSNumber numberWithInt:1000] forKey:TCHitsDisplayedPref];
51 [defaults setObject:[NSNumber numberWithBool:NO] forKey:TCRunServerPref];
52 [defaults setObject:broadcastName forKey:TCBroadcastNamePref];
53 [defaults setObject:[NSNumber numberWithInt:TCDefaultListenPort] forKey:TCListenPortPref];
54 [defaults setObject:[NSNumber numberWithFloat:gFadeAnimationDuration] forKey:TCFadeAnimationPref];
55 [defaults setObject:[NSNumber numberWithBool:YES] forKey:TCAskForSavePref];
56 [defaults setObject:[NSNumber numberWithBool:YES] forKey:TCSwitchVariablesPref];
57 [defaults setObject:[NSNumber numberWithBool:YES] forKey:TCAutoStartEditingVarsPref];
58
59 // register it
60 [[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
61
62 // set globals
63 gFadeAnimationDuration = [[NSUserDefaults standardUserDefaults] floatForKey:TCFadeAnimationPref];
64 }
65
66 - (id)init
67 {
68 if ( self = [super init] ) {
69 [self setDelegate:self];
70 }
71
72 return self;
73 }
74
75
76 - (void)dealloc
77 {
78 ChazLog( @"AppController deallocated!!" );
79 [self stopCheatServer];
80 [super dealloc];
81 }
82
83
84 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
85 #pragma mark NSApplication Delegate
86 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
87
88
89 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
90 {
91 // check if this is the first launch
92 if ( ![[NSUserDefaults standardUserDefaults] boolForKey:TCFirstLaunchPref] ) {
93 // FIRST LAUNCH
94 [self showAboutBoxWindow:self];
95 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:TCFirstLaunchPref];
96 }
97
98 // if should check for updates on launch
99 if ( [[NSUserDefaults standardUserDefaults] boolForKey:TCUpdateCheckPref] ) {
100 ChazCheckForUpdate( TCUpdateCheckURL, NO );
101 }
102
103 // automaticall start the cheat server if the pref is set
104 if ( [[NSUserDefaults standardUserDefaults] boolForKey:TCRunServerPref] ) {
105 if ( ![self startCheatServer] ) {
106 // inform the user that the server won't start
107 NSRunAlertPanel( @"The Cheat could not start the server.",
108 @"The cheat server failed to start. Check the server settings and start it manually.",
109 @"OK", nil, nil );
110 // open server prefs
111 [self showPreferenceWindow:self];
112 [_preferenceController chooseServer:self];
113 }
114 }
115 }
116
117
118 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
119 #pragma mark Interface Actions
120 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
121
122
123 - (IBAction)newSearchWindow:(id)sender
124 {
125 NSDocumentController *controller = [NSDocumentController sharedDocumentController];
126 CheatDocument *doc = [controller makeUntitledDocumentOfType:@"Cheat Document"];
127 if ( !doc ) {
128 ChazLog( @"nil document" );
129 }
130 [doc setMode:TCSearchMode];
131 [controller addDocument:doc];
132 [doc makeWindowControllers];
133 [doc showWindows];
134 }
135
136 - (IBAction)newBlankCheatWindow:(id)sender
137 {
138 NSDocumentController *controller = [NSDocumentController sharedDocumentController];
139 CheatDocument *doc = [controller makeUntitledDocumentOfType:@"Cheat Document"];
140 if ( !doc ) {
141 ChazLog( @"nil document" );
142 }
143 [doc setMode:TCCheatMode];
144 [controller addDocument:doc];
145 [doc makeWindowControllers];
146 [doc showWindows];
147 }
148
149 - (IBAction)showAboutBoxWindow:(id)sender
150 {
151 if ( !_aboutBoxController ) {
152 _aboutBoxController = [[AboutBoxController alloc] init];
153 }
154 [_aboutBoxController showWindow:self];
155 }
156
157 - (IBAction)showPreferenceWindow:(id)sender
158 {
159 if ( !_preferenceController ) {
160 _preferenceController = [[PreferenceController alloc] init];
161 }
162 [_preferenceController showWindow:self];
163 }
164
165
166 - (IBAction)launchHelpFile:(id)sender
167 {
168 if ( !_helpController ) {
169 _helpController = [[HelpController alloc] init];
170 }
171 [_helpController showWindow:self];
172 }
173
174 - (IBAction)launchEmailMenu:(id)sender
175 {
176 LaunchEmail();
177 }
178
179 - (IBAction)launchWebsiteMenu:(id)sender
180 {
181 LaunchWebsite();
182 }
183
184
185 - (IBAction)checkForUpdate:(id)sender
186 {
187 ChazCheckForUpdate( TCUpdateCheckURL, YES );
188 }
189
190
191 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
192 #pragma mark CheatServer Stuff
193 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
194
195 - (CheatServer *)cheatServer
196 {
197 if ( !_server ) {
198 _server = [[CheatServer alloc] initWithDelegate:self];
199 }
200 return _server;
201 }
202
203 - (BOOL)startCheatServer
204 {
205 ChazLog( @"cheat server starting..." );
206
207 // start the server with saved settings
208 int port = [[NSUserDefaults standardUserDefaults] integerForKey:TCListenPortPref];
209 NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:TCBroadcastNamePref];
210 if ( [name isEqualToString:@""] ) {
211 name = nil;
212 }
213
214 // stop the cheat server if it's running
215 [self stopCheatServer];
216
217 // start the server
218 if ( [[self cheatServer] listenOnPort:port broadcast:name] ) {
219 [[NSNotificationCenter defaultCenter] postNotificationName:TCServerStartedNote object:[self cheatServer]];
220 return YES;
221 }
222 return NO;
223 }
224
225 - (void)stopCheatServer
226 {
227 if ( _server ) {
228 [_server stop];
229 [[NSNotificationCenter defaultCenter] postNotificationName:TCServerStoppedNote object:[self cheatServer]];
230 }
231 }
232
233
234 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
235 #pragma mark CheatServerDelegate
236 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
237
238 - (void)serverDisconnectedUnexpectedly:(CheatServer *)theServer
239 {
240 ChazLog( @"server disconnected unexpectedly." );
241 [self stopCheatServer];
242 }
243
244 - (void)server:(CheatServer *)theServer failedToBroadcastName:(NSString *)theName
245 {
246 NSBeginInformationalAlertSheet( @"The cheat server can not broadcast.", @"OK", nil, nil, [_preferenceController window], nil, NULL, NULL, NULL,
247 @"The Cheat can't broadcast as \"%@\" because that name is in use by another server. The server will continue running with broadcasting disabled.", theName );
248 }
249
250 - (void)serverChildrenChanged:(CheatServer *)theServer
251 {
252 [[NSNotificationCenter defaultCenter] postNotificationName:TCServerConnectionsChangedNote object:theServer];
253 }
254
255
256 @end
This page took 0.043619 seconds and 4 git commands to generate.