From 8a0634ed0d09c3494b9bf2de1b27b853acee0e12 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sun, 27 Sep 2020 10:56:42 +0200 Subject: [PATCH] - file system update from Raze * support for SSI container format - this is an obscure format used by a few Duke Nukem mods - added to have this subsystem identical between both engines. * removed some dead wrapper code * made the Open methods local to their implementations, this was never called virtually. --- src/CMakeLists.txt | 1 + src/common/filesystem/file_7z.cpp | 2 +- src/common/filesystem/file_directory.cpp | 4 +- src/common/filesystem/file_grp.cpp | 2 +- src/common/filesystem/file_lump.cpp | 2 +- src/common/filesystem/file_pak.cpp | 2 +- src/common/filesystem/file_rff.cpp | 2 +- src/common/filesystem/file_ssi.cpp | 155 +++++++++++++++++++++++ src/common/filesystem/file_wad.cpp | 8 +- src/common/filesystem/file_whres.cpp | 2 +- src/common/filesystem/file_zip.cpp | 2 +- src/common/filesystem/filesystem.cpp | 71 ----------- src/common/filesystem/filesystem.h | 12 -- src/common/filesystem/resourcefile.cpp | 3 +- src/common/filesystem/resourcefile.h | 1 - 15 files changed, 171 insertions(+), 98 deletions(-) create mode 100644 src/common/filesystem/file_ssi.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 140282f071..f817054364 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1106,6 +1106,7 @@ set (PCH_SOURCES common/filesystem/file_zip.cpp common/filesystem/file_pak.cpp common/filesystem/file_whres.cpp + common/filesystem/file_ssi.cpp common/filesystem/file_directory.cpp common/filesystem/resourcefile.cpp common/engine/cycler.cpp diff --git a/src/common/filesystem/file_7z.cpp b/src/common/filesystem/file_7z.cpp index 8b44a9e6f7..8d0d64bb2b 100644 --- a/src/common/filesystem/file_7z.cpp +++ b/src/common/filesystem/file_7z.cpp @@ -367,7 +367,7 @@ FResourceFile *Check7Z(const char *filename, FileReader &file, bool quiet, LumpF file.Seek(0, FileReader::SeekSet); if (!memcmp(head, k7zSignature, k7zSignatureSize)) { - FResourceFile *rf = new F7ZFile(filename, file); + auto rf = new F7ZFile(filename, file); if (rf->Open(quiet, filter)) return rf; file = std::move(rf->Reader); // to avoid destruction of reader diff --git a/src/common/filesystem/file_directory.cpp b/src/common/filesystem/file_directory.cpp index b24d6888d3..7654764cbf 100644 --- a/src/common/filesystem/file_directory.cpp +++ b/src/common/filesystem/file_directory.cpp @@ -254,9 +254,9 @@ int FDirectoryLump::FillCache() 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; delete rf; - return NULL; + return nullptr; } diff --git a/src/common/filesystem/file_grp.cpp b/src/common/filesystem/file_grp.cpp index fbb5148c89..1bf3bd7a40 100644 --- a/src/common/filesystem/file_grp.cpp +++ b/src/common/filesystem/file_grp.cpp @@ -140,7 +140,7 @@ FResourceFile *CheckGRP(const char *filename, FileReader &file, bool quiet, Lump file.Seek(0, FileReader::SeekSet); if (!memcmp(head, "KenSilverman", 12)) { - FResourceFile *rf = new FGrpFile(filename, file); + auto rf = new FGrpFile(filename, file); if (rf->Open(quiet, filter)) return rf; file = std::move(rf->Reader); // to avoid destruction of reader diff --git a/src/common/filesystem/file_lump.cpp b/src/common/filesystem/file_lump.cpp index 60902900d8..abeb9cf71d 100644 --- a/src/common/filesystem/file_lump.cpp +++ b/src/common/filesystem/file_lump.cpp @@ -94,7 +94,7 @@ bool FLumpFile::Open(bool quiet, LumpFilterInfo*) FResourceFile *CheckLump(const char *filename, FileReader &file, bool quiet, LumpFilterInfo* filter) { // always succeeds - FResourceFile *rf = new FLumpFile(filename, file); + auto rf = new FLumpFile(filename, file); if (rf->Open(quiet, filter)) return rf; file = std::move(rf->Reader); // to avoid destruction of reader delete rf; diff --git a/src/common/filesystem/file_pak.cpp b/src/common/filesystem/file_pak.cpp index 60137f2353..5ff9ff886b 100644 --- a/src/common/filesystem/file_pak.cpp +++ b/src/common/filesystem/file_pak.cpp @@ -134,7 +134,7 @@ FResourceFile *CheckPak(const char *filename, FileReader &file, bool quiet, Lump file.Seek(0, FileReader::SeekSet); if (!memcmp(head, "PACK", 4)) { - FResourceFile *rf = new FPakFile(filename, file); + auto rf = new FPakFile(filename, file); if (rf->Open(quiet, filter)) return rf; file = std::move(rf->Reader); // to avoid destruction of reader diff --git a/src/common/filesystem/file_rff.cpp b/src/common/filesystem/file_rff.cpp index 80805d6417..c69b50246e 100644 --- a/src/common/filesystem/file_rff.cpp +++ b/src/common/filesystem/file_rff.cpp @@ -248,7 +248,7 @@ FResourceFile *CheckRFF(const char *filename, FileReader &file, bool quiet, Lump file.Seek(0, FileReader::SeekSet); 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; file = std::move(rf->Reader); // to avoid destruction of reader diff --git a/src/common/filesystem/file_ssi.cpp b/src/common/filesystem/file_ssi.cpp new file mode 100644 index 0000000000..d475037408 --- /dev/null +++ b/src/common/filesystem/file_ssi.cpp @@ -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; +} diff --git a/src/common/filesystem/file_wad.cpp b/src/common/filesystem/file_wad.cpp index 2cbb4a3dcc..054b0721a7 100644 --- a/src/common/filesystem/file_wad.cpp +++ b/src/common/filesystem/file_wad.cpp @@ -70,8 +70,8 @@ public: int GetNamespace() const override { return Namespace; } - int GetFileOffset() { return Position; } - FileReader *GetReader() + int GetFileOffset() override { return Position; } + FileReader *GetReader() override { if(!Compressed) { @@ -80,7 +80,7 @@ public: } return NULL; } - int FillCache() + int FillCache() override { if(!Compressed) { @@ -472,7 +472,7 @@ FResourceFile *CheckWad(const char *filename, FileReader &file, bool quiet, Lump file.Seek(0, FileReader::SeekSet); 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; file = std::move(rf->Reader); // to avoid destruction of reader diff --git a/src/common/filesystem/file_whres.cpp b/src/common/filesystem/file_whres.cpp index 1c2a6d79c9..1c573ad383 100644 --- a/src/common/filesystem/file_whres.cpp +++ b/src/common/filesystem/file_whres.cpp @@ -148,7 +148,7 @@ FResourceFile *CheckWHRes(const char *filename, FileReader &file, bool quiet, Lu if (offset != checkpos || length <= 0) return nullptr; checkpos += (length+4095) / 4096; } - FResourceFile *rf = new FWHResFile(filename, file); + auto rf = new FWHResFile(filename, file); if (rf->Open(quiet, filter)) return rf; file = std::move(rf->Reader); // to avoid destruction of reader delete rf; diff --git a/src/common/filesystem/file_zip.cpp b/src/common/filesystem/file_zip.cpp index ac39d0d2cd..00de98cdcb 100644 --- a/src/common/filesystem/file_zip.cpp +++ b/src/common/filesystem/file_zip.cpp @@ -464,7 +464,7 @@ FResourceFile *CheckZip(const char *filename, FileReader &file, bool quiet, Lump file.Seek(0, FileReader::SeekSet); 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; file = std::move(rf->Reader); // to avoid destruction of reader diff --git a/src/common/filesystem/filesystem.cpp b/src/common/filesystem/filesystem.cpp index 941418fcb8..c57b061130 100644 --- a/src/common/filesystem/filesystem.cpp +++ b/src/common/filesystem/filesystem.cpp @@ -1634,79 +1634,8 @@ static void PrintLastError () // //========================================================================== -FResourceLump *FileSystem::Lookup(const char *name, const char *type) -{ - FStringf fname("%s.%s", name, type); - auto lump = FindFile(fname); - if (lump >= 0) return FileInfo[lump].lump; - else return nullptr; -} - -FResourceLump *FileSystem::Lookup(unsigned int id, const char *type) -{ - auto lump = FindResource(id, type); - if (lump >= 0) return FileInfo[lump].lump; - else return nullptr; -} FResourceLump* FileSystem::GetFileAt(int no) { return FileInfo[no].lump; } -//========================================================================== -// -// Stand-ins for Blood's resource class -// -//========================================================================== - -const void *FileSystem::Lock(int lump) -{ - if ((size_t)lump >= FileInfo.Size()) return nullptr; - auto lumpp = FileInfo[lump].lump; - return lumpp->Lock(); -} - -void FileSystem::Unlock(int lump) -{ - if ((size_t)lump >= FileInfo.Size()) return; - auto lumpp = FileInfo[lump].lump; - lumpp->Unlock(); -} - -const void *FileSystem::Get(int lump) -{ - if ((size_t)lump >= FileInfo.Size()) return nullptr; - auto lumpp = FileInfo[lump].lump; - auto p = lumpp->Lock(); - lumpp->RefCount = INT_MAX/2; // lock forever. - return p; -} - -//========================================================================== -// -// Stand-ins for Blood's resource class -// -//========================================================================== - -const void *FileSystem::Lock(FResourceLump *lump) -{ - if (lump) return lump->Lock(); - else return nullptr; -} - -void FileSystem::Unlock(FResourceLump *lump) -{ - if (lump) lump->Unlock(); -} - -const void *FileSystem::Load(FResourceLump *lump) -{ - if (lump) - { - auto p = lump->Lock(); - lump->RefCount = INT_MAX/2; // lock forever. - return p; - } - else return nullptr; -} - diff --git a/src/common/filesystem/filesystem.h b/src/common/filesystem/filesystem.h index a7c38683f9..89cc288d34 100644 --- a/src/common/filesystem/filesystem.h +++ b/src/common/filesystem/filesystem.h @@ -187,20 +187,8 @@ public: int AddFromBuffer(const char* name, const char* type, char* data, int size, int id, int flags); FileReader* GetFileReader(int wadnum); // Gets a FileReader object to the entire WAD void InitHashChains(); - - // Blood stuff - FResourceLump* Lookup(const char* name, const char* type); - FResourceLump* Lookup(unsigned int id, const char* type); - FResourceLump* GetFileAt(int no); - const void* Lock(int lump); - void Unlock(int lump); - const void* Get(int lump); - static const void* Lock(FResourceLump* lump); - static void Unlock(FResourceLump* lump); - static const void* Load(FResourceLump* lump);; - protected: struct LumpRecord; diff --git a/src/common/filesystem/resourcefile.cpp b/src/common/filesystem/resourcefile.cpp index 2c68b9ac00..d718d74ec1 100644 --- a/src/common/filesystem/resourcefile.cpp +++ b/src/common/filesystem/resourcefile.cpp @@ -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 *CheckZip(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 *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) { diff --git a/src/common/filesystem/resourcefile.h b/src/common/filesystem/resourcefile.h index 4d90f3e2fd..bfd20e816e 100644 --- a/src/common/filesystem/resourcefile.h +++ b/src/common/filesystem/resourcefile.h @@ -164,7 +164,6 @@ public: const FString &GetHash() const { return Hash; } - virtual bool Open(bool quiet, LumpFilterInfo* filter) = 0; virtual FResourceLump *GetLump(int no) = 0; FResourceLump *FindLump(const char *name); };