summaryrefslogtreecommitdiff
path: root/libs/JSystem/src/JKernel/JKRDvdFile.cpp
blob: c56141f90032da1fe30c6b3ada09f2a0c8843c1d (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
#include "JSystem/JSystem.h" // IWYU pragma: keep

#include "JSystem/JKernel/JKRDvdFile.h"
#include "JSystem/JUtility/JUTAssert.h"
#include "JSystem/JUtility/JUTException.h"
#include <stdint.h>

JSUList<JKRDvdFile> JKRDvdFile::sDvdList;

JKRDvdFile::JKRDvdFile() : mDvdLink(this) {
    initiate();
}

JKRDvdFile::JKRDvdFile(const char* name) : mDvdLink(this) {
    initiate();
    mIsAvailable = open(name);
    // weird code. doesn't match without this, maybe remains from assert or something?
    if (!mIsAvailable)
        return;
    else
        return;
}

JKRDvdFile::JKRDvdFile(s32 entryNum) : mDvdLink(this) {
    initiate();
    mIsAvailable = open(entryNum);
    // weird code. doesn't match without this, maybe remains from assert or something?
    if (!mIsAvailable)
        return;
    else
        return;
}

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

void JKRDvdFile::initiate(void) {
    mDvdFile = this;
    OSInitMutex(&mMutex1);
    OSInitMutex(&mMutex2);
    OSInitMessageQueue(&mMessageQueue2, &mMessage2, 1);
    OSInitMessageQueue(&mMessageQueue1, &mMessage1, 1);
    mOSThread = NULL;
    field_0x50 = 0;
    field_0x58 = 0;
}

bool JKRDvdFile::open(const char* name) {
    if (!mIsAvailable) {
        mIsAvailable = DVDOpen(name, &mFileInfo);
        if (mIsAvailable) {
            sDvdList.append(&mDvdLink);
            getStatus();
        }
    }
    return mIsAvailable;
}

bool JKRDvdFile::open(s32 entryNum) {
    if (!mIsAvailable) {
        mIsAvailable = DVDFastOpen(entryNum, &mFileInfo);
        if (mIsAvailable) {
            sDvdList.append(&mDvdLink);
            getStatus();
        }
    }
    return mIsAvailable;
}

void JKRDvdFile::close() {
    if (mIsAvailable) {
        if (DVDClose(&mFileInfo) != 0) {
            mIsAvailable = false;
            sDvdList.remove(&mDvdLink);
        } else {
            JUTException::panic(__FILE__, 213, "cannot close DVD file\n");
        }
    }
}

s32 JKRDvdFile::readData(void* param_1, s32 length, s32 param_3) {
    /* clang-format off */
    // The assert condition gets stringified as "( length & 0x1f ) == 0", 
    // with out disabling clang-format the spaces in the condition will  
    // get removed and the string will be incorrect.
    JUT_ASSERT(238, ( length & 0x1f ) == 0);
    /* clang-format on */

    OSLockMutex(&mMutex1);
    if (mOSThread) {
        OSUnlockMutex(&mMutex1);
        return -1;
    }

    mOSThread = OSGetCurrentThread();

    s32 result = -1;
    if (DVDReadAsyncPrio(&mFileInfo, param_1, length, param_3, JKRDvdFile::doneProcess, 2)) {
        result = sync();
    }

    mOSThread = NULL;
    OSUnlockMutex(&mMutex1);

    return result;
}

s32 JKRDvdFile::writeData(void const* param_0, s32 length, s32 param_2) {
    /* clang-format off */
    JUT_ASSERT(344, ( length & 0x1f ) == 0);
    /* clang-format on */
    return -1;
}

s32 JKRDvdFile::sync(void) {
    OSMessage message;
    OSLockMutex(&mMutex1);
    OSReceiveMessage(&mMessageQueue2, &message, 1);
    mOSThread = NULL;
    OSUnlockMutex(&mMutex1);
    return (intptr_t)message;
}

void JKRDvdFile::doneProcess(s32 id, DVDFileInfo* fileInfo) {
    // fileInfo->field_0x3c looks like some kind of user pointer?
    JKRDvdFile* dvdFile = *(JKRDvdFile**)((u8*)fileInfo + 0x3c);
    OSSendMessage(&dvdFile->mMessageQueue2, (OSMessage)(intptr_t)id, OS_MESSAGE_NOBLOCK);
}

s32 JKRDvdFile::getFileSize(void) const {
    return mFileInfo.length;
}