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 // Privilage elevation libs
29 #include <security/authorization.h>
30 #include <security/authorizationdb.h>
31 #include <security/authorizationtags.h>
32 #include <sys/types.h>
35 @implementation AppController
38 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39 #pragma mark Initialization
40 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
45 NSMutableDictionary
*defaults
= [NSMutableDictionary dictionary
];
47 TCFirstLaunchPref
= [[NSString stringWithFormat
:@
"TC%@%@Pref", ChazAppName(), ChazAppVersion()] retain
];
48 NSString
*broadcastName
= [NSString stringWithFormat
:@
"%@'s Computer", NSFullUserName()];
50 // register user defaults
51 [defaults setObject
:[NSNumber numberWithBool
:NO
] forKey
:TCFirstLaunchPref
];
52 [defaults setObject
:[NSNumber numberWithBool
:NO
] forKey
:TCWindowsOnTopPref
];
53 [defaults setObject
:[NSNumber numberWithBool
:YES
] forKey
:TCUpdateCheckPref
];
54 [defaults setObject
:[NSNumber numberWithBool
:YES
] forKey
:TCDisplayValuesPref
];
55 [defaults setObject
:[NSNumber numberWithFloat
:1.0] forKey
:TCValueUpdatePref
];
56 [defaults setObject
:[NSNumber numberWithInt
:1000] forKey
:TCHitsDisplayedPref
];
57 [defaults setObject
:[NSNumber numberWithBool
:NO
] forKey
:TCRunServerPref
];
58 [defaults setObject
:broadcastName forKey
:TCBroadcastNamePref
];
59 [defaults setObject
:[NSNumber numberWithInt
:TCDefaultListenPort
] forKey
:TCListenPortPref
];
60 [defaults setObject
:[NSNumber numberWithFloat
:gFadeAnimationDuration
] forKey
:TCFadeAnimationPref
];
61 [defaults setObject
:[NSNumber numberWithBool
:YES
] forKey
:TCAskForSavePref
];
62 [defaults setObject
:[NSNumber numberWithBool
:YES
] forKey
:TCSwitchVariablesPref
];
63 [defaults setObject
:[NSNumber numberWithBool
:YES
] forKey
:TCAutoStartEditingVarsPref
];
66 [[NSUserDefaults standardUserDefaults
] registerDefaults
:defaults
];
69 gFadeAnimationDuration
= [[NSUserDefaults standardUserDefaults
] floatForKey
:TCFadeAnimationPref
];
74 if ( self = [super init
] )
78 [self launchAuthPrgm
];
79 [self setDelegate
:self];
85 NSRunAlertPanel(@
"The Cheat must be run as root,",
86 @
"Due to a limitation of Leopard, the application needs elevated privileges to run.",
97 AuthorizationFlags authFlags
;
100 NSLog (@
"MyWindowController: preAuthorize");
103 return errAuthorizationSuccess
;
105 NSLog (@
"MyWindowController: preAuthorize: ** calling AuthorizationCreate...**\n");
107 authFlags
= kAuthorizationFlagDefaults
;
108 err
= AuthorizationCreate (NULL
, kAuthorizationEmptyEnvironment
, authFlags
, &_authRef
);
109 if (err
!= errAuthorizationSuccess
)
112 NSLog (@
"MyWindowController: preAuthorize: ** calling AuthorizationCopyRights...**\n");
114 _authItem.name
= kAuthorizationRightExecute
;
115 _authItem.valueLength
= 0;
116 _authItem.value
= NULL
;
118 _authRights.count
= 1;
119 _authRights.items
= (AuthorizationItem
*) malloc (sizeof (_authItem
));
120 memcpy (&_authRights.items
[0], &_authItem
, sizeof (_authItem
));
121 authFlags
= kAuthorizationFlagDefaults
122 | kAuthorizationFlagExtendRights
123 | kAuthorizationFlagInteractionAllowed
124 | kAuthorizationFlagPreAuthorize
;
125 err
= AuthorizationCopyRights (_authRef
, &_authRights
, kAuthorizationEmptyEnvironment
, authFlags
, NULL
);
130 - (int) launchAuthPrgm
132 AuthorizationFlags authFlags
;
136 NSString
* path
= [[NSBundle mainBundle
] executablePath
];
137 if (![[NSFileManager defaultManager
] isExecutableFileAtPath
: path
])
144 err
= [self preAuthorize
];
145 if (err
!= errAuthorizationSuccess
)
151 NSLog (@
"MyWindowController: launchWithPath: ** calling AuthorizationExecuteWithPrivileges...**\n");
152 authFlags
= kAuthorizationFlagDefaults
;
153 err
= AuthorizationExecuteWithPrivileges (_authRef
, [path cString
], authFlags
, NULL
, NULL
);
154 if(err
==0) [NSApp terminate
:self];
161 ChazLog( @
"AppController deallocated!!" );
162 [self stopCheatServer
];
167 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
168 #pragma mark NSApplication Delegate
169 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
172 - (void)applicationDidFinishLaunching
:(NSNotification
*)aNotification
174 // check if this is the first launch
175 if ( ![[NSUserDefaults standardUserDefaults
] boolForKey
:TCFirstLaunchPref
] ) {
177 [self showAboutBoxWindow
:self];
178 [[NSUserDefaults standardUserDefaults
] setBool
:YES forKey
:TCFirstLaunchPref
];
181 // if should check for updates on launch
182 if ( [[NSUserDefaults standardUserDefaults
] boolForKey
:TCUpdateCheckPref
] ) {
183 ChazCheckForUpdate( TCUpdateCheckURL
, NO
);
186 // automaticall start the cheat server if the pref is set
187 if ( [[NSUserDefaults standardUserDefaults
] boolForKey
:TCRunServerPref
] ) {
188 if ( ![self startCheatServer
] ) {
189 // inform the user that the server won't start
190 NSRunAlertPanel( @
"The Cheat could not start the server.",
191 @
"The cheat server failed to start. Check the server settings and start it manually.",
194 [self showPreferenceWindow
:self];
195 [_preferenceController chooseServer
:self];
201 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
202 #pragma mark Interface Actions
203 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
206 - (IBAction
)newSearchWindow
:(id)sender
208 NSDocumentController
*controller
= [NSDocumentController sharedDocumentController
];
209 CheatDocument
*doc
= [controller makeUntitledDocumentOfType
:@
"Cheat Document"];
211 ChazLog( @
"nil document" );
213 [doc setMode
:TCSearchMode
];
214 [controller addDocument
:doc
];
215 [doc makeWindowControllers
];
219 - (IBAction
)newBlankCheatWindow
:(id)sender
221 NSDocumentController
*controller
= [NSDocumentController sharedDocumentController
];
222 CheatDocument
*doc
= [controller makeUntitledDocumentOfType
:@
"Cheat Document"];
224 ChazLog( @
"nil document" );
226 [doc setMode
:TCCheatMode
];
227 [controller addDocument
:doc
];
228 [doc makeWindowControllers
];
232 - (IBAction
)showAboutBoxWindow
:(id)sender
234 if ( !_aboutBoxController
) {
235 _aboutBoxController
= [[AboutBoxController alloc
] init
];
237 [_aboutBoxController showWindow
:self];
240 - (IBAction
)showPreferenceWindow
:(id)sender
242 if ( !_preferenceController
) {
243 _preferenceController
= [[PreferenceController alloc
] init
];
245 [_preferenceController showWindow
:self];
249 - (IBAction
)launchHelpFile
:(id)sender
251 if ( !_helpController
) {
252 _helpController
= [[HelpController alloc
] init
];
254 [_helpController showWindow
:self];
257 - (IBAction
)launchEmailMenu
:(id)sender
262 - (IBAction
)launchWebsiteMenu
:(id)sender
268 - (IBAction
)checkForUpdate
:(id)sender
270 ChazCheckForUpdate( TCUpdateCheckURL
, YES
);
274 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
275 #pragma mark CheatServer Stuff
276 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
278 - (CheatServer
*)cheatServer
281 _server
= [[CheatServer alloc
] initWithDelegate
:self];
286 - (BOOL)startCheatServer
288 ChazLog( @
"cheat server starting..." );
290 // start the server with saved settings
291 int port
= [[NSUserDefaults standardUserDefaults
] integerForKey
:TCListenPortPref
];
292 NSString
*name
= [[NSUserDefaults standardUserDefaults
] objectForKey
:TCBroadcastNamePref
];
293 if ( [name isEqualToString
:@
""] ) {
297 // stop the cheat server if it's running
298 [self stopCheatServer
];
301 if ( [[self cheatServer
] listenOnPort
:port broadcast
:name
] ) {
302 [[NSNotificationCenter defaultCenter
] postNotificationName
:TCServerStartedNote object
:[self cheatServer
]];
308 - (void)stopCheatServer
312 [[NSNotificationCenter defaultCenter
] postNotificationName
:TCServerStoppedNote object
:[self cheatServer
]];
317 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
318 #pragma mark CheatServerDelegate
319 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
321 - (void)serverDisconnectedUnexpectedly
:(CheatServer
*)theServer
323 ChazLog( @
"server disconnected unexpectedly." );
324 [self stopCheatServer
];
327 - (void)server
:(CheatServer
*)theServer failedToBroadcastName
:(NSString
*)theName
329 NSBeginInformationalAlertSheet( @
"The cheat server can not broadcast.", @
"OK", nil, nil, [_preferenceController window
], nil, NULL
, NULL
, NULL
,
330 @
"The Cheat can't broadcast as \"%@\" because that name is in use by another server. The server will continue running with broadcasting disabled.", theName
);
333 - (void)serverChildrenChanged
:(CheatServer
*)theServer
335 [[NSNotificationCenter defaultCenter
] postNotificationName
:TCServerConnectionsChangedNote object
:theServer
];