Skip to content

Commit

Permalink
- added Build GRP extraction function.
Browse files Browse the repository at this point in the history
Unlike for WADs this will just dump the content to a subdirectory without analyzing anything.
  • Loading branch information
Christoph Oelckers committed Aug 8, 2021
1 parent 5eecfa5 commit 132f038
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 19 deletions.
3 changes: 1 addition & 2 deletions resourcefile.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
61 changes: 61 additions & 0 deletions wadext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<GrpLump> 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<char> 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);
}

57 changes: 40 additions & 17 deletions wadfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
//
//==========================================================================

CWADFile::CWADFile(const char * filename)
CWADFile::CWADFile(const char * filename, FILE* f)
{
char type[4];
int diroffset;
Expand All @@ -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);
Expand Down Expand Up @@ -172,6 +158,14 @@ const char * CWADFile::GetLumpName(int lump)
}
}

//==========================================================================
//
//
//
//==========================================================================

void GrpExtract(const char* filename, FILE* f);

//==========================================================================
//
//
Expand All @@ -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);
}

0 comments on commit 132f038

Please sign in to comment.