1
0
Fork 0
mirror of https://git.rip/DMCA_FUCKER/re3.git synced 2024-11-10 20:39:15 +00:00
re3/src/core/CdStream.cpp

510 lines
10 KiB
C++
Raw Normal View History

2020-04-21 10:28:06 +00:00
#define WITHWINDOWS
2019-06-17 13:32:38 +00:00
#include "common.h"
2020-04-17 13:31:11 +00:00
2019-06-17 13:32:38 +00:00
#include "CdStream.h"
#include "rwcore.h"
#include "RwHelper.h"
2019-07-31 15:54:18 +00:00
#define CDDEBUG(f, ...) debug ("%s: " f "\n", "cdvd_stream", ## __VA_ARGS__)
#define CDTRACE(f, ...) printf("%s: " f "\n", "cdvd_stream", ## __VA_ARGS__)
2019-06-17 13:32:38 +00:00
struct CdReadInfo
{
uint32 nSectorOffset;
uint32 nSectorsToRead;
void *pBuffer;
char field_C;
bool bLocked;
bool bInUse;
char _pad0;
int32 nStatus;
HANDLE hSemaphore;
HANDLE hFile;
OVERLAPPED Overlapped;
};
VALIDATE_SIZE(CdReadInfo, 0x30);
char gCdImageNames[MAX_CDIMAGES+1][64];
int32 gNumImages;
int32 gNumChannels;
HANDLE gImgFiles[MAX_CDIMAGES];
HANDLE _gCdStreamThread;
HANDLE gCdStreamSema;
DWORD _gCdStreamThreadId;
CdReadInfo *gpReadInfo;
Queue gChannelRequestQ;
int32 lastPosnRead;
BOOL _gbCdStreamOverlapped;
BOOL _gbCdStreamAsync;
DWORD _gdwCdStreamFlags;
2020-03-27 19:53:47 +00:00
DWORD WINAPI CdStreamThread(LPVOID lpThreadParameter);
2019-06-17 13:32:38 +00:00
void
CdStreamInitThread(void)
{
SetLastError(0);
if ( gNumChannels > 0 )
{
for ( int32 i = 0; i < gNumChannels; i++ )
{
2019-06-30 10:53:39 +00:00
gpReadInfo[i].hSemaphore = CreateSemaphore(nil, 0, 2, nil);
2019-06-17 13:32:38 +00:00
2019-06-30 10:53:39 +00:00
if ( gpReadInfo[i].hSemaphore == nil )
2019-06-17 13:32:38 +00:00
{
CDTRACE("failed to create sync semaphore");
ASSERT(0);
return;
}
}
}
gChannelRequestQ.items = (int32 *)LocalAlloc(LMEM_ZEROINIT, sizeof(int32) * (gNumChannels + 1));
gChannelRequestQ.head = 0;
gChannelRequestQ.tail = 0;
gChannelRequestQ.size = gNumChannels + 1;
2019-06-30 10:53:39 +00:00
ASSERT(gChannelRequestQ.items != nil );
2019-06-17 13:32:38 +00:00
2019-06-30 10:53:39 +00:00
gCdStreamSema = CreateSemaphore(nil, 0, 5, "CdStream");
2019-06-17 13:32:38 +00:00
2019-06-30 10:53:39 +00:00
if ( gCdStreamSema == nil )
2019-06-17 13:32:38 +00:00
{
CDTRACE("failed to create stream semaphore");
ASSERT(0);
return;
}
2019-06-30 10:53:39 +00:00
_gCdStreamThread = CreateThread(nil, 64*1024/*64KB*/, CdStreamThread, nil, CREATE_SUSPENDED, &_gCdStreamThreadId);
2019-06-17 13:32:38 +00:00
2019-06-30 10:53:39 +00:00
if ( _gCdStreamThread == nil )
2019-06-17 13:32:38 +00:00
{
CDTRACE("failed to create streaming thread");
ASSERT(0);
return;
}
SetThreadPriority(_gCdStreamThread, GetThreadPriority(GetCurrentThread()) - 1);
ResumeThread(_gCdStreamThread);
}
void
CdStreamInit(int32 numChannels)
{
DWORD SectorsPerCluster;
DWORD BytesPerSector;
DWORD NumberOfFreeClusters;
DWORD TotalNumberOfClusters;
2019-06-30 10:53:39 +00:00
GetDiskFreeSpace(nil, &SectorsPerCluster, &BytesPerSector, &NumberOfFreeClusters, &TotalNumberOfClusters);
2019-06-17 13:32:38 +00:00
_gdwCdStreamFlags = 0;
if ( BytesPerSector <= CDSTREAM_SECTOR_SIZE )
{
_gdwCdStreamFlags |= FILE_FLAG_NO_BUFFERING;
debug("Using no buffered loading for streaming\n");
}
_gbCdStreamOverlapped = TRUE;
_gdwCdStreamFlags |= FILE_FLAG_OVERLAPPED;
_gbCdStreamAsync = FALSE;
void *pBuffer = (void *)RwMallocAlign(CDSTREAM_SECTOR_SIZE, BytesPerSector);
2019-06-30 10:53:39 +00:00
ASSERT( pBuffer != nil );
2019-06-17 13:32:38 +00:00
SetLastError(0);
gNumImages = 0;
gNumChannels = numChannels;
gpReadInfo = (CdReadInfo *)LocalAlloc(LMEM_ZEROINIT, sizeof(CdReadInfo) * numChannels);
2019-06-30 10:53:39 +00:00
ASSERT( gpReadInfo != nil );
2019-06-17 13:32:38 +00:00
CDDEBUG("read info %p", gpReadInfo);
CdStreamAddImage("MODELS\\GTA3.IMG");
int32 nStatus = CdStreamRead(0, pBuffer, 0, 1);
CdStreamRemoveImages();
if ( nStatus == STREAM_SUCCESS )
{
_gbCdStreamAsync = TRUE;
debug("Using async loading for streaming\n");
}
else
{
_gdwCdStreamFlags &= ~FILE_FLAG_OVERLAPPED;
_gbCdStreamOverlapped = FALSE;
_gbCdStreamAsync = TRUE;
debug("Using sync loading for streaming\n");
}
CdStreamInitThread();
2019-06-30 10:53:39 +00:00
ASSERT( pBuffer != nil );
2019-06-17 13:32:38 +00:00
RwFreeAlign(pBuffer);
}
uint32
GetGTA3ImgSize(void)
{
2019-06-30 10:53:39 +00:00
ASSERT( gImgFiles[0] != nil );
return (uint32)GetFileSize(gImgFiles[0], nil);
2019-06-17 13:32:38 +00:00
}
void
CdStreamShutdown(void)
{
if ( _gbCdStreamAsync )
{
LocalFree(gChannelRequestQ.items);
CloseHandle(gCdStreamSema);
CloseHandle(_gCdStreamThread);
for ( int32 i = 0; i < gNumChannels; i++ )
CloseHandle(gpReadInfo[i].hSemaphore);
}
LocalFree(gpReadInfo);
}
int32
CdStreamRead(int32 channel, void *buffer, uint32 offset, uint32 size)
{
ASSERT( channel < gNumChannels );
2019-06-30 10:53:39 +00:00
ASSERT( buffer != nil );
2019-06-17 13:32:38 +00:00
lastPosnRead = size + offset;
ASSERT( _GET_INDEX(offset) < MAX_CDIMAGES );
HANDLE hImage = gImgFiles[_GET_INDEX(offset)];
2019-06-30 10:53:39 +00:00
ASSERT( hImage != nil );
2019-06-17 13:32:38 +00:00
CdReadInfo *pChannel = &gpReadInfo[channel];
2019-06-30 10:53:39 +00:00
ASSERT( pChannel != nil );
2019-06-17 13:32:38 +00:00
pChannel->hFile = hImage;
SetLastError(0);
if ( _gbCdStreamAsync )
{
if ( pChannel->nSectorsToRead != 0 || pChannel->bInUse )
return STREAM_NONE;
pChannel->nStatus = STREAM_NONE;
pChannel->nSectorOffset = _GET_OFFSET(offset);
pChannel->nSectorsToRead = size;
pChannel->pBuffer = buffer;
pChannel->bLocked = 0;
AddToQueue(&gChannelRequestQ, channel);
2019-06-30 10:53:39 +00:00
if ( !ReleaseSemaphore(gCdStreamSema, 1, nil) )
2019-06-17 13:32:38 +00:00
printf("Signal Sema Error\n");
return STREAM_SUCCESS;
}
if ( _gbCdStreamOverlapped )
{
ASSERT( channel < gNumChannels );
CdReadInfo *pChannel = &gpReadInfo[channel];
2019-06-30 10:53:39 +00:00
ASSERT( pChannel != nil );
2019-06-17 13:32:38 +00:00
pChannel->Overlapped.Offset = _GET_OFFSET(offset) * CDSTREAM_SECTOR_SIZE;
if ( !ReadFile(hImage, buffer, size * CDSTREAM_SECTOR_SIZE, NULL, &pChannel->Overlapped)
&& GetLastError() != ERROR_IO_PENDING )
return STREAM_NONE;
else
return STREAM_SUCCESS;
}
2019-06-30 10:53:39 +00:00
SetFilePointer(hImage, _GET_OFFSET(offset) * CDSTREAM_SECTOR_SIZE, nil, FILE_BEGIN);
2019-06-17 13:32:38 +00:00
DWORD NumberOfBytesRead;
2019-06-30 10:53:39 +00:00
if ( !ReadFile(hImage, buffer, size * CDSTREAM_SECTOR_SIZE, &NumberOfBytesRead, nil) )
2019-06-17 13:32:38 +00:00
return STREAM_NONE;
else
return STREAM_SUCCESS;
}
int32
CdStreamGetStatus(int32 channel)
{
ASSERT( channel < gNumChannels );
CdReadInfo *pChannel = &gpReadInfo[channel];
2019-06-30 10:53:39 +00:00
ASSERT( pChannel != nil );
2019-06-17 13:32:38 +00:00
if ( _gbCdStreamAsync )
{
if ( pChannel->bInUse )
return STREAM_READING;
if ( pChannel->nSectorsToRead != 0 )
return STREAM_WAITING;
if ( pChannel->nStatus != STREAM_NONE )
{
int32 status = pChannel->nStatus;
pChannel->nStatus = STREAM_NONE;
return status;
}
return STREAM_NONE;
}
if ( _gbCdStreamOverlapped )
{
2019-06-30 10:53:39 +00:00
ASSERT( pChannel->hFile != nil );
2019-06-17 13:32:38 +00:00
if ( WaitForSingleObjectEx(pChannel->hFile, 0, TRUE) == WAIT_OBJECT_0 )
return STREAM_NONE;
else
return STREAM_READING;
}
return STREAM_NONE;
}
int32
CdStreamGetLastPosn(void)
{
return lastPosnRead;
}
int32
CdStreamSync(int32 channel)
{
ASSERT( channel < gNumChannels );
CdReadInfo *pChannel = &gpReadInfo[channel];
2019-06-30 10:53:39 +00:00
ASSERT( pChannel != nil );
2019-06-17 13:32:38 +00:00
if ( _gbCdStreamAsync )
{
if ( pChannel->nSectorsToRead != 0 )
{
pChannel->bLocked = true;
2019-06-30 10:53:39 +00:00
ASSERT( pChannel->hSemaphore != nil );
2019-06-17 13:32:38 +00:00
WaitForSingleObject(pChannel->hSemaphore, INFINITE);
}
pChannel->bInUse = false;
return pChannel->nStatus;
}
DWORD NumberOfBytesTransferred;
if ( _gbCdStreamOverlapped && pChannel->hFile )
{
2019-06-30 10:53:39 +00:00
ASSERT(pChannel->hFile != nil );
2019-06-17 13:32:38 +00:00
if ( GetOverlappedResult(pChannel->hFile, &pChannel->Overlapped, &NumberOfBytesTransferred, TRUE) )
return STREAM_NONE;
else
return STREAM_ERROR;
}
return STREAM_NONE;
}
void
AddToQueue(Queue *queue, int32 item)
{
2019-06-30 10:53:39 +00:00
ASSERT( queue != nil );
ASSERT( queue->items != nil );
2019-06-17 13:32:38 +00:00
queue->items[queue->tail] = item;
queue->tail = (queue->tail + 1) % queue->size;
if ( queue->head == queue->tail )
debug("Queue is full\n");
}
int32
GetFirstInQueue(Queue *queue)
{
2019-06-30 10:53:39 +00:00
ASSERT( queue != nil );
2019-06-17 13:32:38 +00:00
if ( queue->head == queue->tail )
return -1;
2019-06-30 10:53:39 +00:00
ASSERT( queue->items != nil );
2019-06-17 13:32:38 +00:00
return queue->items[queue->head];
}
void
RemoveFirstInQueue(Queue *queue)
{
2019-06-30 10:53:39 +00:00
ASSERT( queue != nil );
2019-06-17 13:32:38 +00:00
if ( queue->head == queue->tail )
{
debug("Queue is empty\n");
return;
}
queue->head = (queue->head + 1) % queue->size;
}
DWORD
WINAPI CdStreamThread(LPVOID lpThreadParameter)
{
debug("Created cdstream thread\n");
while ( true )
{
WaitForSingleObject(gCdStreamSema, INFINITE);
int32 channel = GetFirstInQueue(&gChannelRequestQ);
ASSERT( channel < gNumChannels );
CdReadInfo *pChannel = &gpReadInfo[channel];
2019-06-30 10:53:39 +00:00
ASSERT( pChannel != nil );
2019-06-17 13:32:38 +00:00
pChannel->bInUse = true;
if ( pChannel->nStatus == STREAM_NONE )
{
if ( _gbCdStreamOverlapped )
{
pChannel->Overlapped.Offset = pChannel->nSectorOffset * CDSTREAM_SECTOR_SIZE;
2019-06-30 10:53:39 +00:00
ASSERT(pChannel->hFile != nil );
ASSERT(pChannel->pBuffer != nil );
2019-06-17 13:32:38 +00:00
DWORD NumberOfBytesTransferred;
if ( ReadFile(pChannel->hFile,
pChannel->pBuffer,
pChannel->nSectorsToRead * CDSTREAM_SECTOR_SIZE,
NULL,
&pChannel->Overlapped) )
{
pChannel->nStatus = STREAM_NONE;
}
else if ( GetLastError() == ERROR_IO_PENDING
&& GetOverlappedResult(pChannel->hFile, &pChannel->Overlapped, &NumberOfBytesTransferred, TRUE) )
{
pChannel->nStatus = STREAM_NONE;
}
else
{
pChannel->nStatus = STREAM_ERROR;
}
}
else
{
2019-06-30 10:53:39 +00:00
ASSERT(pChannel->hFile != nil );
ASSERT(pChannel->pBuffer != nil );
2019-06-17 13:32:38 +00:00
2019-06-30 10:53:39 +00:00
SetFilePointer(pChannel->hFile, pChannel->nSectorOffset * CDSTREAM_SECTOR_SIZE, nil, FILE_BEGIN);
2019-06-17 13:32:38 +00:00
DWORD NumberOfBytesRead;
if ( ReadFile(pChannel->hFile,
pChannel->pBuffer,
pChannel->nSectorsToRead * CDSTREAM_SECTOR_SIZE,
&NumberOfBytesRead,
NULL) )
{
pChannel->nStatus = STREAM_NONE;
}
}
}
RemoveFirstInQueue(&gChannelRequestQ);
pChannel->nSectorsToRead = 0;
if ( pChannel->bLocked )
{
2019-06-30 10:53:39 +00:00
ASSERT( pChannel->hSemaphore != nil );
2019-06-17 13:32:38 +00:00
ReleaseSemaphore(pChannel->hSemaphore, 1, NULL);
}
pChannel->bInUse = false;
}
}
bool
CdStreamAddImage(char const *path)
{
2019-06-30 10:53:39 +00:00
ASSERT(path != nil);
2019-06-17 13:32:38 +00:00
ASSERT(gNumImages < MAX_CDIMAGES);
SetLastError(0);
gImgFiles[gNumImages] = CreateFile(path,
2019-06-18 07:50:26 +00:00
GENERIC_READ,
FILE_SHARE_READ,
2019-06-30 10:53:39 +00:00
nil,
2019-06-18 07:50:26 +00:00
OPEN_EXISTING,
_gdwCdStreamFlags | FILE_FLAG_RANDOM_ACCESS | FILE_ATTRIBUTE_READONLY,
2019-06-30 10:53:39 +00:00
nil);
2019-06-17 13:32:38 +00:00
2019-06-30 10:53:39 +00:00
ASSERT( gImgFiles[gNumImages] != nil );
2019-06-17 13:32:38 +00:00
if ( gImgFiles[gNumImages] == NULL )
return false;
strcpy(gCdImageNames[gNumImages], path);
gNumImages++;
return true;
}
char *
CdStreamGetImageName(int32 cd)
{
ASSERT(cd < MAX_CDIMAGES);
2019-06-30 10:53:39 +00:00
if ( gImgFiles[cd] != nil )
2019-06-17 13:32:38 +00:00
return gCdImageNames[cd];
2019-06-30 10:53:39 +00:00
return nil;
2019-06-17 13:32:38 +00:00
}
void
CdStreamRemoveImages(void)
{
for ( int32 i = 0; i < gNumChannels; i++ )
CdStreamSync(i);
for ( int32 i = 0; i < gNumImages; i++ )
{
SetLastError(0);
CloseHandle(gImgFiles[i]);
2019-06-30 10:53:39 +00:00
gImgFiles[i] = nil;
2019-06-17 13:32:38 +00:00
}
gNumImages = 0;
}
int32
CdStreamGetNumImages(void)
{
return gNumImages;
}