-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwinfiles.c
792 lines (601 loc) · 14.7 KB
/
winfiles.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
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <shlobj.h>
#include <direct.h>
#include <assert.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include "files.h"
static char *local_dir;
static char *global_dir;
/*
Sets the local and global directories used by the other functions.
Local = ~/.dir, where config and user-installed files are kept.
Global = installdir, where installed data is stored.
*/
int SetGameDirectories(const char *local, const char *global)
{
local_dir = _strdup(local);
global_dir = _strdup(global);
if( GetFileAttributes( local_dir ) == INVALID_FILE_ATTRIBUTES ) {
_mkdir( local_dir );
}
return 0;
}
#define DIR_SEPARATOR "\\"
static char *FixFilename(const char *filename, const char *prefix, int force)
{
char *f, *ptr;
size_t flen;
size_t plen;
plen = strlen(prefix) + 1;
flen = strlen(filename) + plen + 1;
f = (char *)malloc(flen);
strcpy(f, prefix);
strcat(f, DIR_SEPARATOR);
strcat(f, filename);
/* only the filename part needs to be modified */
ptr = &f[plen+1];
while (*ptr) {
if ((*ptr == '/') || (*ptr == '\\') || (*ptr == ':')) {
*ptr = DIR_SEPARATOR[0];
} else if (*ptr == '\r' || *ptr == '\n') {
*ptr = 0;
break;
} else {
if (force) {
*ptr = tolower(*ptr);
}
}
ptr++;
}
return f;
}
/*
Open a file of type type, with mode mode.
Mode can be:
#define FILEMODE_READONLY 0x01
#define FILEMODE_WRITEONLY 0x02
#define FILEMODE_READWRITE 0x04
#define FILEMODE_APPEND 0x08
Type is (mode = ReadOnly):
#define FILETYPE_PERM 0x08 // try the global dir only
#define FILETYPE_OPTIONAL 0x10 // try the global dir first, then try the local dir
#define FILETYPE_CONFIG 0x20 // try the local dir only
Type is (mode = WriteOnly or ReadWrite):
FILETYPE_PERM: error
FILETYPE_OPTIONAL: error
FILETYPE_CONFIG: try the local dir only
*/
FILE *OpenGameFile(const char *filename, int mode, int type)
{
char *rfilename;
char *openmode;
FILE *fp;
if ((type != FILETYPE_CONFIG) && (mode != FILEMODE_READONLY))
return NULL;
switch(mode) {
case FILEMODE_READONLY:
openmode = "rb";
break;
case FILEMODE_WRITEONLY:
openmode = "wb";
break;
case FILEMODE_READWRITE:
openmode = "w+";
break;
case FILEMODE_APPEND:
openmode = "ab";
break;
default:
return NULL;
}
if (type != FILETYPE_CONFIG) {
rfilename = FixFilename(filename, global_dir, 0);
fp = fopen(rfilename, openmode);
free(rfilename);
if (fp != NULL) {
return fp;
}
rfilename = FixFilename(filename, global_dir, 1);
fp = fopen(rfilename, openmode);
free(rfilename);
if (fp != NULL) {
return fp;
}
}
if (type != FILETYPE_PERM) {
rfilename = FixFilename(filename, local_dir, 0);
fp = fopen(rfilename, openmode);
free(rfilename);
if (fp != NULL) {
return fp;
}
rfilename = FixFilename(filename, local_dir, 1);
fp = fopen(rfilename, openmode);
free(rfilename);
return fp;
}
return NULL;
}
int CloseGameFile(FILE *pfd)
{
return fclose(pfd);
}
/*
Get the filesystem attributes of a file
#define FILEATTR_DIRECTORY 0x0100
#define FILEATTR_READABLE 0x0200
#define FILEATTR_WRITABLE 0x0400
Error or can't access it: return value of 0 (What is the game going to do about it anyway?)
*/
static int GetFA(const char *filename)
{
struct stat buf;
int attr;
attr = 0;
if (stat(filename, &buf) == 0) {
if (buf.st_mode & _S_IFDIR) {
attr |= FILEATTR_DIRECTORY;
}
if (buf.st_mode & _S_IREAD) {
attr |= FILEATTR_READABLE;
}
if (buf.st_mode & _S_IWRITE) {
attr |= FILEATTR_WRITABLE;
}
}
return attr;
}
static time_t GetTS(const char *filename)
{
struct stat buf;
if (stat(filename, &buf) == 0) {
return buf.st_mtime;
}
return 0;
}
int GetGameFileAttributes(const char *filename, int type)
{
struct stat buf;
char *rfilename;
int attr;
attr = 0;
if (type != FILETYPE_CONFIG) {
rfilename = FixFilename(filename, global_dir, 0);
if (stat(rfilename, &buf) == 0) {
if (buf.st_mode & _S_IFDIR) {
attr |= FILEATTR_DIRECTORY;
}
if (buf.st_mode & _S_IREAD) {
attr |= FILEATTR_READABLE;
}
if (buf.st_mode & _S_IWRITE) {
attr |= FILEATTR_WRITABLE;
}
free(rfilename);
return attr;
}
free(rfilename);
rfilename = FixFilename(filename, global_dir, 1);
if (stat(rfilename, &buf) == 0) {
if (buf.st_mode & _S_IFDIR) {
attr |= FILEATTR_DIRECTORY;
}
if (buf.st_mode & _S_IREAD) {
attr |= FILEATTR_READABLE;
}
if (buf.st_mode & _S_IWRITE) {
attr |= FILEATTR_WRITABLE;
}
free(rfilename);
return attr;
}
free(rfilename);
}
if (type != FILETYPE_PERM) {
rfilename = FixFilename(filename, local_dir, 0);
if (stat(rfilename, &buf) == 0) {
if (buf.st_mode & _S_IFDIR) {
attr |= FILEATTR_DIRECTORY;
}
if (buf.st_mode & _S_IREAD) {
attr |= FILEATTR_READABLE;
}
if (buf.st_mode & _S_IWRITE) {
attr |= FILEATTR_WRITABLE;
}
free(rfilename);
return attr;
}
free(rfilename);
rfilename = FixFilename(filename, local_dir, 1);
if (stat(rfilename, &buf) == 0) {
if (buf.st_mode & _S_IFDIR) {
attr |= FILEATTR_DIRECTORY;
}
if (buf.st_mode & _S_IREAD) {
attr |= FILEATTR_READABLE;
}
if (buf.st_mode & _S_IWRITE) {
attr |= FILEATTR_WRITABLE;
}
free(rfilename);
return attr;
}
free(rfilename);
}
return 0;
}
/*
Delete a file: local dir only
*/
int DeleteGameFile(const char *filename)
{
char *rfilename;
int ret;
rfilename = FixFilename(filename, local_dir, 0);
ret = _unlink(rfilename);
free(rfilename);
if (ret == -1) {
rfilename = FixFilename(filename, local_dir, 1);
ret = _unlink(rfilename);
free(rfilename);
}
return ret;
}
/*
Create a directory: local dir only
TODO: maybe also mkdir parent directories, if they do not exist?
*/
int CreateGameDirectory(const char *dirname)
{
char *rfilename;
int ret;
rfilename = FixFilename(dirname, local_dir, 0);
ret = _mkdir(rfilename);
free(rfilename);
if (ret == -1) {
rfilename = FixFilename(dirname, local_dir, 1);
ret = _mkdir(rfilename);
free(rfilename);
}
return ret;
}
/* This struct is private. */
typedef struct GameDirectory
{
intptr_t localdir; /* directory opened with _findfirst */
intptr_t globaldir;
struct _finddata_t tmplocalfinddata;
struct _finddata_t localfinddata;
struct _finddata_t tmpglobalfinddata;
struct _finddata_t globalfinddata;
char *localdirname;
char *globaldirname;
GameDirectoryFile tmp; /* Temp space */
} GameDirectory;
/*
"Open" a directory dirname, with type type
Returns a pointer to a directory datatype
Pattern is the pattern to match
*/
void *OpenGameDirectory(const char *dirname, const char *pattern, int type)
{
char* localdirname;
char* globaldirname;
char* filespec;
intptr_t localdir;
intptr_t globaldir;
GameDirectory *gd;
gd = (GameDirectory *)malloc(sizeof(GameDirectory));
memset( gd, 0, sizeof(GameDirectory) );
globaldir = -1;
globaldirname = NULL;
if (type != FILETYPE_CONFIG) {
globaldirname = FixFilename(dirname, global_dir, 0);
filespec = (char*) malloc(strlen(globaldirname)+1+strlen(pattern)+1);
strcpy( filespec, globaldirname );
strcat( filespec, DIR_SEPARATOR );
strcat( filespec, pattern );
globaldir = _findfirst(filespec, &gd->tmpglobalfinddata);
free(filespec);
if (globaldir == -1L) {
free(globaldirname);
globaldirname = FixFilename(dirname, global_dir, 1);
filespec = (char*) malloc(strlen(globaldirname)+1+strlen(pattern)+1);
strcpy( filespec, globaldirname );
strcat( filespec, DIR_SEPARATOR );
strcat( filespec, pattern );
globaldir = _findfirst(filespec, &gd->tmpglobalfinddata);
free(filespec);
if (globaldir == -1L) {
free(globaldirname);
globaldirname = NULL;
}
}
}
localdir = -1;
localdirname = NULL;
if (type != FILETYPE_PERM) {
localdirname = FixFilename(dirname, local_dir, 0);
filespec = (char*) malloc(strlen(localdirname)+1+strlen(pattern)+1);
strcpy( filespec, localdirname );
strcat( filespec, DIR_SEPARATOR );
strcat( filespec, pattern );
localdir = _findfirst(filespec, &gd->tmplocalfinddata);
free(filespec);
if (localdir == -1L) {
free(localdirname);
localdirname = FixFilename(dirname, local_dir, 1);
filespec = (char*) malloc(strlen(localdirname)+1+strlen(pattern)+1);
strcpy( filespec, localdirname );
strcat( filespec, DIR_SEPARATOR );
strcat( filespec, pattern );
localdir = _findfirst(filespec, &gd->tmplocalfinddata);
free( filespec );
if (localdir == -1L) {
free(localdirname);
localdirname = NULL;
}
}
}
if (localdir == -1L && globaldir == -1L) {
free( gd );
return NULL;
}
gd->localdir = localdir;
gd->globaldir = globaldir;
gd->localdirname = localdirname;
gd->globaldirname = globaldirname;
return gd;
}
/*
This struct is public.
typedef struct GameDirectoryFile
{
char *filename;
int attr;
} GameDirectoryFile;
*/
/*
Returns the next match of pattern with the contents of dir
f is the current file
*/
GameDirectoryFile *ScanGameDirectory(void *dir)
{
char *ptr;
GameDirectory *directory;
directory = (GameDirectory *)dir;
if (directory->globaldir != -1L) {
directory->globalfinddata = directory->tmpglobalfinddata;
ptr = FixFilename(directory->globalfinddata.name, directory->globaldirname, 0);
directory->tmp.attr = GetFA(ptr);
directory->tmp.timestamp = GetTS(ptr);
free(ptr);
directory->tmp.filename = &directory->globalfinddata.name[0];
if( _findnext( directory->globaldir, &directory->tmpglobalfinddata ) == -1 ) {
_findclose(directory->globaldir);
free(directory->globaldirname);
directory->globaldir = -1L;
directory->globaldirname = NULL;
}
return &directory->tmp;
}
if (directory->localdir != -1L) {
directory->localfinddata = directory->tmplocalfinddata;
ptr = FixFilename(directory->localfinddata.name, directory->localdirname, 0);
directory->tmp.attr = GetFA(ptr);
directory->tmp.timestamp = GetTS(ptr);
free(ptr);
directory->tmp.filename = &directory->localfinddata.name[0];
if( _findnext( directory->localdir, &directory->tmplocalfinddata ) == -1 ) {
_findclose(directory->localdir);
free(directory->localdirname);
directory->localdir = -1L;
directory->localdirname = NULL;
}
return &directory->tmp;
}
return NULL;
}
/*
Close directory
*/
int CloseGameDirectory(void *dir)
{
GameDirectory *directory = (GameDirectory *)dir;
if (directory != NULL) {
if (directory->localdirname != NULL) {
free(directory->localdirname);
}
if (directory->globaldirname != NULL) {
free(directory->globaldirname);
}
if (directory->localdir != -1L) {
_findclose(directory->localdir);
}
if (directory->globaldir != -1L) {
_findclose(directory->globaldir);
}
return 0;
}
return -1;
}
/*
Game-specific helper function.
*/
static int try_game_directory(const char *dir, const char *file)
{
char tmppath[MAX_PATH];
DWORD retr;
strncpy(tmppath, dir, MAX_PATH-32);
tmppath[MAX_PATH-32] = 0;
strcat(tmppath, file);
retr = GetFileAttributes(tmppath);
if( retr == INVALID_FILE_ATTRIBUTES ) {
return 0;
}
/*
TODO - expand this check to check for read access
*/
return 1;
}
/*
Game-specific helper function.
*/
static int check_game_directory(const char *dir)
{
if (!dir || !*dir) {
return 0;
}
if (!try_game_directory(dir, "\\avp_huds")) {
return 0;
}
if (!try_game_directory(dir, "\\avp_huds\\alien.rif")) {
return 0;
}
if (!try_game_directory(dir, "\\avp_rifs")) {
return 0;
}
if (!try_game_directory(dir, "\\avp_rifs\\temple.rif")) {
return 0;
}
if (!try_game_directory(dir, "\\fastfile")) {
return 0;
}
if (!try_game_directory(dir, "\\fastfile\\ffinfo.txt")) {
return 0;
}
return 1;
}
static char* GetLocalDirectory(void)
{
char folderPath[2 * MAX_PATH + 10];
char* localdir;
const char* homedrive;
const char* homepath;
char* homedir;
homedir = NULL;
/*
TODO - should check that the directory is actually usable.
*/
/*
1. Check registry (not currently implemented)
*/
/*
2. CSIDL_LOCAL_APPDATA with SHGetFolderPath
*/
if( homedir == NULL ) {
if( SUCCEEDED( SHGetFolderPath( NULL, CSIDL_LOCAL_APPDATA,
NULL, SHGFP_TYPE_CURRENT, &folderPath[0] ) ) ) {
homedir = _strdup( folderPath );
}
}
/*
3. CSIDL_APPDATA with SHGetFolderPath
*/
if( homedir == NULL ) {
if( SUCCEEDED( SHGetFolderPath( NULL, CSIDL_APPDATA,
NULL, SHGFP_TYPE_CURRENT, &folderPath[0] ) ) ) {
homedir = _strdup( folderPath );
}
}
/*
4. HOMEDRIVE+HOMEPATH
*/
if( homedir == NULL ) {
homedrive = getenv("HOMEDRIVE");
homepath = getenv("HOMEPATH");
if( homedrive == NULL ) {
homedrive = "";
}
if( homepath != NULL ) {
homedir = (char*)malloc(strlen(homedrive)+strlen(homepath)+1);
strcpy(homedir, homedrive);
strcat(homedir, homepath);
}
}
/*
5. HOME
*/
if( homedir == NULL ) {
homepath = getenv("HOME");
if( homepath != NULL ) {
homedir = _strdup(homepath);
}
}
/*
6. CWD
*/
if( homedir == NULL ) {
homedir = _strdup(".");
}
localdir = (char*)malloc(strlen(homedir) + 10);
strcpy(localdir, homedir);
strcat(localdir, "\\AvPLinux"); // temp name, maybe
free(homedir);
return localdir;
}
static const char* GetGlobalDirectory(const char* argv0)
{
char* gamedir;
char* tmp;
/*
1. $AVP_DATA overrides all
2. Registry Setting
3. executable path from argv[0]
4. current directory
*/
/* 1. $AVP_DATA */
gamedir = getenv("AVP_DATA");
/* $AVP_DATA overrides all, so no check */
/* 2. Registry Setting */
/* TODO */
if (gamedir == NULL) {
/* 3. executable path from argv[0] */
tmp = _strdup(argv0);
if (tmp == NULL) {
/* ... */
fprintf(stderr, "GetGlobalDirectory failure\n");
exit(EXIT_FAILURE);
}
gamedir = strrchr(tmp, DIR_SEPARATOR[0]);
if (gamedir != NULL) {
*gamedir = 0;
gamedir = tmp;
if (!check_game_directory(gamedir)) {
gamedir = NULL;
}
}
}
/* 4. current directory */
return _strdup(".");
}
/*
Game-specific initialization
*/
extern char const *SecondTex_Directory;
extern char const *SecondSoundDir;
void InitGameDirectories(char *argv0)
{
const char* localdir;
const char* globaldir;
SecondTex_Directory = "graphics\\";
SecondSoundDir = "sound\\";
localdir = GetLocalDirectory();
globaldir = GetGlobalDirectory(argv0);
assert(localdir != NULL);
assert(globaldir != NULL);
/* last chance sanity check */
if (!check_game_directory(globaldir)) {
fprintf(stderr, "Unable to find the AvP gamedata.\n");
fprintf(stderr, "The directory last examined was: %s\n", globaldir);
fprintf(stderr, "Has the game been installed and\n");
fprintf(stderr, "are all game files lowercase?\n");
exit(EXIT_FAILURE);
}
SetGameDirectories(localdir, globaldir);
}