From 165055db7b4bc815f74e92802e8a2b45d864ac3f Mon Sep 17 00:00:00 2001 From: Tyrann Date: Sun, 19 Aug 2007 05:32:46 +0930 Subject: [PATCH] [PATCH] qbsp: Add -wadpath option Add the "-wadpath" command-line argument to allow the user to specify a directory where wad files can be found. If no wadpath is given, the path defaults to the directory where the map file is located. Absolute path names in the worldspawn "wad" key should also work. Signed-off-by: Tyrann --- qbsp/cmdlib.c | 17 +++++++++++++++++ qbsp/qbsp.c | 15 ++++++++++++++- qbsp/qbsp.h | 3 +++ qbsp/wad.c | 17 +++++++++++++++-- 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/qbsp/cmdlib.c b/qbsp/cmdlib.c index 71a5ccd9..18390794 100644 --- a/qbsp/cmdlib.c +++ b/qbsp/cmdlib.c @@ -107,3 +107,20 @@ StripExtension(char *path) if (length) path[length] = 0; } + +void +StripFilename(char *path) +{ + int length; + + length = strlen(path) - 1; + while (length > 0 && path[length] != PATHSEPERATOR) + length--; + path[length] = '\0'; +} + +int +IsAbsolutePath(const char *path) +{ + return path[0] == '/' || (isalpha(path[0]) && path[1] == ':'); +} diff --git a/qbsp/qbsp.c b/qbsp/qbsp.c index 0d2d2313..496c0947 100644 --- a/qbsp/qbsp.c +++ b/qbsp/qbsp.c @@ -315,6 +315,7 @@ PrintOptions(void) printf(" -nopercent Prevents output of percent completion information\n"); printf(" -leakdist [n] Space between leakfile points (default 2)\n"); printf(" -subdivide [n] Use different texture subdivision (default 240)\n"); + printf(" -wadpath Search this directory for wad files\n"); printf(" sourcefile .MAP file to process\n"); printf(" destfile .BSP file to output\n"); @@ -433,6 +434,12 @@ ParseOptions(char *szOptions) Message(msgError, errInvalidOption, szTok); options.dxSubdivide = atoi(szTok2); szTok = szTok2; + } else if (!strcasecmp(szTok, "wadpath")) { + szTok2 = GetTok(szTok + strlen(szTok) + 1, szEnd); + if (!szTok2) + Message(msgError, errInvalidOption, szTok); + strcpy(options.wadPath, szTok2); + szTok = szTok2; } else if (!strcasecmp(szTok, "?") || !strcasecmp(szTok, "help")) PrintOptions(); else @@ -467,7 +474,7 @@ InitQBSP(int argc, char **argv) options.dxLeakDist = 2; options.dxSubdivide = 240; options.fVerbose = true; - options.szMapName[0] = options.szBSPName[0] = 0; + options.szMapName[0] = options.szBSPName[0] = options.wadPath[0] = 0; length = LoadFile("qbsp.ini", (void *)&szBuf, false); if (length) { @@ -509,6 +516,12 @@ InitQBSP(int argc, char **argv) if (options.szBSPName[0] == 0) strcpy(options.szBSPName, options.szMapName); + /* If no wadpath given, default to the map directory */ + if (options.wadPath[0] == 0) { + strcpy(options.wadPath, options.szMapName); + StripFilename(options.wadPath); + } + // Remove already existing files if (!options.fOnlyents) { StripExtension(options.szBSPName); diff --git a/qbsp/qbsp.h b/qbsp/qbsp.h index 1417eca9..00fb98ec 100644 --- a/qbsp/qbsp.h +++ b/qbsp/qbsp.h @@ -163,6 +163,8 @@ double I_FloatTime(void); void DefaultExtension(char *path, char *extension); void StripExtension(char *path); +void StripFilename(char *path); +int IsAbsolutePath(const char *path); char *copystring(char *s); @@ -525,6 +527,7 @@ typedef struct options_s { int dxLeakDist; char szMapName[512]; char szBSPName[512]; + char wadPath[512]; } options_t; extern options_t options; diff --git a/qbsp/wad.c b/qbsp/wad.c index 801c3002..97b3ab4a 100644 --- a/qbsp/wad.c +++ b/qbsp/wad.c @@ -57,6 +57,8 @@ WADList_Init(wad_t **wads, char *wadstring) int i, len, numwads; wad_t *tmp, *w; char *fname; + char *fpath; + int pathlen; numwads = 0; *wads = NULL; @@ -83,14 +85,25 @@ WADList_Init(wad_t **wads, char *wadstring) while (wadstring[i] != 0 && wadstring[i] != ';') i++; wadstring[i++] = 0; - w->file = fopen(fname, "rb"); + + if (!options.wadPath[0] || IsAbsolutePath(fname)) + fpath = copystring(fname); + else { + pathlen = strlen(options.wadPath) + strlen(fname) + 1; + fpath = AllocMem(OTHER, pathlen + 1, false); + sprintf(fpath, "%s/%s", options.wadPath, fname); + } + w->file = fopen(fpath, "rb"); if (w->file) { + if (options.fVerbose) + Message(msgLiteral, "Opened WAD: %s\n", fpath); if (!WAD_LoadInfo(w)) { - Message(msgWarning, warnNotWad, fname); + Message(msgWarning, warnNotWad, fpath); fclose(w->file); } else w++; } + FreeMem(fpath, OTHER, strlen(fpath) + 1); } /* Re-allocate just the required amount */