-
Notifications
You must be signed in to change notification settings - Fork 0
/
JKJALaunchAppDelegate.m
479 lines (430 loc) · 19 KB
/
JKJALaunchAppDelegate.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#import "JKJALaunchAppDelegate.h"
NSString * const JKJAJediCommandLine = @"JediCommandLine";
NSString * const JKJAJediOpenGLErrorCheck = @"JediOpenGL";
NSString * const JKJAJediOpenAL = @"JediUseOpenAL";
NSString * const JKJAForce32BitColour = @"JediForce32";
NSString * const JKJAJediApplicationDictionary = @"JediApplicationDictionary";
NSString * const JKJAUseCustomMaster = @"JediUseCustomMaster";
NSString * const JKJACustomMaster = @"JediCustomMaster";
@implementation Untitled3AppDelegate
@synthesize window;
@synthesize launcherButton;
@synthesize launcherString;
@synthesize launcherAppPath;
@synthesize use32BitColourCheckBox;
@synthesize openGLErrorCheckBox;
@synthesize openALCheckBox;
@synthesize launcherCurrentVersionString;
@synthesize launcherAppPathString;
@synthesize commandLineBox;
@synthesize openGLBox;
@synthesize openALBox;
@synthesize saveOptionsCheckBox;
@synthesize openGLTextBox;
@synthesize openALTextBox;
@synthesize disclosureOpenGL;
@synthesize disclosureOpenAL;
@synthesize customMasterServerCheckBox;
@synthesize customMasterServerURL;
+ (void) initialize
{
NSMutableDictionary *defaultValues = [NSMutableDictionary dictionary];
[defaultValues setObject:[NSNumber numberWithBool:YES] forKey: JKJAJediOpenGLErrorCheck];
[defaultValues setObject:[NSNumber numberWithBool:YES] forKey: JKJAForce32BitColour];
[defaultValues setObject:[NSNumber numberWithBool:NO] forKey: JKJAJediOpenAL];
[defaultValues setObject:@"" forKey: JKJAJediCommandLine];
[defaultValues setObject:[NSNumber numberWithBool:YES] forKey: JKJAUseCustomMaster];
[defaultValues setObject:@"master.jkhub.org" forKey: JKJACustomMaster];
NSBundle *bundle = [NSBundle mainBundle];
NSString *bundlePath = [bundle bundlePath];
NSString *curDir = [bundlePath stringByDeletingLastPathComponent];
NSString *jampBundlePath = [curDir stringByAppendingString:@"/Jedi Academy MP.app"];
NSDictionary *defaultDictionary = [[NSDictionary alloc] initWithObjectsAndKeys: jampBundlePath, bundlePath, nil];
[defaultValues setObject:defaultDictionary forKey: JKJAJediApplicationDictionary];
[[NSUserDefaults standardUserDefaults] registerDefaults: defaultValues];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
//OK, first of all we look to see if there's Jedi Academy MP.app in the same folder as the launcher AND the current preference is not
//set to launch an application in the same folder as the launcher
NSBundle *bundle = [NSBundle mainBundle];
NSString *bundlePath = [bundle bundlePath];
NSString *curDir = [bundlePath stringByDeletingLastPathComponent];
NSString *jampBundlePath = [curDir stringByAppendingString:@"/Jedi Academy MP.app"];
[self addObserver:self forKeyPath:@"launcherAppPathString" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
NSDictionary *applicationsDictionary = [[NSUserDefaults standardUserDefaults] objectForKey: JKJAJediApplicationDictionary];
NSString *prefString = [applicationsDictionary objectForKey: bundlePath];
if (prefString)
{
[self setValue:prefString forKey:@"launcherAppPathString"];
}
else
{
[self setValue:jampBundlePath forKey:@"launcherAppPathString"];
}
//Load remaining options from user prefs
[launcherString setStringValue:[[NSUserDefaults standardUserDefaults] objectForKey: JKJAJediCommandLine]];
[openGLErrorCheckBox setState:([[[NSUserDefaults standardUserDefaults] objectForKey: JKJAJediOpenGLErrorCheck] boolValue])?NSOnState:NSOffState];
[use32BitColourCheckBox setState:([[[NSUserDefaults standardUserDefaults] objectForKey: JKJAForce32BitColour] boolValue])?NSOnState:NSOffState];
[openALCheckBox setState:([[[NSUserDefaults standardUserDefaults] objectForKey: JKJAJediOpenAL] boolValue])?NSOnState:NSOffState];
[customMasterServerCheckBox setState:([[[NSUserDefaults standardUserDefaults] objectForKey: JKJAUseCustomMaster] boolValue])?NSOnState:NSOffState];
[customMasterServerURL setStringValue:[[NSUserDefaults standardUserDefaults] objectForKey: JKJACustomMaster]];
//Shift on startup or invalid application path brings up the options window
NSUInteger launchFlags = [NSEvent modifierFlags];
SUUpdater *updater = [SUUpdater sharedUpdater];
[updater setDelegate: self];
[updater checkForUpdateInformation];
if ((!isValidAppPath) || (NSShiftKeyMask & launchFlags) || hasNewUpdate){
[self toggleOpenGLBox: nil];
[self toggleOpenALBox: nil];
[disclosureOpenAL setState: NSOffState];
[disclosureOpenGL setState: NSOffState];
[window orderFront:self];
}
//or we just launch with standard settings
else {
[self launchJediAcademy:nil];
}
}
- (void)updater:(SUUpdater *)updater didFindValidUpdate:(SUAppcastItem *)update
{
hasNewUpdate = TRUE;
}
- (void)updaterDidNotFindUpdate:(SUUpdater *)update
{
hasNewUpdate = FALSE;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
//works for JKJA, JKII, W:ET
if ([keyPath compare:@"launcherAppPathString"] == 0){
NSString *newPath = [change objectForKey:NSKeyValueChangeNewKey];
jediVersionNumber = [self getVersionNumberFromAppPath: newPath];
switch(jediVersionNumber){
case MAC_JKJA_VERSION_ERR:
isValidAppPath = NO;
[launcherCurrentVersionString setStringValue:@"Current Version Selected: Invalid"];
[launcherButton setEnabled:NO];
break;
case MAC_JKJA_VERSION_UNKNOWN:
isValidAppPath = NO;
[launcherCurrentVersionString setStringValue:@"Current Version Selected: Unsupported"];
[launcherButton setEnabled:NO];
break;
case MAC_JKJA_VERSION_MP:
isValidAppPath = YES;
[launcherButton setEnabled:YES];
[launcherCurrentVersionString setStringValue:@"Current Version Selected: Jedi Academy MP"];
break;
case MAC_JKJA_VERSION_SP:
isValidAppPath = YES;
[launcherButton setEnabled:YES];
[launcherCurrentVersionString setStringValue:@"Current Version Selected: Jedi Academy"];
break;
case MAC_JKII_VERSION_SP:
isValidAppPath = YES;
[launcherButton setEnabled:YES];
[launcherCurrentVersionString setStringValue:@"Current Version Selected: Jedi Knight II"];
break;
case MAC_JKII_VERSION_MP:
isValidAppPath = YES;
[launcherButton setEnabled:YES];
[launcherCurrentVersionString setStringValue:@"Current Version Selected: Jedi Knight II MP"];
break;
case MAC_ET_VERSION_MP:
isValidAppPath = YES;
[launcherButton setEnabled:YES];
[launcherCurrentVersionString setStringValue:@"Current Version Selected: Wolfenstein ET"];
break;
case MAC_ALICE_VERSION_SP:
isValidAppPath = YES;
[launcherButton setEnabled:YES];
[launcherCurrentVersionString setStringValue:@"Current Version Selected: American McGee's Alice™"];
break;
case MAC_JKJA_VERSION_STEAM:
isValidAppPath = YES;
[launcherButton setEnabled:YES];
[launcherCurrentVersionString setStringValue:@"Current Version Selected: Jedi Academy (Steam)"];
break;
case MAC_JKJA_VERSION_APPSTORE:
isValidAppPath = YES;
[launcherButton setEnabled:YES];
[launcherCurrentVersionString setStringValue:@"Current Version Selected: Jedi Academy (App Store)"];
break;
case MAC_JKII_VERSION_STEAM:
isValidAppPath = YES;
[launcherButton setEnabled:YES];
[launcherCurrentVersionString setStringValue:@"Current Version Selected: Jedi Knight II (Steam)"];
break;
case MAC_JKII_VERSION_APPSTORE:
isValidAppPath = YES;
[launcherButton setEnabled:YES];
[launcherCurrentVersionString setStringValue:@"Current Version Selected: Jedi Knight II (App Store)"];
break;
case MAC_OPENJK_VERSION_SP:
isValidAppPath = YES;
[launcherButton setEnabled:YES];
[launcherCurrentVersionString setStringValue:@"Current Version Selected: OpenJK SP"];
break;
case MAC_OPENJK_VERSION_MP:
isValidAppPath = YES;
[launcherButton setEnabled:YES];
[launcherCurrentVersionString setStringValue:@"Current Version Selected: OpenJK MP"];
break;
}
}
}
- (NSInteger)getVersionNumberFromAppPath:(NSString *)appPath {
NSBundle *jampBundle = [NSBundle bundleWithPath: appPath];
if (jampBundle == nil){
return MAC_JKJA_VERSION_ERR;
}
NSString *jampBundleName = [jampBundle objectForInfoDictionaryKey:@"CFBundleName"];
NSString *jampBundleIdentifier = [jampBundle objectForInfoDictionaryKey:@"CFBundleIdentifier"];
if (jampBundleName == nil){
if (jampBundleIdentifier == nil){
return MAC_JKJA_VERSION_ERR;
}
else if ([jampBundleIdentifier compare: @"com.aspyr.jk2.steam"] == 0){
return MAC_JKII_VERSION_STEAM;
}
else if ([jampBundleIdentifier compare: @"com.aspyr.jk2"] == 0){
return MAC_JKII_VERSION_APPSTORE;
}
else {
return MAC_JKJA_VERSION_ERR;
}
}
if([jampBundleName compare:@"Wolfenstein ET"] == 0){
return MAC_ET_VERSION_MP;
}
else if([jampBundleName compare:@"Jedi Knight II"] == 0){
return MAC_JKII_VERSION_SP;
}
else if([jampBundleName compare:@"Jedi Academy"] == 0){
return MAC_JKJA_VERSION_SP;
}
else if([jampBundleName compare:@"Jedi Knight II MP"] == 0){
return MAC_JKII_VERSION_MP;
}
else if([jampBundleName compare:@"Jedi Academy MP"] == 0){
return MAC_JKJA_VERSION_MP;
}
else if([jampBundleName compare:@"American McGee's Alice™"] == 0){
return MAC_ALICE_VERSION_SP;
}
else if ([jampBundleName compare:@"Star Wars Jedi Knight: Jedi Academy"] == 0){
if (jampBundleIdentifier == nil){
return MAC_JKJA_VERSION_ERR;
}
if ([jampBundleIdentifier compare: @"com.aspyr.jediacademy.appstore"] == 0){
return MAC_JKJA_VERSION_APPSTORE;
}
else if ([jampBundleIdentifier compare: @"com.aspyr.jediacademy.steam"] == 0){
return MAC_JKJA_VERSION_STEAM;
}
else {
return MAC_JKJA_VERSION_UNKNOWN;
}
}
else if ([jampBundleName compare:@"OpenJK"] == 0)
{
return MAC_OPENJK_VERSION_MP;
}
else if ([jampBundleName compare:@"OpenJK SP"] == 0)
{
return MAC_OPENJK_VERSION_SP;
}
else {
return MAC_JKJA_VERSION_UNKNOWN;
}
}
- (IBAction) toggleOpenGLBox:(id)sender {
NSRect windowFrame = [[openGLBox window] frame];
[openGLBox setAutoresizingMask:NSViewMinYMargin];
[openGLTextBox setAutoresizingMask:NSViewMinYMargin];
[openALBox setAutoresizingMask:NSViewMaxYMargin];
[openALTextBox setAutoresizingMask:NSViewMaxYMargin];
if ([openGLBox isHidden])
{
windowFrame.size.height += 40;
windowFrame.origin.y -= 40;
[[openGLBox window] setFrame: windowFrame display:YES animate:YES];
[openGLBox setHidden:NO];
}
else {
[openGLBox setHidden:YES];
windowFrame.size.height -= 40;
windowFrame.origin.y += 40;
[[openGLBox window] setFrame: windowFrame display:YES animate:YES];
}
}
- (IBAction) toggleOpenALBox:(id)sender {
NSRect windowFrame = [[openALBox window] frame];
[openGLBox setAutoresizingMask:NSViewMinYMargin];
[openGLTextBox setAutoresizingMask:NSViewMinYMargin];
[openALBox setAutoresizingMask:NSViewMinYMargin];
[openALTextBox setAutoresizingMask:NSViewMinYMargin];
if ([openALBox isHidden])
{
windowFrame.size.height += 25;
windowFrame.origin.y -= 25;
[[openALBox window] setFrame: windowFrame display:YES animate:YES];
[openALBox setHidden:NO];
}
else {
[openALBox setHidden:YES];
windowFrame.size.height -= 25;
windowFrame.origin.y += 25;
[[openALBox window] setFrame: windowFrame display:YES animate:YES];
}
}
- (IBAction) toggleCommandLineBox:(id)sender{
NSRect windowFrame = [[commandLineBox window] frame];
[openGLBox setAutoresizingMask:NSViewMaxYMargin];
[openGLTextBox setAutoresizingMask:NSViewMaxYMargin];
[openALBox setAutoresizingMask:NSViewMaxYMargin];
[openALTextBox setAutoresizingMask:NSViewMaxYMargin];
if ([commandLineBox isHidden])
{
windowFrame.size.height += 25;
windowFrame.origin.y -= 25;
[[commandLineBox window] setFrame: windowFrame display:YES animate:YES];
[commandLineBox setHidden:NO];
}
else {
[commandLineBox setHidden:YES];
windowFrame.size.height -= 25;
windowFrame.origin.y += 25;
[[commandLineBox window] setFrame: windowFrame display:YES animate:YES];
}
}
- (IBAction)launchJediAcademy:(id)sender {
NSString *launchString = launcherAppPathString;
NSString *argsNSString = @"";
NSInteger alertPanel;
BOOL launchMultiplayer = YES;
//Save settings for next time
NSBundle *bundle = [NSBundle mainBundle];
NSString *bundlePath = [bundle bundlePath];
NSMutableDictionary *applicationsDictionary = [NSMutableDictionary dictionaryWithDictionary:[[NSUserDefaults standardUserDefaults] objectForKey: JKJAJediApplicationDictionary]];
[applicationsDictionary setObject:[NSString stringWithString:launchString] forKey:[NSString stringWithString:bundlePath]];
[[NSUserDefaults standardUserDefaults] setObject:applicationsDictionary forKey:JKJAJediApplicationDictionary];
if ([saveOptionsCheckBox state] == NSOnState)
{
[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithString:[launcherString stringValue]] forKey:JKJAJediCommandLine];
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:([use32BitColourCheckBox state] == NSOnState)?YES:NO] forKey:JKJAForce32BitColour];
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:([openGLErrorCheckBox state] == NSOnState)?YES:NO] forKey:JKJAJediOpenGLErrorCheck];
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:([openALCheckBox state] == NSOnState)?YES:NO] forKey:JKJAJediOpenAL];
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:([customMasterServerCheckBox state] == NSOnState)?YES:NO] forKey:JKJAUseCustomMaster];
[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithString:[customMasterServerURL stringValue]] forKey:JKJACustomMaster];
}
//If starting Steam or App Store versions of Jedi Academy or Jedi Knight II, choose MP or SP
switch(jediVersionNumber)
{
case MAC_JKJA_VERSION_STEAM:
case MAC_JKJA_VERSION_APPSTORE:
case MAC_JKII_VERSION_STEAM:
case MAC_JKII_VERSION_APPSTORE:
alertPanel = NSRunAlertPanel(@"Choose game mode", @"Choose whether to launch Multiplayer or Singleplayer (Steam only)", @"Multiplayer", @"Singleplayer", nil);
launchMultiplayer = (alertPanel == NSAlertDefaultReturn) ? YES : NO;
break;
default:
break;
}
NSMutableDictionary *environmentOptions;
//make it so can connect to servers with steam / app store versions
if ((jediVersionNumber == MAC_JKJA_VERSION_APPSTORE) || (jediVersionNumber == MAC_JKJA_VERSION_STEAM) || (jediVersionNumber == MAC_JKII_VERSION_STEAM) || (jediVersionNumber == MAC_JKII_VERSION_APPSTORE))
{
environmentOptions = [NSMutableDictionary dictionaryWithObject:[NSString stringWithFormat:@"%@/Contents/Resources/patchapp.dylib", [[NSBundle mainBundle] bundlePath]] forKey:@"DYLD_INSERT_LIBRARIES"];
if ([customMasterServerCheckBox state] == NSOnState)
{
[environmentOptions setObject:[customMasterServerURL stringValue] forKey:@"JA_MASTERSERVER"];
}
}
//insert OpenGL dylib that ignores ATI FSAA calls
else if (jediVersionNumber < MAC_JKJA_VERSION_STEAM && [openGLErrorCheckBox state] == NSOnState){
environmentOptions = [NSMutableDictionary dictionaryWithObject:[NSString stringWithFormat:@"%@/Contents/Resources/libAGLRedirect.dylib", [[NSBundle mainBundle] bundlePath]] forKey:@"DYLD_INSERT_LIBRARIES"];
}
else{
environmentOptions = [NSMutableDictionary dictionaryWithObject:@"" forKey:@"DYLD_INSERT_LIBRARIES"];
}
switch(jediVersionNumber)
{
case MAC_JKJA_VERSION_APPSTORE:
if (launchMultiplayer)
{
[environmentOptions setObject:@"YS" forKey:@"ASPYR_JAMP"];
}
else
{
[environmentOptions setObject:@"NO" forKey:@"ASPYR_JAMP"];
}
break;
case MAC_JKJA_VERSION_STEAM:
if (launchMultiplayer)
{
launchString = [launchString stringByAppendingString:@"/Contents/Jedi Academy MP.app"];
}
else
{
launchString = [launchString stringByAppendingString:@"/Contents/Jedi Academy.app"];
}
break;
case MAC_JKII_VERSION_APPSTORE:
[environmentOptions setObject:@"YS" forKey:@"ASPYR_JKII_AS"];
case MAC_JKII_VERSION_STEAM:
if (launchMultiplayer)
{
launchString = [launchString stringByAppendingString:@"/Contents/Jedi Knight II MP.app"];
}
break;
default:
break;
}
if ([use32BitColourCheckBox state] == NSOnState){
argsNSString = [argsNSString stringByAppendingString:@"+set r_colorbits 32 "];
}
if ([openALCheckBox state] == NSOnState){
argsNSString = [argsNSString stringByAppendingString:@"+set s_useOpenAL 1 "];
}
else{
argsNSString = [argsNSString stringByAppendingString:@"+set s_useOpenAL 0 "];
}
argsNSString = [argsNSString stringByAppendingString:[launcherString stringValue]];
if ([openALCheckBox state] == NSOnState){
argsNSString = [argsNSString stringByAppendingString:@" +snd_restart"]; //needed in Lion to avoid massive slowdown
}
const char *argsCString = [argsNSString cString];
NSMutableArray *launchArgs = [[[launcherString stringValue] componentsSeparatedByString:@" "] mutableCopy];
if ([customMasterServerCheckBox state] == NSOnState)
{
NSArray *masterServerArgs = [[NSString stringWithFormat:@"+set sv_master1 %@", [customMasterServerURL stringValue]] componentsSeparatedByString:@" "];
[launchArgs addObjectsFromArray: masterServerArgs];
}
//here is AppleEvent stuff that passes the command line
NSAppleEventDescriptor *launchAppDescriptor = [NSAppleEventDescriptor descriptorWithString: launchString];
NSAppleEventDescriptor *launchDescriptor = [NSAppleEventDescriptor appleEventWithEventClass:'aevt' eventID:'oapp' targetDescriptor:launchAppDescriptor returnID:kAutoGenerateReturnID transactionID:kAnyTransactionID];
NSAppleEventDescriptor *launchArgsDescriptor = [NSAppleEventDescriptor descriptorWithDescriptorType: typeChar bytes:argsCString length:strlen(argsCString)];
[launchDescriptor setDescriptor:launchArgsDescriptor forKeyword:'CLin'];
NSDictionary *launchOptions = [NSDictionary dictionaryWithObjectsAndKeys:launchDescriptor, NSWorkspaceLaunchConfigurationAppleEvent, environmentOptions, NSWorkspaceLaunchConfigurationEnvironment, launchArgs, NSWorkspaceLaunchConfigurationArguments, nil];
//now launch!
[[NSWorkspace sharedWorkspace] launchApplicationAtURL:[NSURL fileURLWithPath: launchString]
options:NSWorkspaceLaunchDefault
configuration:launchOptions
error:nil];
[NSTimer scheduledTimerWithTimeInterval:0.2f target:NSApp selector:@selector(terminate:) userInfo:nil repeats:NO];
}
- (IBAction) selectAppPath:(id)sender
{
NSOpenPanel *selectApp = [NSOpenPanel openPanel];
[selectApp setAllowsMultipleSelection:NO];
[selectApp setPrompt:@"OK"];
[selectApp setTitle:@"Select Jedi Academy"];
[selectApp runModal];
NSArray *apps = [selectApp filenames];
NSString *selectedAppPath = [apps objectAtIndex:0];
if (selectedAppPath != nil){
[self setValue:selectedAppPath forKey:@"launcherAppPathString"];
}
}
@end