summaryrefslogtreecommitdiff
path: root/libs/JSystem/JKernel/JKRFileCache.cpp
blob: 33f231947cf15470142a68ff8231433f64e8aea6 (plain)
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
//
// Generated By: dol2asm
// Translation Unit: JKRFileCache
//

#include "JSystem/JKernel/JKRFileCache.h"
#include "JSystem/JKernel/JKRDvdFile.h"
#include "JSystem/JKernel/JKRFileFinder.h"
#include "JSystem/JKernel/JKRHeap.h"
#include "MSL_C/MSL_Common/Src/ctype.h"
#include "MSL_C/MSL_Common/Src/string.h"
#include "dol2asm.h"
#include "global.h"

//
// Forward References:
//

extern "C" void mount__12JKRFileCacheFPCcP7JKRHeapPCc();
extern "C" void __ct__12JKRFileCacheFPCcPCc();
extern "C" void __dt__12JKRFileCacheFv();
extern "C" void becomeCurrent__12JKRFileCacheFPCc();
extern "C" void getResource__12JKRFileCacheFPCc();
extern "C" void getResource__12JKRFileCacheFUlPCc();
extern "C" void readResource__12JKRFileCacheFPvUlPCc();
extern "C" void readResource__12JKRFileCacheFPvUlUlPCc();
extern "C" void removeResourceAll__12JKRFileCacheFv();
extern "C" void removeResource__12JKRFileCacheFPv();
extern "C" void detachResource__12JKRFileCacheFPv();
extern "C" void getResSize__12JKRFileCacheCFPCv();
extern "C" void countFile__12JKRFileCacheCFPCc();
extern "C" void getFirstFile__12JKRFileCacheCFPCc();
extern "C" void findCacheBlock__12JKRFileCacheCFPCv();
extern "C" void findCacheBlock__12JKRFileCacheCFUl();
extern "C" void findFile__12JKRFileCacheCFPcPCc();
extern "C" void getDvdPathName__12JKRFileCacheCFPCc();
extern "C" void convStrLower__12JKRFileCacheCFPc();
extern "C" void __ct__Q212JKRFileCache11CCacheBlockFUlUlPCv();
extern "C" void getFsResource__12JKRFileCacheFPCc();
extern "C" void getNameResource__12JKRFileCacheFUlPCc();
extern "C" void readFsResource__12JKRFileCacheFPvUlPCc();
extern "C" void readNameResource__12JKRFileCacheFPvUlUlPCc();
extern "C" extern char const* const JKRFileCache__stringBase0;

//
// External References:
//

extern "C" void alloc__7JKRHeapFUliP7JKRHeap();
extern "C" void alloc__7JKRHeapFUli();
extern "C" void free__7JKRHeapFPvP7JKRHeap();
extern "C" void free__7JKRHeapFPv();
extern "C" void findFromRoot__7JKRHeapFPv();
extern "C" void* __nw__FUlP7JKRHeapi();
extern "C" void __dl__FPv();
extern "C" void __ct__13JKRFileLoaderFv();
extern "C" void __dt__13JKRFileLoaderFv();
extern "C" void unmount__13JKRFileLoaderFv();
extern "C" void __ct__12JKRDvdFinderFPCc();
extern "C" void read__7JKRFileFPvll();
extern "C" void __ct__10JKRDvdFileFPCc();
extern "C" void __dt__10JKRDvdFileFv();
extern "C" void __ct__10JSUPtrLinkFPv();
extern "C" void __dt__10JSUPtrLinkFv();
extern "C" void __dt__10JSUPtrListFv();
extern "C" void initiate__10JSUPtrListFv();
extern "C" void append__10JSUPtrListFP10JSUPtrLink();
extern "C" void prepend__10JSUPtrListFP10JSUPtrLink();
extern "C" void remove__10JSUPtrListFP10JSUPtrLink();
extern "C" void _savegpr_26();
extern "C" void _savegpr_27();
extern "C" void _savegpr_28();
extern "C" void _savegpr_29();
extern "C" void _restgpr_26();
extern "C" void _restgpr_27();
extern "C" void _restgpr_28();
extern "C" void _restgpr_29();
extern "C" u8 sVolumeList__13JKRFileLoader[12];
extern "C" u8 sSystemHeap__7JKRHeap[4];
extern "C" u8 sCurrentVolume__13JKRFileLoader[4 + 4 /* padding */];

//
// Declarations:
//

