-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDXMemBuffer.cpp
More file actions
635 lines (515 loc) · 14.2 KB
/
Copy pathDXMemBuffer.cpp
File metadata and controls
635 lines (515 loc) · 14.2 KB
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
#include "Common.h"
#include "stdafx.h"
#include "cliparams.h"
#include "console.h"
// do we use xbox internal buffering or try to dma directly into our structures?
#define MB_BUFFERING 0
#ifdef _DIRECTX
#include "MemBuffer.h" //(sic)
#ifndef SIMPLE_MESHES
#include "debuglog.h"
#endif
//#include "Globals.h"
//******************************************************************************************
//#define MEM_BUFFER_SIZE 1024000
#define READ_MEM_BUFFER_SIZE (64 * 1024)
#if TARGET==PC
#define WRITE_MEM_BUFFER_SIZE (2 * 1024 * 1024)
#else
#define WRITE_MEM_BUFFER_SIZE (10 * 1024)
#endif
UINT CDXMemBuffer::mNextReadBufferSize = READ_MEM_BUFFER_SIZE;
//******************************************************************************************
void CDXMemBuffer::SetNextReadBufferSize(UINT size)
{
// has to be a cluster size. Well, it doesn't at the moment but will one day, maybe.
ASSERT(!(size & 0x7fff));
if (size == 0)
{
// default
mNextReadBufferSize = READ_MEM_BUFFER_SIZE;
}
else
{
mNextReadBufferSize = size;
}
}
//******************************************************************************************
CDXMemBuffer::CDXMemBuffer()
{
mData = NULL;
mCRCData = NULL;
mCRCDataUpTo = NULL;
mCRCFile = NULL;
}
//******************************************************************************************
CDXMemBuffer::~CDXMemBuffer()
{
// ASSERT(!(mData)); // should be closed by now...
delete [] mData;
delete [] mCRCData;
};
//******************************************************************************************
BOOL CDXMemBuffer::InitFromMem(const char *fname,EMemoryType memtype)
{
mData = new( memtype ) char[WRITE_MEM_BUFFER_SIZE];
mMemType = memtype;
if (!mData)
{
TRACE("Could not allocate memory for write buffer\n");
return FALSE;
}
mPtr = mData;
mSize = WRITE_MEM_BUFFER_SIZE;
mReading = FALSE;
mEOF = FALSE;
mDataSize = 0;
mPos=0;
// cache file name (for close):
#if TARGET == PC
strncpy( mFileName, fname, 256 );
#elif TARGET == XBOX
sprintf(mFileName, "T:\\%s", fname);
LOG.AddMessage("*CDXMemBuffer* Trying to open file %s\n", mFileName);
#endif
// mFile = fopen(mFileName, "wb");
mFile = CreateFile(mFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
// if (!mFile)
if (mFile == INVALID_HANDLE_VALUE)
{
TRACE("Membuffer could not open file for writing\n");
#if TARGET==XBOX
LOG.AddMessage("*CDXMemBuffer* Membuffer could not open file for writing");
#endif
SAFE_DELETE(mData);
return(FALSE);
}
char crcname[MAX_PATH];
sprintf(crcname, "%s.crc", mFileName);
mCRCFile = fopen(crcname, "wb");
return TRUE;
}
void data_bad()
{
CONSOLE.RenderDiscFailureTextAndHang();
}
//******************************************************************************************
BOOL CDXMemBuffer::InitFromFile(const char *fname,EMemoryType memtype,BOOL mungepath,UINT startskip)
{
mData = new( memtype ) char[mNextReadBufferSize];
mMemType = memtype;
if (!mData)
{
TRACE("Could not allocate memory for read buffer\n");
return FALSE;
}
mPtr = mData;
mSize = mNextReadBufferSize;
mReading = TRUE;
mEOF = FALSE;
mLastBlock = FALSE;
mDataSize = 0;
mPos=0;
DWORD flags;
if (MB_BUFFERING) flags = FILE_ATTRIBUTE_NORMAL ;
else flags = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING;
// load file, and the CRC data too.
char crcdataname[256];
#if TARGET == PC
mFile = CreateFile(fname, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, flags, NULL);
sprintf(crcdataname, "%s.crc", fname);
// mFile = fopen(fname, "rb");
#elif TARGET == XBOX
// if we're given an absolute path, we don't want to go adding to it.
if (mungepath && fname[1] != ':')
{
char temp[256];
sprintf(temp, "%s\\%s\0", CLIPARAMS.mBasePath, fname);
// mFile = fopen(temp, "rb");
mFile = CreateFile(temp, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, flags, NULL);
sprintf(crcdataname, "%s.crc", temp);
}
else
{
mFile = CreateFile(fname, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, flags, NULL);
sprintf(crcdataname, "%s.crc", fname);
// mFile=fopen(fname,"rb");
}
#endif
if (mFile == INVALID_HANDLE_VALUE)
{
#if TARGET == XBOX
LOG.AddMessage("*CDXMemBuffer*: Failed to open file %s", fname);
#endif
delete mData;
mData = NULL;
return FALSE;
}
// Read CRC data.
// Stuff the CRC's - surely the DVD player will see the problem?
FILE *crc = NULL;//fopen(crcdataname, "rb");
if (crc)
{
DWORD crc_size = GetFileSize();
if (crc_size == DWORD(-1))
{
ASSERT(0);
crc_size = 1;
}
else
{
// round it up to the next buffer size.
crc_size += mNextReadBufferSize - 1;
crc_size /= mNextReadBufferSize;
// we've got one byte per block of resource.
}
mCRCData = new char[crc_size];
mCRCDataUpTo = mCRCData;
if (fread(mCRCData, 1, crc_size, crc) != crc_size)
{
// oh dear, that didn't load either.
data_bad();
}
}
else
{
// If we don't have the CRC file, we don't care because all this CRC stuff is frankly bollocks anyway.
// How can bad data get past the DVD controller? It can't.
}
// Sometimes we don't want the first bit of the file, why load it?
DWORD amount_moved;
if (startskip)
{
amount_moved = SetFilePointer(mFile, startskip / mNextReadBufferSize * mNextReadBufferSize, NULL, FILE_BEGIN);
}
else
{
amount_moved = 0;
}
if (amount_moved == 0xffffffff) data_bad();
// and do the move in our counter too.
mPos += amount_moved;
// read first block:
LoadNextBlock();
mPtr = mData;
#if TARGET == XBOX
LOG.AddMessage("DXMemBuffer: Success opening file %s", fname);
#endif
if (startskip - amount_moved > 0)
{
// There's a bit more skipping to do.
Skip(startskip - amount_moved);
}
return TRUE;
}
//******************************************************************************************
SINT CDXMemBuffer::ConvertFromWritingToReading()
{
mReading = TRUE;
mPtr = mData;
return mDataSize;
}
//******************************************************************************************
static char eval_crc(const char *data, int num_bytes)
{
char retval = 0;
while (num_bytes)
{
retval += *data++;
num_bytes--;
}
return retval;
}
// if it's a really bad winerror we'll just hang with an error message.
static void how_bad_is_that()
{
switch(GetLastError())
{
case ERROR_HANDLE_EOF:
// That's fine.
return;
default:
// It's pointless having all these errors listed because of the "default" above
// but now I've searched for them I feel like leaving them
case ERROR_ARENA_TRASHED:
case ERROR_INVALID_BLOCK:
case ERROR_BAD_FORMAT:
case ERROR_INVALID_DATA:
case ERROR_BAD_UNIT:
case ERROR_NOT_READY:
case ERROR_CRC:
case ERROR_NOT_DOS_DISK:
case ERROR_READ_FAULT:
case ERROR_GEN_FAILURE:
case ERROR_OPEN_FAILED:
case ERROR_DISK_OPERATION_FAILED:
data_bad();
}
}
//******************************************************************************************
void CDXMemBuffer::LoadNextBlock()
{
if (!ReadFile(mFile, mData, mNextReadBufferSize, (DWORD *)&mDataSize, NULL))
{
// failed.
how_bad_is_that();
mDataSize = 0;
}
if( mDataSize != mSize )
{
mLastBlock = TRUE;
}
mPtr = mData;
// Either way, we now have some data, and we have to check it for CRC.
if (mCRCData && eval_crc(mData, mDataSize) != *mCRCDataUpTo)
{
// the crc is wrong!
data_bad();
}
// go to next block of CRC data.
mCRCDataUpTo++;
}
//******************************************************************************************
UINT CDXMemBuffer::GetFileSize()
{
ASSERT(mReading);
return ::GetFileSize(mFile, NULL);
/*
UINT retval;
UINT current=ftell(mFile);
fseek(mFile,0,SEEK_END);
UINT l=ftell(mFile);
fseek(mFile,current,SEEK_SET);
mThread.ReleaseMutex();
return(l);*/
}
//******************************************************************************************
// very very much like reading because most of the Skip's are very tiny
SINT CDXMemBuffer::Skip(SINT size)
{
if (!size) return 0;
ASSERT(mReading);
int bytesread=0;
if( mLastBlock && mPtr + size > mData + mDataSize )
{
mEOF = TRUE;
size = ( mData + mDataSize - mPtr );
}
if( size > 0 )
{
while( mPtr + size > mData + mDataSize )
{
UINT lBytesToRead = mData + mDataSize - mPtr;
bytesread+=lBytesToRead;
size -= lBytesToRead;
LoadNextBlock();
if( mLastBlock && mPtr + size > mData + mDataSize )
{
mEOF = TRUE;
size = ( mData + mDataSize - mPtr );
}
}
bytesread+=size;
mPtr += size;
}
ASSERT(mPtr <= mData + mDataSize);
mPos+=bytesread;
return(bytesread);
}
//******************************************************************************************
SINT CDXMemBuffer::Read(void *data, SINT size)
{
ASSERT(mReading);
int bytesread=0;
char * lpData = (char *)data;
if( mLastBlock && mPtr + size > mData + mDataSize )
{
mEOF = TRUE;
size = ( mData + mDataSize - mPtr );
}
if( size > 0 )
{
while( mPtr + size > mData + mDataSize )
{
// We've haven't loaded enough. copy what we have...
UINT lBytesToRead = mData + mDataSize - mPtr;
bytesread+=lBytesToRead;
memcpy( lpData, mPtr, lBytesToRead );
lpData += lBytesToRead;
size -= lBytesToRead;
// And now, what's the point of doing block stuff and a memcpy if it's huge?
// Now we've got non-buffered reads we have to be careful about reading the right size blocks to the right place
// (because it's DMA)
if (0)//!THREADED && size >= mNextReadBufferSize && !(int(lpData) & 3))
{
DWORD amount_loaded;
// do a nice number of blocks (actually should be a multiple of sector size but what the hell
DWORD amount_to_load = size / mNextReadBufferSize * mNextReadBufferSize;
if (!ReadFile(mFile, lpData, amount_to_load, &amount_loaded, NULL))
{
// failed.
how_bad_is_that();
amount_loaded = 0;
}
bytesread += amount_loaded;
lpData += amount_loaded;
size -= amount_loaded;
mPtr = mData;
mDataSize = 0;
if (amount_loaded != amount_to_load)
mLastBlock = true;
}
// we want a small amount now, just do normal stuff.
LoadNextBlock();
if( mLastBlock && mPtr + size > mData + mDataSize )
{
mEOF = TRUE;
size = ( mData + mDataSize - mPtr );
}
}
bytesread+=size;
memcpy( lpData, mPtr, size );
mPtr += size;
}
ASSERT(mPtr <= mData + mDataSize);
mPos+=bytesread;
return(bytesread);
}
//******************************************************************************************
void CDXMemBuffer::ReadString(char *data, SINT maxchars)
{
ASSERT(mReading);
// calculate number of bytes to read:
UINT lBytesRead = 0;
for( SINT i=0; i<maxchars-1; ++i )
{
if( mPtr >= mData + mDataSize )
{
if( mLastBlock )
{
mEOF = TRUE;
break;
}
else
{
LoadNextBlock();
}
}
data[lBytesRead++] = *(mPtr++);
if (data[lBytesRead-1] == 10)
{
break;
}
}
if( lBytesRead > 1 )
{
if( data[lBytesRead-2] == 0x0d )
{
data[lBytesRead-2] = '\n';
data[lBytesRead-1] = '\0';
}
}
data[lBytesRead] = '\0';
mPos+=lBytesRead;
ASSERT(mPtr <= mData + mDataSize);
}
//******************************************************************************************
static void write_crc_data(FILE *file, const char *data, SINT size, SINT buffersize)
{
if (file)
{
for (; size > 0; size -= buffersize, data += buffersize)
{
char crc;
if (size >= buffersize) crc = eval_crc(data, buffersize);
else crc = eval_crc(data, size );
if (fwrite(&crc, 1, 1, file) != 1)
{
SASSERT(0, "Can't write CRC!");
}
}
}
}
//******************************************************************************************
void CDXMemBuffer::Write(void *data, SINT size)
{
ASSERT(!mReading);
DWORD bytes_written;
while(size > 0 && mPtr+size>mData+mSize)
{
// Would run off end of buffer - write out buffer
// But because of the CRC we have to write out precise buffer-size blocks.
SINT bytes_to_write = mSize - (mPtr - mData);
memcpy(mPtr, data, bytes_to_write);
// We save out crc data.
write_crc_data(mCRCFile, mData, mSize, mNextReadBufferSize);
// and then the actual data.
if (!WriteFile(mFile, mData, mSize, &bytes_written, NULL))
{
TRACE("Write failed\n");
DWORD i = GetLastError();
}
mDataSize=0;
mPtr=mData;
size -= bytes_to_write;
mPos += bytes_to_write;
data = (void *)(((char *)data) + bytes_to_write);
}
if (size > 0)
{
// Buffer the last bit.
memcpy(mPtr, data, size);
mPtr += size;
mDataSize += size;
mPos+=size;
}
}
//******************************************************************************************
BOOL CDXMemBuffer::Close()
{
if( mData )
{
if (mReading)
{
// just free up - easy
CloseHandle(mFile);
// fclose( mFile );
delete [] mData;
mData = NULL;
delete [] mCRCData;
mCRCData = NULL;
return TRUE;
}
// otherwise write out the stream.
// fwrite(mData, mDataSize, 1, mFile);
DWORD bytes_written;
if (!WriteFile(mFile, mData, mDataSize, &bytes_written, NULL))
{
TRACE("Write failed");
}
write_crc_data(mCRCFile, mData, mDataSize, mNextReadBufferSize);
CloseHandle(mFile);
// fclose(mFile);
fclose(mCRCFile);
mCRCFile = 0;
// free up
delete [] mData;
mData = NULL;
return TRUE;
}
return FALSE;
}
//******************************************************************************************
void CDXMemBuffer::DeclareInvalidData(CThing *t)
{
char text[300];
sprintf(text, "Invalid data in Thing"); // can't link _GetClassName() without editor...
// CONSOLE.AddString(text);
}
//******************************************************************************************
BOOL CDXMemBuffer::EndOfFile()
{
return( mEOF );
}
//******************************************************************************************
#endif