diff options
| author | Elijah Thomas <42302100+elijah-thomas774@users.noreply.github.com> | 2024-05-22 14:10:08 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-22 14:10:08 -0400 |
| commit | db430ccc91fba6c5ef82907f6b59147d64a997a9 (patch) | |
| tree | 3bfacc8806aee5bd3772e62d22c21b02cf87b901 /src | |
| parent | 62e0778390f838cdb92f16b3582e2d76299ae8a5 (diff) | |
| parent | 77f8365ac5feb49236f8b5237eac91cdb1912b2c (diff) | |
Merge pull request #40 from robojumper/m_dvd
m_dvd work
Diffstat (limited to 'src')
| -rw-r--r-- | src/DynamicLink.cpp | 374 | ||||
| -rw-r--r-- | src/d/d_dvd.cpp | 62 | ||||
| -rw-r--r-- | src/d/d_dvd_unk.cpp | 44 | ||||
| -rw-r--r-- | src/d/d_dylink.cpp | 94 | ||||
| -rw-r--r-- | src/d/d_heap.cpp | 2 | ||||
| -rw-r--r-- | src/m/m_dvd.cpp | 650 |
6 files changed, 1225 insertions, 1 deletions
diff --git a/src/DynamicLink.cpp b/src/DynamicLink.cpp new file mode 100644 index 00000000..2c7947c0 --- /dev/null +++ b/src/DynamicLink.cpp @@ -0,0 +1,374 @@ +#include <DynamicLink.h> + +DynamicModuleControlBase *DynamicModuleControlBase::mFirst; +DynamicModuleControlBase *DynamicModuleControlBase::mLast; + +DynamicModuleControlBase::~DynamicModuleControlBase() { + if (mPrev != nullptr) { + mPrev->mNext = mNext; + } + if (mNext != nullptr) { + mNext->mPrev = mPrev; + } + if (mFirst == this) { + mFirst = mNext; + } + if (mLast == this) { + mLast = mPrev; + } + mNext = nullptr; + mPrev = nullptr; +} + +DynamicModuleControlBase::DynamicModuleControlBase() { + mLinkCount = 0; + mDoLinkCount = 0; + mNext = nullptr; + if (mFirst == nullptr) { + mFirst = this; + } + DynamicModuleControlBase *last = mLast; + mPrev = last; + if (last != nullptr) { + last->mNext = this; + } + mLast = this; +} + +BOOL DynamicModuleControlBase::link() { + if (mLinkCount == 0) { + do_load(); + if (!do_link()) { + return false; + } + + if (mDoLinkCount < 0xFFFF) { + mDoLinkCount++; + } + } + + if (mLinkCount < 0xFFFF) { + mLinkCount++; + } + + return true; +} + +BOOL DynamicModuleControlBase::unlink() { + if (mLinkCount != 0) { + if (mLinkCount < 0xFFFF && --mLinkCount == 0) { + do_unlink(); + do_unload(); + } + } + + return true; +} + +BOOL DynamicModuleControlBase::load_async() { + if (mLinkCount == 0) { + return do_load_async(); + } + return true; +} + +const char *DynamicModuleControlBase::getModuleName() const { + return nullptr; +} + +int DynamicModuleControlBase::getModuleSize() const { + return 0; +} + +const char *DynamicModuleControlBase::getModuleTypeString() const { + return "Base"; +} + +void DynamicModuleControlBase::dump() {} + +bool DynamicModuleControlBase::do_load() { + return true; +} + +BOOL DynamicModuleControlBase::do_load_async() { + return true; +} + +bool DynamicModuleControlBase::do_unload() { + return true; +} + +BOOL DynamicModuleControlBase::do_link() { + return true; +} + +bool DynamicModuleControlBase::do_unlink() { + return true; +} + +u32 DynamicModuleControl::sAllocBytes; +mDvd_toMainRam_base_c *DynamicModuleControl::sDvdFile; +EGG::ExpHeap *DynamicModuleControl::sDylinkHeap; +u32 DynamicModuleControl::sCurrentAllocatableSize; +u32 DynamicModuleControl::sTotalFreeSize; +EGG::Archive *DynamicModuleControl::sArchive; + +DynamicModuleControl::DynamicModuleControl(const char *name, EGG::ExpHeap *heap) + : mModule(nullptr), mBss(nullptr), unk_24(0), mName(name), mResourceType(0), unk_33(0), unk_40(0), + mDvdCallbackRequest(0), mHeap(heap), mpRelMapFile() {} + +DynamicModuleControl::~DynamicModuleControl() {} + +const char *DynamicModuleControl::getModuleName() const { + return mName; +} + +void DynamicModuleControl::initialize(EGG::ExpHeap *heap) { + sDylinkHeap = heap; + sCurrentAllocatableSize = heap->getAllocatableSize(4); + sTotalFreeSize = heap->getTotalFreeSize(); +} + +bool DynamicModuleControl::callback(void *arg) { + return static_cast<DynamicModuleControl *>(arg)->do_load(); +} + +void DynamicModuleControl::checkHeapStatus() { + if (mHeap == sDylinkHeap) { + int size = mHeap->getAllocatableSize(4); + if (sCurrentAllocatableSize > size) { + sCurrentAllocatableSize = size; + } + size = mHeap->getTotalFreeSize(); + if (sTotalFreeSize > size) { + sTotalFreeSize = size; + } + } +} + +const char *DynamicModuleControl::sRelsDir = "/rels"; + +bool DynamicModuleControl::do_load() { + char buf[64]; + if (mModule != nullptr) { + return true; + } + if (mHeap == nullptr) { + mHeap = sDylinkHeap; + } + snprintf(buf, sizeof(buf), "%s/%sNP.rel", sRelsDir, mName); + if (mModule == nullptr) { + if (sArchive != nullptr) { + sDvdFile = mDvd_toMainRam_arc_c::createOrFail(sArchive, buf, 1, mHeap); + } else { + sDvdFile = mDvd_toMainRam_normal_c::createOrFail(buf, 1, mHeap); + } + + if (sDvdFile != nullptr) { + sDvdFile->waitUntilDone(); + checkHeapStatus(); + void *ptr = sDvdFile->mDataPtr; + sDvdFile->mDataPtr = nullptr; + mModule = static_cast<OSModuleHeader *>(ptr); + sDvdFile->do_delete(); + sDvdFile = nullptr; + if (mModule != nullptr) { + unk_40 = 0; + mResourceType = 3; + } + } + } + + if (mModule == nullptr) { + return false; + } + if (unk_33 < 0xFF) { + unk_33++; + } + checkHeapStatus(); + return true; +} + +BOOL DynamicModuleControl::do_load_async() { + if (mDvdCallbackRequest == nullptr) { + if (mModule != nullptr) { + return true; + } + mDvdCallbackRequest = mDvd_callback_c::create(callback, this); + } + + if (mDvdCallbackRequest != nullptr && mDvdCallbackRequest->mStatus != 0) { + mDvdCallbackRequest->do_delete(); + mDvdCallbackRequest = nullptr; + return true; + } else { + return false; + } +} + +bool DynamicModuleControl::do_unload() { + if (mModule != nullptr) { + mHeap->free(mModule); + mModule = nullptr; + } + + return true; +} + +void DynamicModuleControl::dump() {} + +BOOL DynamicModuleControl::do_link() { + char mapPath[64]; + + if (mModule != nullptr) { + int alignedFixSize = ROUND_UP(mModule->fixSize, 0x20); + int alignedFixPtr = (int)mModule + alignedFixSize; + int totalSize = EGG::ExpHeap::getSizeForMBlock(mModule); + BOOL result; + if (totalSize == 0) { + void *bss = mHeap->alloc(mModule->bssSize, 0x20); + if (bss == nullptr) { + goto error; + } + mBss = bss; + result = OSLink(&mModule->info, bss); + if (!result) { + goto error; + } + } else { + if (alignedFixSize + mModule->bssSize < totalSize) { + result = OSLinkFixed(&mModule->info, (void *)alignedFixPtr); + if (!result) { + goto error; + } + mHeap->resizeForMBlock(mModule, alignedFixSize + mModule->bssSize); + } else if ((int)mHeap->resizeForMBlock(mModule, alignedFixSize + mModule->bssSize) > 0) { + // TODO resizeForMBlock should maybe return int? + result = OSLinkFixed(&mModule->info, (void *)alignedFixPtr); + if (!result) { + goto error; + } + } else { + void *bss = mHeap->alloc(mModule->bssSize, 0x20); + if (!bss) { + goto error; + } + mBss = bss; + result = OSLinkFixed(&mModule->info, bss); + if (!result) { + goto error; + } + mHeap->resizeForMBlock(mModule, alignedFixSize); + } + } + + sAllocBytes = sAllocBytes + getModuleSize(); + snprintf(mapPath, sizeof(mapPath), "%s/%sNP.map", "/maps", mName); + mpRelMapFile.RegisterOnDvd(mapPath, &mModule->info); + unk_24 = ((UNKWORD(*)())mModule->prolog)(); + checkHeapStatus(); + return true; + } + +error: + unk_33 = 0; + if (mBss != nullptr) { + mHeap->free(mBss); + mBss = nullptr; + } + if (mModule != nullptr) { + mHeap->free(mModule); + mModule = nullptr; + } + return false; +} + +bool DynamicModuleControl::do_unlink() { + ((void (*)())mModule->epilog)(); + mpRelMapFile.Unregister(); + if (!OSUnlink(&mModule->info)) { + return false; + } + sAllocBytes -= getModuleSize(); + if (mBss != nullptr) { + mHeap->free(mBss); + mBss = nullptr; + } + return true; +} + +int DynamicModuleControl::getModuleSize() const { + if (mModule != nullptr) { + int size = EGG::ExpHeap::getSizeForMBlock(mModule); + if (mBss != nullptr) { + size += mModule->bssSize; + } + + return size; + } + return 0; +} + +const char *DynamicModuleControl::getModuleTypeString() const { + static const char *REL_LOAD_TYPES[4] = { + "????", + "MEM", + "ARAM", + "DVD", + }; + return REL_LOAD_TYPES[mResourceType & 3]; +} + +DbMapFile::~DbMapFile() { + if (unk_0) { + Unregister(); + } +} + +namespace nw4r { +namespace db { +// TODO +typedef struct MapFile { +} MapFile; +extern MapFile *MapFile_RegistOnDvd(void *arg, const char *buf, const OSModuleInfo *info); +extern void *MapFile_Unregist(MapFile *); +} // namespace db +} // namespace nw4r + +void DbMapFile::RegisterOnDvd(const char *path, const OSModuleInfo *info) { + if (mDvd::IsExistPath(path)) { + unk_0 = (UNKWORD)nw4r::db::MapFile_RegistOnDvd(((int *)this) + 1, path, info); + } else { + unk_0 = 0xffffffff; + } +} + +void DbMapFile::Unregister() { + if (unk_0 != 0) { + if (unk_0 != 0xffffffff) { + nw4r::db::MapFile_Unregist((nw4r::db::MapFile *)unk_0); + } + unk_0 = 0; + } +} + +extern "C" { + +void ModuleProlog() {} + +void ModuleEpilog() {} + +void ModuleUnresolved() {} + +void ModuleConstructorsX(void (**ptrs)()) { + for (; *ptrs != nullptr; ptrs++) { + (*ptrs)(); + } +} + +void ModuleDestructorsX(void (**ptrs)()) { + for (; *ptrs != nullptr; ptrs++) { + (*ptrs)(); + } +} +} diff --git a/src/d/d_dvd.cpp b/src/d/d_dvd.cpp new file mode 100644 index 00000000..2f7b9cfa --- /dev/null +++ b/src/d/d_dvd.cpp @@ -0,0 +1,62 @@ +#include <d/d_dvd.h> +#include <m/m_dvd.h> + +namespace dDvd { + +/** 800520f0 */ +void create(s32 priority, EGG::Heap *archiveHeap, EGG::Heap *commandHeap, EGG::Heap *threadHeap) { + mDvd::create(priority, archiveHeap, commandHeap, threadHeap); +} + +/** 80052100 */ +loader_c::loader_c() { + mpCommand = nullptr; + mSize = -1; + mpHeap = nullptr; + mpBuffer = nullptr; +} + +/** 80052130 */ +loader_c::~loader_c() {} + +// sprintf2 +extern "C" void fn_8003D650(char *out, const char *fmt, ...); + +/** 80052170 */ +void *loader_c::request(const char *path, u8 mountDirection, EGG::Heap *heap) { + char buf[128]; + + if (mpBuffer != nullptr) { + return mpBuffer; + } + if (mpCommand == 0) { + mSize = -1; + mpHeap = heap != nullptr ? heap : mDvd::getArchiveHeap(); + buf[0] = '\0'; + fn_8003D650(buf, "US%s", path); + if (!mDvd::IsExistPath(buf)) { + fn_8003D650(buf, "%s", path); + } + mpCommand = mDvd_toMainRam_normal_c::create(buf, mountDirection, heap); + } + if (mpCommand != nullptr && mpCommand->mStatus != 0) { + mpBuffer = mpCommand->mDataPtr; + mSize = mpCommand->mAmountRead; + mpCommand->mDataPtr = nullptr; + mpCommand->do_delete(); + mpCommand = nullptr; + return mpBuffer; + } else { + return nullptr; + } +} + +/** 800522a0 */ +void loader_c::remove() { + if (mpHeap != nullptr && mpBuffer != nullptr) { + mpHeap->free(mpBuffer); + mpBuffer = nullptr; + } +} + +} // namespace dDvd diff --git a/src/d/d_dvd_unk.cpp b/src/d/d_dvd_unk.cpp new file mode 100644 index 00000000..b4ab12d4 --- /dev/null +++ b/src/d/d_dvd_unk.cpp @@ -0,0 +1,44 @@ +#include <egg/core/eggHeap.h> +#include <d/d_dvd_unk.h> +#include <nw4r/ut/ut_ResFont.h> + +namespace dDvdUnk { + +/** 805750d0 */ +static unkstruct_c *singleton; + +static nw4r::ut::ResFont *fontPtr; + +/** 80052300 */ +unkstruct_c *unkstruct_c::create(EGG::Heap *heap) { + singleton = new (heap, 0x04) unkstruct_c(); + singleton->init(); +} + +/** 80052340 */ +char unkstruct_c::getUnk() { + return field_0x4; +} + +/** 80052350 */ +void unkstruct_c::draw() { + // EGG:: +} + +/** 800526b0 */ +void unkstruct_c::execute() { + +} + +/** 80052750 */ +void unkstruct_c::init() { + field_0x0 = 0; + field_0x4 = 0; +} + + +void unkstruct_c::createFont(EGG::Heap *heap) { + fontPtr = new (heap, 0x04) nw4r::ut::ResFont(); +} + +} // namespace dDvdUnk diff --git a/src/d/d_dylink.cpp b/src/d/d_dylink.cpp new file mode 100644 index 00000000..3bec3f39 --- /dev/null +++ b/src/d/d_dylink.cpp @@ -0,0 +1,94 @@ +#include <common.h> +#include <DynamicLink.h> +#include <d/d_dylink.h> +#include <m/m_dvd.h> +#include <m/m_heap.h> +// clang-format off +#include <MSL_C/string.h> +// clang-format on + +struct RelNamePtr { + u16 relId; + const char *name; +}; + +static RelNamePtr *pDynamicNameTable; +static int nDynamicNameTable; + +namespace dDyl { + +DynamicModuleControl **pDMC; +int nDMC; +EGG::Heap *cCc_frmHeap; +u32 Initialized; +mDvd_callback_c *DVD; + +// initDylinkHeap +extern "C" int fn_80052E00(int maxRelId, RelNamePtr *dynNameTable, int dynNameTableNum, EGG::Heap *parentHeap) { + cCc_frmHeap = mHeap::createFrmHeap(maxRelId * 0x10 + dynNameTableNum * 0x48, parentHeap, + "ダイナミックリンク制御用ヒープ(dDyl::cCc_frmHeap)", 0x20, 0); + + mHeap _guard(cCc_frmHeap); + + nDMC = maxRelId; + pDMC = new DynamicModuleControl *[maxRelId](); + memset(pDMC, 0, sizeof(void *) * nDMC); + + pDynamicNameTable = dynNameTable; + nDynamicNameTable = dynNameTableNum; + + for (int i = 0; i < nDynamicNameTable; i++) { + RelNamePtr *rel = &pDynamicNameTable[i]; + if (rel->name != nullptr) { + for (int j = 0; j < nDMC; j++) { + DynamicModuleControl **dmc = &pDMC[j]; + if (*dmc != nullptr) { + const char *nm = (*dmc)->getModuleName(); + if (!strcmp(rel->name, nm)) { + pDMC[rel->relId] = pDMC[j]; + break; + } + } + } + if (pDMC[rel->relId] == nullptr) { + pDMC[rel->relId] = new DynamicModuleControl(rel->name, nullptr); + } + } + } + cCc_frmHeap->adjust(); + return 1; +} + +// isLoaded? +extern "C" bool fn_80052FA0(u16 relId) { + return pDMC[relId] != nullptr ? pDMC[relId]->isLinked() : true; +} + +bool dDyl::Unlink(u16 relId) { + DynamicModuleControl *dmc = pDMC[relId]; + if (dmc != nullptr) { + return dmc->unlink(); + } + + return false; +} + +extern "C" int fn_80052FF0(u16 relId) { + if (!Initialized) { + return 0; + } + + DynamicModuleControl *dmc = pDMC[relId]; + if (dmc != nullptr) { + if (dmc->load_async()) { + // what is going on here? + return (bool)dmc->link() + 2; + } else { + return 0; + } + } else { + return 1; + } +} + +} // namespace dDyl diff --git a/src/d/d_heap.cpp b/src/d/d_heap.cpp index a29ba328..16ad0f70 100644 --- a/src/d/d_heap.cpp +++ b/src/d/d_heap.cpp @@ -18,7 +18,7 @@ extern "C" void fn_802DE710(); // TODO TU splits? void dHeapAllocator::onAlloc(EGG::HeapAllocArg *arg) { - if (arg->heap != nullptr) { + if (arg->ptr != nullptr) { return; } if (lbl_80571C58 == 0) { diff --git a/src/m/m_dvd.cpp b/src/m/m_dvd.cpp new file mode 100644 index 00000000..82fce847 --- /dev/null +++ b/src/m/m_dvd.cpp @@ -0,0 +1,650 @@ +#include <egg/core/eggArchive.h> +#include <egg/core/eggDecomp.h> +#include <egg/core/eggDvdFile.h> +#include <egg/core/eggDvdRipper.h> +#include <egg/core/eggHeap.h> +#include <m/m_dvd.h> +#include <m/m_heap.h> +#include <rvl/DVD.h> +// clang-format off +// TODO a define in here stomps another header +#include <MSL_C/string.h> +// clang-format on + +static int ConvertPathToEntrynum(const char *path, u8 *outType); + +namespace mDvd { + +TUncompressInfo_c<EGG::StreamDecompSZS> s_UncompressInfoSZS(3, ".szs"); +TUncompressInfo_c<EGG::StreamDecompLZ> s_UncompressInfoLZ(5, ".LZ"); +TUncompressInfo_c<EGG::StreamDecompLH> s_UncompressInfoLH(7, ".LH"); +TUncompressInfo_c<EGG::StreamDecompLRC> s_UncompressInfoLRC(8, ".LRC"); +TUncompressInfo_c<EGG::StreamDecompRL> s_UncompressInfoRL(4, ".RL"); + +// TODO This the space within which the actual decompressors +// are allocated. +char likely_align[0x7A04]; +char more_empty_space[0x1638]; +char decompressor_alloc_space[0x9930]; + +mDvd_param_c l_param; + +// unofficial +TUncompressInfo_Base_c *decompressorPtrs[1] = { + &s_UncompressInfoLZ, +}; + +u8 g_mountDirection = 1; +int maxChunkSize = 0x10000; + +void *somePtr; +u32 someNumber; +TUncompressInfo_Base_c **compressors_ptr; +TUncompressInfo_Base_c **compressors_last; +// official +OSThread *l_OSThread; +mDvd::MyThread_c *l_MyThread; +EGG::Heap *l_CommandHeap; +EGG::Heap *l_ArchiveHeap; +bool l_IsAutoStreamDecomp; + +/** 802eece0 */ +static void unk_setDecompressorPtrs(TUncompressInfo_Base_c **ptr, TUncompressInfo_Base_c **last) { + compressors_ptr = ptr; + compressors_last = last; +} + +/** 802eecf0 */ +void unk_initDecompressors() { + unk_setDecompressorPtrs(decompressorPtrs, &decompressorPtrs[1]); + somePtr = decompressor_alloc_space; + someNumber = 0x24; +} + +/** 802eed30 */ +static TUncompressInfo_Base_c *findUncompressInfo(u8 type) { + TUncompressInfo_Base_c **ptr = compressors_ptr; + while (ptr != compressors_last) { + if ((*ptr)->mType == type) { + return *ptr; + } + ptr++; + } + return nullptr; +} + +/** 802eed70 */ +static EGG::StreamDecomp *newUncompressObj(u8 type) { + TUncompressInfo_Base_c *factory = findUncompressInfo(type); + if (factory != nullptr) { + return factory->Construct(); + } else { + return nullptr; + } +} + +/** 802eedb0 */ +static void deleteUncompressObj(u8 type) { + TUncompressInfo_Base_c *factory = findUncompressInfo(type); + if (factory != nullptr) { + factory->Destruct(); + } +} + +OSThread *getOSThread() { + return l_OSThread; +} + +EGG::Heap *getArchiveHeap() { + return l_ArchiveHeap; +} + +/** 802eee10 */ +void *MyThread_c::run(mDvd_param_c *dvd) { + dvd->mainLoop(); + return nullptr; +} + +/** 802eee40 */ +MyThread_c::MyThread_c(int priority, EGG::Heap *heap) : EGG::Thread(0x4000, 0, priority, heap) {} + +/** 802eee90 */ +void *MyThread_c::run() { + return run(mDvd_param_c::mInstance); +} + +/** 802eeea0 */ +void create(int priority, EGG::Heap *commandHeap, EGG::Heap *archiveHeap, EGG::Heap *threadHeap) { + l_CommandHeap = commandHeap; + l_ArchiveHeap = archiveHeap; + mDvd::MyThread_c *thread = new (threadHeap, 0x04) mDvd::MyThread_c(priority, threadHeap); + l_MyThread = thread; + l_OSThread = thread->getMyOsThread(); + if (mHeap::g_assertHeap != nullptr) { + thread->setThreadHeap((EGG::Heap *)mHeap::g_assertHeap); + } + OSResumeThread(l_MyThread->getMyOsThread()); +} + +/** 802eef30 */ +static void *loadToMainRAM(int entryNum, char *dst, EGG::Heap *heap, EGG::DvdRipper::EAllocDirection allocDir, s32 offset, + u32 *outAmountRead, u32 *outFileSize, u32 decompressorType) { + void *result; + u32 amountRead = 0; + u32 fileSize = 0; + + if (decompressorType != 0) { + EGG::StreamDecomp *decomp = newUncompressObj(decompressorType); + static EGG::DvdFile DvdFile; + DvdFile.open(entryNum); + + result = EGG::DvdRipper::loadToMainRAMDecomp(&DvdFile, decomp, (u8 *)dst, heap, allocDir, offset, 0, + maxChunkSize, nullptr, nullptr); + + deleteUncompressObj(decompressorType); + u32 size = DvdFile.mFileInfo.size; + DvdFile.close(); + if (result != nullptr) { + u32 b = EGG::ExpHeap::getSizeForMBlock(result); + DCStoreRangeNoSync(result, b); + fileSize = size; + amountRead = size; + } + } else { + result = EGG::DvdRipper::loadToMainRAM(entryNum, (u8 *)dst, heap, allocDir, offset, &amountRead, &fileSize); + } + + if (outAmountRead != nullptr) { + *outAmountRead = amountRead; + } + if (outFileSize != nullptr) { + *outFileSize = fileSize; + } + return result; +} + +int ConvertPathToEntrynum(const char *path) { + return ::ConvertPathToEntrynum(path, nullptr); +} + +u32 IsExistPath(const char *path) { + u32 entry = ConvertPathToEntrynum(path); + return entry != -1; +} + +} // namespace mDvd + +mDvd_param_c *mDvd_param_c::mInstance; + +/** 802ef0d0 */ +mDvd_command_c::~mDvd_command_c() { + waitUntilDone(); +} + +/** 802ef130 */ +void *mDvd_command_c::operator new(size_t size) { + return EGG::Heap::alloc(size, -4, mDvd::l_CommandHeap); +} + +void mDvd_command_c::operator delete(void *ptr) { + EGG::Heap::free(ptr, mDvd::l_CommandHeap); +} + +/** 802ef150 */ +void mDvd_command_c::done() { + doClear(); +} + +/** 802ef170 */ +void mDvd_command_c::doClear() {} + +void mDvd_command_c::waitDone() { + mStatus = 1; + OSWakeupThread(&mDvd_param_c::mInstance->mThreadQueue2); +} + +/** 802ef190 */ +void mDvd_command_c::waitUntilDone() { + while (mStatus == 0) { + OSSleepThread(&mDvd_param_c::mInstance->mThreadQueue2); + } +} + +/** 802ef1e0 */ +mDvd_param_c::mDvd_param_c() { + mFirstRequest = nullptr; + mLastRequest = nullptr; + OSInitMutex(&mMutex); + OSInitThreadQueue(&mThreadQueue1); + OSInitThreadQueue(&mThreadQueue2); + mDvd_param_c::mInstance = this; +} + +/** 802ef230 */ +void mDvd_param_c::kick() { + OSWakeupThread(&mThreadQueue1); +} + +/** 802ef240 */ +void mDvd_param_c::waitForKick() { + OSSleepThread(&mThreadQueue1); +} + +/** 802ef250 */ +mDvd_command_c *mDvd_param_c::getFirstCommand() { + return mFirstRequest; +} + +/** 802ef260 */ +void mDvd_param_c::addCommand(mDvd_command_c *cmd) { + OSThread *dvdThread = mDvd::getOSThread(); + OSThread *currThread = OSGetCurrentThread(); + if (currThread == dvdThread) { + cmd->execute(); + } else { + OSLockMutex(&mMutex); + cmd->mNext = nullptr; + if (mLastRequest == nullptr) { + mLastRequest = cmd; + mFirstRequest = cmd; + } else { + mLastRequest->mNext = cmd; + mLastRequest = cmd; + } + OSUnlockMutex(&mMutex); + kick(); + } +} + +/** 802ef310 */ +void mDvd_param_c::removeCommand(mDvd_command_c *toRemove) { + OSLockMutex(&mMutex); + mDvd_command_c *prev = nullptr; + + for (mDvd_command_c *cmd = mFirstRequest; cmd != nullptr; cmd = cmd->mNext) { + if (cmd == toRemove) { + if (prev != nullptr) { + prev->mNext = cmd->mNext; + if (mLastRequest == cmd) { + mLastRequest = prev; + } + } else { + if (mFirstRequest == mLastRequest) { + mFirstRequest = nullptr; + mLastRequest = nullptr; + } else { + mFirstRequest = cmd->mNext; + } + } + } + + prev = cmd; + } + + OSUnlockMutex(&mMutex); + kick(); +} + +u32 mDvd_param_c::executeCB(mDvd_command_c **cmd) { + (*cmd)->execute(); + return 0; +} + +/** 802ef410 */ +void mDvd_param_c::mainLoop() { + mDvd_command_c *cmd; + while (true) { + waitForKick(); + while ((cmd = getFirstCommand()) != nullptr) { + removeCommand(cmd); + executeCB(&cmd); + } + } +} + +/** 802ef460 */ +mDvd_command_c::mDvd_command_c() { + mNext = nullptr; + mStatus = 0; +} + +/** 802ef480 */ +void mDvd_command_c::do_delete() { + delete this; +} + +/** 802ef4a0 */ +void mDvd_command_c::destroy(mDvd_command_c **cmd) { + // TODO fake match, this looks like an inlined dtor + if (cmd != nullptr && cmd != nullptr && *cmd != nullptr) { + (*cmd)->done(); + (*cmd)->do_delete(); + *cmd = nullptr; + } +} + +/** 802ef500 */ +mDvd_callback_c::~mDvd_callback_c() {} + +/** 802ef560 */ +mDvd_callback_c::mDvd_callback_c(dvdReadCallback cb, void *cbData) { + mCallback = cb; + mCallbackData = cbData; + mSuccess = false; +} + +/* 802ef5d0 */ +mDvd_callback_c *mDvd_callback_c::create(dvdReadCallback cb, void *cbData) { + mDvd_callback_c *cmd = new mDvd_callback_c(cb, cbData); + if (cmd != nullptr) { + mDvd_param_c::mInstance->addCommand(cmd); + } + return cmd; +} + +/** 802ef650 */ +extern "C" mDvd_callback_c *fn_802EF650(dvdReadCallback cb, void *cbData) { + mDvd_callback_c *cmd = mDvd_callback_c::create(cb, cbData); + while (!cmd) {} + return cmd; +} + +/** 802ef680 */ +u32 mDvd_callback_c::execute() { + mSuccess = (mCallback)(mCallbackData); + waitDone(); + return (bool)mSuccess; +} + +/** 802ef6e0 */ +mDvd_mountMemArchive_c::~mDvd_mountMemArchive_c() {} + +/** 802ef740 */ +mDvd_mountMemArchive_c::mDvd_mountMemArchive_c(int mountDirection) { + if (mountDirection == 0) { + mountDirection = mDvd::g_mountDirection; + } + mMountDirection = mountDirection; + mCompressionType = 0; + mEntryNum = -1; + mDataPtr = nullptr; + mHeap = nullptr; + mAmountRead = 0; +} + +/** 802ef7c0 */ +int findPathWithCompressedExtension(const char *name, u8 *outType) { + u8 type; + char buf[256]; + int num; + char *end; + int size; + + type = 0; + buf[255] = '\0'; + num = -1; + + strncpy(buf, name, sizeof(buf)); + if (buf[255] == '\0') { + end = buf + strlen(buf); + size = sizeof(buf) - (end - buf); + // Append the compressor extension and try to find a compressed version + for (mDvd::TUncompressInfo_Base_c **ptr = mDvd::compressors_ptr; ptr != mDvd::compressors_last; ptr++) { + strncpy(end, (*ptr)->mExtension, size); + num = DVDConvertPathToEntrynum(buf); + if (num != -1) { + type = (*ptr)->mType; + goto out; + } + } + *end = '\0'; + // No compressed version found + end = strrchr(buf, '/'); + if (end == nullptr) { + end = buf; + } + end = strrchr(end, '.'); + if (end != nullptr) { + size = sizeof(buf) - (end - buf); + // Append the compressor extension and try to find a compressed version + for (mDvd::TUncompressInfo_Base_c **ptr = mDvd::compressors_ptr; ptr != mDvd::compressors_last; ptr++) { + strncpy(end, (*ptr)->mExtension, size); + num = DVDConvertPathToEntrynum(buf); + if (num != -1) { + type = (*ptr)->mType; + goto out; + } + } + *end = '\0'; + } + } + +out: + if (num != -1 && outType != nullptr) { + *outType = type; + } + return num; +} + +/** 802ef930 */ +void setAutoStreamDecomp(bool arg) { + mDvd::l_IsAutoStreamDecomp = arg; +} + +/** 802ef940 */ +bool getAutoStreamDecomp() { + return mDvd::l_IsAutoStreamDecomp; +} + +/** 802ef950 */ +static int ConvertPathToEntrynum(const char *path, u8 *outType) { + int num = DVDConvertPathToEntrynum(path); + if (num != -1) { + if (outType != nullptr) { + *outType = 0; + } + } else if (getAutoStreamDecomp()) { + num = findPathWithCompressedExtension(path, outType); + } + return num; +} + +/** 802ef9d0 */ +static int ConvertPathToEntrynum_Thunk(const char *path, u8 *outType) { + return ConvertPathToEntrynum(path, outType); +} + +/** 802ef9e0 */ +mDvd_mountMemArchive_c *mDvd_mountMemArchive_c::create(const char *path, u8 mountDirection, EGG::Heap *heap) { + u8 type; + mDvd_mountMemArchive_c *cmd = nullptr; + int entryNum = ConvertPathToEntrynum_Thunk(path, &type); + if (entryNum != -1) { + cmd = new mDvd_mountMemArchive_c(mountDirection); + if (cmd != nullptr) { + cmd->mEntryNum = entryNum; + cmd->mHeap = heap; + cmd->mCompressionType = type; + mDvd_param_c::mInstance->addCommand(cmd); + } + } + + return cmd; +} + +/** 802efa80 */ +u32 mDvd_mountMemArchive_c::execute() { + EGG::DvdRipper::EAllocDirection allocDirection; + EGG::Archive *archive; + void *data; + EGG::Heap *heap; + + heap = mHeap != nullptr ? mHeap : mDvd::l_ArchiveHeap; + archive = nullptr; + + allocDirection = mMountDirection == 1 ? EGG::DvdRipper::ALLOC_DIR_TOP : EGG::DvdRipper::ALLOC_DIR_BOTTOM; + data = mDvd::loadToMainRAM(mEntryNum, nullptr, heap, allocDirection, 0, &mAmountRead, 0, mCompressionType); + if (data != nullptr) { + archive = EGG::Archive::mount(data, heap, mMountDirection == 1 ? 4 : -4); + } + + if (archive != nullptr) { + mDataPtr = archive; + } else { + if (data != nullptr) { + delete data; + } + mAmountRead = 0; + } + waitDone(); + return (bool)mDataPtr; +} + +/** 802efb80 */ +void *mDvd_mountMemArchive_c::getArcBinary() { + void *result = nullptr; + if (mDataPtr != nullptr) { + result = mDataPtr->mHandle.header; + } + return result; +} + +/** 802efba0 */ +void mDvd_mountMemArchive_c::doClear() { + if (mDataPtr != nullptr) { + void *data = mDataPtr->mHandle.header; + mDataPtr->unmount(); + mDataPtr = nullptr; + if (data != nullptr) { + EGG::Heap::free(data, nullptr); + } + } +} + +/** 802efc10 */ +mDvd_toMainRam_base_c::mDvd_toMainRam_base_c(int mountDirection) { + if (mountDirection == 0) { + mountDirection = mDvd::g_mountDirection; + } + mMountDirection = mountDirection; + mDataPtr = nullptr; + mAmountRead = 0; + mFileSize = 0; + mHeap = nullptr; +} + +/** 802efc80 */ +mDvd_toMainRam_base_c::~mDvd_toMainRam_base_c() {} + +/** 802efce0 */ +mDvd_toMainRam_arc_c::~mDvd_toMainRam_arc_c() {} + +/** 802efd40 */ +mDvd_toMainRam_arc_c::mDvd_toMainRam_arc_c(EGG::Archive *arc, int entryNum, int mountDirection) + : mDvd_toMainRam_base_c(mountDirection), mArcPtr(arc) { + mEntryNum = entryNum; +} + +/** 802efda0 */ +mDvd_toMainRam_arc_c *mDvd_toMainRam_arc_c::makeRequest(EGG::Archive *arc, int entryNum, int mountDirection, + EGG::Heap *heap) { + mDvd_toMainRam_arc_c *cmd = new mDvd_toMainRam_arc_c(arc, entryNum, mountDirection); + if (cmd != nullptr) { + cmd->mHeap = heap; + mDvd_param_c::mInstance->addCommand(cmd); + } + return cmd; +} + +/** 802efe20 */ +mDvd_toMainRam_arc_c *mDvd_toMainRam_arc_c::create(EGG::Archive *arc, const char *path, int mountDirection, + EGG::Heap *heap) { + int entryNum = arc->convertPathToEntryID(path); + mDvd_toMainRam_arc_c *cmd = nullptr; + if (entryNum != -1) { + cmd = mDvd_toMainRam_arc_c::makeRequest(arc, entryNum, mountDirection, heap); + } + return cmd; +} + +/** 802efe90 */ +mDvd_toMainRam_arc_c *mDvd_toMainRam_arc_c::createOrFail(EGG::Archive *arc, const char *path, int mountDirection, EGG::Heap *heap) { + mDvd_toMainRam_arc_c *cmd = mDvd_toMainRam_arc_c::create(arc, path, mountDirection, heap); + while (!cmd) {} + return cmd; +} + +/** 802efec0 */ +u32 mDvd_toMainRam_arc_c::execute() { + EGG::Heap *heap = mHeap != nullptr ? mHeap : mDvd::l_ArchiveHeap; + mDataPtr = mArcPtr->getFileFast(mEntryNum, heap, mMountDirection == 1 ? 0x20 : -0x20); + waitDone(); + return (bool)mDataPtr; +} + +/** 802eff40 */ +mDvd_toMainRam_normal_c::mDvd_toMainRam_normal_c(int mountDirection) : mDvd_toMainRam_base_c(mountDirection) { + mCompressionType2 = 0; + mEntryNum = -1; +} + +/** 802eff90 */ +mDvd_toMainRam_normal_c *mDvd_toMainRam_normal_c::create(const char *path, int mountDirection, EGG::Heap *heap) { + u8 type; + int entryNum = ConvertPathToEntrynum_Thunk(path, &type); + mDvd_toMainRam_normal_c *cmd; + if (entryNum == -1) { + cmd = nullptr; + } else { + cmd = new mDvd_toMainRam_normal_c(mountDirection); + if (cmd != nullptr) { + cmd->mEntryNum = entryNum; + cmd->mHeap = heap; + cmd->mCompressionType2 = type; + mDvd_param_c::mInstance->addCommand(cmd); + } + } + return cmd; +} + +/** 802f0030 */ +mDvd_toMainRam_normal_c *mDvd_toMainRam_normal_c::createOrFail(const char *path, int mountDirection, EGG::Heap *heap) { + mDvd_toMainRam_normal_c *cmd = mDvd_toMainRam_normal_c::create(path, mountDirection, heap); + while (!cmd) {} + return cmd; +} + +/** 802f0060 */ +void mDvd_toMainRam_normal_c::create2(mDvd_toMainRam_normal_c **cmd, const char *path, int mountDirection, + EGG::Heap *heap) { + if (*cmd == nullptr) { + *cmd = mDvd_toMainRam_normal_c::create(path, mountDirection, heap); + } +} + +/** 802f00b0 */ +mDvd_toMainRam_normal_c::~mDvd_toMainRam_normal_c() {} + +/** 802f0110 */ +u32 mDvd_toMainRam_normal_c::execute() { + u32 amountRead; + u32 fileSize; + EGG::Heap *heap = mHeap != nullptr ? mHeap : mDvd::l_ArchiveHeap; + EGG::DvdRipper::EAllocDirection allocDirection = + mMountDirection == 1 ? EGG::DvdRipper::ALLOC_DIR_TOP : EGG::DvdRipper::ALLOC_DIR_BOTTOM; + mDataPtr = mDvd::loadToMainRAM(mEntryNum, 0, heap, allocDirection, 0, &amountRead, &fileSize, mCompressionType2); + if (mDataPtr != nullptr) { + mAmountRead = amountRead; + mFileSize = fileSize; + } + waitDone(); + return (bool)mDataPtr; +} + +/** 802f01b0 */ +void mDvd_toMainRam_normal_c::doClear() { + if (mDataPtr == nullptr) { + return; + } + EGG::Heap::free(mDataPtr, nullptr); +} |
