This repository has been archived by the owner on Nov 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenRA.m
286 lines (230 loc) · 9.03 KB
/
OpenRA.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
/*
* Copyright 2007-2017 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#import <Cocoa/Cocoa.h>
#include <dlfcn.h>
#define RET_MONO_NOT_FOUND 131
#define RET_MONO_INIT_ERROR 132
#define RET_MONO_VERSION_OUTDATED 133
#define RET_OPENRA_RESTARTING 1
@interface ORALauncher : NSObject <NSApplicationDelegate>
- (void)launchGameWithArgs: (NSArray *)gameArgs;
@end
@implementation ORALauncher
BOOL launched = NO;
NSTask *gameTask;
- (NSString *)modName
{
NSDictionary *plist = [[NSBundle mainBundle] infoDictionary];
if (plist)
{
NSString *title = [plist objectForKey:@"CFBundleDisplayName"];
if (title && [title length] > 0)
return title;
}
return @"OpenRA";
}
- (void)showCrashPrompt
{
NSString *modName = [self modName];
NSString *message = [NSString stringWithFormat: @"%@ has encountered a fatal error and must close.\nPlease refer to the crash logs and FAQ for more information.", modName];
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:@"Fatal Error"];
[alert setInformativeText:message];
[alert addButtonWithTitle:@"View Logs"];
[alert addButtonWithTitle:@"View FAQ"];
[alert addButtonWithTitle:@"Quit"];
NSInteger answer = [alert runModal];
[alert release];
if (answer == NSAlertFirstButtonReturn)
{
NSString *logDir = [@"~/Library/Application Support/OpenRA/Logs/" stringByExpandingTildeInPath];
[[NSWorkspace sharedWorkspace] openFile: logDir withApplication:@"Finder"];
}
else if (answer == NSAlertSecondButtonReturn)
{
NSDictionary *plist = [[NSBundle mainBundle] infoDictionary];
if (plist)
{
NSString *faqUrl = [plist objectForKey:@"FaqUrl"];
if (faqUrl && [faqUrl length] > 0)
[[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString:faqUrl]];
}
}
}
// Application was launched via a URL handler
- (void)getUrl:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent
{
NSMutableArray *gameArgs = [[[NSProcessInfo processInfo] arguments] mutableCopy];
NSDictionary *plist = [[NSBundle mainBundle] infoDictionary];
NSString *url = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
if (plist)
{
NSString *joinServerUrl = [plist objectForKey:@"JoinServerUrlScheme"];
if (joinServerUrl && [joinServerUrl length] > 0)
{
NSString *prefix = [joinServerUrl stringByAppendingString: @"://"];
if ([url hasPrefix: prefix])
{
NSString *trimmed = [url substringFromIndex:[prefix length]];
NSArray *parts = [trimmed componentsSeparatedByString:@":"];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
if ([parts count] == 2 && [formatter numberFromString: [parts objectAtIndex:1]] != nil)
[gameArgs addObject: [NSString stringWithFormat: @"Launch.Connect=%@", trimmed]];
[formatter release];
}
}
}
[self launchGameWithArgs: gameArgs];
[gameArgs release];
}
- (void)applicationWillFinishLaunching:(NSNotification *)aNotification
{
// Register for url events
NSDictionary *plist = [[NSBundle mainBundle] infoDictionary];
if (plist)
{
NSString *joinServerUrl = [plist objectForKey:@"JoinServerUrlScheme"];
NSString *bundleIdentifier = [plist objectForKey:@"CFBundleIdentifier"];
if (joinServerUrl && [joinServerUrl length] > 0 && bundleIdentifier)
{
LSSetDefaultHandlerForURLScheme((CFStringRef)joinServerUrl, (CFStringRef)bundleIdentifier);
[[NSAppleEventManager sharedAppleEventManager] setEventHandler:self andSelector:@selector(getUrl:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];
}
}
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[self launchGameWithArgs: [[NSProcessInfo processInfo] arguments]];
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed: (NSApplication *)theApplication
{
return YES;
}
- (void)launchGameWithArgs: (NSArray *)gameArgs
{
if (launched)
{
NSLog(@"launchgame is already running... ignoring request.");
return;
}
launched = YES;
// Default values - can be overriden by setting certain keys Info.plist
NSString *gameName = @"OpenRA.Game.exe";
NSString *modId = nil;
NSDictionary *plist = [[NSBundle mainBundle] infoDictionary];
if (plist)
{
NSString *exeValue = [plist objectForKey:@"MonoGameExe"];
if (exeValue && [exeValue length] > 0)
gameName = exeValue;
NSString *modIdValue = [plist objectForKey:@"ModId"];
if (modIdValue && [modIdValue length] > 0)
modId = modIdValue;
}
NSString *exePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent: @"Contents/MacOS/"];
NSString *gamePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent: @"Contents/Resources/"];
NSString *launchPath = [exePath stringByAppendingPathComponent: @"mono"];
NSString *appPath = [exePath stringByAppendingPathComponent: @"OpenRA"];
NSString *engineLaunchPath = [self resolveTranslocatedPath: appPath];
NSMutableArray *launchArgs = [NSMutableArray arrayWithCapacity: [gameArgs count] + 2];
[launchArgs addObject: @"--debug"];
[launchArgs addObject: [gamePath stringByAppendingPathComponent: gameName]];
[launchArgs addObject: [NSString stringWithFormat:@"Engine.LaunchPath=\"%@\"", engineLaunchPath]];
if (modId)
[launchArgs addObject: [NSString stringWithFormat:@"Game.Mod=%@", modId]];
[launchArgs addObjectsFromArray: gameArgs];
NSLog(@"Running mono with arguments:");
for (size_t i = 0; i < [launchArgs count]; i++)
NSLog(@"%@", [launchArgs objectAtIndex: i]);
gameTask = [[NSTask alloc] init];
[gameTask setCurrentDirectoryPath: gamePath];
[gameTask setLaunchPath: launchPath];
[gameTask setArguments: launchArgs];
NSMutableDictionary *environment = [NSMutableDictionary dictionaryWithDictionary: [[NSProcessInfo processInfo] environment]];
[environment setObject: [gamePath stringByAppendingPathComponent: @"lib/mono/4.5"] forKey: @"MONO_PATH"];
[environment setObject: [gamePath stringByAppendingPathComponent: @"etc"] forKey: @"MONO_CFG_DIR"];
[environment setObject: [gamePath stringByAppendingPathComponent: @"etc/mono/config"] forKey: @"MONO_CONFIG"];
[gameTask setEnvironment: environment];
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(taskExited:)
name: NSTaskDidTerminateNotification
object: gameTask
];
[gameTask launch];
}
- (NSString *)resolveTranslocatedPath: (NSString *)path
{
// macOS 10.12 introduced the "App Translocation" feature, which runs quarantined applications
// from a transient read-only disk image. The read-only image isn't a problem, but the transient
// path breaks the mod registration/switching feature.
// This resolves the original path which can then be written into the mod metadata for future
// launches (which will then be re-translocated)
// Running on macOS < 10.12
if (floor(NSAppKitVersionNumber) <= 1404)
return path;
void *handle = dlopen("/System/Library/Frameworks/Security.framework/Security", RTLD_LAZY);
// Failed to load security framework
if (handle == NULL)
return path;
Boolean (*mySecTranslocateIsTranslocatedURL)(CFURLRef path, bool *isTranslocated, CFErrorRef * __nullable error);
mySecTranslocateIsTranslocatedURL = dlsym(handle, "SecTranslocateIsTranslocatedURL");
CFURLRef __nullable (*mySecTranslocateCreateOriginalPathForURL)(CFURLRef translocatedPath, CFErrorRef * __nullable error);
mySecTranslocateCreateOriginalPathForURL = dlsym(handle, "SecTranslocateCreateOriginalPathForURL");
// Failed to resolve required functions
if (mySecTranslocateIsTranslocatedURL == NULL || mySecTranslocateCreateOriginalPathForURL == NULL)
return path;
bool isTranslocated = false;
CFURLRef pathURLRef = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (__bridge CFStringRef)path, kCFURLPOSIXPathStyle, false);
if (mySecTranslocateIsTranslocatedURL(pathURLRef, &isTranslocated, NULL))
{
if (isTranslocated)
{
CFURLRef resolvedURL = mySecTranslocateCreateOriginalPathForURL(pathURLRef, NULL);
path = [(NSURL *)(resolvedURL) path];
}
}
CFRelease(pathURLRef);
return path;
}
- (void)taskExited:(NSNotification *)note
{
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:NSTaskDidTerminateNotification
object:gameTask
];
int ret = [gameTask terminationStatus];
NSLog(@"launchgame exited with code %d", ret);
[gameTask release];
gameTask = nil;
// We're done here
if (ret == 0)
exit(0);
// Make the error dialog visible
[NSApp setActivationPolicy: NSApplicationActivationPolicyRegular];
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
if (ret != RET_OPENRA_RESTARTING)
{
[self showCrashPrompt];
}
exit(1);
}
@end
int main(int argc, char **argv)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSApplication *application = [NSApplication sharedApplication];
ORALauncher *launcher = [[ORALauncher alloc] init];
[NSApp setActivationPolicy: NSApplicationActivationPolicyProhibited];
[application setDelegate:launcher];
[application run];
[launcher release];
[pool drain];
return EXIT_SUCCESS;
}