Store HFILE in its own member variable

This commit is contained in:
Anonymous Maarten 2024-06-25 15:06:01 +02:00
parent 30b918b30e
commit 47e690e533
2 changed files with 56 additions and 38 deletions

View File

@ -9,6 +9,10 @@
#include <mmsystem.h> #include <mmsystem.h>
// clang-format on // clang-format on
#if defined(_M_IX86) || defined(__i386__)
#define MXIO_MINFO_MFILE
#endif
// SIZE 0x48 // SIZE 0x48
class MXIOINFO { class MXIOINFO {
public: public:
@ -29,6 +33,9 @@ class MXIOINFO {
// NOTE: In MXIOINFO, the `hmmio` member of MMIOINFO is used like // NOTE: In MXIOINFO, the `hmmio` member of MMIOINFO is used like
// an HFILE (int) instead of an HMMIO (WORD). // an HFILE (int) instead of an HMMIO (WORD).
MMIOINFO m_info; MMIOINFO m_info;
#ifndef MXIO_MINFO_MFILE
HFILE m_file;
#endif
}; };
#endif // MXIO_H #endif // MXIO_H

View File

@ -9,6 +9,16 @@
// but this assert will enforce the size if we decide to change that. // but this assert will enforce the size if we decide to change that.
DECOMP_SIZE_ASSERT(MXIOINFO, sizeof(MMIOINFO)); DECOMP_SIZE_ASSERT(MXIOINFO, sizeof(MMIOINFO));
#ifdef MXIO_MINFO_MFILE
#define ASSIGN_M_FILE(X) m_info.hmmio = (HMMIO) (X)
#define M_FILE (HFILE)(m_info.hmmio)
#define RAW_M_FILE m_info.hmmio
#else
#define ASSIGN_M_FILE(X) m_file = (X)
#define M_FILE (m_file)
#define RAW_M_FILE m_file
#endif
// FUNCTION: LEGO1 0x100cc800 // FUNCTION: LEGO1 0x100cc800
// FUNCTION: BETA10 0x1015e140 // FUNCTION: BETA10 0x1015e140
MXIOINFO::MXIOINFO() MXIOINFO::MXIOINFO()
@ -33,9 +43,10 @@ MxU16 MXIOINFO::Open(const char* p_filename, MxULong p_flags)
m_info.lDiskOffset = m_info.lBufOffset = 0; m_info.lDiskOffset = m_info.lBufOffset = 0;
// DECOMP: Cast of p_flags to u16 forces the `movzx` instruction // DECOMP: Cast of p_flags to u16 forces the `movzx` instruction
m_info.hmmio = (HMMIO) OpenFile(p_filename, &unused, (MxU16) p_flags); // original: m_info.hmmio = OpenFile(p_filename, &unused, (MxU16) p_flags);
ASSIGN_M_FILE(OpenFile(p_filename, &unused, (MxU16) p_flags));
if ((HFILE) m_info.hmmio != HFILE_ERROR) { if (M_FILE != HFILE_ERROR) {
m_info.dwFlags = p_flags; m_info.dwFlags = p_flags;
if (m_info.dwFlags & MMIO_ALLOCBUF) { if (m_info.dwFlags & MMIO_ALLOCBUF) {
@ -75,10 +86,10 @@ MxU16 MXIOINFO::Close(MxLong p_unused)
{ {
MxU16 result = 0; MxU16 result = 0;
if (m_info.hmmio) { if (RAW_M_FILE) {
result = Flush(0); result = Flush(0);
_lclose((HFILE) m_info.hmmio); _lclose(M_FILE);
m_info.hmmio = 0; ASSIGN_M_FILE(0);
if (m_info.dwFlags & MMIO_ALLOCBUF) { if (m_info.dwFlags & MMIO_ALLOCBUF) {
delete[] m_info.pchBuffer; delete[] m_info.pchBuffer;
@ -127,12 +138,12 @@ MxLong MXIOINFO::Read(void* p_buf, MxLong p_len)
} }
} }
} }
else if (m_info.hmmio && p_len > 0) { else if (RAW_M_FILE && p_len > 0) {
bytesRead = _hread((HFILE) m_info.hmmio, p_buf, p_len); bytesRead = _hread(M_FILE, p_buf, p_len);
if (bytesRead == -1) { if (bytesRead == -1) {
bytesRead = 0; bytesRead = 0;
m_info.lDiskOffset = _llseek((HFILE) m_info.hmmio, 0, SEEK_CUR); m_info.lDiskOffset = _llseek(M_FILE, 0, SEEK_CUR);
} }
else { else {
m_info.lDiskOffset += bytesRead; m_info.lDiskOffset += bytesRead;
@ -180,12 +191,12 @@ MxLong MXIOINFO::Write(void* p_buf, MxLong p_len)
} }
} }
} }
else if (m_info.hmmio && p_len > 0) { else if (RAW_M_FILE && p_len > 0) {
bytesWritten = _hwrite((HFILE) m_info.hmmio, (const char*) p_buf, p_len); bytesWritten = _hwrite(M_FILE, (const char*) p_buf, p_len);
if (bytesWritten == -1) { if (bytesWritten == -1) {
bytesWritten = 0; bytesWritten = 0;
m_info.lDiskOffset = _llseek((HFILE) m_info.hmmio, 0, SEEK_CUR); m_info.lDiskOffset = _llseek(M_FILE, 0, SEEK_CUR);
} }
else { else {
m_info.lDiskOffset += bytesWritten; m_info.lDiskOffset += bytesWritten;
@ -234,11 +245,11 @@ MxLong MXIOINFO::Seek(MxLong p_offset, MxLong p_origin)
} }
else { else {
// we have to read another chunk from disk. // we have to read another chunk from disk.
if (m_info.hmmio && !Flush(0)) { if (RAW_M_FILE && !Flush(0)) {
m_info.lDiskOffset = _llseek((HFILE) m_info.hmmio, p_offset, p_origin); m_info.lDiskOffset = _llseek(M_FILE, p_offset, p_origin);
if (m_info.lDiskOffset == -1) { if (m_info.lDiskOffset == -1) {
m_info.lDiskOffset = _llseek((HFILE) m_info.hmmio, 0, SEEK_CUR); m_info.lDiskOffset = _llseek(M_FILE, 0, SEEK_CUR);
} }
else { else {
@ -248,10 +259,10 @@ MxLong MXIOINFO::Seek(MxLong p_offset, MxLong p_origin)
// do we need to seek again? // do we need to seek again?
// (i.e. are we already aligned to buffer size?) // (i.e. are we already aligned to buffer size?)
if (p_offset != m_info.lBufOffset) { if (p_offset != m_info.lBufOffset) {
m_info.lDiskOffset = _llseek((HFILE) m_info.hmmio, m_info.lBufOffset, SEEK_SET); m_info.lDiskOffset = _llseek(M_FILE, m_info.lBufOffset, SEEK_SET);
if (m_info.lDiskOffset == -1) { if (m_info.lDiskOffset == -1) {
m_info.lDiskOffset = _llseek((HFILE) m_info.hmmio, 0, SEEK_CUR); m_info.lDiskOffset = _llseek(M_FILE, 0, SEEK_CUR);
} }
} }
@ -259,10 +270,10 @@ MxLong MXIOINFO::Seek(MxLong p_offset, MxLong p_origin)
// is the file open for writing only? // is the file open for writing only?
if ((m_info.dwFlags & MMIO_RWMODE) == 0 || (m_info.dwFlags & MMIO_RWMODE) == MMIO_READWRITE) { if ((m_info.dwFlags & MMIO_RWMODE) == 0 || (m_info.dwFlags & MMIO_RWMODE) == MMIO_READWRITE) {
// We can read from the file. Fill the buffer. // We can read from the file. Fill the buffer.
bytesRead = _hread((HFILE) m_info.hmmio, m_info.pchBuffer, m_info.cchBuffer); bytesRead = _hread(M_FILE, m_info.pchBuffer, m_info.cchBuffer);
if (bytesRead == -1) { if (bytesRead == -1) {
m_info.lDiskOffset = _llseek((HFILE) m_info.hmmio, 0, SEEK_CUR); m_info.lDiskOffset = _llseek(M_FILE, 0, SEEK_CUR);
} }
else { else {
m_info.lDiskOffset += bytesRead; m_info.lDiskOffset += bytesRead;
@ -283,19 +294,19 @@ MxLong MXIOINFO::Seek(MxLong p_offset, MxLong p_origin)
} }
} }
} }
else if (m_info.hmmio) { else if (RAW_M_FILE) {
// No buffer so just seek the file directly (if we have a valid handle) // No buffer so just seek the file directly (if we have a valid handle)
// i.e. if we just want to get the current file position // i.e. if we just want to get the current file position
if (p_origin == SEEK_CUR && p_offset == 0) { if (p_origin == SEEK_CUR && p_offset == 0) {
return m_info.lDiskOffset; return m_info.lDiskOffset;
} }
m_info.lDiskOffset = _llseek((HFILE) m_info.hmmio, p_offset, p_origin); m_info.lDiskOffset = _llseek(M_FILE, p_offset, p_origin);
result = m_info.lDiskOffset; result = m_info.lDiskOffset;
if (result == -1) { if (result == -1) {
m_info.lDiskOffset = _llseek((HFILE) m_info.hmmio, 0, SEEK_CUR); m_info.lDiskOffset = _llseek(M_FILE, 0, SEEK_CUR);
} }
} }
@ -334,25 +345,25 @@ MxU16 MXIOINFO::Flush(MxU16 p_unused)
// if we have allocated an IO buffer // if we have allocated an IO buffer
if (m_info.pchBuffer) { if (m_info.pchBuffer) {
// if we have a file open for writing // if we have a file open for writing
if (m_info.hmmio && (m_info.dwFlags & MMIO_RWMODE)) { if (RAW_M_FILE && (m_info.dwFlags & MMIO_RWMODE)) {
// DECOMP: pulling this value out into a variable forces it into EBX // DECOMP: pulling this value out into a variable forces it into EBX
MxLong cchBuffer = m_info.cchBuffer; MxLong cchBuffer = m_info.cchBuffer;
if (cchBuffer > 0) { if (cchBuffer > 0) {
if (m_info.lBufOffset != m_info.lDiskOffset) { if (m_info.lBufOffset != m_info.lDiskOffset) {
m_info.lDiskOffset = _llseek((HFILE) m_info.hmmio, m_info.lBufOffset, SEEK_SET); m_info.lDiskOffset = _llseek(M_FILE, m_info.lBufOffset, SEEK_SET);
} }
// Was the previous seek (if required) successful? // Was the previous seek (if required) successful?
if (m_info.lBufOffset != m_info.lDiskOffset) { if (m_info.lBufOffset != m_info.lDiskOffset) {
result = MMIOERR_CANNOTSEEK; result = MMIOERR_CANNOTSEEK;
m_info.lDiskOffset = _llseek((HFILE) m_info.hmmio, 0, SEEK_CUR); m_info.lDiskOffset = _llseek(M_FILE, 0, SEEK_CUR);
} }
else { else {
bytesWritten = _hwrite((HFILE) m_info.hmmio, m_info.pchBuffer, cchBuffer); bytesWritten = _hwrite(M_FILE, m_info.pchBuffer, cchBuffer);
if (bytesWritten == -1 || bytesWritten != cchBuffer) { if (bytesWritten == -1 || bytesWritten != cchBuffer) {
result = MMIOERR_CANNOTWRITE; result = MMIOERR_CANNOTWRITE;
m_info.lDiskOffset = _llseek((HFILE) m_info.hmmio, 0, SEEK_CUR); m_info.lDiskOffset = _llseek(M_FILE, 0, SEEK_CUR);
} }
else { else {
m_info.lDiskOffset += bytesWritten; m_info.lDiskOffset += bytesWritten;
@ -392,19 +403,19 @@ MxU16 MXIOINFO::Advance(MxU16 p_option)
((p_option & MMIO_WRITE) || (rwmode == MMIO_READWRITE)) && cch > 0) { ((p_option & MMIO_WRITE) || (rwmode == MMIO_READWRITE)) && cch > 0) {
if (m_info.lBufOffset != m_info.lDiskOffset) { if (m_info.lBufOffset != m_info.lDiskOffset) {
m_info.lDiskOffset = _llseek((HFILE) m_info.hmmio, m_info.lBufOffset, SEEK_SET); m_info.lDiskOffset = _llseek(M_FILE, m_info.lBufOffset, SEEK_SET);
} }
if (m_info.lBufOffset != m_info.lDiskOffset) { if (m_info.lBufOffset != m_info.lDiskOffset) {
result = MMIOERR_CANNOTSEEK; result = MMIOERR_CANNOTSEEK;
m_info.lDiskOffset = _llseek((HFILE) m_info.hmmio, 0, SEEK_CUR); m_info.lDiskOffset = _llseek(M_FILE, 0, SEEK_CUR);
} }
else { else {
bytesCounter = _hwrite((HFILE) m_info.hmmio, m_info.pchBuffer, cch); bytesCounter = _hwrite(M_FILE, m_info.pchBuffer, cch);
if (bytesCounter == -1 || bytesCounter != cch) { if (bytesCounter == -1 || bytesCounter != cch) {
result = MMIOERR_CANNOTWRITE; result = MMIOERR_CANNOTWRITE;
m_info.lDiskOffset = _llseek((HFILE) m_info.hmmio, 0, SEEK_CUR); m_info.lDiskOffset = _llseek(M_FILE, 0, SEEK_CUR);
} }
else { else {
m_info.lDiskOffset += bytesCounter; m_info.lDiskOffset += bytesCounter;
@ -418,20 +429,20 @@ MxU16 MXIOINFO::Advance(MxU16 p_option)
m_info.lBufOffset += cch; m_info.lBufOffset += cch;
if ((!rwmode || rwmode == MMIO_READWRITE) && cch > 0) { if ((!rwmode || rwmode == MMIO_READWRITE) && cch > 0) {
if (m_info.lBufOffset != m_info.lDiskOffset) { if (m_info.lBufOffset != m_info.lDiskOffset) {
m_info.lDiskOffset = _llseek((HFILE) m_info.hmmio, m_info.lBufOffset, SEEK_SET); m_info.lDiskOffset = _llseek(M_FILE, m_info.lBufOffset, SEEK_SET);
} }
// if previous seek failed // if previous seek failed
if (m_info.lBufOffset != m_info.lDiskOffset) { if (m_info.lBufOffset != m_info.lDiskOffset) {
result = MMIOERR_CANNOTSEEK; result = MMIOERR_CANNOTSEEK;
m_info.lDiskOffset = _llseek((HFILE) m_info.hmmio, 0, SEEK_CUR); m_info.lDiskOffset = _llseek(M_FILE, 0, SEEK_CUR);
} }
else { else {
bytesCounter = _hread((HFILE) m_info.hmmio, m_info.pchBuffer, cch); bytesCounter = _hread(M_FILE, m_info.pchBuffer, cch);
if (bytesCounter == -1) { if (bytesCounter == -1) {
result = MMIOERR_CANNOTREAD; result = MMIOERR_CANNOTREAD;
m_info.lDiskOffset = _llseek((HFILE) m_info.hmmio, 0, SEEK_CUR); m_info.lDiskOffset = _llseek(M_FILE, 0, SEEK_CUR);
} }
else { else {
m_info.lDiskOffset += bytesCounter; m_info.lDiskOffset += bytesCounter;
@ -574,11 +585,11 @@ MxU16 MXIOINFO::Ascend(MMCKINFO* p_chunkInfo, MxU16 p_ascend)
m_info.dwFlags |= MMIO_DIRTY; m_info.dwFlags |= MMIO_DIRTY;
} }
else { else {
m_info.lDiskOffset = _llseek((HFILE) m_info.hmmio, ofs, SEEK_SET); m_info.lDiskOffset = _llseek(M_FILE, ofs, SEEK_SET);
if (m_info.lDiskOffset == ofs) { if (m_info.lDiskOffset == ofs) {
if (_lwrite((HFILE) m_info.hmmio, (char*) &size, 4) != 4) { if (_lwrite(M_FILE, (char*) &size, 4) != 4) {
m_info.lDiskOffset = _llseek((HFILE) m_info.hmmio, 0, SEEK_CUR); m_info.lDiskOffset = _llseek(M_FILE, 0, SEEK_CUR);
result = MMIOERR_CANNOTWRITE; result = MMIOERR_CANNOTWRITE;
} }
else { else {
@ -586,7 +597,7 @@ MxU16 MXIOINFO::Ascend(MMCKINFO* p_chunkInfo, MxU16 p_ascend)
} }
} }
else { else {
m_info.lDiskOffset = _llseek((HFILE) m_info.hmmio, 0, SEEK_CUR); m_info.lDiskOffset = _llseek(M_FILE, 0, SEEK_CUR);
result = MMIOERR_CANNOTSEEK; result = MMIOERR_CANNOTSEEK;
} }
} }