Skip to content

Commit

Permalink
PK3 file support, file system refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
toni committed Oct 27, 2024
1 parent 723c5c8 commit 55fce64
Show file tree
Hide file tree
Showing 49 changed files with 1,946 additions and 1,228 deletions.
1 change: 1 addition & 0 deletions Quake/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ OBJS = strlcat.o \
crc.o \
cvar.o \
cfgfile.o \
filesys.o \
host.o \
host_cmd.o \
mathlib.o \
Expand Down
1 change: 1 addition & 0 deletions Quake/Makefile.w32
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ OBJS = strlcat.o \
crc.o \
cvar.o \
cfgfile.o \
filesys.o \
host.o \
host_cmd.o \
mathlib.o \
Expand Down
1 change: 1 addition & 0 deletions Quake/Makefile.w64
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ OBJS = strlcat.o \
crc.o \
cvar.o \
cfgfile.o \
filesys.o \
host.o \
host_cmd.o \
mathlib.o \
Expand Down
2 changes: 1 addition & 1 deletion Quake/bgmusic.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ void BGM_PlayCDtrack (byte track, qboolean looping)
// goto _next;
q_snprintf(tmp, sizeof(tmp), "%s/track%02d.%s",
MUSIC_DIRNAME, (int)track, handler->ext);
if (! COM_FileExists(tmp, &path_id))
if (!QFS_FileExists(tmp, &path_id))
goto _next;
if (path_id > prev_id)
{
Expand Down
52 changes: 9 additions & 43 deletions Quake/cfgfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@

#include "quakedef.h"


static fshandle_t *cfg_file;

/*
===================
CFG_ReadCvars
Expand All @@ -35,12 +32,17 @@ the num_vars argument must be the exact number of strings in the
array, otherwise I have nothing against going out of bounds.
===================
*/
void CFG_ReadCvars (const char **vars, int num_vars)
void CFG_ReadCvars (const char *cfg_name, const char **vars, int num_vars)
{
char buff[1024], *tmp;
int i, j;
qfshandle_t *cfg_file;

if (!cfg_file || num_vars < 1)
if (num_vars < 1)
return;

cfg_file = QFS_OpenFile (cfg_name, NULL);
if (!cfg_file)
return;

j = 0;
Expand All @@ -52,7 +54,7 @@ void CFG_ReadCvars (const char **vars, int num_vars)
// writes to the config file. although I'm trying to be as
// much cautious as possible, if the user screws it up by
// editing it, it's his fault.
if (FS_fgets(buff, sizeof(buff), cfg_file))
if (QFS_GetLine (cfg_file, buff, sizeof(buff)))
{
// remove end-of-line characters
while (buff[i])
Expand Down Expand Up @@ -104,9 +106,7 @@ void CFG_ReadCvars (const char **vars, int num_vars)
if (j == num_vars)
break;

} while (!FS_feof(cfg_file) && !FS_ferror(cfg_file));

FS_rewind (cfg_file);
} while (!QFS_Eof (cfg_file));
}

