common.c (searchpath_t): Added new member `path_id', an unsigned

int identifier assigned to the game directory. Remember that main
<install_dir>/game1 and the unix <userdir>/game1 have the same id.
The id starts as 1 for the first data1 directory. For every new
directory, the value is the left-shifted version of the previous
one.  Made COM_FindFile() to accept a path_id pointer argument
and storing the id in it if it isn't NULL. Made COM_OpenFile(),
COM_FOpenFile() and all COM_Load* functions to accept a path_id
pointer argument similarly. Adjusted callers accordingly.


git-svn-id: http://svn.code.sf.net/p/quakespasm/code/trunk/quakespasm@370 af15c1b1-3010-417e-b628-4374ebc0bcbd
This commit is contained in:
sezero 2011-01-02 21:45:16 +00:00
parent 0323d3d388
commit 8dde04f7b5
14 changed files with 77 additions and 47 deletions

View file

@ -297,7 +297,7 @@ void CL_PlayDemo_f (void)
COM_DefaultExtension (name, ".dem");
Con_Printf ("Playing demo from %s.\n", name);
COM_FOpenFile (name, &cls.demofile);
COM_FOpenFile (name, &cls.demofile, NULL);
if (!cls.demofile)
{
Con_Printf ("ERROR: couldn't open.\n");

View file

@ -259,7 +259,7 @@ void Cmd_Exec_f (void)
}
mark = Hunk_LowMark ();
f = (char *)COM_LoadHunkFile (Cmd_Argv(1));
f = (char *)COM_LoadHunkFile (Cmd_Argv(1), NULL);
if (!f)
{
Con_Printf ("couldn't exec %s\n",Cmd_Argv(1));

View file

@ -1109,7 +1109,7 @@ void COM_CheckRegistered (void)
unsigned short check[128];
int i;
COM_OpenFile("gfx/pop.lmp", &h);
COM_OpenFile("gfx/pop.lmp", &h, NULL);
static_registered = 0;
if (h == -1)
@ -1366,6 +1366,9 @@ int file_from_pak; // ZOID: global indicating that file came from a pak
typedef struct searchpath_s
{
unsigned int path_id; // identifier assigned to the game directory
// Note that <install_dir>/game1 and
// <userdir>/game1 have the same id.
char filename[MAX_OSPATH];
pack_t *pack; // only one of filename / pack will be used
struct searchpath_s *next;
@ -1450,7 +1453,8 @@ Finds the file in the search path.
Sets com_filesize and one of handle or file
===========
*/
static int COM_FindFile (const char *filename, int *handle, FILE **file)
static int COM_FindFile (const char *filename, int *handle, FILE **file,
unsigned int *path_id)
{
searchpath_t *search;
char netpath[MAX_OSPATH];
@ -1492,6 +1496,8 @@ static int COM_FindFile (const char *filename, int *handle, FILE **file)
}
file_from_pak = 1;
com_filesize = pak->files[i].filelen;
if (path_id)
*path_id = search->path_id;
return com_filesize;
}
}
@ -1512,6 +1518,8 @@ static int COM_FindFile (const char *filename, int *handle, FILE **file)
// Sys_Printf ("FindFile: %s\n",netpath);
com_filesize = Sys_FileOpenRead (netpath, &i);
if (path_id)
*path_id = search->path_id;
if (handle)
*handle = i;
else
@ -1544,9 +1552,9 @@ returns a handle and a length
it may actually be inside a pak file
===========
*/
int COM_OpenFile (const char *filename, int *handle)
int COM_OpenFile (const char *filename, int *handle, unsigned int *path_id)
{
return COM_FindFile (filename, handle, NULL);
return COM_FindFile (filename, handle, NULL, path_id);
}
/*
@ -1557,9 +1565,9 @@ If the requested file is inside a packfile, a new FILE * will be opened
into the file.
===========
*/
int COM_FOpenFile (const char *filename, FILE **file)
int COM_FOpenFile (const char *filename, FILE **file, unsigned int *path_id)
{
return COM_FindFile (filename, NULL, file);
return COM_FindFile (filename, NULL, file, path_id);
}
/*
@ -1601,7 +1609,7 @@ static byte *loadbuf;
static cache_user_t *loadcache;
static int loadsize;
byte *COM_LoadFile (const char *path, int usehunk)
byte *COM_LoadFile (const char *path, int usehunk, unsigned int *path_id)
{
int h;
byte *buf;
@ -1611,7 +1619,7 @@ byte *COM_LoadFile (const char *path, int usehunk)
buf = NULL; // quiet compiler warning
// look for it in the filesystem or pack files
len = COM_OpenFile (path, &h);
len = COM_OpenFile (path, &h, path_id);
if (h == -1)
return NULL;
@ -1662,35 +1670,35 @@ byte *COM_LoadFile (const char *path, int usehunk)
return buf;
}
byte *COM_LoadHunkFile (const char *path)
byte *COM_LoadHunkFile (const char *path, unsigned int *path_id)
{
return COM_LoadFile (path, LOADFILE_HUNK);
return COM_LoadFile (path, LOADFILE_HUNK, path_id);
}
byte *COM_LoadZoneFile (const char *path)
byte *COM_LoadZoneFile (const char *path, unsigned int *path_id)
{
return COM_LoadFile (path, LOADFILE_ZONE);
return COM_LoadFile (path, LOADFILE_ZONE, path_id);
}
byte *COM_LoadTempFile (const char *path)
byte *COM_LoadTempFile (const char *path, unsigned int *path_id)
{
return COM_LoadFile (path, LOADFILE_TEMPHUNK);
return COM_LoadFile (path, LOADFILE_TEMPHUNK, path_id);
}
void COM_LoadCacheFile (const char *path, struct cache_user_s *cu)
void COM_LoadCacheFile (const char *path, struct cache_user_s *cu, unsigned int *path_id)
{
loadcache = cu;
COM_LoadFile (path, LOADFILE_CACHE);
COM_LoadFile (path, LOADFILE_CACHE, path_id);
}
// uses temp hunk if larger than bufsize
byte *COM_LoadStackFile (const char *path, void *buffer, int bufsize)
byte *COM_LoadStackFile (const char *path, void *buffer, int bufsize, unsigned int *path_id)
{
byte *buf;
loadbuf = (byte *)buffer;
loadsize = bufsize;
buf = COM_LoadFile (path, LOADFILE_STACK);
buf = COM_LoadFile (path, LOADFILE_STACK, path_id);
return buf;
}
@ -1698,13 +1706,13 @@ byte *COM_LoadStackFile (const char *path, void *buffer, int bufsize)
// loads into a previously allocated buffer. if space is insufficient
// or the buffer is NULL, loads onto the hunk. bufsize is the actual
// size (without the +1).
byte *COM_LoadBufFile (const char *path, void *buffer, int *bufsize)
byte *COM_LoadBufFile (const char *path, void *buffer, int *bufsize, unsigned int *path_id)
{
byte *buf;
loadbuf = (byte *)buffer;
loadsize = (*bufsize) + 1;
buf = COM_LoadFile (path, LOADFILE_BUF);
buf = COM_LoadFile (path, LOADFILE_BUF, path_id);
*bufsize = (buf == NULL) ? 0 : com_filesize;
if (loadbuf && buf && buf != loadbuf)
Sys_Printf("LoadBufFile: insufficient buffer for %s not used.\n", path);
@ -1713,9 +1721,9 @@ byte *COM_LoadBufFile (const char *path, void *buffer, int *bufsize)
}
// returns malloc'd memory
byte *COM_LoadMallocFile (const char *path)
byte *COM_LoadMallocFile (const char *path, unsigned int *path_id)
{
return COM_LoadFile (path, LOADFILE_MALLOC);
return COM_LoadFile (path, LOADFILE_MALLOC, path_id);
}
@ -1801,14 +1809,21 @@ COM_AddGameDirectory -- johnfitz -- modified based on topaz's tutorial
void COM_AddGameDirectory (const char *dir)
{
int i;
unsigned int path_id;
searchpath_t *search;
pack_t *pak;
char pakfile[MAX_OSPATH];
strcpy (com_gamedir, dir);
// assign a path_id to this game directory
if (com_searchpaths)
path_id = com_searchpaths->path_id << 1;
else path_id = 1U;
// add the directory to the search path
search = (searchpath_t *) Z_Malloc(sizeof(searchpath_t));
search->path_id = path_id;
strcpy (search->filename, dir);
search->next = com_searchpaths;
com_searchpaths = search;
@ -1821,6 +1836,7 @@ void COM_AddGameDirectory (const char *dir)
if (!pak)
break;
search = (searchpath_t *) Z_Malloc(sizeof(searchpath_t));
search->path_id = path_id;
search->pack = pak;
search->next = com_searchpaths;
com_searchpaths = search;

View file

@ -186,28 +186,31 @@ extern char com_gamedir[MAX_OSPATH];
extern int file_from_pak; // global indicating that file came from a pak
void COM_WriteFile (const char *filename, const void *data, int len);
int COM_OpenFile (const char *filename, int *hndl);
int COM_FOpenFile (const char *filename, FILE **file);
int COM_OpenFile (const char *filename, int *handle, unsigned int *path_id);
int COM_FOpenFile (const char *filename, FILE **file, unsigned int *path_id);
void COM_CloseFile (int h);
// these procedures open a file using COM_FindFile and loads it into a proper
// buffer. the buffer is allocated with a total size of com_filesize + 1. the
// procedures differ by their buffer allocation method.
byte *COM_LoadStackFile (const char *path, void *buffer, int bufsize);
byte *COM_LoadStackFile (const char *path, void *buffer, int bufsize,
unsigned int *path_id);
// uses the specified stack stack buffer with the specified size
// of bufsize. if bufsize is too short, uses temp hunk. the bufsize
// must include the +1
byte *COM_LoadTempFile (const char *path);
byte *COM_LoadTempFile (const char *path, unsigned int *path_id);
// allocates the buffer on the temp hunk.
byte *COM_LoadHunkFile (const char *path);
byte *COM_LoadHunkFile (const char *path, unsigned int *path_id);
// allocates the buffer on the hunk.
byte *COM_LoadZoneFile (const char *path);
byte *COM_LoadZoneFile (const char *path, unsigned int *path_id);
// allocates the buffer on the zone.
void COM_LoadCacheFile (const char *path, struct cache_user_s *cu);
void COM_LoadCacheFile (const char *path, struct cache_user_s *cu,
unsigned int *path_id);
// uses cache mem for allocating the buffer.
byte *COM_LoadMallocFile (const char *path);
byte *COM_LoadMallocFile (const char *path, unsigned int *path_id);
// allocates the buffer on the system mem (malloc).
byte *COM_LoadBufFile (const char *path, void *buffer, int *bufsize);
byte *COM_LoadBufFile (const char *path, void *buffer, int *bufsize,
unsigned int *path_id);
// uses the specified pre-allocated buffer with bufsize + 1 size.
// bufsize is the actual expected size (without the + 1). if the
// space is too short or the buffer is NULL, loads onto the hunk.

View file

@ -288,7 +288,7 @@ qpic_t *Draw_CachePic (const char *path)
//
// load the pic from disk
//
dat = (qpic_t *)COM_LoadTempFile (path);
dat = (qpic_t *)COM_LoadTempFile (path, NULL);
if (!dat)
Sys_Error ("Draw_CachePic: failed to load %s", path);
SwapPic (dat);
@ -322,7 +322,7 @@ extern char *get_conback(void);
static qboolean have_mod_conback;
void Draw_CheckConback (void)
{
have_mod_conback = (COM_LoadTempFile("gfx/conback.lmp") != NULL);
have_mod_conback = (COM_LoadTempFile("gfx/conback.lmp", NULL) != NULL);
}
qpic_t *Draw_ConbackPic (void)
{

View file

@ -277,7 +277,7 @@ model_t *Mod_LoadModel (model_t *mod, qboolean crash)
//
// load the file
//
buf = COM_LoadStackFile (mod->name, stackbuf, sizeof(stackbuf));
buf = COM_LoadStackFile (mod->name, stackbuf, sizeof(stackbuf), NULL);
if (!buf)
{
if (crash)
@ -640,7 +640,7 @@ void Mod_LoadLighting (lump_t *l)
COM_StripExtension(litfilename, litfilename);
strcat(litfilename, ".lit");
mark = Hunk_LowMark();
data = (byte*) COM_LoadHunkFile (litfilename);
data = (byte*) COM_LoadHunkFile (litfilename, NULL);
if (data)
{
if (data[0] == 'Q' && data[1] == 'L' && data[2] == 'I' && data[3] == 'T')

View file

@ -431,7 +431,7 @@ void TexMgr_LoadPalette (void)
int i, mark;
FILE *f;
COM_FOpenFile ("gfx/palette.lmp", &f);
COM_FOpenFile ("gfx/palette.lmp", &f, NULL);
if (!f)
Sys_Error ("Couldn't load gfx/palette.lmp");
@ -1228,7 +1228,7 @@ void TexMgr_ReloadImage (gltexture_t *glt, int shirt, int pants)
if (glt->source_file[0] && glt->source_offset)
{
//lump inside file
data = COM_LoadHunkFile (glt->source_file);
data = COM_LoadHunkFile (glt->source_file, NULL);
if (!data)
goto invalid;
data += glt->source_offset;

View file

@ -834,7 +834,7 @@ void Host_Init (quakeparms_t *parms)
if (cls.state != ca_dedicated)
{
host_colormap = (byte *)COM_LoadHunkFile ("gfx/colormap.lmp");
host_colormap = (byte *)COM_LoadHunkFile ("gfx/colormap.lmp", NULL);
if (!host_colormap)
Sys_Error ("Couldn't load gfx/colormap.lmp");

View file

@ -59,6 +59,7 @@ void Host_Quit_f (void)
//==============================================================================
// Declarations shared with common.c:
// FIXME: **** CLEAN THIS MESS!!! ***
typedef struct
{
char name[MAX_QPATH];
@ -75,6 +76,9 @@ typedef struct pack_s
typedef struct searchpath_s
{
unsigned int path_id; // identifier assigned to the game directory
// Note that <install_dir>/game1 and
// <userdir>/game1 have the same id.
char filename[MAX_OSPATH];
pack_t *pack; // only one of filename / pack will be used
struct searchpath_s *next;
@ -129,6 +133,7 @@ Host_Game_f
void Host_Game_f (void)
{
int i;
unsigned int path_id;
searchpath_t *search = com_searchpaths;
pack_t *pak;
char pakfile[MAX_OSPATH]; //FIXME: it's confusing to use this string for two different things
@ -172,7 +177,12 @@ void Host_Game_f (void)
if (Q_strcasecmp(Cmd_Argv(1), GAMENAME)) //game is not id1
{
// assign a path_id to this game directory
if (com_searchpaths)
path_id = com_searchpaths->path_id << 1;
else path_id = 1U;
search = (searchpath_t *) Z_Malloc(sizeof(searchpath_t));
search->path_id = path_id;
strcpy (search->filename, pakfile);
search->next = com_searchpaths;
com_searchpaths = search;
@ -185,6 +195,7 @@ void Host_Game_f (void)
if (!pak)
break;
search = (searchpath_t *) Z_Malloc(sizeof(searchpath_t));
search->path_id = path_id;
search->pack = pak;
search->next = com_searchpaths;
com_searchpaths = search;
@ -876,7 +887,7 @@ void Host_Changelevel_f (void)
//johnfitz -- check for client having map before anything else
sprintf (level, "maps/%s.bsp", Cmd_Argv(1));
if (COM_OpenFile (level, &i) == -1)
if (COM_OpenFile (level, &i, NULL) == -1)
Host_Error ("cannot find map %s", level);
//johnfitz

View file

@ -38,12 +38,12 @@ byte *Image_LoadImage (char *name, int *width, int *height)
FILE *f;
sprintf (loadfilename, "%s.tga", name);
COM_FOpenFile (loadfilename, &f);
COM_FOpenFile (loadfilename, &f, NULL);
if (f)
return Image_LoadTGA (f, width, height);
sprintf (loadfilename, "%s.pcx", name);
COM_FOpenFile (loadfilename, &f);
COM_FOpenFile (loadfilename, &f, NULL);
if (f)
return Image_LoadPCX (f, width, height);

View file

@ -1026,7 +1026,7 @@ void PR_LoadProgs (void)
CRC_Init (&pr_crc);
progs = (dprograms_t *)COM_LoadHunkFile ("progs.dat");
progs = (dprograms_t *)COM_LoadHunkFile ("progs.dat", NULL);
if (!progs)
Sys_Error ("PR_LoadProgs: couldn't load progs.dat");
Con_DPrintf ("Programs occupy %iK.\n", com_filesize/1024);

View file

@ -270,7 +270,7 @@ void R_ReadPointFile_f (void)
sprintf (name,"maps/%s.pts", sv.name);
COM_FOpenFile (name, &f);
COM_FOpenFile (name, &f, NULL);
if (!f)
{
Con_Printf ("couldn't open %s\n", name);

View file

@ -113,7 +113,7 @@ sfxcache_t *S_LoadSound (sfx_t *s)
// Con_Printf ("loading %s\n",namebuffer);
data = COM_LoadStackFile(namebuffer, stackbuf, sizeof(stackbuf));
data = COM_LoadStackFile(namebuffer, stackbuf, sizeof(stackbuf), NULL);
if (!data)
{

View file

@ -76,7 +76,7 @@ void W_LoadWadFile (void) //johnfitz -- filename is now hard-coded for honesty
//TODO: use cache_alloc
if (wad_base)
free (wad_base);
wad_base = COM_LoadMallocFile (filename);
wad_base = COM_LoadMallocFile (filename, NULL);
if (!wad_base)
Sys_Error ("W_LoadWadFile: couldn't load %s", filename);