engine/EngineCommon.c
/*	Finds the file in the search path.
	Sets com_filesize and one of handle or file
*/
Finds the file in the search path.
Sets com_filesize and one of handle or file
Finds the file in the search path . Sets com_filesize and one of handle or file
int COM_FindFile (const char *filename, int *handle, FILE **file)
{
	pack_t			*pak;
	searchpath_t    *search;
	char            netpath[PLATFORM_MAX_PATH], cachepath[PLATFORM_MAX_PATH];
	int             i;
	int             findtime, cachetime;
	if(!file && !handle)
	{
		Sys_Error ("COM_FindFile: neither handle or file set");
		return 0;
	}
	else if(file && handle)
	{
		Sys_Error ("COM_FindFile: both handle and file set");
		return 0;
	}
	search = com_searchpaths;
	for ( ; search ; search = search->next)
	{
		if (search->pack)
		{
			pak = search->pack;
			for (i=0 ; i<pak->numfiles ; i++)
				if (!strcmp (pak->files[i].name, filename))
				{
					Sys_Printf ("PackFile: %s : %s\n",pak->filename, filename);
					if (handle)
					{
						*handle = pak->handle;
						Sys_FileSeek (pak->handle, pak->files[i].filepos);
					}
					else
					{       
						*file = fopen (pak->filename, "rb");
						if (*file)
							fseek (*file, pak->files[i].filepos, SEEK_SET);
					}
					com_filesize = pak->files[i].filelen;
					return com_filesize;
				}
		}
		else
		{
			sprintf(netpath, "%s/%s", search->filename, filename);
			findtime = Sys_FileTime (netpath);
			if (findtime == -1)
				continue;
			if (!com_cachedir[0])
				strcpy(cachepath, netpath);
			else
			{
#if defined(_WIN32)
				if ((strlen(netpath) < 2) || (netpath[1] != ':'))
					sprintf (cachepath,"%s%s", com_cachedir, netpath);
				else
					sprintf (cachepath,"%s%s", com_cachedir, netpath+2);
#else
				sprintf (cachepath,"%s%s", com_cachedir, netpath);
#endif
				cachetime = Sys_FileTime (cachepath);
				if (cachetime < findtime)
					FileSystem_CopyFile(netpath,cachepath);
				strcpy(netpath, cachepath);
			}
			Sys_Printf ("FindFile: %s\n",netpath);
			com_filesize = Sys_FileOpenRead (netpath, &i);
			if (handle)
				*handle = i;
			else
			{
				Sys_FileClose (i);
				*file = fopen (netpath, "rb");
			}
			return com_filesize;
		}
	}
	Sys_Printf ("FindFile: can't find %s\n", filename);
	if (handle)
		*handle = -1;
	else
		*file = NULL;
	com_filesize = -1;
	return -1;
}
int COM_FindFile ( const char * filename , int * handle , FILE * * file ) { pack_t * pak ; searchpath_t * search ; char netpath [ PLATFORM_MAX_PATH ] , cachepath [ PLATFORM_MAX_PATH ] ; int i ; int findtime , cachetime ; if ( ! file && ! handle ) { Sys_Error ( " " ) ; return 0 ; } else if ( file && handle ) { Sys_Error ( " " ) ; return 0 ; } search = com_searchpaths ; for ( ; search ; search = search -> next ) { if ( search -> pack ) { pak = search -> pack ; for ( i = 0 ; i < pak -> numfiles ; i ++ ) if ( ! strcmp ( pak -> files [ i ] . name , filename ) ) { Sys_Printf ( " \n " , pak -> filename , filename ) ; if ( handle ) { * handle = pak -> handle ; Sys_FileSeek ( pak -> handle , pak -> files [ i ] . filepos ) ; } else { * file = fopen ( pak -> filename , " " ) ; if ( * file ) fseek ( * file , pak -> files [ i ] . filepos , SEEK_SET ) ; } com_filesize = pak -> files [ i ] . filelen ; return com_filesize ; } } else { sprintf ( netpath , " " , search -> filename , filename ) ; findtime = Sys_FileTime ( netpath ) ; if ( findtime == -1 ) continue ; if ( ! com_cachedir [ 0 ] ) strcpy ( cachepath , netpath ) ; else { #if defined ( _WIN32 ) if ( ( strlen ( netpath ) < 2 ) || ( netpath [ 1 ] != ' ' ) ) sprintf ( cachepath , " " , com_cachedir , netpath ) ; else sprintf ( cachepath , " " , com_cachedir , netpath + 2 ) ; #else sprintf ( cachepath , " " , com_cachedir , netpath ) ; #endif cachetime = Sys_FileTime ( cachepath ) ; if ( cachetime < findtime ) FileSystem_CopyFile ( netpath , cachepath ) ; strcpy ( netpath , cachepath ) ; } Sys_Printf ( " \n " , netpath ) ; com_filesize = Sys_FileOpenRead ( netpath , & i ) ; if ( handle ) * handle = i ; else { Sys_FileClose ( i ) ; * file = fopen ( netpath , " " ) ; } return com_filesize ; } } Sys_Printf ( " \n " , filename ) ; if ( handle ) * handle = -1 ; else * file = NULL ; com_filesize = -1 ; return -1 ; }
Finds the file in the search path.
Finds the file in the search path .
// search through the path, one element at a time // is the element a pak file? // look through all the pak file elements // found it! // open a new file on the pakfile // see if the file needs to be updated in the cache