/* 802D49B4-802D4AB4 2CF2F4 0100+00 0/0 2/2 0/0 .text mount__12JKRFileCacheFPCcP7JKRHeapPCc */
JKRFileCache* JKRFileCache::mount(const char* path, JKRHeap* heap, const char* param_3) {
    if (!path || *path != '/') {
        return NULL;
    }

    u32 pathLength = strlen(path);
    if (pathLength != 1 && path[pathLength - 1] == '/') {
        return NULL;
    }

    JSUList<JKRFileLoader>& volumeList = getVolumeList();
    JSUListIterator<JKRFileLoader> iterator;
    for (iterator = volumeList.getFirst(); iterator != volumeList.getEnd(); ++iterator) {
        if (iterator->getVolumeType() == 'CASH') {
            JKRFileCache* fileCache = (JKRFileCache*)iterator.getObject();
            if (fileCache->mRootPath) {
                if (strcmp(fileCache->mRootPath, path) == 0) {
                    fileCache->mMountCount++;
                    return fileCache;
                }
            }
        }
    }

    return new (heap, 0) JKRFileCache(path, param_3);
}

/* ############################################################################################## */
/* 8039D158-8039D158 0297B8 0000+00 0/0 0/0 0/0 .rodata          @stringBase0 */
// #pragma push
// #pragma force_active on
// SECTION_DEAD static char const* const stringBase_8039D158 = "/";
// SECTION_DEAD static char const* const stringBase_8039D15A = "dvd";
// /* @stringBase0 padding */
// SECTION_DEAD static char const* const pad_8039D15E = "\0";
// #pragma pop

/* 802D4AB4-802D4C70 2CF3F4 01BC+00 1/1 0/0 0/0 .text            __ct__12JKRFileCacheFPCcPCc */
JKRFileCache::JKRFileCache(const char* path, const char* volume) : mCacheBlockList() {
    mParentHeap = JKRHeap::findFromRoot(this);
    mMountCount = 1;
    mVolumeType = 'CASH';

    u32 pathLength = strlen(path);
    mRootPath = (char*)JKRAllocFromHeap(mParentHeap, pathLength + 1, 1);
    mCurrentPath = (char*)JKRAllocFromSysHeap(pathLength + 2, 1);
    strcpy(mRootPath, path);
    strcpy(mCurrentPath, path);

    if (path[1] != '\0') {
        convStrLower(mRootPath);
        convStrLower(mCurrentPath);
        strcat(mCurrentPath, "/");

        const char* volumePath = volume;
        if (!volume) {
            volumePath = strrchr(mRootPath, '/');
            volumePath++;
        }

        u32 volumeLength = strlen(volumePath) + 1;
        mVolumePath = (char*)JKRAllocFromSysHeap(volumeLength, 0);
        strcpy(mVolumePath, volumePath);
        convStrLower(mVolumePath);
        mVolumeName = mVolumePath;
    } else {
        const char* volumePath = volume;
        if (!volume) {
            volumePath = "dvd";
        }

        u32 volumeLength = strlen(volumePath) + 1;
        mVolumePath = (char*)JKRAllocFromSysHeap(volumeLength, 0);
        strcpy(mVolumePath, volumePath);
        convStrLower(mVolumePath);
        mVolumeName = mVolumePath;
    }

    getVolumeList().prepend(&mFileLoaderLink);
    mIsMounted = true;
}

/* 802D4C70-802D4D44 2CF5B0 00D4+00 1/0 0/0 0/0 .text            __dt__12JKRFileCacheFv */
JKRFileCache::~JKRFileCache() {
    removeResourceAll();
    if (mRootPath)
        JKRFreeToHeap(mParentHeap, mRootPath);
    if (mCurrentPath)
        JKRFreeToSysHeap(mCurrentPath);
    if (mVolumePath)
        JKRFreeToSysHeap(mVolumePath);

    getVolumeList().remove(&mFileLoaderLink);
}

/* 802D4D44-802D4DD8 2CF684 0094+00 1/0 0/0 0/0 .text            becomeCurrent__12JKRFileCacheFPCc
 */
