forked from beefviper/hode-vs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fs_posix.cpp
103 lines (91 loc) · 2.13 KB
/
fs_posix.cpp
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
#include <assert.h>
#include <dirent.h>
#include <sys/stat.h>
#ifndef _MSC_VER
#include <sys/param.h>
#include <sys/unistd.h>
#endif // !_MSC_VER
#include "fs.h"
#include "util.h"
#include "compat.h"
static const char *_suffixes[] = {
"hod.dem",
"setup.dat",
"setup.dax",
".paf",
"_hod.lvl",
"_hod.sss",
"_hod.mst",
0
};
static bool matchGameData(const char *path) {
const int len = strlen(path);
for (int i = 0; _suffixes[i]; ++i) {
const int suffixLen = strlen(_suffixes[i]);
if (len >= suffixLen && strcasecmp(path + len - suffixLen, _suffixes[i]) == 0) {
return true;
}
}
return false;
}
FileSystem::FileSystem(const char *dataPath, const char *savePath)
: _dataPath(dataPath), _savePath(savePath), _filesCount(0), _filesList(0) {
listFiles(dataPath);
}
FileSystem::~FileSystem() {
for (int i = 0; i < _filesCount; ++i) {
free(_filesList[i]);
}
free(_filesList);
}
FILE *FileSystem::openAssetFile(const char *name) {
FILE *fp = 0;
for (int i = 0; i < _filesCount; ++i) {
const char *p = strrchr(_filesList[i], '/');
assert(p);
if (strcasecmp(name, p + 1) == 0) {
fp = fopen(_filesList[i], "rb");
break;
}
}
return fp;
}
FILE *FileSystem::openSaveFile(const char *filename, bool write) {
char path[MAXPATHLEN];
snprintf(path, sizeof(path), "%s/%s", _savePath, filename);
return fopen(path, write ? "wb" : "rb");
}
int FileSystem::closeFile(FILE *fp) {
const int err = ferror(fp);
fclose(fp);
return err;
}
void FileSystem::addFilePath(const char *path) {
_filesList = (char **)realloc(_filesList, (_filesCount + 1) * sizeof(char *));
if (_filesList) {
_filesList[_filesCount] = strdup(path);
++_filesCount;
}
}
void FileSystem::listFiles(const char *dir) {
DIR *d = opendir(dir);
if (d) {
dirent *de;
while ((de = readdir(d)) != NULL) {
if (de->d_name[0] == '.') {
continue;
}
char filePath[MAXPATHLEN];
snprintf(filePath, sizeof(filePath), "%s/%s", dir, de->d_name);
struct stat st;
if (stat(filePath, &st) == 0) {
if (S_ISDIR(st.st_mode)) {
listFiles(filePath);
} else if (matchGameData(filePath)) {
addFilePath(filePath);
}
}
}
closedir(d);
}
}