* remove some dead code (resource preload callback stuff)

* turn a one-statement function into an inline (resDoResLoadCallback)
 * revive resource loading from file (the function pointer for this was removed in r1394, to actually use this pointer new code had to be written)
 * modify resDataInit to handle memory allocation as well as data initialization
 * modify struct RES_DATA to not have a fixed size char array to contain the filename (is now dynamically allocated together with the rest of the struct, see resDataInit)
 * modify some argument lists to take `const char*` instead of `char*`
 * change resLoadFile to use RES_TYPE.fileLoad if available, also remove an unused if statement (well actually the condition could never be false)
 * resource type "WAV" (see data.c) now directly loads from a file (rather than copying the file entirely into memory and then to operate on that)
 * fix sound_ConstructTrack (track.c) where I used the wrong source to copy the filename from
 * fix sound_LoadTrackFromBuffer (track.c) to *not* add the track to the tracklist because this breaks other code
 * modify sound_ReleaseTrack (track.c) to gracefully deal with NULL pointers
 * remove dataAudioRelease (data.c) and instead just use sound_ReleaseTrack

git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@1397 4a71c877-e1ca-e34f-864e-861f7616d084
master
Giel van Schijndel 2007-04-08 21:39:24 +00:00
parent 61975a7936
commit 99153c743c
7 changed files with 149 additions and 165 deletions

View File

