2
0
mirror of https://github.com/AxioDL/metaforce.git synced 2025-12-14 23:26:09 +00:00

Fix reading/writing saves

This commit is contained in:
2022-03-22 01:37:10 -07:00
parent e4715a2df6
commit 64e662069d
8 changed files with 64 additions and 74 deletions

View File

@@ -98,31 +98,29 @@ u32 CInputStream::ReadBytes(void* dest, u32 len) {
u32 CInputStream::ReadBits(u32 bitCount) {
u32 ret = 0;
const s32 shiftAmt = x20_bitOffset - s32(bitCount);
if (shiftAmt < 0) {
/* OR in remaining bits of cached value */
u32 mask = bitCount == 32 ? UINT32_MAX : ((1U << bitCount) - 1);
ret |= (x1c_bitWord << u32(-shiftAmt)) & mask;
/* Load in exact number of bytes remaining */
auto loadDiv = std::div(-shiftAmt, 8);
if (loadDiv.rem != 0) {
++loadDiv.quot;
}
Get(reinterpret_cast<u8*>(&x1c_bitWord) + 4 - loadDiv.quot, loadDiv.quot);
#if METAFORCE_TARGET_BYTE_ORDER == __ORDER_LITTLE_ENDIAN__
u32 bitOffset = x20_bitOffset;
if (bitOffset < bitCount) {
u32 shiftAmt = bitCount - bitOffset;
const u32 mask = bitOffset == 32 ? 0xffffffff : (1 << bitOffset) - 1;
const u32 bitWord = x1c_bitWord;
x20_bitOffset = 0;
const u32 len = (shiftAmt / 8) + static_cast<unsigned int>((shiftAmt % 8) != 0);
Get(reinterpret_cast<u8*>(&x1c_bitWord), len);
#if METAFORCE_TARGET_BYTE_ORDER == __LITTLE_ENDIAN
x1c_bitWord = CBasics::SwapBytes(x1c_bitWord);
#endif
/* New bit offset */
x20_bitOffset = loadDiv.quot * 8 + shiftAmt;
/* OR in next bits */
mask = (1U << u32(-shiftAmt)) - 1;
ret |= (x1c_bitWord >> x20_bitOffset) & mask;
const u32 mask2 = shiftAmt == 32 ? 0xffffffff : (1 << shiftAmt) - 1;
u32 tmp = x20_bitOffset;
x20_bitOffset = len * 8;
ret = ((mask & (bitWord >> (32 - bitOffset))) << shiftAmt) |
((mask2 & (x1c_bitWord >> (32 - shiftAmt))) << tmp);
x20_bitOffset -= shiftAmt;
x1c_bitWord <<= u64(shiftAmt);
} else {
/* OR in bits of cached value */
const u32 mask = bitCount == 32 ? UINT32_MAX : ((1U << bitCount) - 1);
ret |= (x1c_bitWord >> u32(shiftAmt)) & mask;
/* New bit offset */
u32 baseVal2 = (bitCount == 0x20 ? 0xffffffff : (1 << bitCount) - 1);
x20_bitOffset -= bitCount;
ret = baseVal2 & (x1c_bitWord >> (32 - bitCount));
x1c_bitWord <<= u64(bitCount);
}
return ret;

View File

@@ -18,13 +18,12 @@ class CInputStream {
bool InternalReadNext();
bool GrabAnotherBlock();
virtual u32 Read(void* dest, u32 len) = 0;
public:
explicit CInputStream(s32 len);
CInputStream(const void* ptr, u32 len, bool owned);
virtual ~CInputStream();
virtual u32 Read(void* dest, u32 len) = 0;
u32 GetReadPosition() const { return x18_readPosition; }
u32 ReadBits(u32 bitCount);
u32 ReadBytes(void* dest, u32 len);

View File

@@ -15,13 +15,14 @@ private:
u32 x84_position = 0;
bool x88_owned;
protected:
void Write(const u8* ptr, u32 len) override;
public:
CMemoryStreamOut(u8* workBuf, u32 len, EOwnerShip ownership = EOwnerShip::NotOwned, s32 unk = 4096)
: COutputStream(workBuf, unk), x7c_ptr(workBuf), x80_len(len), x88_owned(ownership == EOwnerShip::Owned) {}
CMemoryStreamOut(u8* workBuf, u32 len, EOwnerShip ownership = EOwnerShip::NotOwned, s32 blockLen = 4096)
: COutputStream(blockLen), x7c_ptr(workBuf), x80_len(len), x88_owned(ownership == EOwnerShip::Owned) {}
~CMemoryStreamOut() override;
void Write(const u8* ptr, u32 len) override;
u32 GetWritePosition() const { return x84_position; }
};
} // namespace metaforce

View File

@@ -7,11 +7,10 @@
namespace metaforce {
static u32 min_containing_bytes(u32 v) {
v = 32 - v;
v = (v >> 3) - (static_cast<s32>(-(v & 7)) >> 31);
return v;
return (v / 8) + static_cast<unsigned int>((v % 8) != 0);
}
COutputStream::COutputStream(u8* ptr, s32 len) : x8_bufLen(len) {
COutputStream::COutputStream(s32 len) : x8_bufLen(len) {
xc_ptr = len <= 64 ? reinterpret_cast<u8*>(((reinterpret_cast<uintptr_t>(x1c_scratch) + 7) & ~7) + 6) : new u8[len];
}
@@ -34,24 +33,23 @@ void COutputStream::DoPut(const u8* ptr, u32 len) {
}
x10_numWrites += len;
u32 offset = x4_position;
u32 curLen = len;
if (x8_bufLen < len + offset) {
if (x8_bufLen < len + x4_position) {
while (curLen != 0) {
offset = x4_position;
u32 count = x8_bufLen - offset;
u32 count = x8_bufLen - x4_position;
if (curLen < count) {
count = curLen;
}
if (count == 0) {
DoFlush();
} else {
memcpy(xc_ptr + offset, ptr + (len - curLen), count);
memcpy(xc_ptr + x4_position, ptr + (len - curLen), count);
x4_position += count;
curLen -= count;
}
}
} else {
memcpy(xc_ptr + offset, ptr, len);
memcpy(xc_ptr + x4_position, ptr, len);
x4_position += len;
}
}
@@ -77,28 +75,21 @@ void COutputStream::Put(const u8* ptr, u32 len) {
DoPut(ptr, len);
}
void COutputStream::WriteBits(u32 val, u32 bitCount) {
const s32 shiftAmt = x18_shiftRegisterOffset - s32(bitCount);
if (shiftAmt < 0) {
/* OR remaining bits to cached value */
const u32 mask = (1U << x18_shiftRegisterOffset) - 1;
x14_shiftRegister |= (val >> u32(-shiftAmt)) & mask;
/* Write out 32-bits */
void COutputStream::WriteBits(u32 value, u32 bitCount) {
u32 bitOffset = x18_shiftRegisterOffset;
if (bitOffset < bitCount) {
u32 shiftAmt = bitCount - bitOffset;
x14_shiftRegister |= (value >> shiftAmt) & (bitOffset == 32 ? 0xffffffff : (1 << bitOffset) - 1);
x18_shiftRegisterOffset = 0;
FlushShiftRegister();
/* Cache remaining bits */
x18_shiftRegisterOffset = 0x20 + shiftAmt;
x14_shiftRegister = val << x18_shiftRegisterOffset;
const u32 mask = (shiftAmt == 0x20 ? 0xffffffff : (1 << shiftAmt) - 1);
x14_shiftRegister = (value & mask) << (0x20 - shiftAmt);
x18_shiftRegisterOffset -= shiftAmt;
} else {
/* OR bits to cached value */
const u32 mask = bitCount == 32 ? UINT32_MAX : ((1U << bitCount) - 1);
x14_shiftRegister |= (val & mask) << u32(shiftAmt);
/* New bit offset */
const u32 mask = bitCount == 32 ? 0xffffffff : (1 << bitCount) - 1;
x14_shiftRegister |= (value & mask) << (bitOffset - bitCount);
x18_shiftRegisterOffset -= bitCount;
}
}
void COutputStream::WriteChar(u8 c) {

View File

@@ -19,11 +19,10 @@ class COutputStream {
protected:
void DoFlush();
void DoPut(const u8* ptr, u32 len);
public:
COutputStream(u8* ptr, s32 unk);
virtual ~COutputStream();
virtual void Write(const u8* ptr, u32 len) = 0;
public:
COutputStream(s32 unk);
virtual ~COutputStream();
void WriteBits(u32 val, u32 bitCount);
void WriteChar(u8 c);

View File

@@ -7,15 +7,15 @@
#include <zlib.h>
namespace metaforce {
class CZipInputStream : public CInputStream {
class CZipInputStream final : public CInputStream {
std::unique_ptr<u8[]> x24_compBuf;
std::unique_ptr<CInputStream> x28_strm;
std::unique_ptr<z_stream> x30_zstrm = {};
u32 Read(void* ptr, u32 len) override;
public:
explicit CZipInputStream(std::unique_ptr<CInputStream>&& strm);
~CZipInputStream() override;
u32 Read(void* ptr, u32 len) override;
};
} // namespace metaforce