summaryrefslogtreecommitdiff
path: root/src/egg/core/eggDvdFile.cpp
blob: 2f92ab08faff5b33ecd484e05faa6ecf097fcde7 (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
#include "egg/core/eggDvdFile.h"

namespace EGG {

bool DvdFile::sIsInitialized;
nw4r::ut::List DvdFile::sDvdList;

void DvdFile::initialize() {
    if (!sIsInitialized) {
        nw4r::ut::List_Init(&sDvdList, offsetof(DvdFile, mNode));
        sIsInitialized = true;
    }
}

DvdFile::DvdFile() {
    initiate();
}

DvdFile::~DvdFile() {
    close();
}

void DvdFile::initiate() {
    mFileInfo.dvdFile = this;
    OSInitMutex(&mMutex);
    OSInitMutex(&mMutexUnused);
    OSInitMessageQueue(&mMsgQueue, &mMsg, 1);
    OSInitMessageQueue(&mMsgQueueUnsued, &mMsgUnused, 1);
    mThread = nullptr;
    field_0x38 = 0;
}

bool DvdFile::open(s32 entryId) {
    if (!mIsOpen && entryId != -1) {
        s32 res = DVDFastOpen(entryId, &mFileInfo);
        mIsOpen = res;
        if (mIsOpen) {
            nw4r::ut::List_Append(&sDvdList, this);
            s32 _status = DVDGetCommandBlockStatus(&mFileInfo.block);
        }
    }
    return mIsOpen;
}

bool DvdFile::open(const char *path) {
    return open(DVDConvertPathToEntrynum(path));
}

bool DvdFile::open(const char *path, void *) {
    return open(path);
}

void DvdFile::close() {
    if (mIsOpen && DVDClose(&mFileInfo)) {
        mIsOpen = false;
        nw4r::ut::List_Remove(&sDvdList, this);
    }
}

s32 DvdFile::readData(void *buffer, s32 length, s32 offset) {
    OSLockMutex(&mMutex);
    if (mThread) {
        OSUnlockMutex(&mMutex);
        return -1;
    }

    mThread = OSGetCurrentThread();
    s32 res = -1;

    if (DVDReadAsyncPrio(&mFileInfo, buffer, length, offset, doneProcess, DVD_PRIO_MEDIUM)) {
        res = sync();
    }

    mThread = nullptr;
    OSUnlockMutex(&mMutex);

    return res;
}

s32 DvdFile::writeData(const void *buffer, s32 length, s32 offset) {
    return -1;
}

s32 DvdFile::sync() {
    OSMessage msg;
    OSLockMutex(&mMutex);
    OSReceiveMessage(&mMsgQueue, &msg, OS_MSG_PERSISTENT);
    mThread = nullptr;
    OSUnlockMutex(&mMutex);
    return (s32)(msg);
}

void DvdFile::doneProcess(s32 result, DVDFileInfo *pFileInfo) {
    OSSendMessage(&((FileInfo *)pFileInfo)->dvdFile->mMsgQueue, (OSMessage)result, 0);
}

u32 DvdFile::getFileSize() const {
    return mFileInfo.size;
}

} // namespace EGG