@ -50,14 +50,8 @@ static void ResetResourceFile(void);
// callback to resload screen.
static RESLOAD_CALLBACK resLoadCallback=NULL;
static RESPRELOAD_CALLBACK resPreLoadCallback=NULL;
void resSetPreLoadCallback(RESPRELOAD_CALLBACK funcToCall)
{
resPreLoadCallback=funcToCall;
}
/* set the callback function for the res loader*/
void resSetLoadCallback(RESLOAD_CALLBACK funcToCall)
{
@ -65,7 +59,7 @@ void resSetLoadCallback(RESLOAD_CALLBACK funcToCall)
}
/* do the callback for the resload display function */
void resDoResLoadCallback(void)
static inline void resDoResLoadCallback(void)
{
if(resLoadCallback)
{
@ -82,7 +76,6 @@ BOOL resInitialise(void)
psResTypes = NULL;
resBlockID = 0;
resLoadCallback = NULL;
resPreLoadCallback = NULL;
ResetResourceFile();
@ -191,6 +184,29 @@ BOOL resAddBufferLoad(const char *pType, RES_BUFFERLOAD buffLoad,
}
psT->buffLoad = buffLoad;
psT->fileLoad = NULL;
psT->release = release;
psT->psNext = psResTypes;
psResTypes = psT;
return TRUE;
}
/* Add a file name load function for a file type */
BOOL resAddFileLoad(const char *pType, RES_FILELOAD fileLoad,
RES_FREE release)
{
RES_TYPE *psT = resAlloc(pType);
if (!psT)
{
return FALSE;
}
psT->buffLoad = NULL;
psT->fileLoad = fileLoad;
psT->release = release;
psT->psNext = psResTypes;
@ -342,117 +358,129 @@ static void FreeResourceFile(RESOURCEFILE *OldResource)
}
static void resDataInit(RES_DATA* psRes, char *DebugName, UDWORD DataIDHash, void *pData, UDWORD BlockID)
static inline RES_DATA* resDataInit(const char *DebugName, UDWORD DataIDHash, void *pData, UDWORD BlockID)
{
RES_DATA* psRes = (RES_DATA*)malloc(sizeof(RES_DATA) + strlen(DebugName) + 1);
char* fileName = (char*)psRes + sizeof(RES_DATA);
if (!psRes)
{
debug(LOG_ERROR, "resDataInit: Out of memory");
return NULL;
}
psRes->pData = pData;
psRes->blockID = BlockID;
psRes->HashedID=DataIDHash;
psRes->HashedID = DataIDHash;
strcpy(fileName, DebugName);
psRes->aID = fileName;
strcpy(psRes->aID, DebugName);
psRes->usage = 0;
return psRes;
}
/* Call the load function for a file */
BOOL resLoadFile(char *pType, char *pFile)
BOOL resLoadFile(const char *pType, const char *pFile)
{
RES_TYPE *psT;
void *pData;
RES_DATA *psRes;
char aFileName[FILE_MAXCHAR];
BOOL loadresource;
UDWORD HashedName;
UDWORD HashedName, HashedType = HashString(pType);
loadresource=TRUE;
if(resPreLoadCallback)
// Find the resource-type
for(psT = psResTypes; psT != NULL; psT = psT->psNext )
{
loadresource=resPreLoadCallback(pType,pFile,aCurrResDir);
if (psT->HashedType == HashedType)
{
break;
}
}
if (loadresource==TRUE)
if (psT == NULL) {
debug(LOG_WZ, "resLoadFile: Unknown type: %s", pType);
return FALSE;
}
// Check for duplicates
HashedName = HashStringIgnoreCase(pFile);
for (psRes = psT->psRes; psRes; psRes = psRes->psNext) {
if(psRes->HashedID == HashedName) {
debug(LOG_WZ, "resLoadFile: Duplicate file name: %s (hash %x) for type %s",
pFile, HashedName, psT->aType);
// assume that they are actually both the same and silently fail
// lovely little hack to allow some files to be loaded from disk (believe it or not!).
return TRUE;
}
}
// Create the file name
if (strlen(aCurrResDir) + strlen(pFile) + 1 >= FILE_MAXCHAR) {
debug(LOG_ERROR, "resLoadFile: Filename too long!! %s%s", aCurrResDir, pFile);
return FALSE;
}
strcpy(aFileName, aCurrResDir);
strcat(aFileName, pFile);
strcpy(LastResourceFilename,pFile); // Save the filename in case any routines need it
SetLastHashName(HashStringIgnoreCase(LastResourceFilename));
// load the resource
if (psT->buffLoad)
{
UDWORD HashedType=HashString(pType);
RESOURCEFILE *Resource;
for(psT = psResTypes; psT != NULL; psT = psT->psNext )
// Load the file in a buffer
if (!RetreiveResourceFile(aFileName,&Resource)) {
debug(LOG_ERROR, "resLoadFile: Unable to retreive resource - %s", aFileName);
return(FALSE);
}
// Now process the buffer data
if (!psT->buffLoad(Resource->pBuffer, Resource->size, &pData))
{
if (psT->HashedType==HashedType)
{
break;
}
}
if (psT == NULL) {
debug(LOG_WZ, "resLoadFile: Unknown type: %s", pType);
return FALSE;
}
HashedName = HashStringIgnoreCase(pFile);
for (psRes = psT->psRes; psRes; psRes = psRes->psNext) {
if(psRes->HashedID == HashedName) {
debug(LOG_WZ, "resLoadFile: Duplicate file name: %s (hash %x) for type %s",
pFile, HashedName, psT->aType);
// assume that they are actually both the same and silently fail
// lovely little hack to allow some files to be loaded from disk (believe it or not!).
return TRUE;
}
}
// Create the file name
if (strlen(aCurrResDir) + strlen(pFile) + 1 >= FILE_MAXCHAR) {
debug(LOG_ERROR, "resLoadFile: Filename too long!! %s%s", aCurrResDir, pFile);
return FALSE;
}
strcpy(aFileName, aCurrResDir);
strcat(aFileName, pFile);
strcpy(LastResourceFilename,pFile); // Save the filename in case any routines need it
resToLower(LastResourceFilename);
SetLastHashName(HashStringIgnoreCase(LastResourceFilename));
// load the resource
if (psT->buffLoad) {
RESOURCEFILE *Resource;
BOOL Result;
Result=RetreiveResourceFile(aFileName,&Resource);
if (Result == FALSE) {
debug(LOG_ERROR, "resLoadFile: Unable to retreive resource - %s", aFileName);
return(FALSE);
}
// Now process the buffer data
if (!psT->buffLoad(Resource->pBuffer, Resource->size, &pData))
{
FreeResourceFile(Resource);
psT->release( pData );
return FALSE;
}
FreeResourceFile(Resource);
resDoResLoadCallback(); // do callback.
} else {
debug(LOG_ERROR, "resLoadFile: No load functions for this type (%s)", pType);
psT->release( pData );
return FALSE;
}
// Set up the resource structure if there is something to store
if (pData != NULL)
FreeResourceFile(Resource);
}
else if(psT->fileLoad)
{
// Process data directly from file
if (!psT->fileLoad(aFileName, &pData))
{
psRes = (RES_DATA*)MALLOC(sizeof(RES_DATA));
if (!psRes)
{
debug(LOG_ERROR, "resLoadFile: Out of memory");
psT->release(pData);
return FALSE;
}
// LastResourceFilename may have been changed (e.g. by TEXPAGE loading)
resDataInit( psRes, LastResourceFilename, HashStringIgnoreCase(LastResourceFilename), pData, resBlockID );
// Add the resource to the list
psRes->psNext = psT->psRes;
psT->psRes = psRes;
psT->release( pData );
return FALSE;
}
}
else
{
debug(LOG_ERROR, "resLoadFile: No load functions for this type (%s)", pType);
return FALSE;
}
resDoResLoadCallback(); // do callback.
// Set up the resource structure if there is something to store
if (pData != NULL)
{
// LastResourceFilename may have been changed (e.g. by TEXPAGE loading)
psRes = resDataInit( LastResourceFilename, HashStringIgnoreCase(LastResourceFilename), pData, resBlockID );
if (!psRes)
{
psT->release(pData);
return FALSE;
}
// Add the resource to the list
psRes->psNext = psT->psRes;
psT->psRes = psRes;
}
return TRUE;
}

View File

@ -32,6 +32,8 @@
/* Function pointer for a function that loads from a memory buffer */
typedef BOOL (*RES_BUFFERLOAD)(char *pBuffer, UDWORD size, void **pData);
/* Function pointer for a function that loads from a filename */
typedef BOOL (*RES_FILELOAD)(const char *pFile, void **pData);
/* Function pointer for releasing a resource loaded by the above functions */
typedef void (*RES_FREE)(void *pData);
@ -42,7 +44,7 @@ typedef void (*RESLOAD_CALLBACK)(void);
typedef struct res_data
{
char aID[RESID_MAXCHAR]; // ID of the resource - filename from the .wrf - e.g. "TRON.PIE"
const char *aID; // ID of the resource - filename from the .wrf - e.g. "TRON.PIE"
void *pData; // pointer to the acutal data
SDWORD blockID; // which of the blocks is it in (so we can clear some of them...)
@ -66,6 +68,7 @@ typedef struct _res_type
RES_DATA *psRes; // Linked list of data items of this type
UDWORD HashedType; // hashed version of the name of the id - // a null hashedtype indicates end of list
RES_FILELOAD fileLoad; // This isn't really used any more ?
struct _res_type *psNext;
} RES_TYPE;
@ -73,15 +76,6 @@ typedef struct _res_type
/* set the function to call when loading files with resloadfile*/
extern void resSetLoadCallback(RESLOAD_CALLBACK funcToCall);
/* callback type for res pre-load callback*/
typedef BOOL (*RESPRELOAD_CALLBACK)(char *type, char *name, char *directory);
/* set the function to call when loading files with resloadfile*/
extern void resSetPreLoadCallback(RESPRELOAD_CALLBACK funcToCall);
/* Initialise the resource module */
extern BOOL resInitialise(void);
@ -108,8 +102,12 @@ extern void resReleaseAllData(void);
extern BOOL resAddBufferLoad(const char *pType, RES_BUFFERLOAD buffLoad,
RES_FREE release);
/* Add a file name load and release function for a file type */
extern BOOL resAddFileLoad(const char *pType, RES_FILELOAD fileLoad,
RES_FREE release);
/* Call the load function for a file */
extern BOOL resLoadFile(char *pType, char *pFile);
extern BOOL resLoadFile(const char *pType, const char *pFile);
// Add data to the resource system
extern BOOL resAddData(char *pType, char *pID, void *pData);
@ -123,8 +121,6 @@ void resToLower(char *pStr);
// return the HashedID string for a piece of data
extern BOOL resGetHashfromData(const char *pType, const void *pData, UDWORD *pHash);
void resDoResLoadCallback(void);
//return last imd resource
char *GetLastResourceFilename(void);
// Set the resource name of the last resource file loaded

View File

@ -472,14 +472,14 @@ TRACK* sound_ReadTrackFromBuffer( TRACK *psTrack, void *pBuffer, UDWORD udwSize
// =======================================================================================================================
// =======================================================================================================================
//
TRACK* sound_ReadTrackFromFile(TRACK *psTrack, char szFileName[])
TRACK* sound_ReadTrackFromFile(TRACK *psTrack, const char *fileName)
{
fileInfo fileHandle;
fileHandle.allowSeeking = TRUE;
// Use PhysicsFS to open the file
fileHandle.fileHandle = PHYSFS_openRead(szFileName);
fileHandle.fileHandle = PHYSFS_openRead(fileName);
if (fileHandle.fileHandle == NULL)
{

View File

@ -148,34 +148,11 @@ BOOL sound_SetTrackVals
return TRUE;
}
//*
// =======================================================================================================================
// =======================================================================================================================
//
static BOOL sound_AddTrack( TRACK *pTrack )
{
if ( !(g_iCurTracks < MAX_TRACKS) )
{
debug( LOG_ERROR, "sound_AddTrack: all tracks used: increase MAX_TRACKS\n" );
abort();
return FALSE;
}
// add to sound array
// set pointer in table
g_apTrack[g_iCurTracks] = pTrack;
// increment current sound
g_iCurTracks++;
return TRUE;
}
static inline TRACK *sound_ConstructTrack(char *fileName)
static inline TRACK *sound_ConstructTrack(const char *fileName)
{
// allocate track, plus the memory required to contain the filename
// one malloc call ensures only one free call is required
TRACK* pTrack = (TRACK*)malloc(sizeof(TRACK) + strlen(fileName));
TRACK* pTrack = (TRACK*)malloc(sizeof(TRACK) + strlen(fileName) + 1);
if (pTrack == NULL)
{
@ -189,7 +166,7 @@ static inline TRACK *sound_ConstructTrack(char *fileName)
// Set filename pointer and copy the filename into struct
pTrack->pName = (char*)pTrack + sizeof(TRACK);
strcpy( pTrack->pName, GetLastResourceFilename() );
strcpy( pTrack->pName, fileName );
return pTrack;
}
@ -218,7 +195,7 @@ TRACK *sound_LoadTrackFromBuffer(char *pBuffer, UDWORD udwSize)
// =======================================================================================================================
// =======================================================================================================================
//
TRACK* sound_LoadTrackFromFile(char *fileName)
TRACK* sound_LoadTrackFromFile(const char *fileName)
{
TRACK *pTrack = sound_ConstructTrack(fileName);
@ -229,21 +206,7 @@ TRACK* sound_LoadTrackFromFile(char *fileName)
pTrack->bMemBuffer = FALSE;
pTrack = sound_ReadTrackFromFile(pTrack, fileName);
if (pTrack == NULL)
{
return NULL;
}
if (!sound_AddTrack( pTrack ))
{
sound_FreeTrack(pTrack);
free(pTrack);
return NULL;
}
return pTrack;
return sound_ReadTrackFromFile(pTrack, fileName);
}
//*
@ -254,6 +217,9 @@ void sound_ReleaseTrack( TRACK *psTrack )
{
SDWORD iTrack;
if (!psTrack)
return;
for ( iTrack = 0; iTrack < g_iCurTracks; iTrack++ )
{
if ( g_apTrack[iTrack] == psTrack )

View File

@ -102,7 +102,7 @@ typedef struct TRACK
BOOL sound_Init( SDWORD iMaxSameSamples );
BOOL sound_Shutdown(void);
TRACK * sound_LoadTrackFromFile(char *fileName);
TRACK * sound_LoadTrackFromFile(const char *fileName);
TRACK * sound_LoadTrackFromBuffer(char *pBuffer, UDWORD udwSize);
BOOL sound_SetTrackVals( TRACK *psTrack, BOOL bLoop, SDWORD iTrack,
SDWORD iVol, SDWORD iAudibleRadius);

View File

@ -44,7 +44,7 @@
BOOL sound_InitLibrary( void );
void sound_ShutdownLibrary( void );
TRACK* sound_ReadTrackFromFile(TRACK * psTrack, char szFileName[]);
TRACK* sound_ReadTrackFromFile(TRACK * psTrack, const char *fileName);
TRACK* sound_ReadTrackFromBuffer( TRACK * psTrack, void *pBuffer,
UDWORD udwSize );
void sound_FreeTrack( TRACK * psTrack );

View File

@ -969,7 +969,7 @@ static void dataTexPageRelease(void *pData)
/* Load an audio file */
static BOOL dataAudioLoad(char *pBuffer, UDWORD size, void **ppData)
static BOOL dataAudioLoadFile(const char* fileName, void **ppData)
{
if ( audio_Disabled() == TRUE )
{
@ -977,23 +977,13 @@ static BOOL dataAudioLoad(char *pBuffer, UDWORD size, void **ppData)
// No error occurred (sound is just disabled), so we return TRUE
return TRUE;
}
// Load the track from a file
*ppData = sound_LoadTrackFromBuffer( pBuffer, size );
// Load the track from a file
*ppData = sound_LoadTrackFromFile( fileName );
return *ppData != NULL;
}
static void dataAudioRelease( void *pData )
{
TRACK *psTrack = (TRACK *) pData;
ASSERT( psTrack != NULL,
"dataAudioRelease: invalid track pointer" );
audio_ReleaseTrack( psTrack );
}
/* Load an audio file */
static BOOL dataAudioCfgLoad(char *pBuffer, UDWORD size, void **ppData)
{
@ -1184,7 +1174,6 @@ static const RES_TYPE_MIN ResourceTypes[] =
{"TERTILES", NULL, NULL}, // This version was used when running with the software renderer.
{"HWTERTILES", dataHWTERTILESLoad, dataHWTERTILESRelease}, // freed by 3d shutdow},// Tertiles Files. This version used when running with hardware renderer.
{"AUDIOCFG", dataAudioCfgLoad, NULL},
{"WAV", dataAudioLoad, dataAudioRelease},
{"ANI", dataAnimLoad, dataAnimRelease},
{"ANIMCFG", dataAnimCfgLoad, NULL},
{"IMG", dataIMGLoad, dataIMGRelease},
@ -1213,5 +1202,10 @@ BOOL dataInitLoadFuncs(void)
}
}
if(!resAddFileLoad("WAV", dataAudioLoadFile, (RES_FREE)sound_ReleaseTrack))
{
return FALSE;
}
return TRUE;
}