/*
Expand Down Expand Up @@ -139,37 +139,3 @@ void CFG_ReadCvarOverrides (const char **vars, int num_vars)
}
}
}

void CFG_CloseConfig (void)
{
if (cfg_file)
{
FS_fclose(cfg_file);
Z_Free(cfg_file);
cfg_file = NULL;
}
}

int CFG_OpenConfig (const char *cfg_name)
{
FILE *f;
long length;
qboolean pak;

CFG_CloseConfig ();

length = (long) COM_FOpenFile (cfg_name, &f, NULL);
pak = file_from_pak;
if (length == -1)
return -1;

cfg_file = (fshandle_t *) Z_Malloc(sizeof(fshandle_t));
cfg_file->file = f;
cfg_file->start = ftell(f);
cfg_file->pos = 0;
cfg_file->length = length;
cfg_file->pak = pak;

return 0;
}

9 changes: 1 addition & 8 deletions Quake/cfgfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,7 @@
#ifndef __CFGFILE_H
#define __CFGFILE_H

int CFG_OpenConfig (const char *cfg_name);
// opens the given config file. only one open config file is
// kept: previosly opened one, if any, will be closed.

void CFG_CloseConfig (void);
// closes the currently open config file.

void CFG_ReadCvars (const char **vars, int num_vars);
void CFG_ReadCvars (const char *cfg_name, const char **vars, int num_vars);
// reads the values of cvars in the given list from the opened
// config file.

Expand Down
54 changes: 25 additions & 29 deletions Quake/cl_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,12 @@ void CL_StopPlayback (void)
if (!cls.demoplayback)
return;

fclose (cls.demofile);
QFS_CloseFile (cls.inpdemo);
cls.demoplayback = false;
cls.demopaused = false;
cls.demospeed = 1.f;
cls.demofile = NULL;
cls.inpdemo = NULL;
cls.demofilesize = 0;
cls.demofilestart = 0;
cls.demofilename[0] = '\0';
cls.state = ca_disconnected;

Expand All @@ -137,14 +136,14 @@ static void CL_WriteDemoMessage (void)
float f;

len = LittleLong (net_message.cursize);
fwrite (&len, 4, 1, cls.demofile);
fwrite (&len, 4, 1, cls.outpdemo);
for (i = 0; i < 3; i++)
{
f = LittleFloat (cl.viewangles[i]);
fwrite (&f, 4, 1, cls.demofile);
fwrite (&f, 4, 1, cls.outpdemo);
}
fwrite (net_message.data, net_message.cursize, 1, cls.demofile);
fflush (cls.demofile);
fwrite (net_message.data, net_message.cursize, 1, cls.outpdemo);
fflush (cls.outpdemo);
}

/*
Expand Down Expand Up @@ -257,7 +256,7 @@ static qboolean CL_NextDemoFrame (void)
demoframe_t newframe;

memset (&newframe, 0, sizeof (newframe));
newframe.fileofs = Sys_ftell (cls.demofile);
newframe.fileofs = QFS_Tell (cls.inpdemo);
newframe.intermission = cl.intermission;
newframe.forceunderwater = cl.forceunderwater;
VEC_PUSH (demo_rewind.frames, newframe);
Expand All @@ -276,7 +275,7 @@ static qboolean CL_NextDemoFrame (void)
return false;

lastframe = &demo_rewind.frames[framecount - 1];
Sys_fseek (cls.demofile, lastframe->fileofs, SEEK_SET);
QFS_Seek (cls.inpdemo, lastframe->fileofs, SEEK_SET);

if (framecount == 1)
demo_rewind.backstop = true;
Expand Down Expand Up @@ -448,20 +447,20 @@ static int CL_GetDemoMessage (void)
if (!CL_NextDemoFrame ())
return 0;

if (fread (&net_message.cursize, 4, 1, cls.demofile) != 1)
if (QFS_ReadFile (cls.inpdemo, &net_message.cursize, 4) != 4)
goto readerror;
VectorCopy (cl.mviewangles[0], cl.mviewangles[1]);
for (i = 0 ; i < 3 ; i++)
{
if (fread (&f, 4, 1, cls.demofile) != 1)
if (QFS_ReadFile (cls.inpdemo, &f, 4) != 4)
goto readerror;
cl.mviewangles[0][i] = LittleFloat (f);
}

net_message.cursize = LittleLong (net_message.cursize);
if (net_message.cursize > MAX_MSGLEN)
Sys_Error ("Demo message > MAX_MSGLEN");
if (fread (net_message.data, net_message.cursize, 1, cls.demofile) != 1)
if (QFS_ReadFile (cls.inpdemo, net_message.data, net_message.cursize) != (size_t)net_message.cursize)
{
readerror:
CL_StopPlayback ();
Expand Down Expand Up @@ -538,8 +537,8 @@ void CL_Stop_f (void)
CL_WriteDemoMessage ();

// finish up
fclose (cls.demofile);
cls.demofile = NULL;
fclose (cls.outpdemo);
cls.outpdemo = NULL;
cls.demorecording = false;
Con_Printf ("Completed demo\n");

Expand Down Expand Up @@ -629,15 +628,15 @@ void CL_Record_f (void)
Con_LinkPrintf (name, "%s", relname);
Con_SafePrintf (".\n");

cls.demofile = Sys_fopen (name, "wb");
if (!cls.demofile)
cls.outpdemo = Sys_fopen (name, "wb");
if (!cls.outpdemo)
{
Con_Printf ("ERROR: couldn't create %s\n", relname);
return;
}

cls.forcetrack = track;
fprintf (cls.demofile, "%i\n", cls.forcetrack);
fprintf (cls.outpdemo, "%i\n", cls.forcetrack);
q_strlcpy (cls.demofilename, name, sizeof (cls.demofilename));

cls.demorecording = true;
Expand Down Expand Up @@ -760,6 +759,7 @@ play [demoname]
void CL_PlayDemo_f (void)
{
char name[MAX_OSPATH];
char linebuf[32];

if (cmd_source != src_command)
return;
Expand All @@ -779,22 +779,19 @@ void CL_PlayDemo_f (void)

Con_Printf ("Playing demo from %s.\n", name);

COM_FOpenFile (name, &cls.demofile, NULL);
if (!cls.demofile)
cls.inpdemo = QFS_FOpenFile (name, NULL);
if (!cls.inpdemo)
{
Con_Printf ("ERROR: couldn't open %s\n", name);
cls.demonum = -1; // stop demo loop
return;
}

// ZOID, fscanf is evil
// O.S.: if a space character e.g. 0x20 (' ') follows '\n',
// fscanf skips that byte too and screws up further reads.
// fscanf (cls.demofile, "%i\n", &cls.forcetrack);
if (fscanf (cls.demofile, "%i", &cls.forcetrack) != 1 || fgetc (cls.demofile) != '\n')
if (QFS_GetLine (cls.inpdemo, linebuf, sizeof(linebuf)) == 0
|| sscanf (linebuf, "%i", &cls.forcetrack) != 1)
{
fclose (cls.demofile);
cls.demofile = NULL;
QFS_CloseFile (cls.inpdemo);
cls.inpdemo = NULL;
cls.demonum = -1; // stop demo loop
Con_Printf ("ERROR: demo \"%s\" is invalid\n", name);
return;
Expand All @@ -810,8 +807,7 @@ void CL_PlayDemo_f (void)
q_strlcpy (cls.demofilename, name, sizeof (cls.demofilename));
cls.state = ca_connected;
cls.demoloop = Cmd_Argc () >= 3 ? Q_atoi (Cmd_Argv (2)) != 0 : false;
cls.demofilestart = Sys_ftell (cls.demofile);
cls.demofilesize = com_filesize;
cls.demofilesize = QFS_FileSize (cls.inpdemo);

// if this is a player-initiated demo, get rid of the console
if (cls.demonum == -1 && key_dest == key_console)
Expand Down Expand Up @@ -858,7 +854,7 @@ void CL_TimeDemo_f (void)
}

CL_PlayDemo_f ();
if (!cls.demofile)
if (!cls.inpdemo)
return;

// cls.td_starttime will be grabbed at the second frame of the demo, so
Expand Down
4 changes: 2 additions & 2 deletions Quake/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ typedef struct
qboolean timedemo;
int forcetrack; // -1 = use normal cd track
char demofilename[MAX_OSPATH];
FILE *demofile;
qfileofs_t demofilestart; // for demos in pak files
qfshandle_t *inpdemo;
FILE *outpdemo;
qfileofs_t demofilesize;
int td_lastframe; // to meter out one message a frame
int td_startframe; // host_framecount at start
Expand Down
4 changes: 2 additions & 2 deletions Quake/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,15 @@ void Cmd_Exec_f (void)
// "exec config.cfg pls" will execute config.cfg
if (Cmd_Argc () == 2 && !strcmp (path, "config.cfg"))
{
f = (const char *)COM_LoadHunkFile (CONFIG_NAME, NULL);
f = (const char *)QFS_LoadHunkFile (CONFIG_NAME, NULL, NULL);
if (f)
{
path = CONFIG_NAME;
goto exec;
}
}

f = (const char *)COM_LoadHunkFile (path, NULL);
f = (const char *)QFS_LoadHunkFile (path, NULL, NULL);
if (!f && !strcmp(Cmd_Argv(1), "default.cfg")) {
f = default_cfg; /* see above.. */
}
Expand Down
Loading

0 comments on commit 55fce64

Please sign in to comment.