Skip to content

Commit

Permalink
- uncouple the file system from the engine's Printf function.
Browse files Browse the repository at this point in the history
This is needed to use it in non GZDoom based projects.
  • Loading branch information
coelckers committed Aug 17, 2023
1 parent 9b790d2 commit 7fee89d
Show file tree
Hide file tree
Showing 23 changed files with 270 additions and 243 deletions.
2 changes: 1 addition & 1 deletion src/common/audio/music/i_soundfont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ FileReader FSF2Reader::OpenFile(const char *name)
FZipPatReader::FZipPatReader(const char *filename)
{
mAllowAbsolutePaths = true;
resf = FResourceFile::OpenResourceFile(filename, true);
resf = FResourceFile::OpenResourceFile(filename);
}

FZipPatReader::~FZipPatReader()
Expand Down
43 changes: 19 additions & 24 deletions src/common/filesystem/file_7z.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@

#include "resourcefile.h"
#include "cmdlib.h"
#include "printf.h"



Expand Down Expand Up @@ -189,7 +188,7 @@ class F7ZFile : public FResourceFile

public:
F7ZFile(const char * filename, FileReader &filer);
bool Open(bool quiet, LumpFilterInfo* filter);
bool Open(LumpFilterInfo* filter, FileSystemMessageFunc Printf);
virtual ~F7ZFile();
virtual FResourceLump *GetLump(int no) { return ((unsigned)no < NumLumps)? &Lumps[no] : NULL; }
};
Expand All @@ -216,7 +215,7 @@ F7ZFile::F7ZFile(const char * filename, FileReader &filer)
//
//==========================================================================

bool F7ZFile::Open(bool quiet, LumpFilterInfo *filter)
bool F7ZFile::Open(LumpFilterInfo *filter, FileSystemMessageFunc Printf)
{
Archive = new C7zArchive(Reader);
int skipped = 0;
Expand All @@ -227,25 +226,21 @@ bool F7ZFile::Open(bool quiet, LumpFilterInfo *filter)
{
delete Archive;
Archive = NULL;
if (!quiet)
if (res == SZ_ERROR_UNSUPPORTED)
{
Printf("\n" TEXTCOLOR_RED "%s: ", FileName.GetChars());
if (res == SZ_ERROR_UNSUPPORTED)
{
Printf("Decoder does not support this archive\n");
}
else if (res == SZ_ERROR_MEM)
{
Printf("Cannot allocate memory\n");
}
else if (res == SZ_ERROR_CRC)
{
Printf("CRC error\n");
}
else
{
Printf("error #%d\n", res);
}
Printf(FSMessageLevel::Error, "%s: Decoder does not support this archive\n", FileName.GetChars());
}
else if (res == SZ_ERROR_MEM)
{
Printf(FSMessageLevel::Error, "Cannot allocate memory\n");
}
else if (res == SZ_ERROR_CRC)
{
Printf(FSMessageLevel::Error, "CRC error\n");
}
else
{
Printf(FSMessageLevel::Error, "error #%d\n", res);
}
return false;
}
Expand Down Expand Up @@ -308,7 +303,7 @@ bool F7ZFile::Open(bool quiet, LumpFilterInfo *filter)

if (SZ_OK != Archive->Extract(Lumps[0].Position, &temp[0]))
{
if (!quiet) Printf("\n%s: unsupported 7z/LZMA file!\n", FileName.GetChars());
Printf(FSMessageLevel::Error, "%s: unsupported 7z/LZMA file!\n", FileName.GetChars());
return false;
}
}
Expand Down Expand Up @@ -356,7 +351,7 @@ int F7ZLump::FillCache()
//
//==========================================================================

