-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCClmReader.cpp
343 lines (278 loc) · 8.28 KB
/
CClmReader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include "stdafx.h"
#include "CClmReader.h"
#include "CMemoryStreamReader.h"
#include "GlobalFunctions.h"
extern int g_cLocks;
const char CClmReader::tagClmFile[] = "OP2 Clump File Version 1.0\x01A";
ULONG __stdcall CClmReader::AddRef()
{
return ++m_cRef;
}
ULONG __stdcall CClmReader::Release()
{
if (--m_cRef != 0)
return m_cRef;
delete this;
return 0;
}
HRESULT __stdcall CClmReader::QueryInterface(REFIID riid, void** ppv)
{
if(riid == IID_IUnknown)
*ppv = (IUnknown*)this;
else if(riid == IID_ArchiveReader)
*ppv = (ArchiveReader*)this;
else
{
*ppv = NULL;
return E_NOINTERFACE;
}
AddRef();
return S_OK;
}
// ArchiveReader
// *************
HRESULT CClmReader::get_NumFiles(int *numFiles)
{
*numFiles = this->numFiles;
return S_OK;
}
HRESULT CClmReader::get_FileName(int index, BSTR *fileName)
{
int stringLength;
// Cache string length
for (stringLength = 0; stringLength < 8; stringLength++)
if (indexTable[index].fileName[stringLength] == NULL) break;
// Allocate space for the string
SysReAllocStringLen(fileName, NULL, stringLength);
// Convert the string to Unicode
MultiByteToWideChar(CP_ACP, 0, indexTable[index].fileName,
stringLength, *fileName, stringLength);
return S_OK;
}
HRESULT CClmReader::OpenStreamRead(BSTR fileName, StreamReader **stream)
{
int fileIndex;
int numBytesRead;
CMemoryStreamReader *memStream;
HRESULT hr;
LPVOID buffer;
int bAttachToBuffer = 0;
// Initialize returned stream pointer to NULL
*stream = NULL;
// Lookup the filename in the index
fileIndex = GetFileIndex(fileName);
// Check if the file was not found
if (fileIndex == -1)
{
// File was not found
return E_INVALIDARG;
}
// Cache the data buffer pointer
buffer = dataBuffer[fileIndex];
// Check if data hasn't been loaded yet
if (buffer == NULL)
{
// Load the data from the stream
// Allocate a buffer for this file
buffer = new char[indexTable[fileIndex].size];
// Seek to the start of the file
attachedStream->Seek(streamStartOffset + indexTable[fileIndex].offset);
// Read in the file data
attachedStream->Read(indexTable[fileIndex].size, (int)dataBuffer[fileIndex], &numBytesRead);
bAttachToBuffer = 1;
}
// Create a stream to read the data
try
{
// **TODO** Create a new reader class that will load data
// from the origianl CLM file. Would make rebuilding the
// CLM file much less memory intensive.
// Create a memory stream reading with the file contents
memStream = new CMemoryStreamReader(indexTable[fileIndex].size,
(char*)(dataBuffer[fileIndex]),
bAttachToBuffer);
if (memStream == NULL)
{
// Check if buffer is discardable
if (bAttachToBuffer == 1)
delete [] buffer;
// Return error code
return E_OUTOFMEMORY;
}
}
catch(...)
{
// Check if buffer is discardable
if (bAttachToBuffer == 1)
delete [] buffer;
// Return error code
return E_FAIL;
}
hr = memStream->QueryInterface(IID_StreamReader, (void**)stream);
memStream->Release();
return hr;
}
// Class specific
// **************
CClmReader::CClmReader(SeekableStreamReader *inStream, int bAttachToStream) : m_cRef(1)
{
// Initialize variables
numFiles = 0;
indexTable = NULL;
dataBuffer = NULL;
attachedStream = NULL;
// Check if reader should attach to the stream
if (bAttachToStream == 1)
{
attachedStream = inStream;
inStream->AddRef();
}
// Load the Clm info from the stream
if (LoadClm(inStream, bAttachToStream) != 0)
{
// Error loading Clm info
FreeMemory();
throw L"Error loading CLM info.";
}
g_cLocks++;
}
CClmReader::~CClmReader()
{
g_cLocks--;
FreeMemory();
// Check if reader attached to a stream
if (attachedStream != NULL)
attachedStream->Release();
}
void CClmReader::FreeMemory()
{
unsigned int i;
// Check if the index table needs to be deleted
if (indexTable != NULL)
delete [] indexTable; // Delete the index table
// Check if the array of data buffer pointers needs to be deleted
if (dataBuffer != NULL)
{
// Release each data buffer
for (i = 0; i < numFiles; i++)
{
// Check if the data buffer exists
if (dataBuffer[i] != NULL)
delete [] dataBuffer[i]; // Delete the data buffer
}
// Delete the array of data buffer pointers
delete [] dataBuffer;
}
// Reset variables
indexTable = NULL;
dataBuffer = NULL;
numFiles = 0;
}
int CClmReader::GetFileIndex(BSTR fileName)
{
int stringLength;
char *tempString;
unsigned int i;
// Allocate space for a temporary ASCII fileName string
stringLength = SysStringLen(fileName);
tempString = new char[stringLength+1];
tempString[stringLength] = 0; // NULL terminate it
// Convert the BSTR to ASCII
WideCharToMultiByte(CP_ACP, 0, fileName, stringLength, tempString, stringLength, 0, 0);
// **TODO** Improve this to a binary search? Or is that not general enough
// Outpost2 doesn't seem to require a sorted index
// Perform a linear search of the index
for (i = 0; i < numFiles; i++)
{
// Check if fileName matches this index entry
if (strncmp(indexTable[i].fileName, tempString, 8) == 0)
{
// File names match. Return index
delete [] tempString;
return i;
}
}
delete [] tempString;
return -1; // File not found
}
int CClmReader::LoadClm(SeekableStreamReader *inStream, int bAttachToStream)
{
char buff[32];
int numBytesRead;
unsigned int i, j, maxOffset;
try
{
// Record the starting stream position for later seeking
// (Note: This allows CLM contents to be part of a larger stream)
inStream->get_ReadOffset(&streamStartOffset);
// Read header tag
inStream->Read(32, (int)buff, &numBytesRead);
// Check if header tag is invalid
if (strncmp(buff, tagClmFile, 32) != 0)
throw L"Format Error. Invalid Clump file header tag.";
// Read WAVEFORMATEX structure
inStream->Read(sizeof(waveFormat), (int)&waveFormat, &numBytesRead);
// Check for format errors
if (waveFormat.wFormatTag != WAVE_FORMAT_PCM)
throw L"Format Error. Wave format is not encoded as WAVE_FORMAT_PCM.";
if (waveFormat.nChannels != 1)
throw L"Format Error. Wave format is not mono channel.";
if (waveFormat.nBlockAlign != 2)
throw L"Format Error. Wave format is not block aligned on a 2 byte boundary.";
if (waveFormat.nSamplesPerSec != 22050)
throw L"Format Error. Wave format is not 22050 samples per second.";
// Pass over alignment bytes
inStream->Read(2, (int)buff, &numBytesRead);
// Read ??? **TODO** Find out what this DWORD means
inStream->Read(4, (int)&unknown, &numBytesRead);
// Read numFiles stored in CLM
inStream->Read(4, (int)&numFiles, &numBytesRead);
// Allocate space for CLM index table
indexTable = new IndexEntry[numFiles];
// Read CLM index table
inStream->Read(numFiles*sizeof(IndexEntry), (int)indexTable, &numBytesRead);
// Allocate space to hold all the file buffer pointers
dataBuffer = new LPVOID[numFiles];
memset(dataBuffer, 0, numFiles*sizeof(LPVOID));
// Check if data needs to be loaded into memory
if (bAttachToStream == 0)
{
// Data must be loaded into memory
// Load each internal file
for (i = 0; i < numFiles; i++)
{
// Allocate a buffer for this file
dataBuffer[i] = new char[indexTable[i].size];
// Seek to the start of the file
inStream->Seek(streamStartOffset + indexTable[i].offset);
// Read in the file data
inStream->Read(indexTable[i].size, (int)dataBuffer[i], &numBytesRead);
}
}
// Determine maximum offset of CLM data
maxOffset = 0;
j = 0;
for (i = 0; i < numFiles; i++)
{
j = indexTable[i].offset + indexTable[i].size;
if (j > maxOffset)
maxOffset = j;
}
if (maxOffset < 0x3C + numFiles*sizeof(IndexEntry) + 1)
maxOffset = 0x3C + numFiles*sizeof(IndexEntry) + 1;
// Seek to end of CLM data
inStream->Seek(streamStartOffset + maxOffset);
}
catch(WCHAR *errorMsg)
{
// Return a COM error message
PostErrorMsg(errorMsg);
return 1; // Failed to load file
}
catch(...)
{
// Error loading Clm data
return -1; // Failed to load file
}
return 0; // Success
}