From 427136bbd6cc68f7d24599f8ed3bafe276c7a459 Mon Sep 17 00:00:00 2001 From: "Jasper St. Pierre" Date: Sun, 24 Sep 2023 13:29:46 -0700 Subject: [PATCH] JUTDirectFile match --- configure.py | 2 +- include/JSystem/JUtility/JUTDirectFile.h | 12 +-- include/dolphin/dvd/dvd.h | 3 + src/JSystem/JUtility/JUTDirectFile.cpp | 123 +++++++++++++++++++++-- 4 files changed, 123 insertions(+), 17 deletions(-) diff --git a/configure.py b/configure.py index 4fac3a52e..631d5fa6f 100644 --- a/configure.py +++ b/configure.py @@ -820,7 +820,7 @@ config.libs = [ Object(Matching, "JSystem/JUtility/JUTFader.cpp"), Object(NonMatching, "JSystem/JUtility/JUTProcBar.cpp"), Object(NonMatching, "JSystem/JUtility/JUTConsole.cpp"), - Object(NonMatching, "JSystem/JUtility/JUTDirectFile.cpp"), + Object(Matching, "JSystem/JUtility/JUTDirectFile.cpp"), Object(NonMatching, "JSystem/JUtility/JUTGba.cpp"), Object(NonMatching, "JSystem/JUtility/JUTFontData_Ascfont_fix12.s"), ], diff --git a/include/JSystem/JUtility/JUTDirectFile.h b/include/JSystem/JUtility/JUTDirectFile.h index 583e7c88c..ec527ac3b 100644 --- a/include/JSystem/JUtility/JUTDirectFile.h +++ b/include/JSystem/JUtility/JUTDirectFile.h @@ -6,12 +6,12 @@ #define JUTDF_BUFSIZE (0x800) struct JUTDirectFile { - /* 802E8730 */ int fetch32byte(); - /* 802E87F8 */ JUTDirectFile(); - /* 802E881C */ ~JUTDirectFile(); - /* 802E8860 */ bool fopen(char const*); - /* 802E88FC */ void fclose(); - /* 802E8958 */ int fgets(void*, int); + int fetch32byte(); + JUTDirectFile(); + ~JUTDirectFile(); + bool fopen(char const*); + void fclose(); + int fgets(void*, int); /* 0x000 */ u8 mBuffer[0x820]; /* 0x820 */ u8* mSectorStart; diff --git a/include/dolphin/dvd/dvd.h b/include/dolphin/dvd/dvd.h index 3c55da4c8..c2998bf5e 100644 --- a/include/dolphin/dvd/dvd.h +++ b/include/dolphin/dvd/dvd.h @@ -109,6 +109,9 @@ typedef struct DVDBB2 { typedef void (*DVDOptionalCommandChecker)(DVDCommandBlock* block, void (*cb)(u32 intType)); +#define DVDGetLength(fi) (fi)->length +#define DVDGetFileInfoStatus(fi) (DVDGetCommandBlockStatus(&(fi)->block)) + void DVDInit(void); BOOL DVDOpen(const char* filename, DVDFileInfo* fileinfo); BOOL DVDClose(DVDFileInfo* fileinfo); diff --git a/src/JSystem/JUtility/JUTDirectFile.cpp b/src/JSystem/JUtility/JUTDirectFile.cpp index 7e120f6a3..f1966e8c5 100644 --- a/src/JSystem/JUtility/JUTDirectFile.cpp +++ b/src/JSystem/JUtility/JUTDirectFile.cpp @@ -4,34 +4,137 @@ // #include "JSystem/JUtility/JUTDirectFile.h" -#include "dolphin/types.h" +#include "dolphin/dvd/dvd.h" +#include "dolphin/os/OS.h" +#include "global.h" /* 802CBAD8-802CBBA0 .text fetch32byte__13JUTDirectFileFv */ -void JUTDirectFile::fetch32byte() { - /* Nonmatching */ +int JUTDirectFile::fetch32byte() { + mToRead = mLength - ALIGN_PREV(mPos, 0x20); + if (mToRead > 0x800) + mToRead = 0x800; + + BOOL interrupt = OSEnableInterrupts(); + BOOL ret = DVDReadAsyncPrio(&mFileInfo, mSectorStart, ALIGN_NEXT(mToRead, 0x20), ALIGN_PREV(mPos, 0x20), NULL, 2); + OSRestoreInterrupts(interrupt); + + if (!ret) + return -1; + + interrupt = OSEnableInterrupts(); + while (true) { + if (!DVDGetFileInfoStatus(&mFileInfo)) + break; + } + OSRestoreInterrupts(interrupt); + return mToRead; } /* 802CBBA0-802CBBC4 .text __ct__13JUTDirectFileFv */ JUTDirectFile::JUTDirectFile() { - /* Nonmatching */ + mLength = 0; + mPos = 0; + mToRead = 0; + mSectorStart = (u8*)ALIGN_NEXT((u32)mBuffer, 0x20); + mIsOpen = false; } /* 802CBBC4-802CBC08 .text __dt__13JUTDirectFileFv */ JUTDirectFile::~JUTDirectFile() { - /* Nonmatching */ + mIsOpen = false; } /* 802CBC08-802CBCA4 .text fopen__13JUTDirectFileFPCc */ -void JUTDirectFile::fopen(const char*) { - /* Nonmatching */ +bool JUTDirectFile::fopen(const char* pFilename) { + if (pFilename == NULL) + return false; + + BOOL interrupt = OSEnableInterrupts(); + BOOL ret = DVDOpen(pFilename, &mFileInfo); + OSRestoreInterrupts(interrupt); + + if (!ret) { + mIsOpen = false; + return false; + } + + interrupt = OSEnableInterrupts(); + mLength = DVDGetLength(&mFileInfo); + OSRestoreInterrupts(interrupt); + mPos = 0; + mIsOpen = true; + return true; } /* 802CBCA4-802CBD00 .text fclose__13JUTDirectFileFv */ void JUTDirectFile::fclose() { - /* Nonmatching */ + if (mIsOpen) { + BOOL interrupt = OSEnableInterrupts(); + DVDClose(&mFileInfo); + OSRestoreInterrupts(interrupt); + mIsOpen = false; + } } /* 802CBD00-802CBEB0 .text fgets__13JUTDirectFileFPvi */ -void JUTDirectFile::fgets(void*, int) { - /* Nonmatching */ +int JUTDirectFile::fgets(void* pBuf, int n) { + if (!mIsOpen) + return -1; + + if (n == 0) + return 0; + if (n == 1) + return 1; + if (pBuf == NULL) + return -1; + if (mPos >= mLength) + return -1; + + u8* pDst = (u8*)pBuf; + s32 size = 0; + + while (mPos < mLength) { + if (mToRead == 0 && fetch32byte() < 0) + return -1; + + u32 readPos = mPos & 0x7FF; + u32 bufLeft = mToRead - readPos; + if ((size + bufLeft) > (n - 1)) + bufLeft = n - size - 1; + + BOOL endLine = FALSE; + for (u32 i = 0; i < bufLeft; i++) { + u8 c = mSectorStart[readPos++]; + *pDst++ = c; + + if (c == '\n') { + endLine = true; + bufLeft = i + 1; + break; + } + } + + if (readPos >= 0x800) + mToRead = 0; + + if (endLine == TRUE) { + *pDst = '\0'; + mPos += bufLeft; + size += bufLeft; + break; + } + + mPos += bufLeft; + size += bufLeft; + + if (size >= (n - 1)) { + *pDst = '\0'; + break; + } + } + + if (mPos >= mLength) + *pDst = '\0'; + + return size; }