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"
29 @implementation AppController
32 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
33 #pragma mark Initialization
34 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39 NSMutableDictionary
*defaults
= [NSMutableDictionary dictionary
];
41 TCFirstLaunchPref
= [[NSString stringWithFormat
:@
"TC%@%@Pref", ChazAppName(), ChazAppVersion()] retain
];
42 NSString
*broadcastName
= [NSString stringWithFormat
:@
"%@'s Computer", NSFullUserName()];
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
];
60 [[NSUserDefaults standardUserDefaults
] registerDefaults
:defaults
];
63 gFadeAnimationDuration
= [[NSUserDefaults standardUserDefaults
] floatForKey
:TCFadeAnimationPref
];
68 if ( self = [super init
] ) {
69 [self setDelegate
:self];
78 ChazLog( @
"AppController deallocated!!" );
79 [self stopCheatServer
];
84 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
85 #pragma mark NSApplication Delegate
86 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
89 - (void)applicationDidFinishLaunching
:(NSNotification
*)aNotification
91 // check if this is the first launch
92 if ( ![[NSUserDefaults standardUserDefaults
] boolForKey
:TCFirstLaunchPref
] ) {
94 [self showAboutBoxWindow
:self];
95 [[NSUserDefaults standardUserDefaults
] setBool
:YES forKey
:TCFirstLaunchPref
];
98 // if should check for updates on launch
99 if ( [[NSUserDefaults standardUserDefaults
] boolForKey
:TCUpdateCheckPref
] ) {
100 ChazCheckForUpdate( TCUpdateCheckURL
, NO
);
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.",
111 [self showPreferenceWindow
:self];
112 [_preferenceController chooseServer
:self];
118 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
119 #pragma mark Interface Actions
120 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
123 - (IBAction
)newSearchWindow
:(id)sender
125 NSDocumentController
*controller
= [NSDocumentController sharedDocumentController
];
126 CheatDocument
*doc
= [controller makeUntitledDocumentOfType
:@
"Cheat Document"];
128 ChazLog( @
"nil document" );
130 [doc setMode
:TCSearchMode
];
131 [controller addDocument
:doc
];
132 [doc makeWindowControllers
];
136 - (IBAction
)newBlankCheatWindow
:(id)sender
138 NSDocumentController
*controller
= [NSDocumentController sharedDocumentController
];
139 CheatDocument
*doc
= [controller makeUntitledDocumentOfType
:@
"Cheat Document"];
141 ChazLog( @
"nil document" );
143 [doc setMode
:TCCheatMode
];
144 [controller addDocument
:doc
];
145 [doc makeWindowControllers
];
149 - (IBAction
)showAboutBoxWindow
:(id)sender
151 if ( !_aboutBoxController
) {
152 _aboutBoxController
= [[AboutBoxController alloc
] init
];
154 [_aboutBoxController showWindow
:self];
157 - (IBAction
)showPreferenceWindow
:(id)sender
159 if ( !_preferenceController
) {
160 _preferenceController
= [[PreferenceController alloc
] init
];
162 [_preferenceController showWindow
:self];
166 - (IBAction
)launchHelpFile
:(id)sender
168 if ( !_helpController
) {
169 _helpController
= [[HelpController alloc
] init
];
171 [_helpController showWindow
:self];
174 - (IBAction
)launchEmailMenu
:(id)sender
179 - (IBAction
)launchWebsiteMenu
:(id)sender
185 - (IBAction
)checkForUpdate
:(id)sender
187 ChazCheckForUpdate( TCUpdateCheckURL
, YES
);
191 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
192 #pragma mark CheatServer Stuff
193 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
195 - (CheatServer
*)cheatServer
198 _server
= [[CheatServer alloc
] initWithDelegate
:self];
203 - (BOOL)startCheatServer
205 ChazLog( @
"cheat server starting..." );
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
:@
""] ) {
214 // stop the cheat server if it's running
215 [self stopCheatServer
];
218 if ( [[self cheatServer
] listenOnPort
:port broadcast
:name
] ) {
219 [[NSNotificationCenter defaultCenter
] postNotificationName
:TCServerStartedNote object
:[self cheatServer
]];
225 - (void)stopCheatServer
229 [[NSNotificationCenter defaultCenter
] postNotificationName
:TCServerStoppedNote object
:[self cheatServer
]];
234 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
235 #pragma mark CheatServerDelegate
236 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
238 - (void)serverDisconnectedUnexpectedly
:(CheatServer
*)theServer
240 ChazLog( @
"server disconnected unexpectedly." );
241 [self stopCheatServer
];
244 - (void)server
:(CheatServer
*)theServer failedToBroadcastName
:(NSString
*)theName
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
);
250 - (void)serverChildrenChanged
:(CheatServer
*)theServer
252 [[NSNotificationCenter defaultCenter
] postNotificationName
:TCServerConnectionsChangedNote object
:theServer
];