/
GLLauncherController.m
443 lines (400 loc) · 13 KB
/
GLLauncherController.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
//
// GLLauncherController.m
// GameLauncher
//
// Created by Adam on 25/05/05.
// Copyright 2005 2Sublime.com. All rights reserved.
//
#import "GLLauncherController.h"
#import <GLGUIController.h>
@implementation GLLauncherController
static GLLauncherController *sharedInstance = nil;
+ (GLLauncherController *)sharedInstance
{
return sharedInstance ? sharedInstance : [[self alloc] init];
}
- (id)init
{
if (sharedInstance) {
[self dealloc];
} else {
sharedInstance = [super init];
}
return sharedInstance;
}
- (void)awakeFromNib
{
launchNotificationPath = [NSString string];
}
- (void)startRunLoop:(NSThread *)sender
{
[NSAutoreleasePool new];
mainThread = [sender retain];
myThread = [[NSThread currentThread] retain];
[NSThread prepareForInterThreadMessages];
[[NSRunLoop currentRunLoop] run];
}
- (BOOL)isLaunching
{
return isLaunching;
}
- (NSThread *)myThread
{
return myThread;
}
- (double)launchProgress
{
return launchProgress;
}
- (void)doQuitAllAndLaunch
{
//handles quitting all applications,
//launching new applications,
//and relaunching old applications
#ifdef __DEBUG__
NSLog(@"%@: begin \"doQuitAllAndLaunch:\"", [self class]);
#endif
isLaunching = YES;
NSNotificationCenter *notify = [NSNotificationCenter defaultCenter];
//update speedDelta and alwaysOnTop
[notify postNotificationName:GLBeginQuitAllNotification
object:self
inThread:mainThread];
speedDelta = [[GLPreferences sharedInstance] computerSpeed];
//generate list of selected applications
NSArray *applicationList = [[GLGUIController sharedInstance] applicationList];
NSMutableArray *quitList;
quitList = [NSMutableArray array];
NSEnumerator *enumerator = [applicationList objectEnumerator];
GLProcess *process;
while ((process = [enumerator nextObject])) {
if ([process enabled]) {
[quitList addObject:process];
}
}
//Sort list by process ID
NSSortDescriptor *tempSort = [[NSSortDescriptor new] initWithKey:@"pid" ascending:NO];
[quitList sortUsingDescriptors:[NSArray arrayWithObject:tempSort]];
//Quit applications
NSArray *listOfQuitApplications = [self
quitAllSelectedApplications:quitList];
//Launch app
if (longLaunchAnswer != kCancelLaunch) {
//quit dashboard
//if 10.4 or greater
if (([[[NSCoder alloc] init] systemVersion] >= 1000) &&
([listOfQuitApplications count] > 0)){
[[GLProcess processForCommand:@"Dock"] quitGracefully];
}
//update
[notify postNotificationName:GLWillLaunchNotification
object:self
inThread:mainThread];
/* LAUNCH GAME */
didLaunch = YES;
[self waitUntilExitAppWithPath:[[GLGUIController sharedInstance] launchAppPath]];
didLaunch = NO;
}
//Relaunch quit apps
[notify postNotificationName:GLBeginRelaunchAllNotification
object:self
inThread:mainThread];
[self relaunchAllSelectedApplications:listOfQuitApplications];
//send final notifications
[notify postNotificationName:GLDidFinishLaunchingNotification
object:self
inThread:mainThread];
isLaunching = NO;
#ifdef __DEBUG__
NSLog(@"%@: end \"doQuitAllAndLaunch:\"", [self class]);
#endif
return;
}
- (void)cancelLaunch
{
longLaunchAnswer = kCancelLaunch;
NSNotificationCenter *notify = [NSNotificationCenter defaultCenter];
[notify postNotificationName:GLRevertSystemUINotification
object:self
inThread:mainThread];
if (didLaunch) {
GLProcess *proc = (GLProcess*)[GLProcess processForProcessIdentifier:[launchTask processIdentifier]];
[self quitProcess:proc];
}
}
- (NSArray *)quitAllSelectedApplications:(NSArray *)quitList
{
NSEnumerator *enumerator;
//Most recent apps quit first.
enumerator = [quitList objectEnumerator];
GLProcess *process;
//BOOL didQuitGracefully = YES;
double increment = (100.0/[quitList count]);
double progress = 0.0;
GLLaunchQueryState quitAnswer = kStateUndefined;
NSMutableArray *listOfQuitApplications = [NSMutableArray array];
while ((process = [enumerator nextObject])) {
quitAnswer = [self quitProcess:process];
if (quitAnswer == kSkipApplication) {
//do nothing
}
if (quitAnswer == kCancelLaunch) {
//cancel and return
break;
}
//update progress bar
progress = progress + increment;
launchProgress = progress;
//add to relaunch list
if (quitAnswer != kSkipApplication) {
[listOfQuitApplications addObject:process];
}
}
if (quitAnswer != kCancelLaunch) {
launchProgress = 99.0;
}
NSArray *retArr = [NSArray arrayWithArray:listOfQuitApplications];
[listOfQuitApplications release];
return [retArr retain];
}
- (GLLaunchQueryState)quitProcess:(GLProcess *)process
{
speedDelta = [[GLPreferences sharedInstance] computerSpeed];
BOOL longLaunchQuestion;
NSDate *timeOutStart = [NSDate date];
longLaunchQuestion = NO;
longLaunchAnswer = kStateUndefined;
[process quitGracefully];
while (([process isRunning]) &&
((longLaunchAnswer == kStateUndefined) ||
(longLaunchAnswer == kContinueWaiting))) {
if (longLaunchAnswer == kContinueWaiting) {
longLaunchAnswer = kStateUndefined;
longLaunchQuestion = NO;
timeOutStart = [NSDate date];
}
[NSThread sleepUntilDate:[NSDate
dateWithTimeIntervalSinceNow:0.5]];
double waitTime = [[NSDate date] timeIntervalSinceDate:timeOutStart];
//NSLog(@"%f", waitTime);
if ((waitTime > speedDelta) && !longLaunchQuestion) {
//show sheet and ask what to do
[slowLaunchSheetIcon setImage:[[NSWorkspace sharedWorkspace]
iconForFile:[process path]]];
[slowLaunchSheetApplicationName setStringValue:[process command]];
[NSApp beginSheet:slowLaunchSheet
modalForWindow:[[GLGUIController sharedInstance] mainWindow]
modalDelegate:self
didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:)
contextInfo:NULL];
longLaunchQuestion = YES;
}
}
if (longLaunchQuestion &&
(longLaunchAnswer == kStateUndefined)) {
//process exited while question being asked
[slowLaunchSheet orderOut:self];
[NSApp endSheet:slowLaunchSheet returnCode:kStateUndefined];
}
if (longLaunchAnswer == kKillApplication) {
[process terminate];
}
return longLaunchAnswer;
}
/*
Used to launch game
Waits until app quits.
*/
- (void)waitUntilExitAppWithPath:(NSString *)path
{
launchTask = [self launchTaskWithPath:path args:[NSArray array]];
NSLog(@"%@ launched", [[path pathComponents] lastObject]);
[NSThread sleepUntilDate:[NSDate
dateWithTimeIntervalSinceNow:speedDelta/3]];
[[GLProcess processForProcessIdentifier:[launchTask processIdentifier]] moveToFrontProcess];
[launchTask waitUntilExit];
int status = [launchTask terminationStatus];
if (status != 0) {
NSAlert *crashAlert = [[NSAlert alloc] init];
[crashAlert setMessageText:NSLocalizedStringFromTable(GLAlertAppCrashTitle, GLLocalizablePlist, GLShouldQuitTitle)];
NSString *temp = [NSString stringWithFormat:NSLocalizedStringFromTable(GLAlertAppCrashMsg, GLLocalizablePlist, GLAlertAppCrashMsg), [[path pathComponents] lastObject], status];
[crashAlert setInformativeText:temp];
[crashAlert addButtonWithTitle:NSLocalizedStringFromTable(GLAlertAppCrashBut1, GLLocalizablePlist, GLAlertAppCrashBut1)];
[crashAlert addButtonWithTitle:NSLocalizedStringFromTable(GLAlertAppCrashBut2, GLLocalizablePlist, GLAlertAppCrashBut2)];
int quitRequestButton = [crashAlert runModal];
if (quitRequestButton == NSAlertFirstButtonReturn) {
//relaunch
[self waitUntilExitAppWithPath:path];
} else {
//cancel
}
}
#ifdef __DEBUG__
NSLog(@"++++++ %@ finished launch with status (%d) ++++++", [[path pathComponents] lastObject], status);
#endif
}
- (NSString *)runCommandLine:(NSString *)command withArguments:(NSArray *)args
{
NSTask *task = [[NSTask alloc] init];
NSPipe *newPipe = [NSPipe pipe];
NSFileHandle *readHandle = [newPipe fileHandleForReading];
NSData *inData;
NSString *tempString;
[task setCurrentDirectoryPath:NSHomeDirectory()];
[task setLaunchPath:command];
[task setArguments:args];
[task setStandardOutput:newPipe];
[task setStandardError:newPipe];
[task launch];
inData = [readHandle readDataToEndOfFile];
tempString = [[NSString alloc] initWithData:inData encoding:NSASCIIStringEncoding];
[task release];
[tempString autorelease];
return tempString;
}
-(NSString *)resolveAliasInPath:(NSString *)path
{
NSString *resolvedPath = nil;
CFURLRef url;
url = CFURLCreateWithFileSystemPath(NULL /*allocator*/, (CFStringRef)path,
kCFURLPOSIXPathStyle, NO /*isDirectory*/);
if (url != NULL)
{
FSRef fsRef;
if (CFURLGetFSRef(url, &fsRef))
{
Boolean targetIsFolder, wasAliased;
if (FSResolveAliasFile (&fsRef, true /*resolveAliasChains*/,
&targetIsFolder, &wasAliased) == noErr && wasAliased)
{
CFURLRef resolvedUrl = CFURLCreateFromFSRef(NULL, &fsRef);
if (resolvedUrl != NULL)
{
resolvedPath = (NSString*)
CFURLCopyFileSystemPath(resolvedUrl,
kCFURLPOSIXPathStyle);
CFRelease(resolvedUrl);
}
} else {
UInt8 fsPath[PATH_MAX];
FSRefMakePath(&fsRef, fsPath, PATH_MAX);
resolvedPath = [[NSString alloc] initWithUTF8String:(char *)fsPath];
}
}
CFRelease(url);
}
if (resolvedPath==nil)
resolvedPath = [[NSString alloc] initWithString:path];
return [resolvedPath autorelease];
}
- (NSTask *)launchTaskWithPath:(NSString *)path args:(NSArray *)args
{
#ifdef __DEBUG__
NSLog(@"%@: launchTaskWithPath:%@ args:%@", [self class], path, [args description]);
#endif
NSTask *aTask = [[NSTask alloc] init];
NSString *runDir, *executable, *name;
name = [[path pathComponents] lastObject];
executable = [[NSBundle bundleWithPath:path] executablePath];
runDir = [path stringByDeletingLastPathComponent];
if (executable == nil) {
// Application is Carbon
executable = @"/System/Library/Frameworks/Carbon.framework/Versions/Current/Support/LaunchCFMApp";
args = [NSArray arrayWithObject:path];
} else {
executable = [self resolveAliasInPath:executable];
NSString *fileType = [self runCommandLine:@"/usr/bin/file" withArguments:[NSArray arrayWithObject:executable]];
AGRegex *regex = [AGRegex regexWithPattern:@"Mach-O"];
AGRegexMatch *match = nil;
match = [regex findInString:fileType];
if (!match) {
// Application is Carbon
args = [NSArray arrayWithObject:executable];
runDir = [executable stringByDeletingLastPathComponent];
executable = @"/System/Library/Frameworks/Carbon.framework/Versions/Current/Support/LaunchCFMApp";
}
}
/* set arguments */
[aTask setLaunchPath:executable];
[aTask setArguments:args];
[aTask setCurrentDirectoryPath:runDir];
[aTask launch];
return [aTask autorelease];
}
- (void)relaunchProcess:(GLProcess *)process args:(NSArray *)args
{
NSString *name;
name = [[[process path] pathComponents] lastObject];
if ([args count] == 0) {
[[NSWorkspace sharedWorkspace] launchApplication:[process path]];
#ifdef __DEBUG__
NSLog(@"%@: %@(pid%d) relaunched (NSWorkspace) args:()", [self class], name, [process pid]);
#endif
} else {
//Note:launches as a child of current process
// not good once GameLauncher quits.
[self launchTaskWithPath:[process path] args:args];
#ifdef __DEBUG__
NSLog(@"%@: %@(pid%d) relaunched (NSTask) args:%@", [self class], name, [process pid], args);
#endif
}
}
- (void)relaunchAllSelectedApplications:(NSArray *)relaunchList
{
NSEnumerator *enumerator;
enumerator = [relaunchList reverseObjectEnumerator];
GLProcess *process;
//Note: launchProgress may not equal 100.0
// if launch is cancelled.
double progress = launchProgress;
double decrement = (progress/[relaunchList count]);
NSNotificationCenter *notify = [[NSWorkspace sharedWorkspace] notificationCenter];
[notify addObserver:self selector:@selector(appDidLaunch:)
name:NSWorkspaceDidLaunchApplicationNotification
object:nil];
//launchNotificationPath used to check through notifications
//speeds relaunch times
while ((process = [enumerator nextObject])) {
if (![GLProcess processForCommand:[process command]]) {
[self relaunchProcess:process args:[process arguments]];
NSDate *waitDate = [NSDate dateWithTimeIntervalSinceNow:speedDelta];
while (![[NSDate date] isGreaterThan:waitDate] &&
![launchNotificationPath isEqualToString:[process path]]){
[NSThread sleepUntilDate:[NSDate
dateWithTimeIntervalSinceNow:speedDelta/3]];
}
}
//update progress bar
progress = progress - decrement;
launchProgress = progress;
}
[notify removeObserver:self];
launchProgress = 0.0;
}
#pragma mark NSNotification observer selectors
- (void)appDidLaunch:(NSNotification *)notification
{
[launchNotificationPath release];
launchNotificationPath = [[[notification userInfo] valueForKey:@"NSApplicationPath"] retain];
}
#pragma mark Sheet Delegate Actions
- (IBAction)cancelLaunch:(id)sender
{
[slowLaunchSheet orderOut:sender];
[NSApp endSheet:slowLaunchSheet returnCode:kCancelLaunch];
}
- (IBAction)continueWaiting:(id)sender
{
int returnCode = [[radioList selectedCell] tag];
[slowLaunchSheet orderOut:sender];
[NSApp endSheet:slowLaunchSheet returnCode:returnCode];
}
- (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode
contextInfo:(void *)contextInfo
{
longLaunchAnswer = returnCode;
if (longLaunchAnswer == kCancelLaunch) {
[self cancelLaunch];
}
}
@end