From ce23544a159b954158945976c1e5c70f333a94b3 Mon Sep 17 00:00:00 2001 From: Phillip Stephens Date: Fri, 18 Feb 2022 21:32:36 -0800 Subject: [PATCH] Fix CInputStream::Get/ReadBytes not properly updating position --- Runtime/CInputStream.cpp | 60 +++++++++++++++------------------ Runtime/RetroTypes.cpp | 4 +-- Runtime/World/CEnvFxManager.cpp | 4 +-- 3 files changed, 31 insertions(+), 37 deletions(-) diff --git a/Runtime/CInputStream.cpp b/Runtime/CInputStream.cpp index 1113bc7c8..2c0b450bf 100644 --- a/Runtime/CInputStream.cpp +++ b/Runtime/CInputStream.cpp @@ -31,30 +31,29 @@ bool CInputStream::InternalReadNext() { bool CInputStream::GrabAnotherBlock() { return InternalReadNext(); } void CInputStream::Get(u8* dest, u32 len) { + u32 readLen = 0; x20_bitOffset = 0; - u32 offset = 0; while (len != 0) { u32 blockLen = x8_blockLen - x4_blockOffset; if (len < blockLen) { blockLen = len; } - if (blockLen == 0) { - if (len <= 256) { - GrabAnotherBlock(); - } else { - u32 readLen = Read(dest + offset, len); - len -= readLen; - offset += readLen; - } - } else { - memcpy(dest + offset, x10_ptr + x4_blockOffset, blockLen); + if (blockLen != 0) { + memcpy(dest + readLen, x10_ptr + x4_blockOffset, blockLen); len -= blockLen; + readLen += blockLen; x4_blockOffset += blockLen; + } else if (len > 256) { + u32 tmp = Read(dest + readLen, len); + len -= tmp; + readLen += tmp; + } else { + GrabAnotherBlock(); } } - x18_readPosition += offset; + x18_readPosition += readLen; } u32 CInputStream::ReadBytes(void* dest, u32 len) { @@ -66,31 +65,26 @@ u32 CInputStream::ReadBytes(void* dest, u32 len) { GrabAnotherBlock(); } - u32 curReadLen = 0; u32 curLen = len; + u32 curReadLen = 0; - do { - while (true) { - if (len <= curReadLen) { - x18_readPosition += curReadLen; - return curReadLen; - } - - u32 readCount = x8_blockLen - x4_blockOffset; - if (readCount == 0) { - break; - } - - if (curLen < readCount) { - readCount = curLen; - } - - memcpy(reinterpret_cast(dest) + curReadLen, x10_ptr + x4_blockOffset, readCount); - curReadLen += readCount; - curLen -= readCount; + while (curReadLen < len) { + if ((x8_blockLen - x4_blockOffset) == 0 && !InternalReadNext()) { + break; } - } while (InternalReadNext()); + u32 readCount = x8_blockLen - x4_blockOffset; + if (curLen < (x8_blockLen - x4_blockOffset)) { + readCount = curLen; + } + + memcpy(reinterpret_cast(dest) + curReadLen, x10_ptr + x4_blockOffset, readCount); + curReadLen += readCount; + curLen -= readCount; + x4_blockOffset += readCount; + } + + x18_readPosition += curReadLen; return curReadLen; } diff --git a/Runtime/RetroTypes.cpp b/Runtime/RetroTypes.cpp index 45ca652f8..c05f36c11 100644 --- a/Runtime/RetroTypes.cpp +++ b/Runtime/RetroTypes.cpp @@ -9,13 +9,13 @@ namespace metaforce { logvisor::Module Log("metaforce::RetroTypes::CAssetId"); SObjectTag::SObjectTag(CInputStream& in) { - in.ReadBytes(reinterpret_cast(&type), 4); + in.Get(reinterpret_cast(&type), 4); id = in.Get(); } void SObjectTag::ReadMLVL(CInputStream& in) { id = in.Get(); - in.ReadBytes(reinterpret_cast(&type), 4); + in.Get(reinterpret_cast(&type), 4); } CAssetId::CAssetId(CInputStream& in) { diff --git a/Runtime/World/CEnvFxManager.cpp b/Runtime/World/CEnvFxManager.cpp index 53a9e1dce..5e79fd098 100644 --- a/Runtime/World/CEnvFxManager.cpp +++ b/Runtime/World/CEnvFxManager.cpp @@ -599,9 +599,9 @@ void CEnvFxManager::Cleanup() { void CEnvFxManager::Initialize() { const SObjectTag* tag = g_ResFactory->GetResourceIdByName("DUMB_SnowForces"); std::unique_ptr data = g_ResFactory->LoadResourceSync(*tag); - athena::io::MemoryReader r(data.get(), 2048); + CMemoryInStream r(data.get(), 2048, CMemoryInStream::EOwnerShip::NotOwned); for (int i = 0; i < 256; ++i) - g_SnowForces.push_back(r.readVec2fBig()); + g_SnowForces.push_back(r.Get()); } } // namespace metaforce