Use SDL IOStream in for MXIOINFO

This commit is contained in:
Christian Semmler 2024-12-16 13:14:19 -07:00
parent 3b7e60bdc0
commit b0357a79c1
12 changed files with 181 additions and 101 deletions

View File

@ -98,7 +98,6 @@ EXPORTS
?RemoveAll@ViewManager@@QAEXPAVViewROI@@@Z ?RemoveAll@ViewManager@@QAEXPAVViewROI@@@Z
?RemoveWorld@LegoOmni@@QAEXABVMxAtomId@@J@Z ?RemoveWorld@LegoOmni@@QAEXABVMxAtomId@@J@Z
?Save@LegoGameState@@QAEJK@Z ?Save@LegoGameState@@QAEJK@Z
?Seek@MxDSFile@@UAEJJH@Z
?SerializePlayersInfo@LegoGameState@@QAEXF@Z ?SerializePlayersInfo@LegoGameState@@QAEXF@Z
?SerializeScoreHistory@LegoGameState@@QAEXF@Z ?SerializeScoreHistory@LegoGameState@@QAEXF@Z
?SetCD@MxOmni@@SAXPBD@Z ?SetCD@MxOmni@@SAXPBD@Z

View File

@ -146,7 +146,6 @@ _ZN8MxDSFile13GetBufferSizeEv
_ZN8MxDSFile19GetStreamBuffersNumEv _ZN8MxDSFile19GetStreamBuffersNumEv
_ZN8MxDSFile4OpenEj _ZN8MxDSFile4OpenEj
_ZN8MxDSFile4ReadEPhj _ZN8MxDSFile4ReadEPhj
_ZN8MxDSFile4SeekEii
_ZN8MxDSFile5CloseEv _ZN8MxDSFile5CloseEv
_ZN8MxDSFileC1EPKcj _ZN8MxDSFileC1EPKcj
_ZN8MxDSFileC2EPKcj _ZN8MxDSFileC2EPKcj

View File

@ -6,8 +6,6 @@
#include "mxstring.h" #include "mxstring.h"
#include "mxtypes.h" #include "mxtypes.h"
#include <windows.h>
// VTABLE: LEGO1 0x100dc890 // VTABLE: LEGO1 0x100dc890
// VTABLE: BETA10 0x101c2418 // VTABLE: BETA10 0x101c2418
// SIZE 0x7c // SIZE 0x7c
@ -43,14 +41,14 @@ class MxDSFile : public MxDSSource {
MxResult Open(MxULong) override; // vtable+0x14 MxResult Open(MxULong) override; // vtable+0x14
MxResult Close() override; // vtable+0x18 MxResult Close() override; // vtable+0x18
MxResult Read(unsigned char*, MxULong) override; // vtable+0x20 MxResult Read(unsigned char*, MxULong) override; // vtable+0x20
MxResult Seek(MxLong, int) override; // vtable+0x24 MxResult Seek(MxLong, SDL_IOWhence) override; // vtable+0x24
MxULong GetBufferSize() override; // vtable+0x28 MxULong GetBufferSize() override; // vtable+0x28
MxULong GetStreamBuffersNum() override; // vtable+0x2c MxULong GetStreamBuffersNum() override; // vtable+0x2c
// FUNCTION: BETA10 0x1015e110 // FUNCTION: BETA10 0x1015e110
void SetFileName(const char* p_filename) { m_filename = p_filename; } void SetFileName(const char* p_filename) { m_filename = p_filename; }
MxS32 CalcFileSize() { return GetFileSize(m_io.m_info.hmmio, NULL); } MxS32 CalcFileSize() { return SDL_GetIOSize(m_io.m_file); }
// SYNTHETIC: LEGO1 0x100c01e0 // SYNTHETIC: LEGO1 0x100c01e0
// SYNTHETIC: BETA10 0x10148e40 // SYNTHETIC: BETA10 0x10148e40
@ -63,7 +61,7 @@ class MxDSFile : public MxDSSource {
MxS16 m_majorVersion; // 0x00 MxS16 m_majorVersion; // 0x00
MxS16 m_minorVersion; // 0x02 MxS16 m_minorVersion; // 0x02
MxULong m_bufferSize; // 0x04 MxU32 m_bufferSize; // 0x04
MxS16 m_streamBuffersNum; // 0x08 MxS16 m_streamBuffersNum; // 0x08
MxS16 m_reserved; // 0x0a MxS16 m_reserved; // 0x0a
}; };

View File