#ifdef NONMATCHING
bool JKRFileCache::becomeCurrent(const char* path) {
    char* name = getDvdPathName(path);
    BOOL result = DVDChangeDir(name);
    bool didChangeDir = (result != 0);
    if (didChangeDir) {
        setCurrentVolume(this);
        JKRFreeToSysHeap(mCurrentPath);

        mCurrentPath = name;
        if (mCurrentPath[1]) {
            strcat(mCurrentPath, "/");
        }
    } else {
        JKRFreeToSysHeap(name);
    }

    return didChangeDir;
}
#else
#pragma push
#pragma optimization_level 0
#pragma optimizewithasm off
asm bool JKRFileCache::becomeCurrent(char const* param_0) {
    nofralloc
#include "asm/JSystem/JKernel/JKRFileCache/becomeCurrent__12JKRFileCacheFPCc.s"
}
#pragma pop
#endif

/* 802D4DD8-802D4EDC 2CF718 0104+00 1/0 0/0 0/0 .text            getResource__12JKRFileCacheFPCc */
void* JKRFileCache::getResource(const char* path) {
    ASSERT(isMounted());

    void* buffer = NULL;
    char* name = getDvdPathName(path);
    JKRDvdFile dvdFile(name);
    if (dvdFile.isAvailable()) {
        CCacheBlock* cacheBlock = findCacheBlock(dvdFile.getFileID());
        if (!cacheBlock) {
            // dvdFile.getFileSize() not inlined
            u32 fileSize = dvdFile.getFileInfo()->length;
            u32 alignedSize = ALIGN_NEXT(fileSize, 0x20);
            buffer = JKRAllocFromHeap(mParentHeap, alignedSize, 0x20);
            if (buffer) {
                dvdFile.read(buffer, alignedSize, 0);

                cacheBlock = new (JKRHeap::getSystemHeap(), 0)
                    CCacheBlock(dvdFile.getFileID(), dvdFile.getFileInfo()->length, buffer);
                mCacheBlockList.append(&cacheBlock->mCacheBlockLink);
            }
        } else {
            cacheBlock->mReferenceCount++;
            buffer = cacheBlock->mMemoryPtr;
        }
    }

    JKRFreeToSysHeap(name);
    return buffer;
}

/* 802D4EDC-802D4F64 2CF81C 0088+00 1/0 0/0 0/0 .text            getResource__12JKRFileCacheFUlPCc
 */
void* JKRFileCache::getResource(u32, const char* path) {
    ASSERT(isMounted());

    char finalPath[256];
    u32 rootLength = strlen(mRootPath);
    char* filePath = finalPath + rootLength;
    strcpy(finalPath, mRootPath);

    bool found = findFile(finalPath, path);
    if (!found)
        return NULL;

    return getResource(filePath);
}

/* 802D4F64-802D503C 2CF8A4 00D8+00 1/0 0/0 0/0 .text readResource__12JKRFileCacheFPvUlPCc */
u32 JKRFileCache::readResource(void* dst, u32 dstLength, const char* path) {
    ASSERT(isMounted());

    char* name = getDvdPathName(path);
    JKRDvdFile dvdFile(name);

    u32 resourceSize = 0;

// !@bug: (maybe?) Infinite Loop: Because dvdFile.isAvailable() is never updated in the loop-body
// will would never exit the loop.
loop:
    if (dvdFile.isAvailable()) {
        // dvdFile.getFileSize() not inlined
        u32 fileSize = dvdFile.getFileInfo()->length;
        resourceSize = ALIGN_NEXT(fileSize, 0x20);
        dstLength = ALIGN_PREV(dstLength, 0x20);
        if (resourceSize > dstLength) {
            resourceSize = dstLength;
        }

        CCacheBlock* cacheBlock = findCacheBlock(dvdFile.getFileID());
        if (!cacheBlock) {
            dvdFile.read(dst, resourceSize, 0);
        } else {
            memcpy(dst, cacheBlock->mMemoryPtr, resourceSize);
        }
        goto loop;
    }

    JKRFreeToSysHeap(name);
    return resourceSize;
}

/* 802D503C-802D50D4 2CF97C 0098+00 1/0 0/0 0/0 .text readResource__12JKRFileCacheFPvUlUlPCc */
u32 JKRFileCache::readResource(void* dst, u32 dstLength, u32, const char* path) {
    ASSERT(isMounted());

    char finalPath[256];
    u32 rootLength = strlen(mRootPath);
    char* filePath = finalPath + rootLength;
    strcpy(finalPath, mRootPath);

    bool found = findFile(finalPath, path);
    if (!found)
        return NULL;

    return readResource(dst, dstLength, filePath);
}

/* 802D50D4-802D5164 2CFA14 0090+00 1/0 0/0 0/0 .text            removeResourceAll__12JKRFileCacheFv
 */
