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
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, 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 License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $URL$
* $Id$
*
*/
// Disable symbol overrides for FILE as that is used in FLAC headers
#define FORBIDDEN_SYMBOL_EXCEPTION_FILE
#include "common/str.h"
#include "common/util.h"
//#include <NDS/ARM9/console.h> //basic print funcionality
#include "backends/fs/ds/ds-fs.h"
#include "backends/fs/stdiostream.h"
#include "dsmain.h"
#include "fat/gba_nds_fat.h"
#include "common/bufferedstream.h"
namespace DS {
//////////////////////////////////////////////////////////////
// DSFileSystemNode - Flash ROM file system using Zip files //
//////////////////////////////////////////////////////////////
ZipFile* DSFileSystemNode::_zipFile = NULL; // FIXME: Avoid non-const global vars
char currentDir[128]; // FIXME: Avoid non-const global vars
bool readPastEndOfFile = false; // FIXME: Avoid non-const global vars
enum {
WRITE_BUFFER_SIZE = 512
};
DSFileSystemNode::DSFileSystemNode() {
_displayName = "ds:/";
_path = "ds:/";
_isValid = true;
_isDirectory = true;
_path = "ds:/";
/* if (!_archive) {
_archive = (GBFS_FILE *) find_first_gbfs_file(scummdata);
if (!_archive) consolePrintf("No GBFS archive found!\n");
}*/
if (!_zipFile) {
_zipFile = new ZipFile();
}
}
DSFileSystemNode::DSFileSystemNode(const Common::String& path) {
// consolePrintf("--%s ",path.c_str());
int lastSlash = 3;
for (int r = 0; r < (int) path.size() - 1; r++) {
if (path[r] == '\\') {
lastSlash = r;
}
}
_displayName = Common::String(path.c_str() + lastSlash + 1);
_path = path;
// _isValid = true;
// _isDirectory = false;
const char *pathStr = path.c_str();
if (path.hasPrefix("ds:/")) {
pathStr += 4;
}
if (*pathStr == '\0') {
_isValid = true;
_isDirectory = true;
return;
}
_zipFile->setAllFilesVisible(true);
if (_zipFile->findFile(pathStr)) {
_isValid = true;
_isDirectory = _zipFile->isDirectory();
} else {
_isValid = false;
_isDirectory = false;
}
_zipFile->setAllFilesVisible(false);
// consolePrintf("%s - Found: %d, Dir: %d\n", pathStr, _isValid, _isDirectory);
}
DSFileSystemNode::DSFileSystemNode(const Common::String& path, bool isDir) {
// consolePrintf("--%s ",path.c_str());
int lastSlash = 3;
for (int r = 0; r < (int) path.size() - 1; r++) {
if (path[r] == '\\') {
lastSlash = r;
}
}
_displayName = Common::String(path.c_str() + lastSlash + 1);
_path = path;
_isValid = true;
_isDirectory = isDir;
// consolePrintf("Found: %d, Dir: %d\n", _isValid, _isDirectory);
}
DSFileSystemNode::DSFileSystemNode(const DSFileSystemNode *node) {
//TODO: not implemented?
}
AbstractFSNode *DSFileSystemNode::getChild(const Common::String& n) const {
if (_path.lastChar() == '\\') {
return new DSFileSystemNode(_path + n);
} else {
return new DSFileSystemNode(_path + "\\" + n);
}
return NULL;
}
bool DSFileSystemNode::getChildren(AbstractFSList &dirList, ListMode mode, bool hidden) const {
// consolePrintf("Listdir\n");
// consolePrintf("Directory\n");
//TODO: honor the hidden flag
// consolePrintf("This dir: %s\n", _path.c_str());
if (_path.hasPrefix("ds:/")) {
if (_path.size() > 4) {
_zipFile->changeDirectory(_path.c_str() + 4);
} else {
_zipFile->changeToRoot();
/* // This is the root dir, so add the RAM folder
DSFileSystemNode *dsfsn = new DSFileSystemNode("ds:/ram");
dsfsn->_isDirectory = true;
dirList->push_back(wrap(dsfsn));
*/
}
} else {
_zipFile->changeDirectory(_path.c_str());
}
if (_zipFile->restartFile()) {
do {
char n[128];
_zipFile->getFileName(n);
// consolePrintf("file: %s\n", n);
if ( (_zipFile->isDirectory() && ((mode == Common::FSNode::kListDirectoriesOnly) || (mode == Common::FSNode::kListAll)) )
|| (!_zipFile->isDirectory() && ((mode == Common::FSNode::kListFilesOnly) || (mode == Common::FSNode::kListAll)) ) )
{
DSFileSystemNode *dsfsn = new DSFileSystemNode("ds:/" + Common::String(n), _zipFile->isDirectory());
dsfsn->_isDirectory = _zipFile->isDirectory();
dirList.push_back((dsfsn));
}
} while (_zipFile->skipFile());
}
return true;
}
AbstractFSNode *DSFileSystemNode::getParent() const {
// consolePrintf("parent\n");
DSFileSystemNode *p;
if (_path != "ds:/") {
const char *path = (const char *)_path.c_str();
int lastSlash = 4;
for (uint r = 4; r < _path.size(); r++) {
if (path[r] == '\\') {
lastSlash = r;
}
}
p = new DSFileSystemNode(Common::String(path, lastSlash));
p->_isDirectory = true;
} else {
p = new DSFileSystemNode();
}
return p;
}
Common::SeekableReadStream *DSFileSystemNode::createReadStream() {
return DSFileStream::makeFromPath(getPath(), false);
}
Common::WriteStream *DSFileSystemNode::createWriteStream() {
Common::WriteStream *stream = DSFileStream::makeFromPath(getPath(), true);
return Common::wrapBufferedWriteStream(stream, WRITE_BUFFER_SIZE);
}
//////////////////////////////////////////////////////////////////////////
// GBAMPFileSystemNode - File system using GBA Movie Player and CF card //
//////////////////////////////////////////////////////////////////////////
GBAMPFileSystemNode::GBAMPFileSystemNode() {
_displayName = "mp:/";
_path = "mp:/";
_isValid = false;
_isDirectory = true;
}
GBAMPFileSystemNode::GBAMPFileSystemNode(const Common::String& path) {
// consolePrintf("'%s'",path.c_str());
int lastSlash = 3;
for (int r = 0; r < (int) path.size() - 1; r++) {
if ((path[r] == '\\') || (path[r] == '/')) {
lastSlash = r;
}
}
if (path == "mp:/") {
// This is the root directory
_isDirectory = true;
_isValid = false; // Old code returned false here, but I'm not sure why
} else {
char check[128];
memset(check, 0, 128);
if (path.size() > 4 && path.hasPrefix("mp:/")) {
// Files which start with mp:/
// Clear the filename to 128 bytes, because a libfat bug occasionally tries to read in this area.
strcpy(check, path.c_str() + 3);
} else {
// Clear the filename to 128 bytes, because a libfat bug occationally tries to read in this area.
strcpy(check, path.c_str());
}
// Remove terminating slash - FileExists fails without this
if (check[strlen(check) - 1] == '/') {
check[strlen(check) - 1] = 0;
}
int fileOrDir = FAT_FileExists(check);
_isDirectory = fileOrDir == FT_DIR;
_isValid = fileOrDir == FT_FILE;
}
// consolePrintf("Path: %s \n", check);
_displayName = Common::String(path.c_str() + lastSlash + 1);
_path = path;
}
GBAMPFileSystemNode::GBAMPFileSystemNode(const Common::String& path, bool isDir) {
//consolePrintf("'%s'",path.c_str());
int lastSlash = 3;
for (int r = 0; r < (int) path.size() - 1; r++) {
if ((path[r] == '\\') || (path[r] == '/')) {
lastSlash = r;
}
}
_displayName = Common::String(path.c_str() + lastSlash + 1);
_path = path;
_isValid = true;
_isDirectory = isDir;
}
GBAMPFileSystemNode::GBAMPFileSystemNode(const GBAMPFileSystemNode *node) {
//TODO: not implemented?
}
AbstractFSNode *GBAMPFileSystemNode::getChild(const Common::String& n) const {
if (_path.lastChar() == '\\') {
return new GBAMPFileSystemNode(_path + n);
} else {
return new GBAMPFileSystemNode(_path + "\\" + n);
}
return NULL;
}
bool GBAMPFileSystemNode::getChildren(AbstractFSList& dirList, ListMode mode, bool hidden) const {
// consolePrintf("Listdir\n");
//TODO: honor the hidden flag
enum { TYPE_NO_MORE = 0, TYPE_FILE = 1, TYPE_DIR = 2 };
char temp[128], fname[256], *path, *pathTemp;
strcpy(temp, _path.c_str());
path = temp + 3;
pathTemp = path;
while (*pathTemp) {
if (*pathTemp == '\\') {
*pathTemp = '/';
}
pathTemp++;
}
// consolePrintf("This dir: %s\n", path);
FAT_chdir(path);
int entryType = FAT_FindFirstFileLFN(fname);
while (entryType != TYPE_NO_MORE) {
if ( ((entryType == TYPE_DIR) && ((mode == Common::FSNode::kListDirectoriesOnly) || (mode == Common::FSNode::kListAll)))
|| ((entryType == TYPE_FILE) && ((mode == Common::FSNode::kListFilesOnly) || (mode == Common::FSNode::kListAll))) ) {
GBAMPFileSystemNode *dsfsn;
//consolePrintf("Fname: %s\n", fname);
if (strcmp(fname, ".") && strcmp(fname, "..")) {
if (!strcmp(path, "/")) {
dsfsn = new GBAMPFileSystemNode("mp:" + Common::String(path) + Common::String(fname), entryType == TYPE_DIR);
} else {
dsfsn = new GBAMPFileSystemNode("mp:" + Common::String(path) + Common::String("/") + Common::String(fname), entryType == TYPE_DIR);
}
// dsfsn->_isDirectory = entryType == DIR;
dirList.push_back((dsfsn));
}
} else {
// consolePrintf("Skipping %s\n", fname);
}
entryType = FAT_FindNextFileLFN(fname);
}
// consolePrintf("No more");
FAT_chdir("/");
return true;
}
AbstractFSNode *GBAMPFileSystemNode::getParent() const {
// consolePrintf("parent\n");
GBAMPFileSystemNode *p;
if (_path != "mp:/") {
const char *path = (const char *)_path.c_str();
int lastSlash = 4;
for (uint r = 4; r < strlen(path); r++) {
if (path[r] == '/') {
lastSlash = r;
}
}
p = new GBAMPFileSystemNode(Common::String(path, lastSlash));
p->_isDirectory = true;
} else {
p = new GBAMPFileSystemNode();
}
return p;
}
Common::SeekableReadStream *GBAMPFileSystemNode::createReadStream() {
// consolePrintf("Opening: %s\n", getPath().c_str());
if (!strncmp(getPath().c_str(), "mp:/", 4)) {
return DSFileStream::makeFromPath(getPath().c_str() + 3, false);
} else {
return DSFileStream::makeFromPath(getPath(), false);
}
}
Common::WriteStream *GBAMPFileSystemNode::createWriteStream() {
Common::WriteStream *stream = DSFileStream::makeFromPath(getPath(), true);
return Common::wrapBufferedWriteStream(stream, WRITE_BUFFER_SIZE);
}
DSFileStream::DSFileStream(void *handle) : _handle(handle) {
assert(handle);
}
DSFileStream::~DSFileStream() {
std_fclose((FILE *)_handle);
}
bool DSFileStream::err() const {
return std_ferror((FILE *)_handle) != 0;
}
void DSFileStream::clearErr() {
std_clearerr((FILE *)_handle);
}
bool DSFileStream::eos() const {
return std_feof((FILE *)_handle) != 0;
}
int32 DSFileStream::pos() const {
return std_ftell((FILE *)_handle);
}
int32 DSFileStream::size() const {
int32 oldPos = std_ftell((FILE *)_handle);
std_fseek((FILE *)_handle, 0, SEEK_END);
int32 length = std_ftell((FILE *)_handle);
std_fseek((FILE *)_handle, oldPos, SEEK_SET);
return length;
}
bool DSFileStream::seek(int32 offs, int whence) {
return std_fseek((FILE *)_handle, offs, whence) == 0;
}
uint32 DSFileStream::read(void *ptr, uint32 len) {
return std_fread(ptr, 1, len, (FILE *)_handle);
}
uint32 DSFileStream::write(const void *ptr, uint32 len) {
return std_fwrite(ptr, 1, len, (FILE *)_handle);
}
bool DSFileStream::flush() {
return std_fflush((FILE *)_handle) == 0;
}
DSFileStream *DSFileStream::makeFromPath(const Common::String &path, bool writeMode) {
FILE *handle = std_fopen(path.c_str(), writeMode ? "wb" : "rb");
if (handle)
return new DSFileStream(handle);
return 0;
}
// Stdio replacements
enum {
MAX_FILE_HANDLES = 32
};
static bool inited = false; // FIXME: Avoid non-const global vars
static DS::fileHandle s_handle[MAX_FILE_HANDLES]; // FIXME: Avoid non-const global vars
FILE *std_fopen(const char *name, const char *mode) {
if (!inited) {
for (int r = 0; r < MAX_FILE_HANDLES; r++) {
s_handle[r].used = false;
}
inited = true;
currentDir[0] = '\0';
}
char realName[MAXPATHLEN];
// Remove file system prefix
if ((name[0] == 'd') && (name[1] == 's') && (name[2] == ':') && (name[3] == '/')) {
strlcpy(realName, name + 4, MAXPATHLEN);
} else if ((name[0] == 'm') && (name[1] == 'p') && (name[2] == ':') && (name[3] == '/')) {
strlcpy(realName, name + 4, MAXPATHLEN);
} else {
strlcpy(realName, name, MAXPATHLEN);
}
// consolePrintf("Open file:");
// consolePrintf("'%s', [%s]", name, realName);
if (DS::isGBAMPAvailable()) {
FAT_chdir("/");
// Turn all back slashes into forward slashes for gba_nds_fat
char *p = realName;
while (*p) {
if (*p == '\\')
*p = '/';
p++;
}
FAT_FILE *result = FAT_fopen(realName, mode);
if (result == 0) {
// consolePrintf("Error code %d\n", result);
//consolePrintf("Opening file %s\n", realName);
} else {
// consolePrintf("Opened file %d\n", result);
}
// MT_memoryReport();
return (FILE *)result;
}
// Fail to open file for writing. It's in ROM!
// Allocate a file handle
int r = 0;
while (s_handle[r].used) {
r++;
assert(r < MAX_FILE_HANDLES);
}
char *data;
ZipFile *zip = DSFileSystemNode::getZip();
if (!zip) {
// consolePrintf("No zip yet!");
return NULL;
}
// Grab the data if it exists
zip->setAllFilesVisible(true);
if (currentDir[0] != 0) {
char nameWithPath[128];
sprintf(nameWithPath, "%s\\%s", currentDir, realName);
strcpy(realName, nameWithPath);
}
// consolePrintf("fopen(%s, %s)\n", realName, name);
if (zip->findFile(realName)) {
data = zip->getFile();
zip->setAllFilesVisible(false);
// Allocate a file handle
r = 0;
while (s_handle[r].used)
r++;
s_handle[r].used = true;
s_handle[r].pos = 0;
s_handle[r].data = data;
s_handle[r].size = zip->getFileSize();
// consolePrintf("Opened file %d: %s (%s) ", r, realName, name);
return &s_handle[r];
} else {
zip->setAllFilesVisible(false);
// consolePrintf("Not found: %s (%s) ", realName, name);
return NULL;
}
}
void std_fclose(FILE *handle) {
if (DS::isGBAMPAvailable()) {
FAT_fclose((FAT_FILE *) handle);
return;
}
handle->used = false;
}
size_t std_fread(void *ptr, size_t size, size_t numItems, FILE *handle) {
// consolePrintf("fread %d,%d %d ", size, numItems, ptr);
if (DS::isGBAMPAvailable()) {
readPastEndOfFile = false;
int bytes = FAT_fread(ptr, size, numItems, (FAT_FILE *) handle);
if (!FAT_feof((FAT_FILE *) handle)) {
return numItems;
} else {
// consolePrintf("Read past end of file: %d read out of %d\n", bytes / size, numItems);
if ((size_t)bytes != size * numItems) readPastEndOfFile = true;
return bytes / size;
}
return numItems;
}
if (handle->pos > handle->size)
numItems = 0;
else if ((int)(handle->pos + size * numItems) > handle->size)
numItems = (handle->size - handle->pos) / size;
// consolePrintf("read %d ", size * numItems);
memcpy(ptr, handle->data + handle->pos, size * numItems);
handle->pos += size * numItems;
return numItems;
}
size_t std_fwrite(const void *ptr, size_t size, size_t numItems, FILE *handle) {
if ((handle == stdin))
return 0;
if ((handle == stderr) || (handle == stdout)) {
#ifndef DISABLE_TEXT_CONSOLE
nocashMessage((char *) ptr);
// consolePrintf((char *) ptr);
#endif
return size;
}
//consolePrintf("fwrite size=%d\n", size * numItems);
if (DS::isGBAMPAvailable()) {
FAT_fwrite(ptr, size, numItems, (FAT_FILE *) handle);
return numItems;
int length = size * numItems;
int pos = 0;
while (pos < length) {
int amount = length > 512? 512: length;
FAT_fwrite(((char *) (ptr)) + pos, 1, amount, (FAT_FILE *) handle);
length -= amount;
pos += amount;
}
return numItems;
}
return 0;
}
bool std_feof(FILE *handle) {
// consolePrintf("feof ");
if (DS::isGBAMPAvailable()) {
return readPastEndOfFile && FAT_feof((FAT_FILE *) handle);
}
// consolePrintf("feof %s", handle->pos >= handle->size? "true": "false");
return handle->pos >= handle->size;
}
int std_fflush(FILE *handle) {
//FIXME: not implemented?
// consolePrintf("fflush ");
return 0;
}
long int std_ftell(FILE *handle) {
if (DS::isGBAMPAvailable()) {
return FAT_ftell((FAT_FILE *) handle);
}
return handle->pos;
}
int std_fseek(FILE *handle, long int offset, int whence) {
// consolePrintf("fseek %d %d ", offset, whence);
if (DS::isGBAMPAvailable()) {
return FAT_fseek((FAT_FILE *) handle, offset, whence);
}
switch (whence) {
case SEEK_CUR:
handle->pos += offset;
break;
case SEEK_SET:
handle->pos = offset;
break;
case SEEK_END:
handle->pos = handle->size + offset;
break;
default:
handle->pos = offset;
break;
}
return 0;
}
int std_ferror(FILE *handle) {
//FIXME: not implemented?
// consolePrintf("ferror ");
return readPastEndOfFile;
}
void std_clearerr(FILE *handle) {
//FIXME: not implemented?
readPastEndOfFile = false;
// consolePrintf("clearerr ");
}
} // namespace DS
|