diff --git a/bsputil/bsputil.c b/bsputil/bsputil.c index 242d0fe9..c962798d 100644 --- a/bsputil/bsputil.c +++ b/bsputil/bsputil.c @@ -22,6 +22,62 @@ #include #include +/* FIXME - share header with qbsp, etc. */ +typedef struct { + char identification[4]; // should be WAD2 + int numlumps; + int infotableofs; +} wadinfo_t; + +typedef struct { + int filepos; + int disksize; + int size; // uncompressed + char type; + char compression; + char pad1, pad2; + char name[16]; // must be null terminated +} lumpinfo_t; + +void +ExportWad(FILE *f) +{ + wadinfo_t header; + dmiptexlump_t *m; + miptex_t *mt; + int i, data; + lumpinfo_t l; + + m = (dmiptexlump_t *)dtexdata; + + memcpy(&header.identification, "WAD2", 4); + header.numlumps = m->nummiptex; + header.infotableofs = sizeof(wadinfo_t); + fwrite(&header, sizeof(wadinfo_t), 1, f); + + data = sizeof(wadinfo_t) + sizeof(lumpinfo_t) * m->nummiptex; + + for (i = 0; i < m->nummiptex; i++) { + mt = (miptex_t *)((byte *)m + m->dataofs[i]); + + l.filepos = data; + l.disksize = sizeof(miptex_t) + mt->width * mt->height / 64 * 85; + l.size = l.disksize; + l.type = 'D'; + l.compression = 0; + l.pad1 = l.pad2 = 0; + memcpy(l.name, mt->name, 15); + l.name[15] = 0; + fwrite(&l, sizeof(lumpinfo_t), 1, f); + + data += l.disksize; + } + for (i = 0; i < m->nummiptex; i++) { + mt = (miptex_t *)((byte *)m + m->dataofs[i]); + fwrite(mt, sizeof(miptex_t) + mt->width * mt->height / 64 * 85, 1, f); + } +} + int main(int argc, char **argv) { @@ -31,7 +87,8 @@ main(int argc, char **argv) FILE *f; if (argc == 1) - Error("usage: bsputil [--extract-entities] bspfile"); + Error("usage: bsputil [--extract-entities] [--extract-textures] " + "bspfile"); strcpy(source, argv[argc - 1]); DefaultExtension(source, ".bsp"); @@ -48,12 +105,28 @@ main(int argc, char **argv) f = fopen(source, "w"); if (!f) - Error("couldn't open %s for writing\n"); + Error("couldn't open %s for writing\n", source); err = fwrite(dentdata, sizeof(char), entdatasize - 1, f); if (err != entdatasize - 1) Error("%s", strerror(errno)); + err = fclose(f); + if (err) + Error("%s", strerror(errno)); + + printf("done.\n"); + } else if (!strcmp(argv[i], "--extract-textures")) { + StripExtension(source); + DefaultExtension(source, ".wad"); + printf("-> writing %s... ", source); + + f = fopen(source, "wb"); + if (!f) + Error("couldn't open %s for writing\n", source); + + ExportWad(f); + err = fclose(f); if (err) Error("%s", strerror(errno));