2 // **********************************************************************
3 // The Cheat - A universal game cheater for Mac OS X
4 // (C) 2003-2005 Chaz McGarvey (BrokenZipper)
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)
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.
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.
21 #import "AppController.h"
23 #import "CheatDocument.h"
24 #import "AboutBoxController.h"
25 #import "HelpController.h"
26 #import "PreferenceController.h"
28 @implementation AppController
31 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
32 #pragma mark Initialization
33 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
38 NSMutableDictionary
*defaults
= [NSMutableDictionary dictionary
];
40 TCFirstLaunchPref
= [[NSString stringWithFormat
:@
"TC%@%@Pref", ChazAppName(), ChazAppVersion()] retain
];
41 NSString
*broadcastName
= [NSString stringWithFormat
:@
"%@'s Computer", NSFullUserName()];
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
];
59 [[NSUserDefaults standardUserDefaults
] registerDefaults
:defaults
];
62 gFadeAnimationDuration
= [[NSUserDefaults standardUserDefaults
] floatForKey
:TCFadeAnimationPref
];
67 if ( self = [super init
] ) {
68 [self setDelegate
:self];
77 ChazLog( @
"AppController deallocated!!" );
78 [self stopCheatServer
];
82 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
83 #pragma mark NSApplication Delegate
84 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
86 - (void)applicationDidFinishLaunching
:(NSNotification
*)aNotification
88 [NSApp activateIgnoringOtherApps
:YES
];
90 // check if this is the first launch
91 if ( ![[NSUserDefaults standardUserDefaults
] boolForKey
:TCFirstLaunchPref
] ) {
93 [self showAboutBoxWindow
:self];
94 [[NSUserDefaults standardUserDefaults
] setBool
:YES forKey
:TCFirstLaunchPref
];
97 // if should check for updates on launch
98 if ( [[NSUserDefaults standardUserDefaults
] boolForKey
:TCUpdateCheckPref
] ) {
99 ChazCheckForUpdate( TCUpdateCheckURL
, NO
);
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.",
110 [self showPreferenceWindow
:self];
111 [_preferenceController chooseServer
:self];
117 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
118 #pragma mark Interface Actions
119 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
122 - (IBAction
)newSearchWindow
:(id)sender
124 NSDocumentController
*controller
= [NSDocumentController sharedDocumentController
];
125 CheatDocument
*doc
= [controller makeUntitledDocumentOfType
:@
"Cheat Document"];
127 ChazLog( @
"nil document" );
129 [doc setMode
:TCSearchMode
];
130 [controller addDocument
:doc
];
131 [doc makeWindowControllers
];
135 - (IBAction
)newBlankCheatWindow
:(id)sender
137 NSDocumentController
*controller
= [NSDocumentController sharedDocumentController
];
138 CheatDocument
*doc
= [controller makeUntitledDocumentOfType
:@
"Cheat Document"];
140 ChazLog( @
"nil document" );
142 [doc setMode
:TCCheatMode
];
143 [controller addDocument
:doc
];
144 [doc makeWindowControllers
];
148 - (IBAction
)showAboutBoxWindow
:(id)sender
150 if ( !_aboutBoxController
) {
151 _aboutBoxController
= [[AboutBoxController alloc
] init
];
153 [_aboutBoxController showWindow
:self];
156 - (IBAction
)showPreferenceWindow
:(id)sender
158 if ( !_preferenceController
) {
159 _preferenceController
= [[PreferenceController alloc
] init
];
161 [_preferenceController showWindow
:self];
165 - (IBAction
)launchHelpFile
:(id)sender
167 if ( !_helpController
) {
168 _helpController
= [[HelpController alloc
] init
];
170 [_helpController showWindow
:self];
173 - (IBAction
)launchEmailMenu
:(id)sender
178 - (IBAction
)launchWebsiteMenu
:(id)sender
184 - (IBAction
)checkForUpdate
:(id)sender
186 ChazCheckForUpdate( TCUpdateCheckURL
, YES
);
190 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
191 #pragma mark CheatServer Stuff
192 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
194 - (CheatServer
*)cheatServer
197 _server
= [[CheatServer alloc
] initWithDelegate
:self];
202 - (BOOL)startCheatServer
204 ChazLog( @
"cheat server starting..." );
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
:@
""] ) {
213 // stop the cheat server if it's running
214 [self stopCheatServer
];
217 if ( [[self cheatServer
] listenOnPort
:port broadcast
:name
] ) {
218 [[NSNotificationCenter defaultCenter
] postNotificationName
:TCServerStartedNote object
:[self cheatServer
]];
224 - (void)stopCheatServer
228 [[NSNotificationCenter defaultCenter
] postNotificationName
:TCServerStoppedNote object
:[self cheatServer
]];
233 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
234 #pragma mark CheatServerDelegate
235 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
237 - (void)serverDisconnectedUnexpectedly
:(CheatServer
*)theServer
239 ChazLog( @
"server disconnected unexpectedly." );
240 [self stopCheatServer
];
243 - (void)server
:(CheatServer
*)theServer failedToBroadcastName
:(NSString
*)theName
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
);
249 - (void)serverChildrenChanged
:(CheatServer
*)theServer
251 [[NSNotificationCenter defaultCenter
] postNotificationName
:TCServerConnectionsChangedNote object
:theServer
];