-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfs.c
214 lines (167 loc) · 4.37 KB
/
fs.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
#include "fs.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "assets/default-rom.h"
#if defined(_WIN32)
#include <direct.h>
#include <windows.h>
#define SEP "\\"
#define mkdir(dir, mode) _mkdir(dir)
#define chdir(dir) _chdir(dir)
#define getcwd _getcwd
#define strcasecmp _stricmp
#define FILENAME(ent) (ent)->cFileName
#define ISDIR(ent) ((ent)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
#define CLOSEDIR(dir) FindClose(dir)
#define READDIR(dir, ent) (FindNextFileA(dir, ent) ? 1 : 0)
typedef HANDLE OS_DIR;
typedef WIN32_FIND_DATAA OS_DIRENT;
#else
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <libgen.h>
#define SEP "/"
#define FILENAME(ent) (*(ent))->d_name
#define ISDIR(ent) ((*(ent))->d_type == DT_DIR)
#define CLOSEDIR(dir) closedir(dir)
#define READDIR(dir, ent) (*(ent) = readdir(dir), *(ent) ? 1 : 0)
typedef DIR * OS_DIR;
typedef struct dirent * OS_DIRENT;
#endif
static uint32_t fs_crc32(uint8_t *data, size_t n_bytes)
{
uint32_t crc = 0;
uint32_t table[0x100];
for (uint32_t x = 0; x < 0x100; x++) {
uint32_t r = x;
for (uint8_t y = 0; y < 8; y++)
r = (r & 1 ? 0 : 0xEDB88320) ^ r >> 1;
table[x] = r ^ 0xFF000000;
}
for (size_t x = 0; x < n_bytes; x++)
crc = table[(uint8_t) crc ^ data[x]] ^ crc >> 8;
return crc;
}
uint8_t *fs_read(char *file_name, size_t *size)
{
FILE *f = fopen(file_name, "rb");
if (!f)
return NULL;
fseek(f, 0, SEEK_END);
*size = ftell(f);
fseek(f, 0, SEEK_SET);
// leave a '\0' so the buffer can be treated as a string
uint8_t *bytes = calloc(*size + 1, 1);
fread(bytes, *size, 1, f);
fclose(f);
return bytes;
}
void fs_write(char *file_name, uint8_t *bytes, size_t size)
{
FILE *f = fopen(file_name, "wb");
fwrite(bytes, size, 1, f);
fclose(f);
}
uint8_t *fs_load_sram(char *crc32str, size_t *sram_size)
{
char sram_file[30];
snprintf(sram_file, 30, "save%s%s.sav", SEP, crc32str);
return fs_read(sram_file, sram_size);
}
void fs_save_sram(struct nes *nes, char *crc32)
{
size_t sram_len = nes_cart_sram_dirty(nes);
if (sram_len > 0) {
mkdir("save", 0755);
char sav_name[30];
snprintf(sav_name, 30, "save%s%s.sav", SEP, crc32);
uint8_t *sram = calloc(1, sram_len);
nes_cart_sram_get(nes, sram, sram_len);
fs_write(sav_name, sram, sram_len);
free(sram);
}
}
void fs_load_rom(struct nes *nes, char *rom_name, char *crc32)
{
bool should_free = false;
uint8_t *rom = DEFAULT_ROM;
size_t rom_size = sizeof(DEFAULT_ROM);
if (rom_name[0] != '\0') {
should_free = true;
rom_size = 0;
rom = fs_read(rom_name, &rom_size);
if (!rom)
assert(!"Failed to read ROM file");
}
snprintf(crc32, 10, "%08X", fs_crc32(rom + 16, rom_size - 16));
size_t sram_size = 0;
uint8_t *sram = fs_load_sram(crc32, &sram_size);
nes_cart_load(nes, rom, rom_size, sram, sram_size, NULL);
free(sram);
if (should_free)
free(rom);
}
void fs_cwd(char *cwd, int32_t len)
{
getcwd(cwd, len);
}
void fs_path(char *buf, char *path, char *name)
{
char im[MAX_FILE_NAME];
snprintf(im, MAX_FILE_NAME, "%s" SEP "%s", path, name);
snprintf(buf, MAX_FILE_NAME, "%s", im);
}
static int32_t fs_file_compare(const void *p1, const void *p2)
{
struct finfo *fi1 = (struct finfo *) p1;
struct finfo *fi2 = (struct finfo *) p2;
if (fi1->dir && !fi2->dir) {
return -1;
} else if (!fi1->dir && fi2->dir) {
return 1;
} else {
int32_t r = strcasecmp(fi1->name, fi2->name);
if (r != 0)
return r;
return -strcmp(fi1->name, fi2->name);
}
}
uint32_t fs_list(char *path, struct finfo **fi)
{
*fi = NULL;
uint32_t n = 0;
OS_DIRENT ent;
#if defined(_WIN32)
size_t path_wildcard_len = strlen(path) + 3;
char *path_wildcard = calloc(path_wildcard_len, 1);
snprintf(path_wildcard, path_wildcard_len, "%s\\*", path);
OS_DIR dir = FindFirstFileA(path_wildcard, &ent);
bool ok = (dir != INVALID_HANDLE_VALUE);
free(path_wildcard);
#else
bool ok = false;
OS_DIR dir = opendir(path);
if (dir) {
ent = readdir(dir);
ok = ent;
}
#endif
while (ok) {
char *name = FILENAME(&ent);
bool is_dir = ISDIR(&ent);
if (is_dir || strstr(name, ".nes")) {
*fi = realloc(*fi, (n + 1) * sizeof(struct finfo));
snprintf((*fi)[n].name, MAX_FILE_NAME, "%s", name);
(*fi)[n].dir = is_dir;
n++;
}
ok = READDIR(dir, &ent);
if (!ok) CLOSEDIR(dir);
}
if (n > 0)
qsort(*fi, n, sizeof(struct finfo), fs_file_compare);
return n;
}