#ifdef NONMATCHING
void JKRFileCache::removeResourceAll(void) {
    ASSERT(isMounted());

    JSUListIterator<CCacheBlock> iterator;
    iterator = mCacheBlockList.getFirst();
    while (iterator != mCacheBlockList.getEnd()) {
        JKRFreeToHeap(mParentHeap, iterator->mMemoryPtr);
        mCacheBlockList.remove(&iterator->mLink);
        JSUListIterator<CCacheBlock> next = iterator++;
        CCacheBlock* cacheBlock = next.getObject();
        delete cacheBlock;
    }
}
#else
#pragma push
#pragma optimization_level 0
#pragma optimizewithasm off
asm void JKRFileCache::removeResourceAll() {
    nofralloc
#include "asm/JSystem/JKernel/JKRFileCache/removeResourceAll__12JKRFileCacheFv.s"
}
#pragma pop
#endif

/* 802D5164-802D51F8 2CFAA4 0094+00 1/0 0/0 0/0 .text            removeResource__12JKRFileCacheFPv
 */
bool JKRFileCache::removeResource(void* resource) {
    ASSERT(isMounted());

    CCacheBlock* cacheBlock = findCacheBlock(resource);
    if (!cacheBlock)
        return false;

    u32 referenceCount = cacheBlock->mReferenceCount - 1;
    cacheBlock->mReferenceCount = referenceCount;
    if (referenceCount == 0) {
        JKRFreeToHeap(mParentHeap, resource);
        mCacheBlockList.remove(&cacheBlock->mCacheBlockLink);
        delete cacheBlock;
    }

    return true;
}

/* 802D51F8-802D526C 2CFB38 0074+00 1/0 0/0 0/0 .text            detachResource__12JKRFileCacheFPv
 */
bool JKRFileCache::detachResource(void* resource) {
    ASSERT(isMounted());

    CCacheBlock* cacheBlock = findCacheBlock(resource);
    if (!cacheBlock)
        return false;

    mCacheBlockList.remove(&cacheBlock->mCacheBlockLink);
    delete cacheBlock;
    return true;
}

/* 802D526C-802D52A0 2CFBAC 0034+00 1/0 0/0 0/0 .text            getResSize__12JKRFileCacheCFPCv */
u32 JKRFileCache::getResSize(const void* resource) const {
    ASSERT(isMounted());

    CCacheBlock* cacheBlock = findCacheBlock(resource);
    if (cacheBlock == NULL) {
        return -1;
    } else {
        return cacheBlock->mFileSize;
    }
}

/* 802D52A0-802D531C 2CFBE0 007C+00 1/0 0/0 0/0 .text            countFile__12JKRFileCacheCFPCc */
u32 JKRFileCache::countFile(const char* path) const {
    ASSERT(isMounted());

    DVDDirectory dir;
    DVDDirectoryEntry dirEntry;

    u32 count = 0;
    char* name = getDvdPathName(path);
    BOOL result = DVDOpenDir(name, &dir);
    if (result != 0) {
        while (result = DVDReadDir(&dir, &dirEntry), result != FALSE) {
            count = count + 1;
        }

        DVDCloseDir(&dir);
    }

    JKRFreeToSysHeap(name);
    return count;
}

/* 802D531C-802D53B8 2CFC5C 009C+00 1/0 0/0 0/0 .text            getFirstFile__12JKRFileCacheCFPCc
 */
JKRFileFinder* JKRFileCache::getFirstFile(const char* path) const {
    char* name = getDvdPathName(path);
    JKRHeap* systemHeap = JKRHeap::getSystemHeap();
    JKRDvdFinder* finder = new (systemHeap, 0) JKRDvdFinder(name);
    JKRFreeToSysHeap(name);

    if (finder->isAvailable() != true) {
        delete finder;
        return NULL;
    }

    return finder;
}

/* 802D53B8-802D53E4 2CFCF8 002C+00 3/3 0/0 0/0 .text            findCacheBlock__12JKRFileCacheCFPCv
 */
JKRFileCache::CCacheBlock* JKRFileCache::findCacheBlock(const void* resource) const {
    JSUListIterator<CCacheBlock> iterator;
    for (iterator = mCacheBlockList.getFirst(); iterator != mCacheBlockList.getEnd(); ++iterator) {
        if (iterator->mMemoryPtr == resource) {
            return iterator.getObject();
        }
    }

    return NULL;
}

/* 802D53E4-802D5410 2CFD24 002C+00 2/2 0/0 0/0 .text            findCacheBlock__12JKRFileCacheCFUl
 */