FResourceFile *Check7Z(const char *filename, FileReader &file, bool quiet, LumpFilterInfo* filter)
FResourceFile *Check7Z(const char *filename, FileReader &file, LumpFilterInfo* filter, FileSystemMessageFunc Printf)
{
char head[k7zSignatureSize];

Expand All @@ -368,7 +363,7 @@ FResourceFile *Check7Z(const char *filename, FileReader &file, bool quiet, LumpF
if (!memcmp(head, k7zSignature, k7zSignatureSize))
{
auto rf = new F7ZFile(filename, file);
if (rf->Open(quiet, filter)) return rf;
if (rf->Open(filter, Printf)) return rf;

file = std::move(rf->Reader); // to avoid destruction of reader
delete rf;
Expand Down
19 changes: 9 additions & 10 deletions src/common/filesystem/file_directory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

#include "resourcefile.h"
#include "cmdlib.h"
#include "printf.h"
#include "findfile.h"

//==========================================================================
Expand Down Expand Up @@ -67,12 +66,12 @@ class FDirectory : public FResourceFile
TArray<FDirectoryLump> Lumps;
const bool nosubdir;

int AddDirectory(const char *dirpath);
int AddDirectory(const char* dirpath, FileSystemMessageFunc Printf);
void AddEntry(const char *fullpath, int size);

public:
FDirectory(const char * dirname, bool nosubdirflag = false);
bool Open(bool quiet, LumpFilterInfo* filter);
bool Open(LumpFilterInfo* filter, FileSystemMessageFunc Printf);
virtual FResourceLump *GetLump(int no) { return ((unsigned)no < NumLumps)? &Lumps[no] : NULL; }
};

Expand Down Expand Up @@ -110,7 +109,7 @@ FDirectory::FDirectory(const char * directory, bool nosubdirflag)
//
//==========================================================================

int FDirectory::AddDirectory(const char *dirpath)
int FDirectory::AddDirectory(const char *dirpath, FileSystemMessageFunc Printf)
{
void * handle;
int count = 0;
Expand All @@ -122,7 +121,7 @@ int FDirectory::AddDirectory(const char *dirpath)
handle = I_FindFirst(dirmatch.GetChars(), &find);
if (handle == ((void *)(-1)))
{
Printf("Could not scan '%s': %s\n", dirpath, strerror(errno));
Printf(FSMessageLevel::Error, "Could not scan '%s': %s\n", dirpath, strerror(errno));
}
else
{
Expand All @@ -148,7 +147,7 @@ int FDirectory::AddDirectory(const char *dirpath)
}
FString newdir = dirpath;
newdir << fi << '/';
count += AddDirectory(newdir);
count += AddDirectory(newdir, Printf);
}
else
{
Expand Down Expand Up @@ -193,9 +192,9 @@ int FDirectory::AddDirectory(const char *dirpath)
//
//==========================================================================

bool FDirectory::Open(bool quiet, LumpFilterInfo* filter)
bool FDirectory::Open(LumpFilterInfo* filter, FileSystemMessageFunc Printf)
{
NumLumps = AddDirectory(FileName);
NumLumps = AddDirectory(FileName, Printf);
PostProcessArchive(&Lumps[0], sizeof(FDirectoryLump), filter);
return true;
}
Expand Down Expand Up @@ -265,10 +264,10 @@ int FDirectoryLump::FillCache()
//
//==========================================================================

FResourceFile *CheckDir(const char *filename, bool quiet, bool nosubdirflag, LumpFilterInfo* filter)
FResourceFile *CheckDir(const char *filename, bool nosubdirflag, LumpFilterInfo* filter, FileSystemMessageFunc Printf)
{
auto rf = new FDirectory(filename, nosubdirflag);
if (rf->Open(quiet, filter)) return rf;
if (rf->Open(filter, Printf)) return rf;
delete rf;
return nullptr;
}
Expand Down
9 changes: 4 additions & 5 deletions src/common/filesystem/file_grp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
*/

#include "resourcefile.h"
#include "printf.h"

//==========================================================================
//
Expand Down Expand Up @@ -72,7 +71,7 @@ class FGrpFile : public FUncompressedFile
{
public:
FGrpFile(const char * filename, FileReader &file);
bool Open(bool quiet, LumpFilterInfo* filter);
bool Open(LumpFilterInfo* filter);
};


Expand All @@ -93,7 +92,7 @@ FGrpFile::FGrpFile(const char *filename, FileReader &file)
//
//==========================================================================

bool FGrpFile::Open(bool quiet, LumpFilterInfo*)
bool FGrpFile::Open(LumpFilterInfo* filter)
{
GrpHeader header;

Expand Down Expand Up @@ -129,7 +128,7 @@ bool FGrpFile::Open(bool quiet, LumpFilterInfo*)
//
//==========================================================================

FResourceFile *CheckGRP(const char *filename, FileReader &file, bool quiet, LumpFilterInfo* filter)
FResourceFile *CheckGRP(const char *filename, FileReader &file, LumpFilterInfo* filter, FileSystemMessageFunc Printf)
{
char head[12];

Expand All @@ -141,7 +140,7 @@ FResourceFile *CheckGRP(const char *filename, FileReader &file, bool quiet, Lump
if (!memcmp(head, "KenSilverman", 12))
{
auto rf = new FGrpFile(filename, file);
if (rf->Open(quiet, filter)) return rf;
if (rf->Open(filter)) return rf;

file = std::move(rf->Reader); // to avoid destruction of reader
delete rf;
Expand Down
13 changes: 4 additions & 9 deletions src/common/filesystem/file_lump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

#include "resourcefile.h"
#include "cmdlib.h"
#include "printf.h"

//==========================================================================
//
Expand All @@ -46,7 +45,7 @@ class FLumpFile : public FUncompressedFile
{
public:
FLumpFile(const char * filename, FileReader &file);
bool Open(bool quiet, LumpFilterInfo* filter);
bool Open(LumpFilterInfo* filter);
};


Expand All @@ -67,7 +66,7 @@ FLumpFile::FLumpFile(const char *filename, FileReader &file)
//
//==========================================================================

bool FLumpFile::Open(bool quiet, LumpFilterInfo*)
bool FLumpFile::Open(LumpFilterInfo*)
{
FString name(ExtractFileBase(FileName, true));

Expand All @@ -78,10 +77,6 @@ bool FLumpFile::Open(bool quiet, LumpFilterInfo*)
Lumps[0].LumpSize = (int)Reader.GetLength();
Lumps[0].Flags = 0;
NumLumps = 1;
if (!quiet)
{
Printf("\n");
}
return true;
}

Expand All @@ -91,11 +86,11 @@ bool FLumpFile::Open(bool quiet, LumpFilterInfo*)
//
//==========================================================================

FResourceFile *CheckLump(const char *filename, FileReader &file, bool quiet, LumpFilterInfo* filter)
FResourceFile *CheckLump(const char *filename, FileReader &file, LumpFilterInfo* filter, FileSystemMessageFunc Printf)
{
// always succeeds
auto rf = new FLumpFile(filename, file);
if (rf->Open(quiet, filter)) return rf;
if (rf->Open(filter)) return rf;
file = std::move(rf->Reader); // to avoid destruction of reader
delete rf;
return NULL;
Expand Down
9 changes: 4 additions & 5 deletions src/common/filesystem/file_pak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
*/

#include "resourcefile.h"
#include "printf.h"

//==========================================================================
//
Expand Down Expand Up @@ -65,7 +64,7 @@ class FPakFile : public FUncompressedFile
{
public:
FPakFile(const char * filename, FileReader &file);
bool Open(bool quiet, LumpFilterInfo* filter);
bool Open(LumpFilterInfo* filter);
};


Expand All @@ -88,7 +87,7 @@ FPakFile::FPakFile(const char *filename, FileReader &file)
//
//==========================================================================

bool FPakFile::Open(bool quiet, LumpFilterInfo* filter)
bool FPakFile::Open(LumpFilterInfo* filter)
{
dpackheader_t header;

Expand Down Expand Up @@ -123,7 +122,7 @@ bool FPakFile::Open(bool quiet, LumpFilterInfo* filter)
//
//==========================================================================

FResourceFile *CheckPak(const char *filename, FileReader &file, bool quiet, LumpFilterInfo* filter)
FResourceFile *CheckPak(const char *filename, FileReader &file, LumpFilterInfo* filter, FileSystemMessageFunc Printf)
{
char head[4];

Expand All @@ -135,7 +134,7 @@ FResourceFile *CheckPak(const char *filename, FileReader &file, bool quiet, Lump
if (!memcmp(head, "PACK", 4))
{
auto rf = new FPakFile(filename, file);
if (rf->Open(quiet, filter)) return rf;
if (rf->Open(filter)) return rf;

file = std::move(rf->Reader); // to avoid destruction of reader
delete rf;
Expand Down
10 changes: 4 additions & 6 deletions src/common/filesystem/file_rff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@

#include "resourcefile.h"

#include "printf.h"

//==========================================================================
//
//
Expand Down Expand Up @@ -111,7 +109,7 @@ class FRFFFile : public FResourceFile
public:
FRFFFile(const char * filename, FileReader &file);
virtual ~FRFFFile();
virtual bool Open(bool quiet, LumpFilterInfo* filter);
virtual bool Open(LumpFilterInfo* filter);
virtual FResourceLump *GetLump(int no) { return ((unsigned)no < NumLumps)? &Lumps[no] : NULL; }
};

Expand All @@ -134,7 +132,7 @@ FRFFFile::FRFFFile(const char *filename, FileReader &file)
//
//==========================================================================

bool FRFFFile::Open(bool quiet, LumpFilterInfo*)
bool FRFFFile::Open(LumpFilterInfo*)
{
RFFLump *lumps;
RFFInfo header;
Expand Down Expand Up @@ -237,7 +235,7 @@ int FRFFLump::FillCache()
//
//==========================================================================

FResourceFile *CheckRFF(const char *filename, FileReader &file, bool quiet, LumpFilterInfo* filter)
FResourceFile *CheckRFF(const char *filename, FileReader &file, LumpFilterInfo* filter, FileSystemMessageFunc Printf)
{
char head[4];

Expand All @@ -249,7 +247,7 @@ FResourceFile *CheckRFF(const char *filename, FileReader &file, bool quiet, Lump
if (!memcmp(head, "RFF\x1a", 4))
{
auto rf = new FRFFFile(filename, file);
if (rf->Open(quiet, filter)) return rf;
if (rf->Open(filter)) return rf;

file = std::move(rf->Reader); // to avoid destruction of reader
delete rf;
Expand Down
Loading

0 comments on commit 7fee89d

Please sign in to comment.