3 * The Cheat - The legendary universal game trainer for Mac OS X.
4 * http://www.brokenzipper.com/trac/wiki/TheCheat
6 * Copyright (c) 2003-2011, 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 NSError
*error
= nil;
116 NSDocumentController
*controller
= [NSDocumentController sharedDocumentController
];
117 CheatDocument
*doc
= [controller makeUntitledDocumentOfType
:@
"Cheat Document" error
:&error
];
119 ChazLog( @
"nil document, error=%@", error
);
121 [doc setMode
:TCSearchMode
];
122 [controller addDocument
:doc
];
123 [doc makeWindowControllers
];
127 - (IBAction
)newBlankCheatWindow
:(id)sender
129 NSError
*error
= nil;
130 NSDocumentController
*controller
= [NSDocumentController sharedDocumentController
];
131 CheatDocument
*doc
= [controller makeUntitledDocumentOfType
:@
"Cheat Document" error
:&error
];
133 ChazLog( @
"nil document, error=%@", error
);
135 [doc setMode
:TCCheatMode
];
136 [controller addDocument
:doc
];
137 [doc makeWindowControllers
];
141 - (IBAction
)showAboutBoxWindow
:(id)sender
143 if ( !_aboutBoxController
) {
144 _aboutBoxController
= [[AboutBoxController alloc
] init
];
146 [_aboutBoxController showWindow
:self];
149 - (IBAction
)showPreferenceWindow
:(id)sender
151 if ( !_preferenceController
) {
152 _preferenceController
= [[PreferenceController alloc
] init
];
154 [_preferenceController showWindow
:self];
158 - (IBAction
)launchHelpFile
:(id)sender
160 if ( !_helpController
) {
161 _helpController
= [[HelpController alloc
] init
];
163 [_helpController showWindow
:self];
166 - (IBAction
)launchEmailMenu
:(id)sender
171 - (IBAction
)launchWebsiteMenu
:(id)sender
177 - (IBAction
)checkForUpdate
:(id)sender
179 ChazCheckForUpdate( TCUpdateCheckURL
, YES
);
183 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
184 #pragma mark CheatServer Stuff
185 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
187 - (CheatServer
*)cheatServer
190 _server
= [[CheatServer alloc
] initWithDelegate
:self];
195 - (BOOL)startCheatServer
197 ChazLog( @
"cheat server starting..." );
199 // start the server with saved settings
200 int port
= [[NSUserDefaults standardUserDefaults
] integerForKey
:TCListenPortPref
];
201 NSString
*name
= [[NSUserDefaults standardUserDefaults
] objectForKey
:TCBroadcastNamePref
];
202 if ( [name isEqualToString
:@
""] ) {
206 // stop the cheat server if it's running
207 [self stopCheatServer
];
210 if ( [[self cheatServer
] listenOnPort
:port broadcast
:name
] ) {
211 [[NSNotificationCenter defaultCenter
] postNotificationName
:TCServerStartedNote object
:[self cheatServer
]];
217 - (void)stopCheatServer
221 [[NSNotificationCenter defaultCenter
] postNotificationName
:TCServerStoppedNote object
:[self cheatServer
]];
226 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
227 #pragma mark CheatServerDelegate
228 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
230 - (void)serverDisconnectedUnexpectedly
:(CheatServer
*)theServer
232 ChazLog( @
"server disconnected unexpectedly." );
233 [self stopCheatServer
];
236 - (void)server
:(CheatServer
*)theServer failedToBroadcastName
:(NSString
*)theName
238 NSBeginInformationalAlertSheet( @
"The cheat server can not broadcast.", @
"OK", nil, nil, [_preferenceController window
], nil, NULL
, NULL
, NULL
,
239 @
"The Cheat can't broadcast as \"%@\" because that name is in use by another server. The server will continue running with broadcasting disabled.", theName
);
242 - (void)serverChildrenChanged
:(CheatServer
*)theServer
244 [[NSNotificationCenter defaultCenter
] postNotificationName
:TCServerConnectionsChangedNote object
:theServer
];