From a2e9503fdcf6ed0894ca838e54c37071e175bcf2 Mon Sep 17 00:00:00 2001 From: Tyrann Date: Mon, 2 Oct 2006 12:31:35 +0930 Subject: [PATCH] [PATCH] qbsp: WADList_Init/LoadLumpInfo cleanup Move the logic from WADList_LoadLumpInfo into WADList_Init. Now the init function will open all the wad files, testing for validity. Note that I still need to tidy up the error handling and warnings a little bit to handle the difference between filesystem/read errors and invalid/corrupt wad files. Signed-off-by: Tyrann --- qbsp/qbsp.c | 27 +++++----- qbsp/wad.c | 140 +++++++++++++++++++++++++--------------------------- qbsp/wad.h | 6 +-- 3 files changed, 83 insertions(+), 90 deletions(-) diff --git a/qbsp/qbsp.c b/qbsp/qbsp.c index bf3211c5..50584cc7 100644 --- a/qbsp/qbsp.c +++ b/qbsp/qbsp.c @@ -232,10 +232,8 @@ ProcessFile static void ProcessFile(void) { - char *szWad; - wadlist_t wad; - - WADList_Init(&wad); + char *wadstring; + wadlist_t wads; // load brushes and entities LoadMapFile(); @@ -244,15 +242,18 @@ ProcessFile(void) return; } - szWad = ValueForKey(0, "_wad"); - if (!szWad) { - szWad = ValueForKey(0, "wad"); - if (!szWad) { + wadstring = ValueForKey(0, "_wad"); + if (!wadstring) { + wadstring = ValueForKey(0, "wad"); + if (!wadstring) { Message(msgWarning, warnNoWadKey); pWorldEnt->cTexdata = 0; - } else if (!WADList_LoadLumpInfo(&wad, szWad)) { - Message(msgWarning, warnNoValidWads); - pWorldEnt->cTexdata = 0; + } else { + WADList_Init(&wads, wadstring); + if (!wads.numwads) { + Message(msgWarning, warnNoValidWads); + pWorldEnt->cTexdata = 0; + } } } // init the tables to be shared by all models @@ -264,10 +265,10 @@ ProcessFile(void) CreateHulls(); WriteEntitiesToString(); - WADList_Process(&wad); + WADList_Process(&wads); FinishBSPFile(); - WADList_Free(&wad); + WADList_Free(&wads); } diff --git a/qbsp/wad.c b/qbsp/wad.c index 0b325955..72c598fa 100644 --- a/qbsp/wad.c +++ b/qbsp/wad.c @@ -29,11 +29,74 @@ static void WADList_AddAnimatingTextures(wadlist_t *w); static int WAD_LoadLump(wad_t *w, char *name, byte *dest); -void -WADList_Init(wadlist_t *w) +static bool +WAD_LoadInfo(wad_t *w) { - w->numwads = 0; - w->wads = NULL; + wadinfo_t *hdr = &w->header; + bool ret = false; + int len; + + len = fread(hdr, 1, sizeof(wadinfo_t), w->file); + if (len == sizeof(wadinfo_t)) + if (!strncmp(hdr->identification, "WAD2", 4)) { + const int lumpinfo = sizeof(lumpinfo_t) * hdr->numlumps; + fseek(w->file, hdr->infotableofs, SEEK_SET); + w->lumps = AllocMem(OTHER, lumpinfo, true); + len = fread(w->lumps, 1, lumpinfo, w->file); + if (len == lumpinfo) + ret = true; + } + + return ret; +} + + +void +WADList_Init(wadlist_t *list, char *wadstring) +{ + int i, len; + wad_t *tmp, *w; + char *fname; + + list->numwads = 0; + list->wads = NULL; + + if (!wadstring) + return; + + len = strlen(wadstring); + if (len == 0) + return; + + // Count # of wads + list->numwads = 1; + for (i = 0; i < len; i++) + if (wadstring[i] == ';' && wadstring[i + 1] != ';') + list->numwads++; + + tmp = AllocMem(OTHER, list->numwads * sizeof(wad_t), true); + + w = tmp; + i = 0; + while (i < len) { + fname = wadstring + i; + while (wadstring[i] != 0 && wadstring[i] != ';') + i++; + wadstring[i++] = 0; + w->file = fopen(fname, "rb"); + if (w->file) { + if (WAD_LoadInfo(w)) + w++; + else + fclose(w->file); + } + } + + /* Re-allocate just the required amount */ + list->wads = AllocMem(OTHER, (w - tmp) * sizeof(wad_t), false); + memcpy(list->wads, tmp, (w - tmp) * sizeof(wad_t)); + list->numwads = w - tmp; + FreeMem(tmp, OTHER, list->numwads * sizeof(wad_t)); } @@ -53,75 +116,6 @@ WADList_Free(wadlist_t *w) } -bool -WADList_LoadLumpInfo(wadlist_t *list, char *wadstring) -{ - int i, len, ret; - wad_t *w, *tmp; - char *fname; - - if (!wadstring) - return false; - - len = strlen(wadstring); - if (len == 0) - return false; - - // Should never happen, but just in case... - assert(!list->wads); - - // Count # of wads - list->numwads = 1; - for (i = 0; i < len; i++) - if (wadstring[i] == ';' && wadstring[i + 1] != ';') - list->numwads++; - - list->wads = AllocMem(OTHER, list->numwads * sizeof(wad_t), true); - - // Verify that at least one WAD file exists - w = list->wads; - i = 0; - while (i < len) { - fname = wadstring + i; - while (wadstring[i] != 0 && wadstring[i] != ';') - i++; - wadstring[i] = 0; - i++; - - w->file = fopen(fname, "rb"); - if (w->file) { - ret = fread(&w->header, 1, sizeof(wadinfo_t), w->file); - if (ret != sizeof(wadinfo_t)) - Message(msgError, errReadFailure); - if (strncmp(w->header.identification, "WAD2", 4)) { - Message(msgWarning, warnNotWad, fname); - fclose(w->file); - } else { - fseek(w->file, w->header.infotableofs, SEEK_SET); - w->lumps = AllocMem(OTHER, sizeof(lumpinfo_t) * - w->header.numlumps, true); - ret = fread(w->lumps, 1, w->header.numlumps * - sizeof(lumpinfo_t), w->file); - if (ret != w->header.numlumps * sizeof(lumpinfo_t)) - Message(msgError, errReadFailure); - w++; - // Note that the file is NOT closed here! - // Also iWad is only incremented for valid files - } - } - } - - // Remove invalid wads from memory - tmp = AllocMem(OTHER, (w - list->wads) * sizeof(wad_t), true); - memcpy(tmp, list->wads, (w - list->wads) * sizeof(wad_t)); - FreeMem(list->wads, OTHER, list->numwads * sizeof(wad_t)); - list->numwads = w - list->wads; - list->wads = tmp; - - return list->numwads > 0; -} - - void WADList_Process(wadlist_t *w) { diff --git a/qbsp/wad.h b/qbsp/wad.h index 70751233..d486466e 100644 --- a/qbsp/wad.h +++ b/qbsp/wad.h @@ -51,10 +51,8 @@ typedef struct { int numwads; } wadlist_t; -void WADList_Init(wadlist_t *w); +void WADList_Init(wadlist_t *list, char *wadstring); +void WADList_Process(wadlist_t *w); void WADList_Free(wadlist_t *w); -bool WADList_LoadLumpInfo(wadlist_t *w, char *list); -void WADList_Process(wadlist_t *w); - #endif /* WAD_H */