Fix reading/writing saves

This commit is contained in:
Phillip Stephens 2022-03-22 01:37:10 -07:00
parent e4715a2df6
commit 64e662069d
Signed by: Antidote
GPG Key ID: F8BEE4C83DACA60D
8 changed files with 64 additions and 74 deletions

View File

@ -216,14 +216,16 @@ void CMemoryCardSys::CCardFileInfo::BuildCardBuffer() {
u32 bannerSz = CalculateBannerDataSize();
x104_cardBuffer.resize((bannerSz + xf4_saveBuffer.size() + 8191) & ~8191);
CMemoryStreamOut w(x104_cardBuffer.data(), x104_cardBuffer.size(), CMemoryStreamOut::EOwnerShip::NotOwned);
w.WriteLong(0);
char comment[64];
std::memset(comment, 0, std::size(comment));
std::strncpy(comment, x28_comment.data(), std::size(comment) - 1);
w.Write(reinterpret_cast<const u8*>(comment), 64);
WriteBannerData(w);
WriteIconData(w);
{
CMemoryStreamOut w(x104_cardBuffer.data(), x104_cardBuffer.size(), CMemoryStreamOut::EOwnerShip::NotOwned);
w.WriteLong(0);
char comment[64];
std::memset(comment, 0, std::size(comment));
std::strncpy(comment, x28_comment.data(), std::size(comment) - 1);
w.Put(reinterpret_cast<const u8*>(comment), 64);
WriteBannerData(w);
WriteIconData(w);
}
memmove(x104_cardBuffer.data() + bannerSz, xf4_saveBuffer.data(), xf4_saveBuffer.size());
reinterpret_cast<u32&>(*x104_cardBuffer.data()) =
CBasics::SwapBytes(CCRC32::Calculate(x104_cardBuffer.data() + 4, x104_cardBuffer.size() - 4));
@ -237,12 +239,12 @@ void CMemoryCardSys::CCardFileInfo::WriteBannerData(COutputStream& out) const {
const auto format = tex->GetTextureFormat();
const auto* texels = tex->GetConstBitMapData(0);
if (format == ETexelFormat::RGB5A3) {
out.Write(texels, 6144);
out.Put(texels, 6144);
} else {
out.Write(texels, 3072);
out.Put(texels, 3072);
}
if (format == ETexelFormat::C8) {
out.Write(tex->GetPalette()->GetEntries(), 512);
out.Put(tex->GetPalette()->GetEntries(), 512);
}
}
}
@ -253,16 +255,16 @@ void CMemoryCardSys::CCardFileInfo::WriteIconData(COutputStream& out) const {
const auto format = icon.x8_tex->GetTextureFormat();
const auto* texels = icon.x8_tex->GetConstBitMapData(0);
if (format == ETexelFormat::RGB5A3) {
out.Write(texels, 2048);
out.Put(texels, 2048);
} else {
out.Write(texels, 1024);
out.Put(texels, 1024);
}
if (format == ETexelFormat::C8) {
palette = icon.x8_tex->GetPalette()->GetEntries();
}
}
if (palette != nullptr) {
out.Write(palette, 512);
out.Put(palette, 512);
}
}

View File

@ -60,7 +60,7 @@ std::string CMemoryCardSys::_CreateDolphinCard(kabufuda::ECardSlot slot, bool do
}
auto path = *dolphinPath + "GC";
int ret = mkdir(path.c_str(), 0755);
if (ret != 0 && ret != EEXIST) {
if (ret != 0 && errno != EEXIST) {
return {};
}

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