diff options
Diffstat (limited to 'src/hexen/w_wad.c')
-rw-r--r-- | src/hexen/w_wad.c | 573 |
1 files changed, 292 insertions, 281 deletions
diff --git a/src/hexen/w_wad.c b/src/hexen/w_wad.c index 50b2ac92..7ad7bb2f 100644 --- a/src/hexen/w_wad.c +++ b/src/hexen/w_wad.c @@ -1,14 +1,26 @@ +// Emacs style mode select -*- C++ -*- +//----------------------------------------------------------------------------- +// +// Copyright(C) 1993-1996 Id Software, Inc. +// Copyright(C) 1993-2008 Raven Software +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +// 02111-1307, USA. +// +//----------------------------------------------------------------------------- -//************************************************************************** -//** -//** w_wad.c : Heretic 2 : Raven Software, Corp. -//** -//** $RCSfile: w_wad.c,v $ -//** $Revision: 1.6 $ -//** $Date: 95/10/06 20:56:47 $ -//** $Author: cjr $ -//** -//************************************************************************** // HEADER FILES ------------------------------------------------------------ @@ -35,16 +47,16 @@ typedef struct { - char identification[4]; - int numlumps; - int infotableofs; + char identification[4]; + int numlumps; + int infotableofs; } wadinfo_t; typedef struct { - int filepos; - int size; - char name[8]; + int filepos; + int size; + char name[8]; } filelump_t; // EXTERNAL FUNCTION PROTOTYPES -------------------------------------------- @@ -83,8 +95,8 @@ boolean AuxiliaryOpened = false; void strupr(char *s) { - while(*s) - *s++ = toupper(*s); + while (*s) + *s++ = toupper(*s); } //========================================================================== @@ -97,10 +109,10 @@ int filelength(int handle) { struct stat fileinfo; - if(fstat(handle, &fileinfo) == -1) - { - I_Error("Error fstating"); - } + if (fstat(handle, &fileinfo) == -1) + { + I_Error("Error fstating"); + } return fileinfo.st_size; } #endif @@ -116,71 +128,71 @@ int filelength(int handle) void W_AddFile(char *filename) { - wadinfo_t header; - lumpinfo_t *lump_p; - unsigned i; - int handle, length; - int startlump; - filelump_t *fileinfo, singleinfo; - filelump_t *freeFileInfo; - - if((handle = open(filename, O_RDONLY|O_BINARY)) == -1) - { // Didn't find file - return; - } - startlump = numlumps; - if(strcmpi(filename+strlen(filename)-3, "wad")) - { // Single lump file - fileinfo = &singleinfo; - freeFileInfo = NULL; - singleinfo.filepos = 0; - singleinfo.size = LONG(filelength(handle)); - M_ExtractFileBase(filename, singleinfo.name); - numlumps++; - } - else - { // WAD file - read(handle, &header, sizeof(header)); - if(strncmp(header.identification, "IWAD", 4)) - { - if(strncmp(header.identification, "PWAD", 4)) - { // Bad file id - I_Error("Wad file %s doesn't have IWAD or PWAD id\n", - filename); - } - } - header.numlumps = LONG(header.numlumps); - header.infotableofs = LONG(header.infotableofs); - length = header.numlumps*sizeof(filelump_t); -// fileinfo = alloca(length); - if(!(fileinfo = malloc(length))) - { - I_Error("W_AddFile: fileinfo malloc failed\n"); - } - freeFileInfo = fileinfo; - lseek(handle, header.infotableofs, SEEK_SET); - read(handle, fileinfo, length); - numlumps += header.numlumps; - } - - // Fill in lumpinfo - lumpinfo = realloc(lumpinfo, numlumps*sizeof(lumpinfo_t)); - if(!lumpinfo) - { - I_Error("Couldn't realloc lumpinfo"); - } - lump_p = &lumpinfo[startlump]; - for(i = startlump; i < numlumps; i++, lump_p++, fileinfo++) - { - lump_p->handle = handle; - lump_p->position = LONG(fileinfo->filepos); - lump_p->size = LONG(fileinfo->size); - strncpy(lump_p->name, fileinfo->name, 8); - } - if(freeFileInfo) - { - free(freeFileInfo); - } + wadinfo_t header; + lumpinfo_t *lump_p; + unsigned i; + int handle, length; + int startlump; + filelump_t *fileinfo, singleinfo; + filelump_t *freeFileInfo; + + if ((handle = open(filename, O_RDONLY | O_BINARY)) == -1) + { // Didn't find file + return; + } + startlump = numlumps; + if (strcmpi(filename + strlen(filename) - 3, "wad")) + { // Single lump file + fileinfo = &singleinfo; + freeFileInfo = NULL; + singleinfo.filepos = 0; + singleinfo.size = LONG(filelength(handle)); + M_ExtractFileBase(filename, singleinfo.name); + numlumps++; + } + else + { // WAD file + read(handle, &header, sizeof(header)); + if (strncmp(header.identification, "IWAD", 4)) + { + if (strncmp(header.identification, "PWAD", 4)) + { // Bad file id + I_Error("Wad file %s doesn't have IWAD or PWAD id\n", + filename); + } + } + header.numlumps = LONG(header.numlumps); + header.infotableofs = LONG(header.infotableofs); + length = header.numlumps * sizeof(filelump_t); +// fileinfo = alloca(length); + if (!(fileinfo = malloc(length))) + { + I_Error("W_AddFile: fileinfo malloc failed\n"); + } + freeFileInfo = fileinfo; + lseek(handle, header.infotableofs, SEEK_SET); + read(handle, fileinfo, length); + numlumps += header.numlumps; + } + + // Fill in lumpinfo + lumpinfo = realloc(lumpinfo, numlumps * sizeof(lumpinfo_t)); + if (!lumpinfo) + { + I_Error("Couldn't realloc lumpinfo"); + } + lump_p = &lumpinfo[startlump]; + for (i = startlump; i < numlumps; i++, lump_p++, fileinfo++) + { + lump_p->handle = handle; + lump_p->position = LONG(fileinfo->filepos); + lump_p->size = LONG(fileinfo->size); + strncpy(lump_p->name, fileinfo->name, 8); + } + if (freeFileInfo) + { + free(freeFileInfo); + } } //========================================================================== @@ -196,33 +208,33 @@ void W_AddFile(char *filename) void W_InitMultipleFiles(char **filenames) { - int size; - - // Open all the files, load headers, and count lumps - numlumps = 0; - lumpinfo = malloc(1); // Will be realloced as lumps are added - - for(; *filenames; filenames++) - { - W_AddFile(*filenames); - } - if(!numlumps) - { - I_Error("W_InitMultipleFiles: no files found"); - } - - // Set up caching - size = numlumps*sizeof(*lumpcache); - lumpcache = malloc(size); - if(!lumpcache) - { - I_Error("Couldn't allocate lumpcache"); - } - memset(lumpcache, 0, size); - - PrimaryLumpInfo = lumpinfo; - PrimaryLumpCache = lumpcache; - PrimaryNumLumps = numlumps; + int size; + + // Open all the files, load headers, and count lumps + numlumps = 0; + lumpinfo = malloc(1); // Will be realloced as lumps are added + + for (; *filenames; filenames++) + { + W_AddFile(*filenames); + } + if (!numlumps) + { + I_Error("W_InitMultipleFiles: no files found"); + } + + // Set up caching + size = numlumps * sizeof(*lumpcache); + lumpcache = malloc(size); + if (!lumpcache) + { + I_Error("Couldn't allocate lumpcache"); + } + memset(lumpcache, 0, size); + + PrimaryLumpInfo = lumpinfo; + PrimaryLumpCache = lumpcache; + PrimaryNumLumps = numlumps; } //========================================================================== @@ -235,11 +247,11 @@ void W_InitMultipleFiles(char **filenames) void W_InitFile(char *filename) { - char *names[2]; + char *names[2]; - names[0] = filename; - names[1] = NULL; - W_InitMultipleFiles(names); + names[0] = filename; + names[1] = NULL; + W_InitMultipleFiles(names); } //========================================================================== @@ -250,64 +262,63 @@ void W_InitFile(char *filename) void W_OpenAuxiliary(char *filename) { - int i; - int size; - wadinfo_t header; - int handle; - int length; - filelump_t *fileinfo; - filelump_t *sourceLump; - lumpinfo_t *destLump; - - if(AuxiliaryOpened) - { - W_CloseAuxiliary(); - } - if((handle = open(filename, O_RDONLY|O_BINARY)) == -1) - { - I_Error("W_OpenAuxiliary: %s not found.", filename); - return; - } - AuxiliaryHandle = handle; - read(handle, &header, sizeof(header)); - if(strncmp(header.identification, "IWAD", 4)) - { - if(strncmp(header.identification, "PWAD", 4)) - { // Bad file id - I_Error("Wad file %s doesn't have IWAD or PWAD id\n", - filename); - } - } - header.numlumps = LONG(header.numlumps); - header.infotableofs = LONG(header.infotableofs); - length = header.numlumps*sizeof(filelump_t); - fileinfo = Z_Malloc(length, PU_STATIC, 0); - lseek(handle, header.infotableofs, SEEK_SET); - read(handle, fileinfo, length); - numlumps = header.numlumps; - - // Init the auxiliary lumpinfo array - lumpinfo = Z_Malloc(numlumps*sizeof(lumpinfo_t), PU_STATIC, 0); - sourceLump = fileinfo; - destLump = lumpinfo; - for(i = 0; i < numlumps; i++, destLump++, sourceLump++) - { - destLump->handle = handle; - destLump->position = LONG(sourceLump->filepos); - destLump->size = LONG(sourceLump->size); - strncpy(destLump->name, sourceLump->name, 8); - } - Z_Free(fileinfo); - - // Allocate the auxiliary lumpcache array - size = numlumps*sizeof(*lumpcache); - lumpcache = Z_Malloc(size, PU_STATIC, 0); - memset(lumpcache, 0, size); - - AuxiliaryLumpInfo = lumpinfo; - AuxiliaryLumpCache = lumpcache; - AuxiliaryNumLumps = numlumps; - AuxiliaryOpened = true; + int i; + int size; + wadinfo_t header; + int handle; + int length; + filelump_t *fileinfo; + filelump_t *sourceLump; + lumpinfo_t *destLump; + + if (AuxiliaryOpened) + { + W_CloseAuxiliary(); + } + if ((handle = open(filename, O_RDONLY | O_BINARY)) == -1) + { + I_Error("W_OpenAuxiliary: %s not found.", filename); + return; + } + AuxiliaryHandle = handle; + read(handle, &header, sizeof(header)); + if (strncmp(header.identification, "IWAD", 4)) + { + if (strncmp(header.identification, "PWAD", 4)) + { // Bad file id + I_Error("Wad file %s doesn't have IWAD or PWAD id\n", filename); + } + } + header.numlumps = LONG(header.numlumps); + header.infotableofs = LONG(header.infotableofs); + length = header.numlumps * sizeof(filelump_t); + fileinfo = Z_Malloc(length, PU_STATIC, 0); + lseek(handle, header.infotableofs, SEEK_SET); + read(handle, fileinfo, length); + numlumps = header.numlumps; + + // Init the auxiliary lumpinfo array + lumpinfo = Z_Malloc(numlumps * sizeof(lumpinfo_t), PU_STATIC, 0); + sourceLump = fileinfo; + destLump = lumpinfo; + for (i = 0; i < numlumps; i++, destLump++, sourceLump++) + { + destLump->handle = handle; + destLump->position = LONG(sourceLump->filepos); + destLump->size = LONG(sourceLump->size); + strncpy(destLump->name, sourceLump->name, 8); + } + Z_Free(fileinfo); + + // Allocate the auxiliary lumpcache array + size = numlumps * sizeof(*lumpcache); + lumpcache = Z_Malloc(size, PU_STATIC, 0); + memset(lumpcache, 0, size); + + AuxiliaryLumpInfo = lumpinfo; + AuxiliaryLumpCache = lumpcache; + AuxiliaryNumLumps = numlumps; + AuxiliaryOpened = true; } //========================================================================== @@ -318,24 +329,24 @@ void W_OpenAuxiliary(char *filename) void W_CloseAuxiliary(void) { - int i; - - if(AuxiliaryOpened) - { - W_UseAuxiliary(); - for(i = 0; i < numlumps; i++) - { - if(lumpcache[i]) - { - Z_Free(lumpcache[i]); - } - } - Z_Free(AuxiliaryLumpInfo); - Z_Free(AuxiliaryLumpCache); - W_CloseAuxiliaryFile(); - AuxiliaryOpened = false; - } - W_UsePrimary(); + int i; + + if (AuxiliaryOpened) + { + W_UseAuxiliary(); + for (i = 0; i < numlumps; i++) + { + if (lumpcache[i]) + { + Z_Free(lumpcache[i]); + } + } + Z_Free(AuxiliaryLumpInfo); + Z_Free(AuxiliaryLumpCache); + W_CloseAuxiliaryFile(); + AuxiliaryOpened = false; + } + W_UsePrimary(); } //========================================================================== @@ -349,11 +360,11 @@ void W_CloseAuxiliary(void) void W_CloseAuxiliaryFile(void) { - if(AuxiliaryHandle) - { - close(AuxiliaryHandle); - AuxiliaryHandle = 0; - } + if (AuxiliaryHandle) + { + close(AuxiliaryHandle); + AuxiliaryHandle = 0; + } } //========================================================================== @@ -364,9 +375,9 @@ void W_CloseAuxiliaryFile(void) void W_UsePrimary(void) { - lumpinfo = PrimaryLumpInfo; - numlumps = PrimaryNumLumps; - lumpcache = PrimaryLumpCache; + lumpinfo = PrimaryLumpInfo; + numlumps = PrimaryNumLumps; + lumpcache = PrimaryLumpCache; } //========================================================================== @@ -377,13 +388,13 @@ void W_UsePrimary(void) void W_UseAuxiliary(void) { - if(AuxiliaryOpened == false) - { - I_Error("W_UseAuxiliary: WAD not opened."); - } - lumpinfo = AuxiliaryLumpInfo; - numlumps = AuxiliaryNumLumps; - lumpcache = AuxiliaryLumpCache; + if (AuxiliaryOpened == false) + { + I_Error("W_UseAuxiliary: WAD not opened."); + } + lumpinfo = AuxiliaryLumpInfo; + numlumps = AuxiliaryNumLumps; + lumpcache = AuxiliaryLumpCache; } //========================================================================== @@ -392,9 +403,9 @@ void W_UseAuxiliary(void) // //========================================================================== -int W_NumLumps(void) +int W_NumLumps(void) { - return numlumps; + return numlumps; } //========================================================================== @@ -407,27 +418,27 @@ int W_NumLumps(void) int W_CheckNumForName(char *name) { - char name8[9]; - int v1, v2; - lumpinfo_t *lump_p; - - // Make the name into two integers for easy compares - strncpy(name8, name, 8); - name8[8] = 0; // in case the name was a full 8 chars - strupr(name8); // case insensitive - v1 = *(int *)name8; - v2 = *(int *)&name8[4]; - - // Scan backwards so patch lump files take precedence - lump_p = lumpinfo+numlumps; - while(lump_p-- != lumpinfo) - { - if(*(int *)lump_p->name == v1 && *(int *)&lump_p->name[4] == v2) - { - return lump_p-lumpinfo; - } - } - return -1; + char name8[9]; + int v1, v2; + lumpinfo_t *lump_p; + + // Make the name into two integers for easy compares + strncpy(name8, name, 8); + name8[8] = 0; // in case the name was a full 8 chars + strupr(name8); // case insensitive + v1 = *(int *) name8; + v2 = *(int *) &name8[4]; + + // Scan backwards so patch lump files take precedence + lump_p = lumpinfo + numlumps; + while (lump_p-- != lumpinfo) + { + if (*(int *) lump_p->name == v1 && *(int *) &lump_p->name[4] == v2) + { + return lump_p - lumpinfo; + } + } + return -1; } //========================================================================== @@ -438,17 +449,17 @@ int W_CheckNumForName(char *name) // //========================================================================== -int W_GetNumForName (char *name) +int W_GetNumForName(char *name) { - int i; - - i = W_CheckNumForName(name); - if(i != -1) - { - return i; - } - I_Error("W_GetNumForName: %s not found!", name); - return -1; + int i; + + i = W_CheckNumForName(name); + if (i != -1) + { + return i; + } + I_Error("W_GetNumForName: %s not found!", name); + return -1; } //========================================================================== @@ -461,11 +472,11 @@ int W_GetNumForName (char *name) int W_LumpLength(int lump) { - if(lump >= numlumps) - { - I_Error("W_LumpLength: %i >= numlumps", lump); - } - return lumpinfo[lump].size; + if (lump >= numlumps) + { + I_Error("W_LumpLength: %i >= numlumps", lump); + } + return lumpinfo[lump].size; } //========================================================================== @@ -478,23 +489,23 @@ int W_LumpLength(int lump) void W_ReadLump(int lump, void *dest) { - int c; - lumpinfo_t *l; - - if(lump >= numlumps) - { - I_Error("W_ReadLump: %i >= numlumps", lump); - } - l = lumpinfo+lump; - //I_BeginRead(); - lseek(l->handle, l->position, SEEK_SET); - c = read(l->handle, dest, l->size); - if(c < l->size) - { - I_Error("W_ReadLump: only read %i of %i on lump %i", - c, l->size, lump); - } - //I_EndRead(); + int c; + lumpinfo_t *l; + + if (lump >= numlumps) + { + I_Error("W_ReadLump: %i >= numlumps", lump); + } + l = lumpinfo + lump; + //I_BeginRead(); + lseek(l->handle, l->position, SEEK_SET); + c = read(l->handle, dest, l->size); + if (c < l->size) + { + I_Error("W_ReadLump: only read %i of %i on lump %i", + c, l->size, lump); + } + //I_EndRead(); } //========================================================================== @@ -505,22 +516,22 @@ void W_ReadLump(int lump, void *dest) void *W_CacheLumpNum(int lump, int tag) { - byte *ptr; - - if((unsigned)lump >= numlumps) - { - I_Error("W_CacheLumpNum: %i >= numlumps", lump); - } - if(!lumpcache[lump]) - { // Need to read the lump in - ptr = Z_Malloc(W_LumpLength(lump), tag, &lumpcache[lump]); - W_ReadLump(lump, lumpcache[lump]); - } - else - { - Z_ChangeTag(lumpcache[lump], tag); - } - return lumpcache[lump]; + byte *ptr; + + if ((unsigned) lump >= numlumps) + { + I_Error("W_CacheLumpNum: %i >= numlumps", lump); + } + if (!lumpcache[lump]) + { // Need to read the lump in + ptr = Z_Malloc(W_LumpLength(lump), tag, &lumpcache[lump]); + W_ReadLump(lump, lumpcache[lump]); + } + else + { + Z_ChangeTag(lumpcache[lump], tag); + } + return lumpcache[lump]; } //========================================================================== @@ -531,7 +542,7 @@ void *W_CacheLumpNum(int lump, int tag) void *W_CacheLumpName(char *name, int tag) { - return W_CacheLumpNum(W_GetNumForName(name), tag); + return W_CacheLumpNum(W_GetNumForName(name), tag); } //========================================================================== |