123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479 |
- #ifdef HAVE_CONFIG_H
- #include "config.h"
- #endif
- #ifdef HAVE_UNISTD_H
- #include <unistd.h>
- #endif
- #ifdef _MSC_VER
- #include <stddef.h>
- #include <io.h>
- #endif
- #include <fcntl.h>
- #include "doomstat.h"
- #include "d_net.h"
- #include "doomtype.h"
- #include "i_system.h"
- #ifdef __GNUG__
- #pragma implementation "w_wad.h"
- #endif
- #include "w_wad.h"
- #include "lprintf.h"
- lumpinfo_t *lumpinfo;
- int numlumps;
- void ExtractFileBase (const char *path, char *dest)
- {
- const char *src = path + strlen(path) - 1;
- int length;
-
- while (src != path && src[-1] != ':'
- && *(src-1) != '\\'
- && *(src-1) != '/')
- {
- src--;
- }
-
- memset(dest,0,8);
- length = 0;
- while ((*src) && (*src != '.') && (++length<9))
- {
- *dest++ = toupper(*src);
- src++;
- }
-
- }
- char *AddDefaultExtension(char *path, const char *ext)
- {
- char *p = path;
- while (*p++);
- while (p-->path && *p!='/' && *p!='\\')
- if (*p=='.')
- return path;
- if (*ext!='.')
- strcat(path,".");
- return strcat(path,ext);
- }
- static void W_AddFile(wadfile_info_t *wadfile)
- {
- wadinfo_t header;
- lumpinfo_t* lump_p;
- unsigned i;
- int length;
- int startlump;
- filelump_t *fileinfo, *fileinfo2free=NULL;
- filelump_t singleinfo;
-
- wadfile->handle = open(wadfile->name,O_RDONLY | O_BINARY);
- #ifdef HAVE_NET
- if (wadfile->handle == -1 && D_NetGetWad(wadfile->name))
- wadfile->handle = open(wadfile->name,O_RDONLY | O_BINARY);
- #endif
-
- if (wadfile->handle == -1)
- {
- if ( strlen(wadfile->name)<=4 ||
- (strcasecmp(wadfile->name+strlen(wadfile->name)-4 , ".lmp" ) &&
- strcasecmp(wadfile->name+strlen(wadfile->name)-4 , ".gwa" ) )
- )
- I_Error("W_AddFile: couldn't open %s",wadfile->name);
- return;
- }
-
- lprintf (LO_INFO," adding %s\n",wadfile->name);
- startlump = numlumps;
- if ( strlen(wadfile->name)<=4 ||
- (
- strcasecmp(wadfile->name+strlen(wadfile->name)-4,".wad") &&
- strcasecmp(wadfile->name+strlen(wadfile->name)-4,".gwa")
- )
- )
- {
-
- fileinfo = &singleinfo;
- singleinfo.filepos = 0;
- singleinfo.size = LONG(I_Filelength(wadfile->handle));
- ExtractFileBase(wadfile->name, singleinfo.name);
- numlumps++;
- }
- else
- {
-
- I_Read(wadfile->handle, &header, sizeof(header));
- if (strncmp(header.identification,"IWAD",4) &&
- strncmp(header.identification,"PWAD",4))
- I_Error("W_AddFile: Wad file %s doesn't have IWAD or PWAD id", wadfile->name);
- header.numlumps = LONG(header.numlumps);
- header.infotableofs = LONG(header.infotableofs);
- length = header.numlumps*sizeof(filelump_t);
- fileinfo2free = fileinfo = malloc(length);
- lseek(wadfile->handle, header.infotableofs, SEEK_SET);
- I_Read(wadfile->handle, fileinfo, length);
- numlumps += header.numlumps;
- }
-
- lumpinfo = realloc(lumpinfo, numlumps*sizeof(lumpinfo_t));
- lump_p = &lumpinfo[startlump];
- for (i=startlump ; (int)i<numlumps ; i++,lump_p++, fileinfo++)
- {
- lump_p->wadfile = wadfile;
- lump_p->position = LONG(fileinfo->filepos);
- lump_p->size = LONG(fileinfo->size);
- lump_p->li_namespace = ns_global;
- strncpy (lump_p->name, fileinfo->name, 8);
- lump_p->source = wadfile->src;
- }
- free(fileinfo2free);
- }
- static int IsMarker(const char *marker, const char *name)
- {
- return !strncasecmp(name, marker, 8) ||
- (*name == *marker && !strncasecmp(name+1, marker, 7));
- }
- static void W_CoalesceMarkedResource(const char *start_marker,
- const char *end_marker, int li_namespace)
- {
- lumpinfo_t *marked = malloc(sizeof(*marked) * numlumps);
- size_t i, num_marked = 0, num_unmarked = 0;
- int is_marked = 0, mark_end = 0;
- lumpinfo_t *lump = lumpinfo;
- for (i=numlumps; i--; lump++)
- if (IsMarker(start_marker, lump->name))
- {
- if (!num_marked)
- {
- strncpy(marked->name, start_marker, 8);
- marked->size = 0;
- marked->li_namespace = ns_global;
- marked->wadfile = NULL;
- num_marked = 1;
- }
- is_marked = 1;
- }
- else
- if (IsMarker(end_marker, lump->name))
- {
- mark_end = 1;
- is_marked = 0;
- }
- else
- if (is_marked)
- {
- marked[num_marked] = *lump;
- marked[num_marked++].li_namespace = li_namespace;
- }
- else
- lumpinfo[num_unmarked++] = *lump;
-
- memcpy(lumpinfo + num_unmarked, marked, num_marked * sizeof(*marked));
- free(marked);
- numlumps = num_unmarked + num_marked;
- if (mark_end)
- {
- lumpinfo[numlumps].size = 0;
- lumpinfo[numlumps].wadfile = NULL;
- lumpinfo[numlumps].li_namespace = ns_global;
- strncpy(lumpinfo[numlumps++].name, end_marker, 8);
- }
- }
- unsigned W_LumpNameHash(const char *s)
- {
- unsigned hash;
- (void) ((hash = toupper(s[0]), s[1]) &&
- (hash = hash*3+toupper(s[1]), s[2]) &&
- (hash = hash*2+toupper(s[2]), s[3]) &&
- (hash = hash*2+toupper(s[3]), s[4]) &&
- (hash = hash*2+toupper(s[4]), s[5]) &&
- (hash = hash*2+toupper(s[5]), s[6]) &&
- (hash = hash*2+toupper(s[6]),
- hash = hash*2+toupper(s[7]))
- );
- return hash;
- }
- int (W_CheckNumForName)(register const char *name, register int li_namespace)
- {
-
-
-
- register int i = (numlumps==0)?(-1):(lumpinfo[W_LumpNameHash(name) % (unsigned) numlumps].index);
-
-
-
-
-
- while (i >= 0 && (strncasecmp(lumpinfo[i].name, name, 8) ||
- lumpinfo[i].li_namespace != li_namespace))
- i = lumpinfo[i].next;
-
- return i;
- }
- void W_HashLumps(void)
- {
- int i;
- for (i=0; i<numlumps; i++)
- lumpinfo[i].index = -1;
-
-
-
- for (i=0; i<numlumps; i++)
- {
- int j = W_LumpNameHash(lumpinfo[i].name) % (unsigned) numlumps;
- lumpinfo[i].next = lumpinfo[j].index;
- lumpinfo[j].index = i;
- }
- }
- int W_GetNumForName (const char* name)
- {
- int i = W_CheckNumForName (name);
- if (i == -1)
- I_Error("W_GetNumForName: %.8s not found", name);
- return i;
- }
- wadfile_info_t *wadfiles=NULL;
- size_t numwadfiles = 0;
- void W_Init(void)
- {
-
- numlumps = 0;
- free(lumpinfo);
- lumpinfo = NULL;
-
- numlumps = 0; lumpinfo = NULL;
- {
-
- int i;
- for (i=0; (size_t)i<numwadfiles; i++)
- W_AddFile(&wadfiles[i]);
- }
- if (!numlumps)
- I_Error ("W_Init: No files found");
-
-
-
-
-
- W_CoalesceMarkedResource("S_START", "S_END", ns_sprites);
- W_CoalesceMarkedResource("F_START", "F_END", ns_flats);
- W_CoalesceMarkedResource("C_START", "C_END", ns_colormaps);
- W_CoalesceMarkedResource("B_START", "B_END", ns_prboom);
-
- W_HashLumps();
-
- lprintf(LO_INFO,"W_InitCache\n");
- W_InitCache();
- }
- void W_ReleaseAllWads(void)
- {
- W_DoneCache();
- numwadfiles = 0;
- free(wadfiles);
- wadfiles = NULL;
- numlumps = 0;
- free(lumpinfo);
- lumpinfo = NULL;
- }
- int W_LumpLength (int lump)
- {
- if (lump >= numlumps)
- I_Error ("W_LumpLength: %i >= numlumps",lump);
- return lumpinfo[lump].size;
- }
- void W_ReadLump(int lump, void *dest)
- {
- lumpinfo_t *l = lumpinfo + lump;
- #ifdef RANGECHECK
- if (lump >= numlumps)
- I_Error ("W_ReadLump: %i >= numlumps",lump);
- #endif
- {
- if (l->wadfile)
- {
- lseek(l->wadfile->handle, l->position, SEEK_SET);
- I_Read(l->wadfile->handle, dest, l->size);
- }
- }
- }
|