- added support for SSI containers to the file system.

This commit is contained in:
Christoph Oelckers 2020-09-19 10:07:53 +02:00
parent 2bf5933923
commit a27211dc34
13 changed files with 169 additions and 12 deletions

View file

@ -906,6 +906,7 @@ set (PCH_SOURCES
common/filesystem/file_zip.cpp common/filesystem/file_zip.cpp
common/filesystem/file_pak.cpp common/filesystem/file_pak.cpp
common/filesystem/file_whres.cpp common/filesystem/file_whres.cpp
common/filesystem/file_ssi.cpp
common/filesystem/file_directory.cpp common/filesystem/file_directory.cpp
common/filesystem/resourcefile.cpp common/filesystem/resourcefile.cpp
common/engine/cycler.cpp common/engine/cycler.cpp

View file

@ -367,7 +367,7 @@ FResourceFile *Check7Z(const char *filename, FileReader &file, bool quiet, LumpF
file.Seek(0, FileReader::SeekSet); file.Seek(0, FileReader::SeekSet);
if (!memcmp(head, k7zSignature, k7zSignatureSize)) if (!memcmp(head, k7zSignature, k7zSignatureSize))
{ {
FResourceFile *rf = new F7ZFile(filename, file); auto rf = new F7ZFile(filename, file);
if (rf->Open(quiet, filter)) return rf; if (rf->Open(quiet, filter)) return rf;
file = std::move(rf->Reader); // to avoid destruction of reader file = std::move(rf->Reader); // to avoid destruction of reader

View file

@ -254,9 +254,9 @@ int FDirectoryLump::FillCache()
FResourceFile *CheckDir(const char *filename, bool quiet, bool nosubdirflag, LumpFilterInfo* filter) FResourceFile *CheckDir(const char *filename, bool quiet, bool nosubdirflag, LumpFilterInfo* filter)
{ {
FResourceFile *rf = new FDirectory(filename, nosubdirflag); auto rf = new FDirectory(filename, nosubdirflag);
if (rf->Open(quiet, filter)) return rf; if (rf->Open(quiet, filter)) return rf;
delete rf; delete rf;
return NULL; return nullptr;
} }

View file

@ -140,7 +140,7 @@ FResourceFile *CheckGRP(const char *filename, FileReader &file, bool quiet, Lump
file.Seek(0, FileReader::SeekSet); file.Seek(0, FileReader::SeekSet);
if (!memcmp(head, "KenSilverman", 12)) if (!memcmp(head, "KenSilverman", 12))
{ {
FResourceFile *rf = new FGrpFile(filename, file); auto rf = new FGrpFile(filename, file);
if (rf->Open(quiet, filter)) return rf; if (rf->Open(quiet, filter)) return rf;
file = std::move(rf->Reader); // to avoid destruction of reader file = std::move(rf->Reader); // to avoid destruction of reader

View file

@ -94,7 +94,7 @@ bool FLumpFile::Open(bool quiet, LumpFilterInfo*)
FResourceFile *CheckLump(const char *filename, FileReader &file, bool quiet, LumpFilterInfo* filter) FResourceFile *CheckLump(const char *filename, FileReader &file, bool quiet, LumpFilterInfo* filter)
{ {
// always succeeds // always succeeds
FResourceFile *rf = new FLumpFile(filename, file); auto rf = new FLumpFile(filename, file);
if (rf->Open(quiet, filter)) return rf; if (rf->Open(quiet, filter)) return rf;
file = std::move(rf->Reader); // to avoid destruction of reader file = std::move(rf->Reader); // to avoid destruction of reader
delete rf; delete rf;

View file

@ -134,7 +134,7 @@ FResourceFile *CheckPak(const char *filename, FileReader &file, bool quiet, Lump
file.Seek(0, FileReader::SeekSet); file.Seek(0, FileReader::SeekSet);
if (!memcmp(head, "PACK", 4)) if (!memcmp(head, "PACK", 4))
{ {
FResourceFile *rf = new FPakFile(filename, file); auto rf = new FPakFile(filename, file);
if (rf->Open(quiet, filter)) return rf; if (rf->Open(quiet, filter)) return rf;
file = std::move(rf->Reader); // to avoid destruction of reader file = std::move(rf->Reader); // to avoid destruction of reader

View file

@ -248,7 +248,7 @@ FResourceFile *CheckRFF(const char *filename, FileReader &file, bool quiet, Lump
file.Seek(0, FileReader::SeekSet); file.Seek(0, FileReader::SeekSet);
if (!memcmp(head, "RFF\x1a", 4)) if (!memcmp(head, "RFF\x1a", 4))
{ {
FResourceFile *rf = new FRFFFile(filename, file); auto rf = new FRFFFile(filename, file);
if (rf->Open(quiet, filter)) return rf; if (rf->Open(quiet, filter)) return rf;
file = std::move(rf->Reader); // to avoid destruction of reader file = std::move(rf->Reader); // to avoid destruction of reader

View file

@ -0,0 +1,155 @@
/*
** file_grp.cpp
**
**---------------------------------------------------------------------------
** Copyright 1998-2009 Randy Heit
** Copyright 2005-2020 Christoph Oelckers
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**---------------------------------------------------------------------------
**
**
*/
#include "resourcefile.h"
#include "printf.h"
//==========================================================================
//
// Build GRP file
//
//==========================================================================
class FSSIFile : public FUncompressedFile
{
public:
FSSIFile(const char * filename, FileReader &file);
bool Open(bool quiet, int version, int lumpcount, LumpFilterInfo* filter);
};
//==========================================================================
//
// Initializes a Build GRP file
//
//==========================================================================
FSSIFile::FSSIFile(const char *filename, FileReader &file)
: FUncompressedFile(filename, file)
{
}
//==========================================================================
//
// Open it
// Note that SSIs can contain embedded GRPs which must be flagged accordingly.
//
//==========================================================================
bool FSSIFile::Open(bool quiet, int version, int lumpcount, LumpFilterInfo*)
{
NumLumps = lumpcount*2;
Lumps.Resize(lumpcount*2);
int32_t j = (version == 2 ? 267 : 254) + (lumpcount * 121);
for (int i = 0; i < NumLumps; i+=2)
{
char fn[13];
int strlength = Reader.ReadUInt8();
if (strlength > 12) strlength = 12;
Reader.Read(fn, 12);
fn[strlength] = 0;
int flength = Reader.ReadInt32();
Lumps[i].LumpNameSetup(fn);
Lumps[i].Position = j;
Lumps[i].LumpSize = flength;
Lumps[i].Owner = this;
if (strstr(fn, ".GRP")) Lumps[i].Flags |= LUMPF_EMBEDDED;
// SSI files can swap the order of the extension's characters - but there's no reliable detection for this and it can be mixed inside the same container,
// so we have no choice but to create another file record for the altered name.
std::swap(fn[strlength - 1], fn[strlength - 3]);
Lumps[i+1].LumpNameSetup(fn);
Lumps[i+1].Position = j;
Lumps[i+1].LumpSize = flength;
Lumps[i+1].Owner = this;
if (strstr(fn, ".GRP")) Lumps[i+1].Flags |= LUMPF_EMBEDDED;
j += flength;
Reader.Seek(104, FileReader::SeekCur);
}
return true;
}
//==========================================================================
//
// File open
//
//==========================================================================
FResourceFile* CheckSSI(const char* filename, FileReader& file, bool quiet, LumpFilterInfo* filter)
{
char zerobuf[72];
char buf[72];
memset(zerobuf, 0, 72);
auto skipstring = [&](int length)
{
int strlength = file.ReadUInt8();
if (strlength > length) return false;
int count = file.Read(buf, length);
buf[length] = 0;
if (count != length || strlen(buf) != strlength) return false;
if (length != strlength && memcmp(buf + strlength, zerobuf, length - strlength)) return false;
return true;
};
if (file.GetLength() >= 12)
{
// check if SSI
// this performs several checks because there is no "SSI" magic
int version = file.ReadInt32();
if (version == 1 || version == 2) // if
{
int numfiles = file.ReadInt32();
if (!skipstring(32)) return nullptr;
if (version == 2 && !skipstring(12)) return nullptr;
for (int i = 0; i < 3; i++)
{
if (!skipstring(70)) return nullptr;
}
auto ssi = new FSSIFile(filename, file);
if (ssi->Open(filename, version, numfiles, filter)) return ssi;
file = std::move(ssi->Reader); // to avoid destruction of reader
delete ssi;
}
}
return nullptr;
}

View file

@ -472,7 +472,7 @@ FResourceFile *CheckWad(const char *filename, FileReader &file, bool quiet, Lump
file.Seek(0, FileReader::SeekSet); file.Seek(0, FileReader::SeekSet);
if (!memcmp(head, "IWAD", 4) || !memcmp(head, "PWAD", 4)) if (!memcmp(head, "IWAD", 4) || !memcmp(head, "PWAD", 4))
{ {
FResourceFile *rf = new FWadFile(filename, file); auto rf = new FWadFile(filename, file);
if (rf->Open(quiet, filter)) return rf; if (rf->Open(quiet, filter)) return rf;
file = std::move(rf->Reader); // to avoid destruction of reader file = std::move(rf->Reader); // to avoid destruction of reader

View file

@ -148,7 +148,7 @@ FResourceFile *CheckWHRes(const char *filename, FileReader &file, bool quiet, Lu
if (offset != checkpos || length <= 0) return nullptr; if (offset != checkpos || length <= 0) return nullptr;
checkpos += (length+4095) / 4096; checkpos += (length+4095) / 4096;
} }
FResourceFile *rf = new FWHResFile(filename, file); auto rf = new FWHResFile(filename, file);
if (rf->Open(quiet, filter)) return rf; if (rf->Open(quiet, filter)) return rf;
file = std::move(rf->Reader); // to avoid destruction of reader file = std::move(rf->Reader); // to avoid destruction of reader
delete rf; delete rf;

View file

@ -464,7 +464,7 @@ FResourceFile *CheckZip(const char *filename, FileReader &file, bool quiet, Lump
file.Seek(0, FileReader::SeekSet); file.Seek(0, FileReader::SeekSet);
if (!memcmp(head, "PK\x3\x4", 4)) if (!memcmp(head, "PK\x3\x4", 4))
{ {
FResourceFile *rf = new FZipFile(filename, file); auto rf = new FZipFile(filename, file);
if (rf->Open(quiet, filter)) return rf; if (rf->Open(quiet, filter)) return rf;
file = std::move(rf->Reader); // to avoid destruction of reader file = std::move(rf->Reader); // to avoid destruction of reader

View file

@ -240,10 +240,11 @@ FResourceFile *CheckRFF(const char *filename, FileReader &file, bool quiet, Lump
FResourceFile *CheckPak(const char *filename, FileReader &file, bool quiet, LumpFilterInfo* filter); FResourceFile *CheckPak(const char *filename, FileReader &file, bool quiet, LumpFilterInfo* filter);
FResourceFile *CheckZip(const char *filename, FileReader &file, bool quiet, LumpFilterInfo* filter); FResourceFile *CheckZip(const char *filename, FileReader &file, bool quiet, LumpFilterInfo* filter);
FResourceFile *Check7Z(const char *filename, FileReader &file, bool quiet, LumpFilterInfo* filter); FResourceFile *Check7Z(const char *filename, FileReader &file, bool quiet, LumpFilterInfo* filter);
FResourceFile* CheckSSI(const char* filename, FileReader& file, bool quiet, LumpFilterInfo* filter);
FResourceFile *CheckLump(const char *filename,FileReader &file, bool quiet, LumpFilterInfo* filter); FResourceFile *CheckLump(const char *filename,FileReader &file, bool quiet, LumpFilterInfo* filter);
FResourceFile *CheckDir(const char *filename, bool quiet, bool nosub, LumpFilterInfo* filter); FResourceFile *CheckDir(const char *filename, bool quiet, bool nosub, LumpFilterInfo* filter);
static CheckFunc funcs[] = { CheckWad, CheckZip, Check7Z, CheckPak, CheckGRP, CheckRFF, CheckLump }; static CheckFunc funcs[] = { CheckWad, CheckZip, Check7Z, CheckPak, CheckGRP, CheckRFF, CheckSSI, CheckLump };
FResourceFile *FResourceFile::DoOpenResourceFile(const char *filename, FileReader &file, bool quiet, bool containeronly, LumpFilterInfo* filter) FResourceFile *FResourceFile::DoOpenResourceFile(const char *filename, FileReader &file, bool quiet, bool containeronly, LumpFilterInfo* filter)
{ {

View file

@ -164,7 +164,7 @@ public:
const FString &GetHash() const { return Hash; } const FString &GetHash() const { return Hash; }
virtual bool Open(bool quiet, LumpFilterInfo* filter) = 0; //virtual bool Open(bool quiet, LumpFilterInfo* filter) = 0;
virtual FResourceLump *GetLump(int no) = 0; virtual FResourceLump *GetLump(int no) = 0;
FResourceLump *FindLump(const char *name); FResourceLump *FindLump(const char *name);
}; };