-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileScan.c
More file actions
591 lines (504 loc) · 16.4 KB
/
Copy pathFileScan.c
File metadata and controls
591 lines (504 loc) · 16.4 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
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/*
* SFeditor - Star Fighter 3000 map/mission editor
* Code for building menus of files
* Copyright (C) 2001 Christopher Bazley
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public Licence as published by
* the Free Software Foundation; either version 2 of the Licence, or
* (at your option) any later version.
*
* This program 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 Licence for more details.
*
* You should have received a copy of the GNU General Public Licence
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* "Its not the way that you caress and toy with with my affections..." */
#include "stdlib.h"
#include <string.h>
#include <stdbool.h>
#include <assert.h>
#include <limits.h>
#include "kernel.h"
#include "toolbox.h"
#include "menu.h"
#include "Err.h"
#include "msgtrans.h"
#include "Hourglass.h"
#include "Macros.h"
#include "StrExtra.h"
#include "DirIter.h"
#include "Hourglass.h"
#include "StringBuff.h"
#include "FileScan.h"
#include "FilePaths.h"
#include "Config.h"
#include "Utils.h"
#include "SFInit.h"
#include "Debug.h"
#include "DataType.h"
#ifdef USE_OPTIONAL
#include "Optional.h"
#endif
struct fs_dir_info {
DataType data_type;
_Optional filescan_leafname *leaf_names;
bool rescan_needed;
int scan_no;
};
static struct fs_dir_info knowledge[FS_LAST] =
{
[FS_MISSION_E] = {DataType_Mission, NULL, true, 1},
[FS_MISSION_M] = {DataType_Mission, NULL, true, 1},
[FS_MISSION_H] = {DataType_Mission, NULL, true, 1},
[FS_MISSION_U] = {DataType_Mission, NULL, true, 1},
[FS_BASE_SPRSCAPE] = {DataType_BaseMap, NULL, true, 1},
[FS_BASE_FXDOBJ] = {DataType_BaseObjects, NULL, true, 1},
[FS_BASE_ANIMS] = {DataType_BaseMapAnimations, NULL, true, 1},
[FS_SPRITES] = {DataType_MapTextures, NULL, true, 1},
[FS_GRAPHICS] = {DataType_PolygonMeshes, NULL, true, 1},
[FS_HILL] = {DataType_HillColours, NULL, true, 1},
[FS_PALETTE] = {DataType_PolygonColours, NULL, true, 1},
[FS_SKY] = {DataType_SkyColours, NULL, true, 1},
[FS_PLANETS] = {DataType_SkyImages, NULL, true, 1},
};
enum {
ARRAY_EXTEND_FACTOR = 2,
MIN_EXTEND = 4 /* avoid frequent use of realloc() */
};
typedef struct {
_Optional filescan_leafname *names;
size_t used;
size_t len;
} fs_array;
/* ---------------- Private functions ---------------- */
static void fs_array_init(fs_array *const array)
{
assert(array != NULL);
*array = (fs_array){
.names = NULL,
.used = 0,
.len = 0,
};
}
static bool fs_array_push(fs_array *const array, const filescan_leafname *const name)
{
assert(array != NULL);
assert(array->used <= array->len);
assert(name != NULL);
if (array->used == array->len) {
/* Create/extend output array */
size_t const new_len = HIGHEST(MIN_EXTEND, array->len * ARRAY_EXTEND_FACTOR);
DEBUGF("Extending array from %zu to %zu\n", array->len, new_len);
_Optional void *const new_ptr = realloc(array->names, new_len * sizeof(*array->names));
if (new_ptr == NULL) {
report_error(SFERROR(NoMem), "", "");
return false;
}
array->names = new_ptr;
array->len = new_len;
}
assert(array->used < array->len);
assert(array->names);
if (!array->names) {
return false;
}
array->names[array->used] = *name;
DEBUGF("Have written array entry %zu '%s'%s\n", array->used,
name->leaf_name, name->is_internal ? " (internal)" : "");
array->used++;
return true;
}
static bool fs_array_end(fs_array *const array)
{
assert(array != NULL);
assert(array->used <= array->len);
DEBUGF("Marking end of array of %zu names\n", array->used);
static filescan_leafname const end = {.leaf_name = "", .is_internal = false};
return fs_array_push(array, &end);
}
static void fs_array_destroy(fs_array *const array)
{
assert(array != NULL);
assert(array->used <= array->len);
free(array->names);
}
#ifdef USE_REPORTER
static void print_list(const filescan_leafname *const filenames)
{
for (size_t ptr = 0; *filenames[ptr].leaf_name != '\0'; ptr++)
DEBUG("%s%s", filenames[ptr].leaf_name, filenames[ptr].is_internal ?
" (internal)" : "");
}
#endif
static int fs_file_type(filescan_type const directory)
{
return data_type_to_file_type(filescan_get_data_type(directory));
}
static bool fs_not_empty(char const *const s, filescan_type const directory)
{
/* Returns true if the directory exists and contains one or more files of the
specified type */
if (!file_exists(s)) {
return false;
}
int const file_type = fs_file_type(directory);
DEBUG("Checking for existence of files of type %x in %s", file_type, s);
bool found_object = false;
_Optional DirIterator *iter = NULL;
for (_Optional const _kernel_oserror *e = diriterator_make(&iter, 0, s, NULL);
!E(e) && iter;
e = diriterator_advance(&*iter)) {
DirIteratorObjectInfo info;
int const object_type = diriterator_get_object_info(&*iter, &info);
if (object_type == ObjectType_NotFound) {
break;
}
if (info.file_type != file_type) {
continue;
}
/* Check that filename within length limit */
filescan_leafname tmp;
if (diriterator_get_object_leaf_name(&*iter, tmp.leaf_name,
sizeof(tmp.leaf_name)) >= sizeof(tmp.leaf_name)) {
DEBUGF("%s exceeds the character limit\n", tmp.leaf_name);
continue;
}
found_object = true;
break;
}
diriterator_destroy(iter);
return found_object;
}
static _Optional filescan_leafname *fs_dir(char const *const s,
filescan_type const directory, bool const internal)
{
/* Scan directory and build array of 11-character leafnames */
fs_array scan_results;
fs_array_init(&scan_results);
bool success = false;
if (!file_exists(s)) {
success = true;
} else {
int const file_type = fs_file_type(directory);
hourglass_on();
_Optional DirIterator *iter = NULL;
for (_Optional const _kernel_oserror *e = diriterator_make(&iter, 0, s, NULL);
!E(e) && iter;
e = diriterator_advance(&*iter)) {
DirIteratorObjectInfo info;
int const object_type = diriterator_get_object_info(&*iter, &info);
if (object_type == ObjectType_NotFound) {
success = true;
break;
}
/* Check that file is of correct type */
if (info.file_type == file_type) {
/* Check that filename within length limit */
filescan_leafname tmp = {.is_internal = internal};
if (diriterator_get_object_leaf_name(&*iter, tmp.leaf_name, sizeof(tmp.leaf_name)) >=
sizeof(tmp.leaf_name)) {
DEBUGF("%s exceeds the character limit\n", tmp.leaf_name);
continue;
}
if (!fs_array_push(&scan_results, &tmp)) {
report_error(SFERROR(NoMem), "", "");
break;
}
}
}
diriterator_destroy(iter);
hourglass_off();
}
if (success && !fs_array_end(&scan_results)) {
report_error(SFERROR(NoMem), "", "");
success = false;
}
if (!success) {
fs_array_destroy(&scan_results);
}
return success ? scan_results.names : NULL; /* return finished array */
}
static _Optional filescan_leafname *fs_scanlevelspath(filescan_type const directory)
{
/* Scan the currently configured levels paths, and combine */
_Optional filescan_leafname *combined = NULL;
StringBuffer full_path;
stringbuffer_init(&full_path);
/* construct FULL path for internal game directory to scan */
_Optional filescan_leafname *intern_files = NULL;
char const *const relative_scanpath = filescan_get_directory(directory);
_Optional char *const intern_path = make_file_path_in_dir(Config_get_read_dir(), relative_scanpath);
if (intern_path)
{
intern_files = fs_dir(&*intern_path, directory, true);
free(intern_path);
}
/* do we have an external levels directory configured? */
if (!Config_get_use_extern_levels_dir() || intern_files == NULL)
{
/* Return pointer to results from internal game directory: */
combined = intern_files;
}
else
{
_Optional filescan_leafname *extern_files = NULL;
_Optional char *const extern_path = make_file_path_in_dir(Config_get_extern_levels_dir(),
relative_scanpath);
if (extern_path)
{
extern_files = fs_dir(&*extern_path, directory, false);
free(extern_path);
if (extern_files)
{
/* Combine the internal and external scan results together: */
combined = filescan_combine_filenames(&*extern_files, &*intern_files);
}
}
free(extern_files);
free(intern_files);
}
/* Return pointer to combined results */
return combined;
}
static void fs_cleanup(void)
{
for (filescan_type directory = FS_FIRST; directory < FS_LAST; ++directory)
{
free(knowledge[directory].leaf_names);
}
}
/* ---------------- Public functions ---------------- */
void filescan_init(void)
{
atexit(fs_cleanup);
}
char const *filescan_get_emh_path(filescan_type const directory)
{
char const *sub_dir;
switch (directory) {
case FS_MISSION_E:
sub_dir = E_PATH;
break;
case FS_MISSION_M:
sub_dir = M_PATH;
break;
case FS_MISSION_H:
sub_dir = H_PATH;
break;
default:
assert(directory == FS_MISSION_U);
sub_dir = U_PATH;
break;
}
return sub_dir;
}
DataType filescan_get_data_type(filescan_type const directory)
{
assert(directory >= 0);
assert(directory < ARRAY_SIZE(knowledge));
return knowledge[directory].data_type;
}
char const *filescan_get_directory(filescan_type const directory)
{
char const *sub_dir;
switch (directory)
{
case FS_MISSION_E:
sub_dir = MISSION_E_DIR;
break;
case FS_MISSION_M:
sub_dir = MISSION_M_DIR;
break;
case FS_MISSION_H:
sub_dir = MISSION_H_DIR;
break;
case FS_MISSION_U:
sub_dir = MISSION_U_DIR;
break;
default:
sub_dir = data_type_to_sub_dir(filescan_get_data_type(directory));
break;
}
DEBUG("Path to directory %d is '%s'", directory, sub_dir);
return sub_dir;
}
bool filescan_dir_not_empty(filescan_type const directory)
{
bool not_empty = false;
if (!Config_get_lazydirscan() || knowledge[directory].rescan_needed)
{
/* Rather than scanning the entire directory we simply check for the
presence of one or more files */
char const *const sub_dir = filescan_get_directory(directory);
/* construct FULL path for internal game directory to scan */
_Optional char *const intern_path = make_file_path_in_dir(Config_get_read_dir(), sub_dir);
if (intern_path)
{
/* Scan internal game directory */
not_empty = fs_not_empty(&*intern_path, directory);
free(intern_path);
/* do we have an external levels directory configured? */
if (!not_empty && Config_get_use_extern_levels_dir())
{
/* construct FULL path for external game directory to scan */
_Optional char *const extern_path = make_file_path_in_dir(Config_get_extern_levels_dir(), sub_dir);
if (extern_path)
{
/* Scan external game directory */
not_empty = fs_not_empty(&*extern_path, directory);
free(extern_path);
}
}
}
}
else
{
/* Check cached catalogue of directory contents */
not_empty = knowledge[directory].leaf_names &&
*knowledge[directory].leaf_names[0].leaf_name != '\0';
}
return not_empty;
}
_Optional filescan_leafname *filescan_get_leaf_names(filescan_type const directory,
_Optional int *const vsn)
{
assert(directory >= 0);
assert(directory < ARRAY_SIZE(knowledge));
DEBUG("Filescan received request for catalogue of directory %d", directory);
/* Scan directory? */
if (!Config_get_lazydirscan() || knowledge[directory].rescan_needed)
{
DEBUG("filescan_get_leaf_names about to scan directory %d", directory);
_Optional filescan_leafname *const newfiles = fs_scanlevelspath(directory);
if (newfiles != NULL)
{
free(knowledge[directory].leaf_names);
knowledge[directory].leaf_names = &*newfiles;
knowledge[directory].rescan_needed = false;
if (knowledge[directory].scan_no < INT_MAX)
{
knowledge[directory].scan_no++;
}
else
{
knowledge[directory].scan_no = 1;
}
}
DEBUG("Rescan count for directory %d is now %d", directory,
knowledge[directory].scan_no);
}
if (vsn != NULL)
{
*vsn = knowledge[directory].scan_no;
}
DEBUG("Filescan returning leafnames array %p with version %d",
(void *)knowledge[directory].leaf_names, knowledge[directory].scan_no);
return knowledge[directory].leaf_names;
}
void filescan_directory_updated(filescan_type const directory)
{
assert(directory >= 0);
assert(directory < ARRAY_SIZE(knowledge));
DEBUG("filescan notified that directory %d updated", directory);
knowledge[directory].rescan_needed = true;
}
filescan_type filescan_get_emh_type(char const *const filename)
{
static filescan_type const mission_dirs[] = {
FS_MISSION_E, FS_MISSION_M, FS_MISSION_H, FS_MISSION_U};
for (size_t i = 0; i < ARRAY_SIZE(mission_dirs); ++i)
{
filescan_type const dir = mission_dirs[i];
char const *const emh_path = filescan_get_emh_path(dir);
if (strnicmp(filename, emh_path, strlen(emh_path)) == 0) {
return dir;
}
}
return FS_LAST;
}
_Optional filescan_leafname *filescan_combine_filenames(filescan_leafname *filenames_A, filescan_leafname *filenames_B)
{
/* Combine two arrays of leafnames into one */
DEBUGF("Filescan about to combine leafname arrays %p and %p\n",
(void *)filenames_A, (void *)filenames_B);
fs_array combined_results;
fs_array_init(&combined_results);
size_t A_pos = 0, B_pos = 0;
assert(filenames_A != NULL);
assert(filenames_B != NULL);
bool A_finished = (*filenames_A[A_pos].leaf_name == '\0'),
B_finished = (*filenames_B[B_pos].leaf_name == '\0');
/* loop until we reach the end of BOTH input arrays */
bool success = true;
for (int output_pos = 0;
success && (!A_finished || !B_finished);
++output_pos)
{
NOT_USED(output_pos);
DEBUG_VERBOSEF("A: %s B: %s\n",filenames_A[A_pos].leaf_name,
filenames_B[B_pos].leaf_name);
/* Where we have both A and B waiting, compare them alphabetically */
int A_compared_B = 0;
if (!A_finished && !B_finished)
{
A_compared_B = stricmp(filenames_A[A_pos].leaf_name,
filenames_B[B_pos].leaf_name);
}
filescan_leafname tmp;
if (!A_finished && (B_finished || A_compared_B <= 0))
{
/* It is arbitrarily decided that where A equals B,
we will insert A rather than B into the array */
DEBUG_VERBOSEF("inserting A name into combined array\n");
tmp = filenames_A[A_pos];
if (!B_finished && A_compared_B == 0)
{
/* 'Internal' flag should be set if either file is internal */
if (filenames_B[B_pos].is_internal)
{
tmp.is_internal = true;
}
/* If A and B are equal then we do not insert B, just skip it.
This is to avoid duplication, since A has already been inserted */
DEBUG_VERBOSEF("Ignoring B name (duplicates A name)\n");
// NOLINTNEXTLINE(clang-analyzer-security.ArrayBound)
B_finished = (*filenames_B[++B_pos].leaf_name == '\0');
}
DEBUG_VERBOSEF("Advancing to next A name\n");
// NOLINTNEXTLINE(clang-analyzer-security.ArrayBound)
A_finished = (*filenames_A[++A_pos].leaf_name == '\0');
}
else
{
/* A is finished, or (B not finished and A>B)
Insert current name B into array */
DEBUG_VERBOSEF("Inserting B name into combined array\n");
tmp = filenames_B[B_pos];
DEBUG_VERBOSEF("Advancing to next B name\n");
// NOLINTNEXTLINE(clang-analyzer-security.ArrayBound)
B_finished = (*filenames_B[++B_pos].leaf_name == '\0');
}
success = fs_array_push(&combined_results, &tmp);
}
if (success && !fs_array_end(&combined_results))
{
success = false;
}
if (success)
{
#ifdef USE_REPORTER
print_list(combined_results.names);
#endif
}
else
{
report_error(SFERROR(NoMem), "", "");
fs_array_destroy(&combined_results);
}
return success ? combined_results.names : NULL;
}