Isle
Loading...
Searching...
No Matches
mxdsfile.cpp
Go to the documentation of this file.
1#include "mxdsfile.h"
2
3#include "decomp.h"
4#include "mxdebug.h"
5
6#include <stdio.h>
7
8#define SI_MAJOR_VERSION 2
9#define SI_MINOR_VERSION 2
10
12DECOMP_SIZE_ASSERT(MxDSFile::ChunkHeader, 0x0c)
14
15// FUNCTION: LEGO1 0x100cc4b0
16// FUNCTION: BETA10 0x1015db90
17MxDSFile::MxDSFile(const char* p_filename, MxULong p_skipReadingChunks)
18{
19 SetFileName(p_filename);
20 m_skipReadingChunks = p_skipReadingChunks;
21}
22
23// FUNCTION: LEGO1 0x100cc590
24// FUNCTION: BETA10 0x1015dc57
26{
27 MxResult result = -FAILURE; // Non-standard value of 1 here
28 memset(&m_io, 0, sizeof(MXIOINFO));
29
30 if (m_io.Open(m_filename.GetData(), p_uStyle) != 0) {
31 return -1;
32 }
33
34 m_io.SetBuffer(NULL, 0, 0);
35 m_position = 0;
36
37 if (m_skipReadingChunks == 0) {
38 result = ReadChunks();
39 }
40
41 if (result != SUCCESS) {
42 Close();
43 }
44 else {
45 Seek(0, SEEK_SET);
46 }
47
48 return result;
49}
50
51// FUNCTION: LEGO1 0x100cc620
52// FUNCTION: BETA10 0x1015dd18
53MxResult MxDSFile::ReadChunks()
54{
55 _MMCKINFO topChunk;
56 _MMCKINFO childChunk;
57 char tempBuffer[80];
58
59 topChunk.fccType = FOURCC('O', 'M', 'N', 'I');
60 if (m_io.Descend(&topChunk, NULL, MMIO_FINDRIFF) != 0) {
61 MxTrace("Unable to find Streamer RIFF chunk in file: %s\n", m_filename);
62 return FAILURE;
63 }
64
65 childChunk.ckid = FOURCC('M', 'x', 'H', 'd');
66 if (m_io.Descend(&childChunk, &topChunk, 0) != 0) {
67 MxTrace("Unable to find Header chunk in file: %s\n", m_filename);
68 return FAILURE;
69 }
70
71 m_io.Read(&m_header, 0x0c);
72 if ((m_header.m_majorVersion != SI_MAJOR_VERSION) || (m_header.m_minorVersion != SI_MINOR_VERSION)) {
73 sprintf(tempBuffer, "Wrong SI file version. %d.%d expected.", SI_MAJOR_VERSION, SI_MINOR_VERSION);
74 MessageBoxA(NULL, tempBuffer, NULL, MB_ICONERROR);
75 return FAILURE;
76 }
77
78 childChunk.ckid = FOURCC('M', 'x', 'O', 'f');
79 if (m_io.Descend(&childChunk, &topChunk, 0) != 0) {
80 MxTrace("Unable to find Header chunk in file: %s\n", m_filename);
81 return FAILURE;
82 }
83
84 m_io.Read(&m_lengthInDWords, 4);
87 return SUCCESS;
88}
89
90// FUNCTION: LEGO1 0x100cc740
91// FUNCTION: BETA10 0x1015ded2
93{
94 m_io.Close(0);
95 m_position = -1;
96 memset(&m_header, 0, sizeof(m_header));
97 if (m_lengthInDWords != 0) {
99 delete[] m_pBuffer;
100 m_pBuffer = NULL;
101 }
102
103 return SUCCESS;
104}
105
106// FUNCTION: LEGO1 0x100cc780
107// FUNCTION: BETA10 0x1015df50
108MxResult MxDSFile::Read(unsigned char* p_buf, MxULong p_nbytes)
109{
110 if (m_io.Read(p_buf, p_nbytes) != p_nbytes) {
111 return FAILURE;
112 }
113
114 m_position += p_nbytes;
115 return SUCCESS;
116}
117
118// FUNCTION: LEGO1 0x100cc7b0
119// FUNCTION: BETA10 0x1015dfee
120MxResult MxDSFile::Seek(MxLong p_lOffset, MxS32 p_iOrigin)
121{
122 m_position = m_io.Seek(p_lOffset, p_iOrigin);
123 if (m_position == -1) {
124 return FAILURE;
125 }
126
127 return SUCCESS;
128}
129
130// FUNCTION: LEGO1 0x100cc7e0
131// FUNCTION: BETA10 0x10148d80
133{
134 return m_header.m_bufferSize;
135}
136
137// FUNCTION: LEGO1 0x100cc7f0
138// FUNCTION: BETA10 0x10148da0
140{
141 return m_header.m_streamBuffersNum;
142}
[AI] A wrapper for low-level file I/O, abstracting MMIOINFO functionality, and providing additional b...
Definition: mxio.h:22
MxU16 Descend(MMCKINFO *p_chunkInfo, const MMCKINFO *p_parentInfo, MxU16 p_descend)
[AI] Descends into a specific RIFF or LIST chunk in a hierarchical file, reading chunk headers and up...
Definition: mxio.cpp:464
MxLong Seek(MxLong p_offset, MxLong p_origin)
[AI] Seeks to a specific position in the file (or file buffer).
Definition: mxio.cpp:215
MxU16 SetBuffer(char *p_buf, MxLong p_len, MxLong p_unused)
[AI] Changes the buffer used for internal I/O (applies and releases old buffer if owned).
Definition: mxio.cpp:318
MxLong Read(void *p_buf, MxLong p_len)
[AI] Reads a number of bytes from the file or buffer into the destination buffer.
Definition: mxio.cpp:107
MxU16 Open(const char *p_filename, MxULong p_flags)
[AI] Opens a file for buffered/unbuffered I/O, initializing MMIOINFO and (optionally) internal buffer...
Definition: mxio.cpp:38
MxU16 Close(MxLong p_unused)
[AI] Closes the file and releases its resources, including buffer memory if owned.
Definition: mxio.cpp:85
[AI] Represents a source file handler for SI (Streamer Interface) files, providing buffered access fo...
Definition: mxdsfile.h:20
MxResult Read(unsigned char *, MxULong) override
[AI] Reads up to the specified number of bytes from the file into a buffer.
Definition: mxdsfile.cpp:108
MxResult Seek(MxLong, MxS32) override
[AI] Seeks to an absolute or relative position in the file.
Definition: mxdsfile.cpp:120
MxResult Close() override
[AI] Closes the file and releases internal file buffers.
Definition: mxdsfile.cpp:92
MxULong GetStreamBuffersNum() override
[AI] Returns the number of streaming buffers, as specified in the SI file header.
Definition: mxdsfile.cpp:139
MxResult Open(MxULong) override
[AI] Opens the SI file for reading or writing with the specified style flags.
Definition: mxdsfile.cpp:25
MxULong GetBufferSize() override
[AI] Returns the SI chunk's buffer size, as obtained from the SI file header.
Definition: mxdsfile.cpp:132
[AI] Abstract base class representing a source of streamable data, providing an interface for reading...
Definition: mxdssource.h:20
MxLong m_position
[AI] Current seek/read/write position in the source. -1 if uninitialized, otherwise logical/physical ...
Definition: mxdssource.h:127
MxU32 * m_pBuffer
[AI] Pointer to allocated buffer, or NULL if uninitialized. Contains the raw read/streamed data.
Definition: mxdssource.h:126
MxULong m_lengthInDWords
[AI] Length of valid data in the stream, in DWORDs (32-bit units). Used for sizing and range checking...
Definition: mxdssource.h:125
char * GetData() const
Returns a pointer to the internal character buffer.
Definition: mxstring.h:110
#define DECOMP_SIZE_ASSERT(T, S)
Definition: decomp.h:19
#define NULL
[AI] Null pointer value (C/C++ semantics).
Definition: legotypes.h:26
#define FAILURE
[AI] Used to indicate a failed operation in result codes.
Definition: legotypes.h:34
#define SUCCESS
[AI] Used to indicate a successful operation in result codes.
Definition: legotypes.h:30
#define MxTrace(args)
[AI] Macro for trace logging (non-variadic version, MSVC compatibility), expands to nothing.
Definition: mxdebug.h:55
#define SI_MAJOR_VERSION
Definition: mxdsfile.cpp:8
#define SI_MINOR_VERSION
Definition: mxdsfile.cpp:9
MxLong MxResult
[AI]
Definition: mxtypes.h:106
int MxLong
[AI]
Definition: mxtypes.h:83
#define FOURCC(a, b, c, d)
[AI] Macro to compose a 32-bit code from four 8-bit characters (e.g., for resource and chunk IDs).
Definition: mxtypes.h:162
signed int MxS32
[AI]
Definition: mxtypes.h:38
unsigned int MxULong
[AI]
Definition: mxtypes.h:93
unsigned int MxU32
[AI]
Definition: mxtypes.h:32
MxS16 m_majorVersion
[AI] Major SI file version (e.g., 2 for LEGO Island SI files).
Definition: mxdsfile.h:120
MxS16 m_streamBuffersNum
[AI] Number of streaming buffers for this SI file.
Definition: mxdsfile.h:123
MxS16 m_minorVersion
[AI] Minor SI file version (e.g., 2 for LEGO Island SI files).
Definition: mxdsfile.h:121
MxULong m_bufferSize
[AI] Buffer size defined for the SI stream (bytes).
Definition: mxdsfile.h:122