From 132f0387180d68d2f5730a0b112dc1fc73ccf711 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sun, 8 Aug 2021 09:13:29 +0200 Subject: [PATCH] - added Build GRP extraction function. Unlike for WADs this will just dump the content to a subdirectory without analyzing anything. --- resourcefile.h | 3 +-- wadext.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ wadfile.cpp | 57 ++++++++++++++++++++++++++++++++-------------- 3 files changed, 102 insertions(+), 19 deletions(-) diff --git a/resourcefile.h b/resourcefile.h index d94c401..73a168f 100644 --- a/resourcefile.h +++ b/resourcefile.h @@ -28,8 +28,7 @@ class CWADFile uint64_t m_LumpStart; public: - CWADFile(const char * filename); - CWADFile(const char * name, const char * memory); + CWADFile(const char * filename, FILE* f); ~CWADFile(void); const char * GetName() const diff --git a/wadext.cpp b/wadext.cpp index 16511d1..d9b9dbe 100644 --- a/wadext.cpp +++ b/wadext.cpp @@ -670,3 +670,64 @@ void ConvertTextureX() if (ft2) GenerateTextureFile("textures.txt2", bt2, bpn, 0, false); } +//========================================================================== +// +// +// +//========================================================================== + +struct GrpInfo +{ + uint32_t Magic[3]; + uint32_t NumLumps; +}; + +struct GrpLump +{ + union + { + struct + { + char Name[12]; + uint32_t Size; + }; + char NameWithZero[13]; + }; +}; + + +void GrpExtract(const char* filename, FILE* f) +{ + TArray fileinfo; + + GrpInfo header; + + if (1 != fread(&header, sizeof(header), 1, f)) return; + + fileinfo.Resize(header.NumLumps); + if (header.NumLumps != fread(&fileinfo[0], sizeof(GrpLump), header.NumLumps, f)) return; + if (memcmp(header.Magic, "KenSilverman", 12)) + { + return; + } + + auto name = ExtractFileBase(filename, false); + mkdir(name.c_str()); + chdir(name.c_str()); + + TArray buffer; + for (uint32_t i = 0; i < header.NumLumps; i++) + { + buffer.Resize(fileinfo[i].Size); + fileinfo[i].NameWithZero[12] = '\0'; // Be sure filename is null-terminated + if (fileinfo[i].Size != fread(&buffer[0], 1, fileinfo[i].Size, f)) return; + FILE* fout = fopen(fileinfo[i].NameWithZero, "wb"); + if (fout) + { + fwrite(&buffer[0], 1, fileinfo[i].Size, fout); + fclose(fout); + } + } + exit(1); +} + diff --git a/wadfile.cpp b/wadfile.cpp index c02463a..0ebfa3d 100644 --- a/wadfile.cpp +++ b/wadfile.cpp @@ -30,7 +30,7 @@ // //========================================================================== -CWADFile::CWADFile(const char * filename) +CWADFile::CWADFile(const char * filename, FILE* f) { char type[4]; int diroffset; @@ -40,20 +40,6 @@ CWADFile::CWADFile(const char * filename) m_LumpStart=0; m_Filename = strdup(filename); - m_File=NULL; - - if (filename == NULL) - { - printf("No file name specified\n"); - return; - } - - FILE *f = fopen(filename, "rb"); - if (!f) - { - printf("%s: unable to open\n", filename); - return; - } m_File = f; fread(&type, 1, 4, f); @@ -172,6 +158,14 @@ const char * CWADFile::GetLumpName(int lump) } } +//========================================================================== +// +// +// +//========================================================================== + +void GrpExtract(const char* filename, FILE* f); + //========================================================================== // // @@ -181,9 +175,38 @@ CWADFile *mainwad; void OpenMainWad(char *filename) { - mainwad = new CWADFile(filename); - if (mainwad->NumLumps() == 0) + if (filename == NULL) + { + printf("No file name specified\n"); + exit(1); + } + + FILE *f = fopen(filename, "rb"); + if (!f) + { + printf("%s: unable to open\n", filename); + exit(1); + } + + + char type[4]; + fread(&type, 1, 4, f); + fseek(f, 0, SEEK_SET); + + if (memcmp(type, "IWAD", 4) == 0 || memcmp(type, "PWAD", 4) == 0) { + mainwad = new CWADFile(filename, f); + if (mainwad->NumLumps() == 0) + { + exit(1); + } + } + else if (memcmp(type, "KenS", 4) == 0) + { + GrpExtract(filename, f); exit(1); } + printf("%s: not a WAD file\n", filename); + exit(1); } +