Skip to content

Commit

Permalink
Stub in filesystem calls, add wad files
Browse files Browse the repository at this point in the history
find include/doom src/doom -name "*.[h,c]" -exec sed -i 's/lseek/I_Lseek/g' {} +
find include/doom src/doom -name "*.[h,c]" -exec sed -i 's/fclose/I_FClose/g' {} +
find include/doom src/doom -name "*.[h,c]" -exec sed -i 's/fseek/I_FSeek/g' {} +
find include/doom src/doom -name "*.[h,c]" -exec sed -i 's/fwrite/I_FWrite/g' {} +
find include/doom src/doom -name "*.[h,c]" -exec sed -i 's/fread/I_FRead/g' {} +

Then, add a wad file compressor tool, and stub the filesystem calls to
read that wad file after it has been decompressed during runtime.
  • Loading branch information
ErnWong committed Sep 11, 2018
1 parent dbc8b47 commit 171f126
Show file tree
Hide file tree
Showing 11 changed files with 9,118 additions and 118 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
bin
res
include/doom1wad.h
10 changes: 10 additions & 0 deletions include/doom/i_system.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef __I_SYSTEM__
#define __I_SYSTEM__

#include "doom/doomtype.h"
#include "doom/m_fixed.h"
#include <stdio.h>