@ -3,6 +3,8 @@
#include "mxcore.h" #include "mxcore.h"
#include <SDL3/SDL_iostream.h>
class MxDSBuffer; class MxDSBuffer;
// VTABLE: LEGO1 0x100dc8c8 // VTABLE: LEGO1 0x100dc8c8
@ -31,7 +33,7 @@ class MxDSSource : public MxCore {
virtual MxLong Close() = 0; // vtable+0x18 virtual MxLong Close() = 0; // vtable+0x18
virtual MxResult ReadToBuffer(MxDSBuffer* p_buffer); // vtable+0x1c virtual MxResult ReadToBuffer(MxDSBuffer* p_buffer); // vtable+0x1c
virtual MxResult Read(unsigned char*, MxULong) = 0; // vtable+0x20 virtual MxResult Read(unsigned char*, MxULong) = 0; // vtable+0x20
virtual MxLong Seek(MxLong, int) = 0; // vtable+0x24 virtual MxLong Seek(MxLong, SDL_IOWhence) = 0; // vtable+0x24
virtual MxULong GetBufferSize() = 0; // vtable+0x28 virtual MxULong GetBufferSize() = 0; // vtable+0x28
virtual MxULong GetStreamBuffersNum() = 0; // vtable+0x2c virtual MxULong GetStreamBuffersNum() = 0; // vtable+0x2c
virtual MxLong GetLengthInDWords(); // vtable+0x30 virtual MxLong GetLengthInDWords(); // vtable+0x30

View File

@ -3,15 +3,70 @@
#include "mxtypes.h" #include "mxtypes.h"
// mmsystem.h requires inclusion of windows.h before #include <SDL3/SDL_iostream.h>
// clang-format off
#include <windows.h>
#include <mmsystem.h>
// clang-format on
#if defined(_M_IX86) || defined(__i386__) // [library:filesystem]
#define MXIO_MINFO_MFILE // We define the bare minimum constants and structures to be compatible with the code in mxio.cpp
#endif // This is mostly copy-pasted from the MMSYSTEM.H Windows header.
/* MMIO error return values */
#define MMIOERR_BASE 256
#define MMIOERR_OUTOFMEMORY (MMIOERR_BASE + 2) /* out of memory */
#define MMIOERR_CANNOTOPEN (MMIOERR_BASE + 3) /* cannot open */
#define MMIOERR_CANNOTREAD (MMIOERR_BASE + 5) /* cannot read */
#define MMIOERR_CANNOTWRITE (MMIOERR_BASE + 6) /* cannot write */
#define MMIOERR_CANNOTSEEK (MMIOERR_BASE + 7) /* cannot seek */
#define MMIOERR_CHUNKNOTFOUND (MMIOERR_BASE + 9) /* chunk not found */
#define MMIOERR_UNBUFFERED (MMIOERR_BASE + 10) /* */
/* bit field masks */
#define MMIO_RWMODE 0x00000003 /* open file for reading/writing/both */
/* constants for dwFlags field of MMIOINFO */
#define MMIO_ALLOCBUF 0x00010000 /* mmioOpen() should allocate a buffer */
#define MMIO_DIRTY 0x10000000 /* I/O buffer is dirty */
/* read/write mode numbers (bit field MMIO_RWMODE) */
#define MMIO_READ 0x00000000 /* open file for reading only */
#define MMIO_WRITE 0x00000001 /* open file for writing only */
#define MMIO_READWRITE 0x00000002 /* open file for reading and writing */
/* MMIO macros */
#define mmioFOURCC(ch0, ch1, ch2, ch3) FOURCC(ch0, ch1, ch2, ch3)
/* standard four character codes */
#define FOURCC_RIFF mmioFOURCC('R', 'I', 'F', 'F')
#define FOURCC_LIST mmioFOURCC('L', 'I', 'S', 'T')
/* various MMIO flags */
#define MMIO_FINDRIFF 0x0020 /* mmioDescend: find a LIST chunk */
#define MMIO_FINDLIST 0x0040 /* mmioDescend: find a RIFF chunk */
/* general MMIO information data structure */
typedef struct _ISLE_MMIOINFO {
/* general fields */
MxU32 dwFlags; /* general status flags */
/* fields maintained by MMIO functions during buffered I/O */
Sint64 cchBuffer; /* size of I/O buffer (or 0L) */
char* pchBuffer; /* start of I/O buffer (or NULL) */
char* pchNext; /* pointer to next byte to read/write */
char* pchEndRead; /* pointer to last valid byte to read */
char* pchEndWrite; /* pointer to last byte to write */
Sint64 lBufOffset; /* disk offset of start of buffer */
/* fields maintained by I/O procedure */
Sint64 lDiskOffset; /* disk offset of next read or write */
} ISLE_MMIOINFO;
/* RIFF chunk information data structure */
typedef struct _ISLE_MMCKINFO {
MxU32 ckid; /* chunk ID */
MxU32 cksize; /* chunk size */
MxU32 fccType; /* form type or list type */
MxU32 dwDataOffset; /* offset of data portion of chunk */
MxU32 dwFlags; /* flags used by MMIO functions */
} ISLE_MMCKINFO;
// SIZE 0x48 // SIZE 0x48
class MXIOINFO { class MXIOINFO {
@ -23,19 +78,18 @@ class MXIOINFO {
MxU16 Close(MxLong); MxU16 Close(MxLong);
MxLong Read(void*, MxLong); MxLong Read(void*, MxLong);
MxLong Write(void*, MxLong); MxLong Write(void*, MxLong);
MxLong Seek(MxLong, MxLong); MxLong Seek(MxLong, SDL_IOWhence);
MxU16 SetBuffer(char*, MxLong, MxLong); MxU16 SetBuffer(char*, MxLong, MxLong);
MxU16 Flush(MxU16); MxU16 Flush(MxU16);
MxU16 Advance(MxU16); MxU16 Advance(MxU16);
MxU16 Descend(MMCKINFO*, const MMCKINFO*, MxU16); MxU16 Descend(ISLE_MMCKINFO*, const ISLE_MMCKINFO*, MxU16);
MxU16 Ascend(MMCKINFO*, MxU16); MxU16 Ascend(ISLE_MMCKINFO*, MxU16);
// 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; ISLE_MMIOINFO m_info;
#ifndef MXIO_MINFO_MFILE // [library:filesystem] This handle is always used instead of the `hmmio` member in m_info.
HFILE m_file; SDL_IOStream* m_file;
#endif
}; };
#endif // MXIO_H #endif // MXIO_H

View File

@ -36,6 +36,7 @@ class MxOmni : public MxCore {
static void SetCD(const char* p_cd); static void SetCD(const char* p_cd);
static void SetHD(const char* p_hd); static void SetHD(const char* p_hd);
static void SetSound3D(MxBool p_use3dSound); static void SetSound3D(MxBool p_use3dSound);
static void NormalizePath(char* p_path);
MxOmni(); MxOmni();
~MxOmni() override; ~MxOmni() override;

View File

@ -25,6 +25,7 @@ class MxString : public MxCore {
MxString& operator+=(const char* p_str); MxString& operator+=(const char* p_str);
static void CharSwap(char* p_a, char* p_b); static void CharSwap(char* p_a, char* p_b);
static void NormalizePath(char* p_path);
// FUNCTION: BETA10 0x10017c50 // FUNCTION: BETA10 0x10017c50
char* GetData() const { return m_data; } char* GetData() const { return m_data; }

View File

@ -2,6 +2,7 @@
#include "decomp.h" #include "decomp.h"
#include <SDL3/SDL_platform_defines.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -185,3 +186,22 @@ void MxString::CharSwap(char* p_a, char* p_b)
*p_a = *p_b; *p_a = *p_b;
*p_b = t; *p_b = t;
} }
void MxString::NormalizePath(char* p_path)
{
// [library:filesystem]
// This function is used to build a consistent path that will eventually be used to read a file.
// The input may come with Windows path separators, i.e. \lego\scripts\infocntr\infomain
// We have to replace the backslashes with forward slashes to be able to access the files
// on non-Windows systems
#if !defined(SDL_PLATFORM_WINDOWS)
char* path = p_path;
while (*path) {
if (*path == '\\') {
*path = '/';
}
path++;
}
#endif
}

View File

@ -77,6 +77,9 @@ void MakeSourceName(char* p_output, const char* p_input)
if (extLoc) { if (extLoc) {
*extLoc = 0; *extLoc = 0;
} }
// [library:filesystem] Normalize this name since it will be part of a path.
MxString::NormalizePath(p_output);
} }
// FUNCTION: LEGO1 0x100b7050 // FUNCTION: LEGO1 0x100b7050
@ -164,9 +167,9 @@ void FUN_100b7220(MxDSAction* p_action, MxU32 p_newFlags, MxBool p_setFlags)
MxDSObject* CreateStreamObject(MxDSFile* p_file, MxS16 p_ofs) MxDSObject* CreateStreamObject(MxDSFile* p_file, MxS16 p_ofs)
{ {
MxU8* buf; MxU8* buf;
_MMCKINFO tmpChunk; ISLE_MMCKINFO tmpChunk;
if (p_file->Seek(((MxLong*) p_file->GetBuffer())[p_ofs], 0)) { if (p_file->Seek(((MxLong*) p_file->GetBuffer())[p_ofs], SDL_IO_SEEK_SET)) {
return NULL; return NULL;
} }

View File

@ -245,7 +245,7 @@ void MxDiskStreamProvider::PerformWork()
buffer = streamingAction->GetUnknowna0(); buffer = streamingAction->GetUnknowna0();
if (m_pFile->GetPosition() == streamingAction->GetBufferOffset() || if (m_pFile->GetPosition() == streamingAction->GetBufferOffset() ||
m_pFile->Seek(streamingAction->GetBufferOffset(), 0) == 0) { m_pFile->Seek(streamingAction->GetBufferOffset(), SDL_IO_SEEK_SET) == 0) {
buffer->SetUnknown14(m_pFile->GetPosition()); buffer->SetUnknown14(m_pFile->GetPosition());
if (m_pFile->ReadToBuffer(buffer) == SUCCESS) { if (m_pFile->ReadToBuffer(buffer) == SUCCESS) {

View File

@ -3,6 +3,7 @@
#include "decomp.h" #include "decomp.h"
#include "mxdebug.h" #include "mxdebug.h"
#include <SDL3/SDL.h>
#include <stdio.h> #include <stdio.h>
#define SI_MAJOR_VERSION 2 #define SI_MAJOR_VERSION 2
@ -41,7 +42,7 @@ MxResult MxDSFile::Open(MxULong p_uStyle)
Close(); Close();
} }
else { else {
Seek(0, 0); Seek(0, SDL_IO_SEEK_SET);
} }
return result; return result;
@ -51,8 +52,8 @@ MxResult MxDSFile::Open(MxULong p_uStyle)
// FUNCTION: BETA10 0x1015dd18 // FUNCTION: BETA10 0x1015dd18
MxResult MxDSFile::ReadChunks() MxResult MxDSFile::ReadChunks()
{ {
_MMCKINFO topChunk; ISLE_MMCKINFO topChunk;
_MMCKINFO childChunk; ISLE_MMCKINFO childChunk;
char tempBuffer[80]; char tempBuffer[80];
topChunk.fccType = FOURCC('O', 'M', 'N', 'I'); topChunk.fccType = FOURCC('O', 'M', 'N', 'I');
@ -70,7 +71,7 @@ MxResult MxDSFile::ReadChunks()
m_io.Read(&m_header, 0x0c); m_io.Read(&m_header, 0x0c);
if ((m_header.m_majorVersion != SI_MAJOR_VERSION) || (m_header.m_minorVersion != SI_MINOR_VERSION)) { if ((m_header.m_majorVersion != SI_MAJOR_VERSION) || (m_header.m_minorVersion != SI_MINOR_VERSION)) {
sprintf(tempBuffer, "Wrong SI file version. %d.%d expected.", SI_MAJOR_VERSION, SI_MINOR_VERSION); sprintf(tempBuffer, "Wrong SI file version. %d.%d expected.", SI_MAJOR_VERSION, SI_MINOR_VERSION);
MessageBoxA(NULL, tempBuffer, NULL, MB_ICONERROR); SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "LEGO® Island Error", tempBuffer, NULL);
return FAILURE; return FAILURE;
} }
@ -116,7 +117,7 @@ MxResult MxDSFile::Read(unsigned char* p_buf, MxULong p_nbytes)
// FUNCTION: LEGO1 0x100cc7b0 // FUNCTION: LEGO1 0x100cc7b0
// FUNCTION: BETA10 0x1015dfee // FUNCTION: BETA10 0x1015dfee
MxResult MxDSFile::Seek(MxLong p_lOffset, MxS32 p_iOrigin) MxResult MxDSFile::Seek(MxLong p_lOffset, SDL_IOWhence p_iOrigin)
{ {
m_position = m_io.Seek(p_lOffset, p_iOrigin); m_position = m_io.Seek(p_lOffset, p_iOrigin);
if (m_position == -1) { if (m_position == -1) {

View File

@ -3,21 +3,16 @@
#include "decomp.h" #include "decomp.h"
#include <assert.h> #include <assert.h>
#include <limits.h>
// This class should be 72 bytes in size, same as the MMIOINFO struct. // This class should be 72 bytes in size, same as the MMIOINFO struct.
// The current implementation has MMIOINFO as the only member of the class, // The current implementation has MMIOINFO as the only member of the class,
// 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 ASSIGN_M_FILE(X) m_file = (X)
#define M_FILE (m_file) #define M_FILE (m_file)
#define RAW_M_FILE m_file #define RAW_M_FILE m_file
#endif
// FUNCTION: LEGO1 0x100cc800 // FUNCTION: LEGO1 0x100cc800
// FUNCTION: BETA10 0x1015e140 // FUNCTION: BETA10 0x1015e140
@ -33,25 +28,32 @@ MXIOINFO::~MXIOINFO()
Close(0); Close(0);
} }
#include <windows.h>
// FUNCTION: LEGO1 0x100cc830 // FUNCTION: LEGO1 0x100cc830
// FUNCTION: BETA10 0x1015e189 // FUNCTION: BETA10 0x1015e189
MxU16 MXIOINFO::Open(const char* p_filename, MxULong p_flags) MxU16 MXIOINFO::Open(const char* p_filename, MxULong p_flags)
{ {
OFSTRUCT unused; OutputDebugString("OPENING -> ");
OutputDebugString(p_filename);
OutputDebugString("\n");
// [library:filesystem] p_flags is always 0 (OF_READ)
assert(p_flags == 0);
MxU16 result = 0; MxU16 result = 0;
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
// original: m_info.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)); ASSIGN_M_FILE(SDL_IOFromFile(p_filename, "rb"));
if (M_FILE != HFILE_ERROR) { if (M_FILE != NULL) {
m_info.dwFlags = p_flags; m_info.dwFlags = p_flags;
if (m_info.dwFlags & MMIO_ALLOCBUF) { if (m_info.dwFlags & MMIO_ALLOCBUF) {
// Default buffer length of 8k if none specified // Default buffer length of 8k if none specified
MxLong len = m_info.cchBuffer; Sint64 len = m_info.cchBuffer;
if (len == 0) { if (len == 0) {
len = 8192; len = 8192;
} }
@ -66,7 +68,7 @@ MxU16 MXIOINFO::Open(const char* p_filename, MxULong p_flags)
} }
else { else {
m_info.cchBuffer = len; m_info.cchBuffer = len;
m_info.pchBuffer = (HPSTR) buf; m_info.pchBuffer = buf;
} }
m_info.pchNext = m_info.pchEndRead = m_info.pchBuffer; m_info.pchNext = m_info.pchEndRead = m_info.pchBuffer;
@ -88,7 +90,7 @@ MxU16 MXIOINFO::Close(MxLong p_unused)
if (RAW_M_FILE) { if (RAW_M_FILE) {
result = Flush(0); result = Flush(0);
_lclose(M_FILE); SDL_CloseIO(M_FILE);
ASSIGN_M_FILE(0); ASSIGN_M_FILE(0);
if (m_info.dwFlags & MMIO_ALLOCBUF) { if (m_info.dwFlags & MMIO_ALLOCBUF) {
@ -106,11 +108,11 @@ MxU16 MXIOINFO::Close(MxLong p_unused)
// FUNCTION: BETA10 0x1015e3b2 // FUNCTION: BETA10 0x1015e3b2
MxLong MXIOINFO::Read(void* p_buf, MxLong p_len) MxLong MXIOINFO::Read(void* p_buf, MxLong p_len)
{ {
MxLong bytesRead = 0; Sint64 bytesRead = 0;
if (m_info.pchBuffer) { if (m_info.pchBuffer) {
MxLong bytesLeft = m_info.pchEndRead - m_info.pchNext; Sint64 bytesLeft = m_info.pchEndRead - m_info.pchNext;
while (p_len > 0) { while (p_len > 0) {
if (bytesLeft > 0) { if (bytesLeft > 0) {
@ -139,11 +141,11 @@ MxLong MXIOINFO::Read(void* p_buf, MxLong p_len)
} }
} }
else if (RAW_M_FILE && p_len > 0) { else if (RAW_M_FILE && p_len > 0) {
bytesRead = _hread(M_FILE, p_buf, p_len); bytesRead = SDL_ReadIO(M_FILE, p_buf, p_len);
if (bytesRead == -1) { if (SDL_GetIOStatus(M_FILE) == SDL_IO_STATUS_ERROR) {
bytesRead = 0; bytesRead = 0;
m_info.lDiskOffset = _llseek(M_FILE, 0, SEEK_CUR); m_info.lDiskOffset = SDL_SeekIO(M_FILE, 0, SDL_IO_SEEK_CUR);
} }
else { else {
m_info.lDiskOffset += bytesRead; m_info.lDiskOffset += bytesRead;
@ -156,11 +158,11 @@ MxLong MXIOINFO::Read(void* p_buf, MxLong p_len)
// FUNCTION: BETA10 0x1015e4fc // FUNCTION: BETA10 0x1015e4fc
MxLong MXIOINFO::Write(void* p_buf, MxLong p_len) MxLong MXIOINFO::Write(void* p_buf, MxLong p_len)
{ {
MxLong bytesWritten = 0; Sint64 bytesWritten = 0;
if (m_info.pchBuffer) { if (m_info.pchBuffer) {
MxLong bytesLeft = m_info.pchEndWrite - m_info.pchNext; Sint64 bytesLeft = m_info.pchEndWrite - m_info.pchNext;
while (p_len > 0) { while (p_len > 0) {
if (bytesLeft > 0) { if (bytesLeft > 0) {
@ -192,11 +194,11 @@ MxLong MXIOINFO::Write(void* p_buf, MxLong p_len)
} }
} }
else if (RAW_M_FILE && p_len > 0) { else if (RAW_M_FILE && p_len > 0) {
bytesWritten = _hwrite(M_FILE, (const char*) p_buf, p_len); bytesWritten = SDL_WriteIO(M_FILE, p_buf, p_len);
if (bytesWritten == -1) { if (SDL_GetIOStatus(M_FILE) == SDL_IO_STATUS_ERROR) {
bytesWritten = 0; bytesWritten = 0;
m_info.lDiskOffset = _llseek(M_FILE, 0, SEEK_CUR); m_info.lDiskOffset = SDL_SeekIO(M_FILE, 0, SDL_IO_SEEK_CUR);
} }
else { else {
m_info.lDiskOffset += bytesWritten; m_info.lDiskOffset += bytesWritten;
@ -212,30 +214,30 @@ MxLong MXIOINFO::Write(void* p_buf, MxLong p_len)
// FUNCTION: LEGO1 0x100cca00 // FUNCTION: LEGO1 0x100cca00
// FUNCTION: BETA10 0x1015e6c4 // FUNCTION: BETA10 0x1015e6c4
MxLong MXIOINFO::Seek(MxLong p_offset, MxLong p_origin) MxLong MXIOINFO::Seek(MxLong p_offset, SDL_IOWhence p_origin)
{ {
MxLong result = -1; MxLong result = -1;
MxLong bytesRead; Sint64 bytesRead;
// If buffered I/O // If buffered I/O
if (m_info.pchBuffer) { if (m_info.pchBuffer) {
if (p_origin == SEEK_CUR) { if (p_origin == SDL_IO_SEEK_CUR) {
if (!p_offset) { if (!p_offset) {
// don't seek at all and just return where we are. // don't seek at all and just return where we are.
return m_info.lBufOffset + (m_info.pchNext - m_info.pchBuffer); return m_info.lBufOffset + (m_info.pchNext - m_info.pchBuffer);
} }
// With SEEK_CUR, p_offset is a relative offset. // With SDL_IO_SEEK_CUR, p_offset is a relative offset.
// Get the absolute position instead and use SEEK_SET. // Get the absolute position instead and use SDL_IO_SEEK_SET.
p_offset += m_info.lBufOffset + (m_info.pchNext - m_info.pchBuffer); p_offset += m_info.lBufOffset + (m_info.pchNext - m_info.pchBuffer);
p_origin = SEEK_SET; p_origin = SDL_IO_SEEK_SET;
} }
else if (p_origin == SEEK_END) { else if (p_origin == SDL_IO_SEEK_END) {
// not possible with buffered I/O // not possible with buffered I/O
return -1; return -1;
} }
// else p_origin == SEEK_SET. // else p_origin == SDL_IO_SEEK_SET.
// is p_offset between the start and end of the buffer? // is p_offset between the start and end of the buffer?
// i.e. can we do the seek without reading more from disk? // i.e. can we do the seek without reading more from disk?
@ -246,10 +248,10 @@ 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 (RAW_M_FILE && !Flush(0)) { if (RAW_M_FILE && !Flush(0)) {
m_info.lDiskOffset = _llseek(M_FILE, p_offset, p_origin); m_info.lDiskOffset = SDL_SeekIO(M_FILE, p_offset, p_origin);
if (m_info.lDiskOffset == -1) { if (m_info.lDiskOffset == -1) {
m_info.lDiskOffset = _llseek(M_FILE, 0, SEEK_CUR); m_info.lDiskOffset = SDL_SeekIO(M_FILE, 0, SDL_IO_SEEK_CUR);
} }
else { else {
@ -259,10 +261,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(M_FILE, m_info.lBufOffset, SEEK_SET); m_info.lDiskOffset = SDL_SeekIO(M_FILE, m_info.lBufOffset, SDL_IO_SEEK_SET);
if (m_info.lDiskOffset == -1) { if (m_info.lDiskOffset == -1) {
m_info.lDiskOffset = _llseek(M_FILE, 0, SEEK_CUR); m_info.lDiskOffset = SDL_SeekIO(M_FILE, 0, SDL_IO_SEEK_CUR);
} }
} }
@ -270,10 +272,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(M_FILE, m_info.pchBuffer, m_info.cchBuffer); bytesRead = SDL_ReadIO(M_FILE, m_info.pchBuffer, m_info.cchBuffer);
if (bytesRead == -1) { if (SDL_GetIOStatus(M_FILE) == SDL_IO_STATUS_ERROR) {
m_info.lDiskOffset = _llseek(M_FILE, 0, SEEK_CUR); m_info.lDiskOffset = SDL_SeekIO(M_FILE, 0, SDL_IO_SEEK_CUR);
} }
else { else {
m_info.lDiskOffset += bytesRead; m_info.lDiskOffset += bytesRead;
@ -297,16 +299,16 @@ MxLong MXIOINFO::Seek(MxLong p_offset, MxLong p_origin)
else if (RAW_M_FILE) { 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 == SDL_IO_SEEK_CUR && p_offset == 0) {
return m_info.lDiskOffset; return m_info.lDiskOffset;
} }
m_info.lDiskOffset = _llseek(M_FILE, p_offset, p_origin); m_info.lDiskOffset = SDL_SeekIO(M_FILE, p_offset, p_origin);
result = m_info.lDiskOffset; result = m_info.lDiskOffset;
if (result == -1) { if (result == -1) {
m_info.lDiskOffset = _llseek(M_FILE, 0, SEEK_CUR); m_info.lDiskOffset = SDL_SeekIO(M_FILE, 0, SDL_IO_SEEK_CUR);
} }
} }
@ -338,7 +340,7 @@ MxU16 MXIOINFO::SetBuffer(char* p_buf, MxLong p_len, MxLong p_unused)
MxU16 MXIOINFO::Flush(MxU16 p_unused) MxU16 MXIOINFO::Flush(MxU16 p_unused)
{ {
MxU16 result = 0; MxU16 result = 0;
MxLong bytesWritten; Sint64 bytesWritten;
// if buffer is dirty // if buffer is dirty
if (m_info.dwFlags & MMIO_DIRTY) { if (m_info.dwFlags & MMIO_DIRTY) {
@ -350,20 +352,20 @@ MxU16 MXIOINFO::Flush(MxU16 p_unused)
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(M_FILE, m_info.lBufOffset, SEEK_SET); m_info.lDiskOffset = SDL_SeekIO(M_FILE, m_info.lBufOffset, SDL_IO_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(M_FILE, 0, SEEK_CUR); m_info.lDiskOffset = SDL_SeekIO(M_FILE, 0, SDL_IO_SEEK_CUR);
} }
else { else {
bytesWritten = _hwrite(M_FILE, m_info.pchBuffer, cchBuffer); bytesWritten = SDL_WriteIO(M_FILE, m_info.pchBuffer, cchBuffer);
if (bytesWritten == -1 || bytesWritten != cchBuffer) { if (SDL_GetIOStatus(M_FILE) == SDL_IO_STATUS_ERROR || bytesWritten != cchBuffer) {
result = MMIOERR_CANNOTWRITE; result = MMIOERR_CANNOTWRITE;
m_info.lDiskOffset = _llseek(M_FILE, 0, SEEK_CUR); m_info.lDiskOffset = SDL_SeekIO(M_FILE, 0, SDL_IO_SEEK_CUR);
} }
else { else {
m_info.lDiskOffset += bytesWritten; m_info.lDiskOffset += bytesWritten;
@ -393,8 +395,8 @@ MxU16 MXIOINFO::Advance(MxU16 p_option)
MxULong rwmode = m_info.dwFlags & MMIO_RWMODE; MxULong rwmode = m_info.dwFlags & MMIO_RWMODE;
if (m_info.pchBuffer) { if (m_info.pchBuffer) {
MxLong cch = m_info.cchBuffer; Sint64 cch = m_info.cchBuffer;
MxLong bytesCounter; Sint64 bytesCounter;
// If we can and should write to the file, // If we can and should write to the file,
// if we are being asked to write to the file, // if we are being asked to write to the file,
@ -403,19 +405,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(M_FILE, m_info.lBufOffset, SEEK_SET); m_info.lDiskOffset = SDL_SeekIO(M_FILE, m_info.lBufOffset, SDL_IO_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(M_FILE, 0, SEEK_CUR); m_info.lDiskOffset = SDL_SeekIO(M_FILE, 0, SDL_IO_SEEK_CUR);
} }
else { else {
bytesCounter = _hwrite(M_FILE, m_info.pchBuffer, cch); bytesCounter = SDL_WriteIO(M_FILE, m_info.pchBuffer, cch);
if (bytesCounter == -1 || bytesCounter != cch) { if (SDL_GetIOStatus(M_FILE) == SDL_IO_STATUS_ERROR || bytesCounter != cch) {
result = MMIOERR_CANNOTWRITE; result = MMIOERR_CANNOTWRITE;
m_info.lDiskOffset = _llseek(M_FILE, 0, SEEK_CUR); m_info.lDiskOffset = SDL_SeekIO(M_FILE, 0, SDL_IO_SEEK_CUR);
} }
else { else {
m_info.lDiskOffset += bytesCounter; m_info.lDiskOffset += bytesCounter;
@ -429,20 +431,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(M_FILE, m_info.lBufOffset, SEEK_SET); m_info.lDiskOffset = SDL_SeekIO(M_FILE, m_info.lBufOffset, SDL_IO_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(M_FILE, 0, SEEK_CUR); m_info.lDiskOffset = SDL_SeekIO(M_FILE, 0, SDL_IO_SEEK_CUR);
} }
else { else {
bytesCounter = _hread(M_FILE, m_info.pchBuffer, cch); bytesCounter = SDL_ReadIO(M_FILE, m_info.pchBuffer, cch);
if (bytesCounter == -1) { if (SDL_GetIOStatus(M_FILE) == SDL_IO_STATUS_ERROR) {
result = MMIOERR_CANNOTREAD; result = MMIOERR_CANNOTREAD;
m_info.lDiskOffset = _llseek(M_FILE, 0, SEEK_CUR); m_info.lDiskOffset = SDL_SeekIO(M_FILE, 0, SDL_IO_SEEK_CUR);
} }
else { else {
m_info.lDiskOffset += bytesCounter; m_info.lDiskOffset += bytesCounter;
@ -461,11 +463,11 @@ MxU16 MXIOINFO::Advance(MxU16 p_option)
// FUNCTION: LEGO1 0x100cce60 // FUNCTION: LEGO1 0x100cce60
// FUNCTION: BETA10 0x1015edef // FUNCTION: BETA10 0x1015edef
MxU16 MXIOINFO::Descend(MMCKINFO* p_chunkInfo, const MMCKINFO* p_parentInfo, MxU16 p_descend) MxU16 MXIOINFO::Descend(ISLE_MMCKINFO* p_chunkInfo, const ISLE_MMCKINFO* p_parentInfo, MxU16 p_descend)
{ {
MxU16 result = 0; MxU16 result = 0;
MxULong ofs; MxULong ofs;
BOOL readOk; MxU32 readOk;
if (!p_chunkInfo) { if (!p_chunkInfo) {
return MMIOERR_BASE; // ? return MMIOERR_BASE; // ?
@ -491,15 +493,15 @@ MxU16 MXIOINFO::Descend(MMCKINFO* p_chunkInfo, const MMCKINFO* p_parentInfo, MxU
} }
} }
else { else {
ofs = MAXLONG; ofs = LONG_MAX;
if (p_parentInfo) { if (p_parentInfo) {
ofs = p_parentInfo->cksize + p_parentInfo->dwDataOffset; ofs = p_parentInfo->cksize + p_parentInfo->dwDataOffset;
} }
BOOL running = TRUE; MxU32 running = TRUE;
readOk = FALSE; readOk = FALSE;
MMCKINFO tmp; ISLE_MMCKINFO tmp;
tmp.dwFlags = 0; tmp.dwFlags = 0;
while (running) { while (running) {
@ -533,7 +535,7 @@ MxU16 MXIOINFO::Descend(MMCKINFO* p_chunkInfo, const MMCKINFO* p_parentInfo, MxU
else if (p_chunkInfo->ckid == tmp.ckid) { else if (p_chunkInfo->ckid == tmp.ckid) {
running = FALSE; running = FALSE;
} }
else if (Seek((tmp.cksize & 1) + tmp.cksize, SEEK_CUR) == -1) { else if (Seek((tmp.cksize & 1) + tmp.cksize, SDL_IO_SEEK_CUR) == -1) {
result = MMIOERR_CANNOTSEEK; result = MMIOERR_CANNOTSEEK;
running = FALSE; running = FALSE;
} }
@ -550,7 +552,7 @@ MxU16 MXIOINFO::Descend(MMCKINFO* p_chunkInfo, const MMCKINFO* p_parentInfo, MxU
} }
// FUNCTION: BETA10 0x1015f08b // FUNCTION: BETA10 0x1015f08b
MxU16 MXIOINFO::Ascend(MMCKINFO* p_chunkInfo, MxU16 p_ascend) MxU16 MXIOINFO::Ascend(ISLE_MMCKINFO* p_chunkInfo, MxU16 p_ascend)
{ {
MxLong ofs; MxLong ofs;
MxULong size; MxULong size;
@ -585,11 +587,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(M_FILE, ofs, SEEK_SET); m_info.lDiskOffset = SDL_SeekIO(M_FILE, ofs, SDL_IO_SEEK_SET);
if (m_info.lDiskOffset == ofs) { if (m_info.lDiskOffset == ofs) {
if (_lwrite(M_FILE, (char*) &size, 4) != 4) { if (SDL_WriteIO(M_FILE, (char*) &size, 4) != 4) {
m_info.lDiskOffset = _llseek(M_FILE, 0, SEEK_CUR); m_info.lDiskOffset = SDL_SeekIO(M_FILE, 0, SDL_IO_SEEK_CUR);
result = MMIOERR_CANNOTWRITE; result = MMIOERR_CANNOTWRITE;
} }
else { else {
@ -597,7 +599,7 @@ MxU16 MXIOINFO::Ascend(MMCKINFO* p_chunkInfo, MxU16 p_ascend)
} }
} }
else { else {
m_info.lDiskOffset = _llseek(M_FILE, 0, SEEK_CUR); m_info.lDiskOffset = SDL_SeekIO(M_FILE, 0, SDL_IO_SEEK_CUR);
result = MMIOERR_CANNOTSEEK; result = MMIOERR_CANNOTSEEK;
} }
} }
@ -606,7 +608,7 @@ MxU16 MXIOINFO::Ascend(MMCKINFO* p_chunkInfo, MxU16 p_ascend)
// Seek past the end of the chunk (plus optional pad byte if size is odd) // Seek past the end of the chunk (plus optional pad byte if size is odd)
if (result == 0 && if (result == 0 &&
Seek((p_chunkInfo->cksize & 1) + p_chunkInfo->cksize + p_chunkInfo->dwDataOffset, SEEK_SET) == -1) { Seek((p_chunkInfo->cksize & 1) + p_chunkInfo->cksize + p_chunkInfo->dwDataOffset, SDL_IO_SEEK_SET) == -1) {
result = MMIOERR_CANNOTSEEK; result = MMIOERR_CANNOTSEEK;
} }