This repository has been archived by the owner on Mar 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource.c
251 lines (229 loc) · 5.45 KB
/
resource.c
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
#include <dirent.h>
#include <sys/stat.h>
#include <sys/param.h>
#include "animation.h"
#include "resource.h"
#include "zipfile.h"
static const char *_data_path;
static char **_filenames;
static int _filenames_count;
static void list_filenames(const char *dirname, int dirname_offset) {
DIR *d = opendir(dirname);
if (d) {
struct dirent *de;
while ((de = readdir(d)) != NULL) {
if (de->d_name[0] == '.') {
continue;
}
char path[MAXPATHLEN];
snprintf(path, sizeof(path), "%s/%s", dirname, de->d_name);
struct stat st;
if (stat(path, &st) == 0) {
if (S_ISDIR(st.st_mode)) {
list_filenames(path, dirname_offset);
} else {
_filenames = (char **)realloc(_filenames, (_filenames_count + 1) * sizeof(char *));
if (_filenames) {
_filenames[_filenames_count] = strdup(path + dirname_offset);
++_filenames_count;
}
}
}
}
closedir(d);
}
}
static const char *DIRS[] = { "interface", "movies", 0 };
#define MAX_ZIPFILES 10
static struct {
char *name;
struct zipfile_t *zf;
} _zipfiles[MAX_ZIPFILES];
static void list_datafiles(const char *path) {
DIR *d = opendir(path);
if (d) {
int count = 0;
struct dirent *de;
while ((de = readdir(d)) != NULL) {
if (de->d_name[0] == '.') {
continue;
}
const char *ext = strrchr(de->d_name, '.');
if (ext && strcasecmp(ext + 1, "he") == 0) {
char hepath[MAXPATHLEN];
snprintf(hepath, sizeof(hepath), "%s/%s", path, de->d_name);
struct zipfile_t *zf = Zipfile_Open(hepath);
if (zf) {
assert(count < MAX_ZIPFILES);
_zipfiles[count].name = strdup(de->d_name);
_zipfiles[count].zf = zf;
++count;
}
continue;
}
struct stat st;
if (stat(path, &st) == 0 && S_ISDIR(st.st_mode)) {
char dirpath[MAXPATHLEN];
snprintf(dirpath, sizeof(dirpath), "%s/%s", path, de->d_name);
for (int i = 0; DIRS[i]; ++i) {
if (strcasecmp(de->d_name, DIRS[i]) == 0) {
list_filenames(dirpath, strlen(path) + 1);
break;
}
}
}
}
closedir(d);
fprintf(stdout, "Found %d files\n", _filenames_count);
fprintf(stdout, "Found %d zipfiles\n", count);
}
}
#define MAX_FILES 256
struct file_t {
FILE *fp;
int animation_num;
struct file_t *next_free;
};
static struct file_t _files[MAX_FILES];
static struct file_t *_next_file;
static struct file_t *find_free_file() {
struct file_t *file = _next_file;
if (file) {
_next_file = file->next_free;
file->next_free = 0;
} else {
fprintf(stderr, "find_free_file MAX_FILES\n");
}
return file;
}
static void free_file(struct file_t *file) {
file->next_free = _next_file;
_next_file = file;
}
void Resource_Init() {
const char *path = getenv("DATAPATH");
if (!path) {
path = ".";
}
fprintf(stdout, "Using %s as data files directory\n", path);
_data_path = path;
list_datafiles(path);
_next_file = &_files[0];
for (int i = 0; i < MAX_FILES - 1; ++i) {
_files[i].next_free = &_files[i + 1];
}
}
void Resource_Fini() {
for (int i = 0; i < _filenames_count; ++i) {
free(_filenames[i]);
}
_filenames_count = 0;
free(_filenames);
_filenames = 0;
}
static void fix_path(const char *name, char *buf) {
int sep = 0;
for (; *name; ++name) {
if (*name == '\\' || *name == '/') {
if (sep) {
continue;
}
*buf++ = '/';
sep = 1;
} else {
*buf++ = *name;
sep = 0;
}
}
*buf = 0;
}
static FILE *open_file(const char *original_name) {
char name[MAXPATHLEN];
fix_path(original_name, name);
FILE *fp = 0;
const char *sep = strchr(name, '/');
if (sep) {
const char *filepath = sep + 1;
for (int i = 0; i < MAX_ZIPFILES && _zipfiles[i].name; ++i) {
if (strncasecmp(_zipfiles[i].name, name, sep - name) == 0) {
struct zipentry_t *ze = Zipfile_Find(_zipfiles[i].zf, filepath);
// fprintf(stdout, "%s in zipfile %s, ze %p\n", name, _zipfiles[i].name, (void *)ze);
if (ze && Zipfile_GetEntrySize(_zipfiles[i].zf, ze) != 0) {
fp = Zipfile_OpenEntry(_zipfiles[i].zf, ze);
}
break;
}
}
}
if (!fp) {
/* local */
fp = fopen(name, "rb");
}
if (!fp) {
char buf[MAXPATHLEN];
snprintf(buf, sizeof(buf), "%s/%s", _data_path, name);
fp = fopen(buf, "rb");
/* case insensitive */
if (!fp) {
for (int i = 0; i < _filenames_count; ++i) {
if (strcasecmp(buf + strlen(_data_path) + 1, _filenames[i]) == 0) {
snprintf(buf, sizeof(buf), "%s/%s", _data_path, _filenames[i]);
fp = fopen(buf, "rb");
break;
}
}
}
}
if (!fp) {
fprintf(stderr, "Failed to open '%s'\n", name);
return 0;
}
return fp;
}
int Resource_Exists(const char *name) {
FILE *fp = open_file(name);
if (fp) {
fclose(fp);
return 1;
}
return 0;
}
FILE *Resource_Open(const char *name) {
return open_file(name);
}
int Resource_LoadAnimation(const char *name) {
FILE *fp = open_file(name);
if (!fp) {
return -1;
}
struct file_t *file = find_free_file();
if (!file) {
fclose(fp);
return -1;
}
const int num = file - _files;
file->fp = fp;
const char *ext = strrchr(name, '.');
if (ext) {
file->animation_num = Animation_Load(fp, ext + 1);
// fprintf(stdout, "animation %s num %d asset %d\n", ext, num, file->animation_num);
} else {
file->animation_num = -1;
}
return num;
}
void Resource_FreeAnimation(int num) {
struct file_t *file = &_files[num];
if (file->fp) {
fclose(file->fp);
file->fp = 0;
}
if (!(file->animation_num < 0)) {
Animation_Free(file->animation_num);
file->animation_num = -1;
}
free_file(file);
}
int Resource_GetAnimationIndex(int num) {
return _files[num].animation_num;
}