typedef struct
{

Expand All @@ -25,7 +29,13 @@ fixed_t I_GetTimeFrac(void);
unsigned long I_GetRandomTimeSeed(void);
void I_uSleep(unsigned long usecs);
char *I_FindFile(const char *wfname, const char *ext);
void I_LSeek(int fd, size_t offset, int whence);
int I_FSeek(FILE *file, long offset, int origin);
int I_Open(const char *filename, int mode);
FILE *I_FOpen(const char *filename, const char *mode);
int I_FClose(FILE *file);
void I_Read(int fd, void *buf, size_t sz);
size_t I_FRead(void *buf, size_t sz, size_t count, FILE * file);
int I_Filelength(int handle);

extern int ms_to_next_tick;
Expand Down
1,328 changes: 1,328 additions & 0 deletions include/miniz.h

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions src/doom/d_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,10 @@ static void CheckIWAD(const char *iwadname, GameMode_t *gmode, boolean *hassec)
size_t length;
filelump_t *fileinfo;

if (!(fp = fopen(iwadname, "rb")))
if (!(fp = I_FOpen(iwadname, "rb")))
I_Error("CheckIWAD: could not open %s", iwadname);

if (fread(&header, sizeof (header), 1, fp) != 1)
if (I_FRead(&header, sizeof (header), 1, fp) != 1)
I_Error("CheckIWAD: could not read %s", iwadname);

if (!(strncmp(header.identification, "IWAD", 4) == 0 || strncmp(header.identification, "PWAD", 4) == 0))
Expand All @@ -256,13 +256,13 @@ static void CheckIWAD(const char *iwadname, GameMode_t *gmode, boolean *hassec)
length = header.numlumps;
fileinfo = malloc(length * sizeof (filelump_t));

if (fseek(fp, header.infotableofs, SEEK_SET))
if (I_FSeek(fp, header.infotableofs, SEEK_SET))
I_Error("CheckIWAD: failed to seek");

if (fread(fileinfo, sizeof (filelump_t), length, fp) != length)
if (I_FRead(fileinfo, sizeof (filelump_t), length, fp) != length)
I_Error("CheckIWAD: failed to read");

fclose(fp);
I_FClose(fp);

while (length--)
{
Expand Down
139 changes: 139 additions & 0 deletions src/doom/prosv5/i_files.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#include "api.h"
#include "miniz.h"
#include "doom1wad.h"
#include "doom/i_system.h"
#include <fcntl.h>

#define WAD_FILE_SIZE 1024 * 1024 * 5
FILE * wadfd = (FILE *)1;
char * wadname = "doom1.wad";
unsigned char * waddata;
size_t wadpos = 0;
size_t wadlen = WAD_FILE_SIZE;

void I_InitFS()
{

int status;
mz_ulong len = wadlen;
waddata = malloc(WAD_FILE_SIZE * sizeof(waddata[0]));
status = uncompress(waddata, &len, res_doom1_zlib, res_doom1_zlib_len);
wadlen = len;
if (status != Z_OK)
{
I_Error("Error decompressing file: %s", zError(status));
}

}

void I_LSeek(int fd, size_t offset, int whence)
{

I_FSeek((FILE*)fd, (long)offset, whence);

}

int I_Open(const char * filename, int mode)
{

if (mode != O_RDONLY)
{
I_Error("IOpen: unrecognized mode %d", mode);
}

int fd = (int)I_FOpen(filename, "r");

if (fd == 0)
{
return -1;
}
return fd;

}

void I_Read(int fd, void *vbuf, size_t sz)
{

unsigned char *buf = vbuf;

while (sz)
{

int rc = (int)I_FRead(buf, sizeof(char), sz, (FILE*)fd);

if (rc <= 0)
I_Error("I_Read: read failed: %s", rc ? strerror(errno) : "EOF");

sz -= rc;
buf += rc;

}

}

int I_FSeek(FILE *file, long offset, int origin)
{

if (origin != SEEK_SET)
{
return 1;
}
wadpos = offset;
return 0;

}

FILE *I_FOpen(const char *filename, const char *mode)
{

if (strcmp(filename, wadname) != 0)
{
return NULL;
}

wadpos = 0;
return wadfd;

}

int I_FClose(FILE *file)
{

return 0;

}

size_t I_FRead(void *buf, size_t sz, size_t count, FILE * file)
{

if (file != wadfd)
{
return 0;
}
if (count * sz + wadpos > wadlen)
{
count = (wadlen - wadpos) / sz;
}
memcpy(buf, waddata + wadpos, count * sz);
wadpos += count * sz;
return count;

}

int I_Filelength(int handle)
{

if ((FILE*)handle != wadfd)
{
return 0;
}
return wadlen;

}

char *I_FindFile(const char *wfname, const char *ext)
{

return strcpy(malloc(strlen(wadname)), wadname);

}
111 changes: 0 additions & 111 deletions src/doom/prosv5/i_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,117 +157,6 @@ unsigned long I_GetRandomTimeSeed(void)

}

void I_Read(int fd, void *vbuf, size_t sz)
{

unsigned char *buf = vbuf;

while (sz)
{

int rc = read(fd, buf, sz);

if (rc <= 0)
I_Error("I_Read: read failed: %s", rc ? strerror(errno) : "EOF");

sz -= rc;
buf += rc;

}

}

int I_Filelength(int handle)
{

struct stat fileinfo;

if (fstat(handle, &fileinfo) == -1)
I_Error("I_Filelength: %s", strerror(errno));

return fileinfo.st_size;

}

static boolean HasTrailingSlash(const char *dn)
{

return ((dn[strlen(dn) - 1] == '/'));

}

char *I_FindFile(const char *wfname, const char *ext)
{

static const struct
{

const char *dir;
const char *sub;
const char *env;
const char *(*func)(void);

} search[] = {
{NULL},
{NULL, "doom", "HOME"},
{NULL, NULL, "HOME"},
{"/usr/local/share/games/doom"},
{"/usr/share/games/doom"},
{"/usr/local/share/doom"},
{"/usr/share/doom"},
};

int i;
size_t pl = strlen(wfname) + strlen(ext) + 4;

for (i = 0; i < sizeof(search) / sizeof(*search); i++)
{

char *p;
const char *d = NULL;
const char *s = NULL;

if (search[i].env)
{

if (!(d = getenv(search[i].env)))
continue;

}

else if (search[i].func)
{

d = search[i].func();

}

else
{

d = search[i].dir;

}

s = search[i].sub;
p = malloc((d ? strlen(d) : 0) + (s ? strlen(s) : 0) + pl);

sprintf(p, "%s%s%s%s%s", d ? d : "", (d && !HasTrailingSlash(d)) ? "/" : "", s ? s : "", (s && !HasTrailingSlash(s)) ? "/" : "", wfname);

if (access(p, F_OK))
strcat(p, ext);

if (!access(p, F_OK))
return p;

free(p);

}

return NULL;

}

void doom_start()
{

Expand Down
4 changes: 2 additions & 2 deletions src/doom/w_wad.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ static void W_AddFile(wadfile_info_t *wadfile)
length = header.numlumps * sizeof(filelump_t);
fileinfo2free = fileinfo = malloc(length);

lseek(wadfile->handle, header.infotableofs, SEEK_SET);
I_LSeek(wadfile->handle, header.infotableofs, SEEK_SET);
I_Read(wadfile->handle, fileinfo, length);

numlumps += header.numlumps;
Expand Down Expand Up @@ -335,7 +335,7 @@ void W_ReadLump(int lump, void *dest)
if (l->wadfile)
{

lseek(l->wadfile->handle, l->position, SEEK_SET);
I_LSeek(l->wadfile->handle, l->position, SEEK_SET);
I_Read(l->wadfile->handle, dest, l->size);

}
Expand Down
Loading

0 comments on commit 171f126

Please sign in to comment.