//=========================================================================== // // Parameter: - // Returns: - // Changes Globals: - //=========================================================================== quakefile_t *FindQuakeFilesInZip( char *zipfile, char *filter ) { unzFile uf; int err; unz_global_info gi; char filename_inzip[MAX_PATH]; unz_file_info file_info; int i; quakefile_t *qfiles, *lastqf, *qf; uf = unzOpen( zipfile ); err = unzGetGlobalInfo( uf, &gi ); if ( err != UNZ_OK ) { return NULL; } unzGoToFirstFile( uf ); qfiles = NULL; lastqf = NULL; for ( i = 0; i < gi.number_entry; i++ ) { err = unzGetCurrentFileInfo( uf, &file_info, filename_inzip, sizeof( filename_inzip ), NULL,0,NULL,0 ); if ( err != UNZ_OK ) { break; } ConvertPath( filename_inzip ); if ( FileFilter( filter, filename_inzip, false ) ) { qf = GetClearedMemory( sizeof( quakefile_t ) ); if ( !qf ) { Error( "out of memory" ); } memset( qf, 0, sizeof( quakefile_t ) ); strcpy( qf->pakfile, zipfile ); strcpy( qf->filename, zipfile ); strcpy( qf->origname, filename_inzip ); qf->zipfile = true; //memcpy( &buildBuffer[i].zipfileinfo, (unz_s*)uf, sizeof(unz_s)); memcpy( &qf->zipinfo, (unz_s*)uf, sizeof( unz_s ) ); qf->offset = 0; qf->length = file_info.uncompressed_size; qf->type = QuakeFileType( filename_inzip ); //add the file ot the list qf->next = NULL; if ( lastqf ) { lastqf->next = qf; } else { qfiles = qf;} lastqf = qf; } //end if unzGoToNextFile( uf ); } //end for unzClose( uf ); return qfiles; } //end of the function FindQuakeFilesInZip
//=========================================================================== // // Parameter: - // Returns: - // Changes Globals: - //=========================================================================== quakefile_t *FindQuakeFilesWithPakFilter(char *pakfilter, char *filter) { #ifdef _WIN32 WIN32_FIND_DATA filedata; HWND handle; struct _stat statbuf; int done; #else glob_t globbuf; struct stat statbuf; int j; #endif quakefile_t *qfiles, *lastqf, *qf; char pakfile[_MAX_PATH], filename[_MAX_PATH], *str; qfiles = NULL; lastqf = NULL; if (pakfilter && strlen(pakfilter)) { #ifdef _WIN32 handle = FindFirstFile(pakfilter, &filedata); done = (handle == INVALID_HANDLE_VALUE); while(!done) { _splitpath(pakfilter, pakfile, NULL, NULL, NULL); _splitpath(pakfilter, NULL, &pakfile[strlen(pakfile)], NULL, NULL); AppendPathSeperator(pakfile, _MAX_PATH); strcat(pakfile, filedata.cFileName); _stat(pakfile, &statbuf); #else glob(pakfilter, 0, NULL, &globbuf); for (j = 0; j < globbuf.gl_pathc; j++) { strcpy(pakfile, globbuf.gl_pathv[j]); stat(pakfile, &statbuf); #endif //if the file with .pak or .pk3 is a folder if (statbuf.st_mode & S_IFDIR) { strcpy(filename, pakfilter); AppendPathSeperator(filename, _MAX_PATH); strcat(filename, filter); qf = FindQuakeFilesWithPakFilter(NULL, filename); if (lastqf) lastqf->next = qf; else qfiles = qf; lastqf = qf; while(lastqf->next) lastqf = lastqf->next; } //end if else { #ifdef _WIN32 str = StringContains(pakfile, ".pk3", false); #else str = StringContains(pakfile, ".pk3", true); #endif if (str && str == pakfile + strlen(pakfile) - strlen(".pk3")) { qf = FindQuakeFilesInZip(pakfile, filter); } //end if else { qf = NULL; } //end else // if (qf) { if (lastqf) lastqf->next = qf; else qfiles = qf; lastqf = qf; while(lastqf->next) lastqf = lastqf->next; } //end if } //end else // #ifdef _WIN32 //find the next file done = !FindNextFile(handle, &filedata); } //end while #else } //end for globfree(&globbuf); #endif } //end if else { #ifdef _WIN32 handle = FindFirstFile(filter, &filedata); done = (handle == INVALID_HANDLE_VALUE); while(!done) { _splitpath(filter, filename, NULL, NULL, NULL); _splitpath(filter, NULL, &filename[strlen(filename)], NULL, NULL); AppendPathSeperator(filename, _MAX_PATH); strcat(filename, filedata.cFileName); #else glob(filter, 0, NULL, &globbuf); for (j = 0; j < globbuf.gl_pathc; j++) { strcpy(filename, globbuf.gl_pathv[j]); #endif // qf = malloc(sizeof(quakefile_t)); if (!qf) Error("out of memory"); memset(qf, 0, sizeof(quakefile_t)); strcpy(qf->pakfile, ""); strcpy(qf->filename, filename); strcpy(qf->origname, filename); qf->length = 0; qf->type = QuakeFileType(filename); //add the file ot the list qf->next = NULL; if (lastqf) lastqf->next = qf; else qfiles = qf; lastqf = qf; #ifdef _WIN32 //find the next file done = !FindNextFile(handle, &filedata); } //end while #else } //end for globfree(&globbuf); #endif } //end else return qfiles; } //end of the function FindQuakeFilesWithPakFilter
//=========================================================================== // // Parameter: - // Returns: - // Changes Globals: - //=========================================================================== quakefile_t *FindQuakeFilesInPak( char *pakfile, char *filter ) { FILE *fp; dpackheader_t packheader; dsinpackfile_t *packfiles; dpackfile_t *idpackfiles; quakefile_t *qfiles, *lastqf, *qf; int numpackdirs, i; qfiles = NULL; lastqf = NULL; //open the pak file fp = fopen( pakfile, "rb" ); if ( !fp ) { Warning( "can't open pak file %s", pakfile ); return NULL; } //end if //read pak header, check for valid pak id and seek to the dir entries if ( ( fread( &packheader, 1, sizeof( dpackheader_t ), fp ) != sizeof( dpackheader_t ) ) || ( packheader.ident != IDPAKHEADER && packheader.ident != SINPAKHEADER ) || ( fseek( fp, LittleLong( packheader.dirofs ), SEEK_SET ) ) ) { fclose( fp ); Warning( "invalid pak file %s", pakfile ); return NULL; } //end if //if it is a pak file from id software if ( packheader.ident == IDPAKHEADER ) { //number of dir entries in the pak file numpackdirs = LittleLong( packheader.dirlen ) / sizeof( dpackfile_t ); idpackfiles = (dpackfile_t *) GetClearedMemory( numpackdirs * sizeof( dpackfile_t ) ); if ( !idpackfiles ) { Error( "out of memory" ); } //read the dir entry if ( fread( idpackfiles, sizeof( dpackfile_t ), numpackdirs, fp ) != numpackdirs ) { fclose( fp ); FreeMemory( idpackfiles ); Warning( "can't read the Quake pak file dir entries from %s", pakfile ); return NULL; } //end if fclose( fp ); //convert to sin pack files packfiles = (dsinpackfile_t *) GetClearedMemory( numpackdirs * sizeof( dsinpackfile_t ) ); if ( !packfiles ) { Error( "out of memory" ); } for ( i = 0; i < numpackdirs; i++ ) { strcpy( packfiles[i].name, idpackfiles[i].name ); packfiles[i].filepos = LittleLong( idpackfiles[i].filepos ); packfiles[i].filelen = LittleLong( idpackfiles[i].filelen ); } //end for FreeMemory( idpackfiles ); } //end if else //its a Sin pack file { //number of dir entries in the pak file numpackdirs = LittleLong( packheader.dirlen ) / sizeof( dsinpackfile_t ); packfiles = (dsinpackfile_t *) GetClearedMemory( numpackdirs * sizeof( dsinpackfile_t ) ); if ( !packfiles ) { Error( "out of memory" ); } //read the dir entry if ( fread( packfiles, sizeof( dsinpackfile_t ), numpackdirs, fp ) != numpackdirs ) { fclose( fp ); FreeMemory( packfiles ); Warning( "can't read the Sin pak file dir entries from %s", pakfile ); return NULL; } //end if fclose( fp ); for ( i = 0; i < numpackdirs; i++ ) { packfiles[i].filepos = LittleLong( packfiles[i].filepos ); packfiles[i].filelen = LittleLong( packfiles[i].filelen ); } //end for } //end else // for ( i = 0; i < numpackdirs; i++ ) { ConvertPath( packfiles[i].name ); if ( FileFilter( filter, packfiles[i].name, false ) ) { qf = GetClearedMemory( sizeof( quakefile_t ) ); if ( !qf ) { Error( "out of memory" ); } memset( qf, 0, sizeof( quakefile_t ) ); strcpy( qf->pakfile, pakfile ); strcpy( qf->filename, pakfile ); strcpy( qf->origname, packfiles[i].name ); qf->zipfile = false; qf->offset = packfiles[i].filepos; qf->length = packfiles[i].filelen; qf->type = QuakeFileType( packfiles[i].name ); //add the file ot the list qf->next = NULL; if ( lastqf ) { lastqf->next = qf; } else { qfiles = qf;} lastqf = qf; } //end if } //end for FreeMemory( packfiles ); return qfiles; } //end of the function FindQuakeFilesInPak