-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosx.m
More file actions
143 lines (118 loc) · 3.84 KB
/
Copy pathosx.m
File metadata and controls
143 lines (118 loc) · 3.84 KB
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
/*****************************************************/
/* Ultimate Blocks */
/* Copyright (c) An Ly 2000, Owen Rudge 2001, 2008 */
/*****************************************************/
#import <Foundation/Foundation.h>
#include <string.h>
#include "b4dirs.h"
static NSString *user_dir;
static NSString *game_dir;
static BOOL init_path = NO;
/*! \brief Returns the full path for this file
*
* This function first checks if the file can be found in the user's
* directory. If it can not, it checks the relevant game directory
* (data, music, lib, etc)
*
* \param base The first part of the string, assuming the file can't be
* found in user_dir (eg. "/usr/local/share/kq")
* \param subdir The second part of the string (eg. "maps")
* \param file The filename
* \returns the combined path
*/
static NSString* get_resource_file_path_osx(NSString* base, NSString* subdir, NSString* file, char userdir)
{
NSFileManager* fm = [NSFileManager defaultManager];
NSString* fullpath;
if (file == nil)
{
if (subdir == nil)
fullpath = [NSString pathWithComponents: [NSArray arrayWithObjects: user_dir, nil]];
else
fullpath = [NSString pathWithComponents: [NSArray arrayWithObjects: user_dir, subdir, nil]];
if (userdir == 1)
{
mkdir(fullpath, 0755);
return fullpath;
}
if (![fm fileExistsAtPath: fullpath])
{
if (subdir == nil)
fullpath = [NSString pathWithComponents: [NSArray arrayWithObjects: base, nil]];
else
fullpath = [NSString pathWithComponents: [NSArray arrayWithObjects: base, subdir, nil]];
mkdir(fullpath, 0755);
}
}
else
{
if (subdir == nil)
{
fullpath = [NSString pathWithComponents: [NSArray arrayWithObjects: user_dir, file, nil]];
if (userdir == 1)
return(fullpath);
if (![fm fileExistsAtPath: fullpath])
fullpath = [NSString pathWithComponents: [NSArray arrayWithObjects: base, file, nil]];
}
else
{
fullpath = [NSString pathWithComponents: [NSArray arrayWithObjects: user_dir, subdir, file, nil]];
if (userdir == 1)
return(fullpath);
if (![fm fileExistsAtPath: fullpath])
fullpath = [NSString pathWithComponents: [NSArray arrayWithObjects: base, subdir, file, nil]];
}
}
return fullpath;
}
/*! \brief Return the name of 'significant' directories.
*
* \param dir Enumerated constant for directory type \sa DATA_DIR et al.
* \param file File name below that directory.
* \returns The combined path
*/
const char *find_resource_file (int dir, const char *file)
{
static char ans[PATH_MAX];
NSString* found;
NSString* nsfile;
if (file != NULL)
nsfile = [NSString stringWithCString: file];
else
nsfile = nil;
if (init_path == NO)
{
/* Get home directory */
NSArray* arr = NSSearchPathForDirectoriesInDomains (NSLibraryDirectory, NSUserDomainMask, YES);
user_dir =[[arr objectAtIndex: 0] stringByAppendingPathComponent:@"Ultimate Blocks"];
[user_dir retain];
[[NSFileManager defaultManager] createDirectoryAtPath: user_dir attributes:nil];
/* Now the data directory */
game_dir =[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent: @"Contents/Resources"];
[game_dir retain];
/* NSLog(@"Putting user data in %@, game data in %@\n", user_dir, game_dir);*/
init_path = YES;
}
switch (dir)
{
case APP_DIR:
found = get_resource_file_path_osx(game_dir, nil, nsfile, 0);
break;
case GRAPHICS_DIR:
found = get_resource_file_path_osx(game_dir, @"graphics", nsfile, 0);
break;
case MUSIC_DIR:
found = get_resource_file_path_osx(game_dir, @"music", nsfile, 0);
break;
case MAP_DIR:
found = get_resource_file_path_osx(game_dir, @"maps", nsfile, 0);
break;
case SAVE_DIR:
case SETTINGS_DIR:
found = get_resource_file_path_osx(user_dir, nil, nsfile, 1);
break;
default:
found = nil;
}
return found == nil ? NULL : strncpy (ans,[found cString], sizeof(ans));
}