-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathAppController.m
246 lines (197 loc) · 7.71 KB
/
AppController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* The Cheat - The legendary universal game trainer for Mac OS X.
* http://www.brokenzipper.com/trac/wiki/TheCheat
*
* Copyright (c) 2003-2011, Charles McGarvey et al.
*
* Distributable under the terms and conditions of the 2-clause BSD
* license; see the file COPYING for the legal text of the license.
*/
#import "AppController.h"
#import "CheatDocument.h"
#import "AboutBoxController.h"
#import "HelpController.h"
#import "PreferenceController.h"
@implementation AppController
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
#pragma mark Initialization
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
+ (void)initialize
{
NSMutableDictionary *defaults = [NSMutableDictionary dictionary];
TCFirstLaunchPref = [[NSString stringWithFormat:@"TC%@%@Pref", ChazAppName(), ChazAppVersion()] retain];
NSString *broadcastName = [NSString stringWithFormat:@"%@'s Computer", NSFullUserName()];
// register user defaults
[defaults setObject:[NSNumber numberWithBool:NO] forKey:TCFirstLaunchPref];
[defaults setObject:[NSNumber numberWithBool:NO] forKey:TCWindowsOnTopPref];
[defaults setObject:[NSNumber numberWithBool:YES] forKey:TCUpdateCheckPref];
[defaults setObject:[NSNumber numberWithBool:YES] forKey:TCDisplayValuesPref];
[defaults setObject:[NSNumber numberWithFloat:1.0] forKey:TCValueUpdatePref];
[defaults setObject:[NSNumber numberWithInt:1000] forKey:TCHitsDisplayedPref];
[defaults setObject:[NSNumber numberWithBool:NO] forKey:TCRunServerPref];
[defaults setObject:broadcastName forKey:TCBroadcastNamePref];
[defaults setObject:[NSNumber numberWithInt:TCDefaultListenPort] forKey:TCListenPortPref];
[defaults setObject:[NSNumber numberWithFloat:gFadeAnimationDuration] forKey:TCFadeAnimationPref];
[defaults setObject:[NSNumber numberWithBool:YES] forKey:TCAskForSavePref];
[defaults setObject:[NSNumber numberWithBool:YES] forKey:TCSwitchVariablesPref];
[defaults setObject:[NSNumber numberWithBool:YES] forKey:TCAutoStartEditingVarsPref];
// register it
[[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
// set globals
gFadeAnimationDuration = [[NSUserDefaults standardUserDefaults] floatForKey:TCFadeAnimationPref];
}
- (id)init
{
if ( self = [super init] ) {
[self setDelegate:self];
}
return self;
}
- (void)dealloc
{
ChazLog( @"AppController deallocated!!" );
[self stopCheatServer];
[super dealloc];
}
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
#pragma mark NSApplication Delegate
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[NSApp activateIgnoringOtherApps:YES];
// check if this is the first launch
if ( ![[NSUserDefaults standardUserDefaults] boolForKey:TCFirstLaunchPref] ) {
// FIRST LAUNCH
[self showAboutBoxWindow:self];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:TCFirstLaunchPref];
}
// if should check for updates on launch
if ( [[NSUserDefaults standardUserDefaults] boolForKey:TCUpdateCheckPref] ) {
ChazCheckForUpdate( TCUpdateCheckURL, NO );
}
// automatically start the cheat server if the pref is set
if ( [[NSUserDefaults standardUserDefaults] boolForKey:TCRunServerPref] ) {
if ( ![self startCheatServer] ) {
// inform the user that the server won't start
NSRunAlertPanel( @"The Cheat could not start the server.",
@"The cheat server failed to start. Check the server settings and start it manually.",
@"OK", nil, nil );
// open server prefs
[self showPreferenceWindow:self];
[_preferenceController chooseServer:self];
}
}
}
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
#pragma mark Interface Actions
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
- (IBAction)newSearchWindow:(id)sender
{
NSDocumentController *controller = [NSDocumentController sharedDocumentController];
CheatDocument *doc = [controller makeUntitledDocumentOfType:@"Cheat Document"];
if ( !doc ) {
ChazLog( @"nil document" );
}
[doc setMode:TCSearchMode];
[controller addDocument:doc];
[doc makeWindowControllers];
[doc showWindows];
}
- (IBAction)newBlankCheatWindow:(id)sender
{
NSDocumentController *controller = [NSDocumentController sharedDocumentController];
CheatDocument *doc = [controller makeUntitledDocumentOfType:@"Cheat Document"];
if ( !doc ) {
ChazLog( @"nil document" );
}
[doc setMode:TCCheatMode];
[controller addDocument:doc];
[doc makeWindowControllers];
[doc showWindows];
}
- (IBAction)showAboutBoxWindow:(id)sender
{
if ( !_aboutBoxController ) {
_aboutBoxController = [[AboutBoxController alloc] init];
}
[_aboutBoxController showWindow:self];
}
- (IBAction)showPreferenceWindow:(id)sender
{
if ( !_preferenceController ) {
_preferenceController = [[PreferenceController alloc] init];
}
[_preferenceController showWindow:self];
}
- (IBAction)launchHelpFile:(id)sender
{
if ( !_helpController ) {
_helpController = [[HelpController alloc] init];
}
[_helpController showWindow:self];
}
- (IBAction)launchEmailMenu:(id)sender
{
LaunchEmail();
}
- (IBAction)launchWebsiteMenu:(id)sender
{
LaunchWebsite();
}
- (IBAction)checkForUpdate:(id)sender
{
ChazCheckForUpdate( TCUpdateCheckURL, YES );
}
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
#pragma mark CheatServer Stuff
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
- (CheatServer *)cheatServer
{
if ( !_server ) {
_server = [[CheatServer alloc] initWithDelegate:self];
}
return _server;
}
- (BOOL)startCheatServer
{
ChazLog( @"cheat server starting..." );
// start the server with saved settings
int port = [[NSUserDefaults standardUserDefaults] integerForKey:TCListenPortPref];
NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:TCBroadcastNamePref];
if ( [name isEqualToString:@""] ) {
name = nil;
}
// stop the cheat server if it's running
[self stopCheatServer];
// start the server
if ( [[self cheatServer] listenOnPort:port broadcast:name] ) {
[[NSNotificationCenter defaultCenter] postNotificationName:TCServerStartedNote object:[self cheatServer]];
return YES;
}
return NO;
}
- (void)stopCheatServer
{
if ( _server ) {
[_server stop];
[[NSNotificationCenter defaultCenter] postNotificationName:TCServerStoppedNote object:[self cheatServer]];
}
}
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
#pragma mark CheatServerDelegate
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
- (void)serverDisconnectedUnexpectedly:(CheatServer *)theServer
{
ChazLog( @"server disconnected unexpectedly." );
[self stopCheatServer];
}
- (void)server:(CheatServer *)theServer failedToBroadcastName:(NSString *)theName
{
NSBeginInformationalAlertSheet( @"The cheat server can not broadcast.", @"OK", nil, nil, [_preferenceController window], nil, NULL, NULL, NULL,
@"The Cheat can't broadcast as \"%@\" because that name is in use by another server. The server will continue running with broadcasting disabled.", theName );
}
- (void)serverChildrenChanged:(CheatServer *)theServer
{
[[NSNotificationCenter defaultCenter] postNotificationName:TCServerConnectionsChangedNote object:theServer];
}
@end