- added Build GRP extraction function.

Unlike for WADs this will just dump the content to a subdirectory without analyzing anything.
This commit is contained in:
Christoph Oelckers 2021-08-08 09:13:29 +02:00
parent 5eecfa5947
commit 132f038718
3 changed files with 102 additions and 19 deletions

View file

@ -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

View file

@ -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);
}

View file

@ -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 (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);
}