-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig_mac.mm
127 lines (108 loc) · 4.92 KB
/
config_mac.mm
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
/************************************************************************/
/* */
/* This file is part of VDrift. */
/* */
/* VDrift is free software: you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation, either version 3 of the License, or */
/* (at your option) any later version. */
/* */
/* VDrift is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with VDrift. If not, see <http://www.gnu.org/licenses/>. */
/* */
/************************************************************************/
#import <Cocoa/Cocoa.h>
template <typename T> T VD_CFReleaseIf(T objRef){
if (NULL != objRef){
CFRelease(objRef);
}
return NULL;
}
char* get_mac_data_dir()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *basePath = [[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent];
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
NSString *path;
if ([[NSFileManager defaultManager] fileExistsAtPath:[resourcePath stringByAppendingString:@"/data/settings/options.config"]])
{
path = [resourcePath stringByAppendingString:@"/data"];
}
else
{
path = [basePath stringByAppendingString:@"/data"];
}
CFStringRef resolvedPath = nil;
CFURLRef url = CFURLCreateWithFileSystemPath(NULL, (CFStringRef)path, kCFURLPOSIXPathStyle, true);
if (url != NULL)
{
FSRef fsRef;
if (CFURLGetFSRef(url, &fsRef))
{
Boolean isFolder, isAlias;
OSErr oserr = FSResolveAliasFile (&fsRef, true, &isFolder, &isAlias);
if(oserr != noErr)
{
NSLog(@"FSResolveAliasFile failed: status = %d", oserr);
}
else
{
if(isAlias)
{
CFURLRef resolved_url = CFURLCreateFromFSRef(NULL, &fsRef);
if (resolved_url != NULL)
{
resolvedPath = CFURLCopyFileSystemPath(resolved_url, kCFURLPOSIXPathStyle);
CFRelease(resolved_url);
}
}
}
}
else // Failed to convert URL to a file or directory object.
{
NSApplication *myApplication;
myApplication = [NSApplication sharedApplication];
NSAlert *theAlert = [NSAlert alertWithMessageText: @"Can't find data"
defaultButton: @"Quit"
alternateButton: nil
otherButton: nil
informativeTextWithFormat: @"Please make sure a folder named \"data\" is in:\n - the same folder as VDrift.app; or\n - VDrift.app/Contents/Resources"];
[theAlert runModal];
[pool release];
exit(1);
}
}
url = VD_CFReleaseIf(url);
if(resolvedPath != nil)
{
path = [NSString stringWithString:(NSString *)resolvedPath];
CFRelease(resolvedPath);
}
if ([path canBeConvertedToEncoding:NSUTF8StringEncoding])
{
int len = [path lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
char* cpath = (char*) malloc(len + 2);
[path getCString:cpath maxLength:len + 1 encoding:NSUTF8StringEncoding];
cpath[len] = '\0';
[pool release];
return cpath;
}
else
{
NSApplication *myApplication;
myApplication = [NSApplication sharedApplication];
NSAlert *theAlert = [NSAlert alertWithMessageText: @"Can't find data"
defaultButton: @"Quit"
alternateButton: nil
otherButton: nil
informativeTextWithFormat: @"Please move VDrift to a sane location on your computer, without weird characters in it's path!"];
[theAlert runModal];
[pool release];
exit(1);
}
}