JKRFileCache::CCacheBlock* JKRFileCache::findCacheBlock(u32 fileID) const {
    JSUListIterator<CCacheBlock> iterator;
    for (iterator = mCacheBlockList.getFirst(); iterator != mCacheBlockList.getEnd(); ++iterator) {
        if (iterator->mFileId == fileID) {
            return iterator.getObject();
        }
    }

    return NULL;
}

/* 802D5410-802D551C 2CFD50 010C+00 2/2 0/0 0/0 .text            findFile__12JKRFileCacheCFPcPCc */
bool JKRFileCache::findFile(char* path, const char* fileName) const {
    DVDDirectory dir;
    DVDDirectoryEntry dirEntry;

    bool result = false;
    u32 pathLength = strlen(path);
    if (DVDOpenDir(path, &dir)) {
        while (DVDReadDir(&dir, &dirEntry)) {
            if (dirEntry.is_directory) {
                char* endOfPath = path + pathLength;
                *endOfPath = '/';
                strcpy(path + pathLength + 1, dirEntry.name);
                result = findFile(path, fileName);
                if (result)
                    break;
                *endOfPath = '\0';
            } else {
                result = (strcmp(fileName, dirEntry.name) == 0);
                if (result) {
                    strcat(path, "/");
                    strcat(path, fileName);
                    break;
                }
            }
        }

        DVDCloseDir(&dir);
    }

    return result;
}

/* 802D551C-802D5624 2CFE5C 0108+00 5/5 0/0 0/0 .text            getDvdPathName__12JKRFileCacheCFPCc
 */
char* JKRFileCache::getDvdPathName(const char* path) const {
    char* newPath;
    if (path[0] == '/') {
        u32 length = strlen(mRootPath) + strlen(path) + 2;
        newPath = (char*)JKRAllocFromSysHeap(length, 1);
        strcpy(newPath, mRootPath);
        if (path[1]) {
            if (mRootPath[1] == 0) {
                strcat(newPath, path + 1);
            } else {
                strcat(newPath, path);
            }
        }
    } else {
        u32 length = strlen(mCurrentPath) + strlen(path) + 2;
        newPath = (char*)JKRAllocFromSysHeap(length, 1);
        strcpy(newPath, mCurrentPath);
        strcat(newPath, path);
    }

    convStrLower(newPath);
    return newPath;
}

/* 802D5624-802D566C 2CFF64 0048+00 2/2 0/0 0/0 .text            convStrLower__12JKRFileCacheCFPc */
void JKRFileCache::convStrLower(char* buffer) const {
    while (*buffer) {
        *buffer++ = tolower(*buffer);
    }
}

/* 802D566C-802D56C8 2CFFAC 005C+00 1/1 0/0 0/0 .text __ct__Q212JKRFileCache11CCacheBlockFUlUlPCv
 */
JKRFileCache::CCacheBlock::CCacheBlock(u32 fileId, u32 fileSize, const void* resource)
    : mCacheBlockLink(this) {
    mReferenceCount = 1;
    mFileId = fileId;
    mFileSize = fileSize;
    mMemoryPtr = (void*)resource;  // todo: don't cast away const
}

/* 802D56C8-802D56F4 2D0008 002C+00 1/0 0/0 0/0 .text            getFsResource__12JKRFileCacheFPCc
 */
void* JKRFileCache::getFsResource(const char* path) {
    return getResource(path);
}

/* 802D56F4-802D5720 2D0034 002C+00 1/0 0/0 0/0 .text getNameResource__12JKRFileCacheFUlPCc */
void* JKRFileCache::getNameResource(u32 type, const char* path) {
    return getResource(type, path);
}

/* 802D5720-802D574C 2D0060 002C+00 1/0 0/0 0/0 .text readFsResource__12JKRFileCacheFPvUlPCc */
u32 JKRFileCache::readFsResource(void* dst, u32 dstLength, const char* path) {
    return readResource(dst, dstLength, path);
}

/* 802D574C-802D5778 2D008C 002C+00 1/0 0/0 0/0 .text readNameResource__12JKRFileCacheFPvUlUlPCc
 */
u32 JKRFileCache::readNameResource(void* dst, u32 dstLength, u32 type, const char* path) {
    return readResource(dst, dstLength, type, path);
}

/* 8039D158-8039D158 0297B8 0000+00 0/0 0/0 0/0 .rodata          @stringBase0 */