3 * The Cheat - The legendary universal game trainer for Mac OS X.
4 * http://www.dogcows.com/chaz/wiki/TheCheat
6 * Copyright (c) 2003-2010, Charles McGarvey et al.
8 * Distributable under the terms and conditions of the 2-clause BSD
9 * license; see the file COPYING for the legal text of the license.
12 #import "AppController.h"
14 #import "CheatDocument.h"
15 #import "AboutBoxController.h"
16 #import "HelpController.h"
17 #import "PreferenceController.h"
19 @implementation AppController
22 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
23 #pragma mark Initialization
24 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
29 NSMutableDictionary
*defaults
= [NSMutableDictionary dictionary
];
31 TCFirstLaunchPref
= [[NSString stringWithFormat
:@
"TC%@%@Pref", ChazAppName(), ChazAppVersion()] retain
];
32 NSString
*broadcastName
= [NSString stringWithFormat
:@
"%@'s Computer", NSFullUserName()];
34 // register user defaults
35 [defaults setObject
:[NSNumber numberWithBool
:NO
] forKey
:TCFirstLaunchPref
];
36 [defaults setObject
:[NSNumber numberWithBool
:NO
] forKey
:TCWindowsOnTopPref
];
37 [defaults setObject
:[NSNumber numberWithBool
:YES
] forKey
:TCUpdateCheckPref
];
38 [defaults setObject
:[NSNumber numberWithBool
:YES
] forKey
:TCDisplayValuesPref
];
39 [defaults setObject
:[NSNumber numberWithFloat
:1.0] forKey
:TCValueUpdatePref
];
40 [defaults setObject
:[NSNumber numberWithInt
:1000] forKey
:TCHitsDisplayedPref
];
41 [defaults setObject
:[NSNumber numberWithBool
:NO
] forKey
:TCRunServerPref
];
42 [defaults setObject
:broadcastName forKey
:TCBroadcastNamePref
];
43 [defaults setObject
:[NSNumber numberWithInt
:TCDefaultListenPort
] forKey
:TCListenPortPref
];
44 [defaults setObject
:[NSNumber numberWithFloat
:gFadeAnimationDuration
] forKey
:TCFadeAnimationPref
];
45 [defaults setObject
:[NSNumber numberWithBool
:YES
] forKey
:TCAskForSavePref
];
46 [defaults setObject
:[NSNumber numberWithBool
:YES
] forKey
:TCSwitchVariablesPref
];
47 [defaults setObject
:[NSNumber numberWithBool
:YES
] forKey
:TCAutoStartEditingVarsPref
];
50 [[NSUserDefaults standardUserDefaults
] registerDefaults
:defaults
];
53 gFadeAnimationDuration
= [[NSUserDefaults standardUserDefaults
] floatForKey
:TCFadeAnimationPref
];
58 if ( self = [super init
] ) {
59 [self setDelegate
:self];
68 ChazLog( @
"AppController deallocated!!" );
69 [self stopCheatServer
];
73 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
74 #pragma mark NSApplication Delegate
75 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
77 - (void)applicationDidFinishLaunching
:(NSNotification
*)aNotification
79 [NSApp activateIgnoringOtherApps
:YES
];
81 // check if this is the first launch
82 if ( ![[NSUserDefaults standardUserDefaults
] boolForKey
:TCFirstLaunchPref
] ) {
84 [self showAboutBoxWindow
:self];
85 [[NSUserDefaults standardUserDefaults
] setBool
:YES forKey
:TCFirstLaunchPref
];
88 // if should check for updates on launch
89 if ( [[NSUserDefaults standardUserDefaults
] boolForKey
:TCUpdateCheckPref
] ) {
90 ChazCheckForUpdate( TCUpdateCheckURL
, NO
);
93 // automatically start the cheat server if the pref is set
94 if ( [[NSUserDefaults standardUserDefaults
] boolForKey
:TCRunServerPref
] ) {
95 if ( ![self startCheatServer
] ) {
96 // inform the user that the server won't start
97 NSRunAlertPanel( @
"The Cheat could not start the server.",
98 @
"The cheat server failed to start. Check the server settings and start it manually.",
101 [self showPreferenceWindow
:self];
102 [_preferenceController chooseServer
:self];
108 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
109 #pragma mark Interface Actions
110 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
113 - (IBAction
)newSearchWindow
:(id)sender
115 NSDocumentController
*controller
= [NSDocumentController sharedDocumentController
];
116 CheatDocument
*doc
= [controller makeUntitledDocumentOfType
:@
"Cheat Document"];
118 ChazLog( @
"nil document" );
120 [doc setMode
:TCSearchMode
];
121 [controller addDocument
:doc
];
122 [doc makeWindowControllers
];
126 - (IBAction
)newBlankCheatWindow
:(id)sender
128 NSDocumentController
*controller
= [NSDocumentController sharedDocumentController
];
129 CheatDocument
*doc
= [controller makeUntitledDocumentOfType
:@
"Cheat Document"];
131 ChazLog( @
"nil document" );
133 [doc setMode
:TCCheatMode
];
134 [controller addDocument
:doc
];
135 [doc makeWindowControllers
];
139 - (IBAction
)showAboutBoxWindow
:(id)sender
141 if ( !_aboutBoxController
) {
142 _aboutBoxController
= [[AboutBoxController alloc
] init
];
144 [_aboutBoxController showWindow
:self];
147 - (IBAction
)showPreferenceWindow
:(id)sender
149 if ( !_preferenceController
) {
150 _preferenceController
= [[PreferenceController alloc
] init
];
152 [_preferenceController showWindow
:self];
156 - (IBAction
)launchHelpFile
:(id)sender
158 if ( !_helpController
) {
159 _helpController
= [[HelpController alloc
] init
];
161 [_helpController showWindow
:self];
164 - (IBAction
)launchEmailMenu
:(id)sender
169 - (IBAction
)launchWebsiteMenu
:(id)sender
175 - (IBAction
)checkForUpdate
:(id)sender
177 ChazCheckForUpdate( TCUpdateCheckURL
, YES
);
181 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
182 #pragma mark CheatServer Stuff
183 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
185 - (CheatServer
*)cheatServer
188 _server
= [[CheatServer alloc
] initWithDelegate
:self];
193 - (BOOL)startCheatServer
195 ChazLog( @
"cheat server starting..." );
197 // start the server with saved settings
198 int port
= [[NSUserDefaults standardUserDefaults
] integerForKey
:TCListenPortPref
];
199 NSString
*name
= [[NSUserDefaults standardUserDefaults
] objectForKey
:TCBroadcastNamePref
];
200 if ( [name isEqualToString
:@
""] ) {
204 // stop the cheat server if it's running
205 [self stopCheatServer
];
208 if ( [[self cheatServer
] listenOnPort
:port broadcast
:name
] ) {
209 [[NSNotificationCenter defaultCenter
] postNotificationName
:TCServerStartedNote object
:[self cheatServer
]];
215 - (void)stopCheatServer
219 [[NSNotificationCenter defaultCenter
] postNotificationName
:TCServerStoppedNote object
:[self cheatServer
]];
224 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
225 #pragma mark CheatServerDelegate
226 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
228 - (void)serverDisconnectedUnexpectedly
:(CheatServer
*)theServer
230 ChazLog( @
"server disconnected unexpectedly." );
231 [self stopCheatServer
];
234 - (void)server
:(CheatServer
*)theServer failedToBroadcastName
:(NSString
*)theName
236 NSBeginInformationalAlertSheet( @
"The cheat server can not broadcast.", @
"OK", nil, nil, [_preferenceController window
], nil, NULL
, NULL
, NULL
,
237 @
"The Cheat can't broadcast as \"%@\" because that name is in use by another server. The server will continue running with broadcasting disabled.", theName
);
240 - (void)serverChildrenChanged
:(CheatServer
*)theServer
242 [[NSNotificationCenter defaultCenter
] postNotificationName
:TCServerConnectionsChangedNote object
:theServer
];