Skip to content

Commit

Permalink
- uncoupled the decompressors from ZDoom's internal error handling.
Browse files Browse the repository at this point in the history
This code made it hard to repurpose this code for other tools, so now the error handler must be passed as a callback to OpenDecompressor.
  • Loading branch information
coelckers committed Aug 20, 2019
1 parent 3cfda93 commit 0abc66d
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 68 deletions.
2 changes: 1 addition & 1 deletion src/gamedata/resourcefiles/file_wad.cpp
Expand Up @@ -83,7 +83,7 @@ class FWadFileLump : public FResourceLump
if(Compressed)
{
FileReader lzss;
if (lzss.OpenDecompressor(Owner->Reader, LumpSize, METHOD_LZSS, false))
if (lzss.OpenDecompressor(Owner->Reader, LumpSize, METHOD_LZSS, false, [](const char* err) { I_Error("%s", err); }))
{
lzss.Read(Cache, LumpSize);
}
Expand Down
2 changes: 1 addition & 1 deletion src/gamedata/resourcefiles/file_zip.cpp
Expand Up @@ -68,7 +68,7 @@ static bool UncompressZipLump(char *Cache, FileReader &Reader, int Method, int L
case METHOD_LZMA:
{
FileReader frz;
if (frz.OpenDecompressor(Reader, LumpSize, Method, false))
if (frz.OpenDecompressor(Reader, LumpSize, Method, false, [](const char* err) { I_Error("%s", err); }))
{
frz.Read(Cache, LumpSize);
}
Expand Down
1 change: 1 addition & 0 deletions src/i_net.cpp
Expand Up @@ -63,6 +63,7 @@
#include "m_misc.h"
#include "doomerrors.h"
#include "atterm.h"
#include "cmdlib.h"

#include "i_net.h"

Expand Down
29 changes: 0 additions & 29 deletions src/m_misc.cpp
Expand Up @@ -656,32 +656,3 @@ UNSAFE_CCMD (screenshot)
G_ScreenShot (argv[1]);
}

//
// M_ZlibError
//
FString M_ZLibError(int zerr)
{
if (zerr >= 0)
{
return "OK";
}
else if (zerr < -6)
{
FString out;
out.Format("%d", zerr);
return out;
}
else
{
static const char *errs[6] =
{
"Errno",
"Stream Error",
"Data Error",
"Memory Error",
"Buffer Error",
"Version Error"
};
return errs[-zerr - 1];
}
}
4 changes: 0 additions & 4 deletions src/m_misc.h
Expand Up @@ -44,10 +44,6 @@ void M_LoadDefaults ();
bool M_SaveDefaults (const char *filename);
void M_SaveCustomKeys (FConfigFile *config, char *section, char *subsection, size_t sublen);



FString M_ZLibError(int zerrnum);

// Get special directory paths (defined in m_specialpaths.cpp)

#ifdef __unix__
Expand Down
16 changes: 12 additions & 4 deletions src/maploader/maploader.cpp
Expand Up @@ -728,13 +728,21 @@ bool MapLoader::LoadExtendedNodes (FileReader &dalump, uint32_t id)
if (compressed)
{
FileReader zip;
if (zip.OpenDecompressor(dalump, -1, METHOD_ZLIB, false))
try
{
LoadZNodes(zip, type);
if (zip.OpenDecompressor(dalump, -1, METHOD_ZLIB, false, [](const char* err) { I_Error("%s", err); }))
{
LoadZNodes(zip, type);
}
else
{
Printf("Error loading nodes: Corrupt data.\n");
return false;
}
}
else
catch (const CRecoverableError& err)
{
Printf("Error loading nodes: Corrupt data.\n");
Printf("Error loading nodes: %s.\n", err.what());
return false;
}
}
Expand Down
30 changes: 30 additions & 0 deletions src/utility/cmdlib.cpp
Expand Up @@ -995,3 +995,33 @@ bool IsAbsPath(const char *name)
#endif /* _WIN32 */
return 0;
}

//
// M_ZlibError
//
FString M_ZLibError(int zerr)
{
if (zerr >= 0)
{
return "OK";
}
else if (zerr < -6)
{
FString out;
out.Format("%d", zerr);
return out;
}
else
{
static const char* errs[6] =
{
"Errno",
"Stream Error",
"Data Error",
"Memory Error",
"Buffer Error",
"Version Error"
};
return errs[-zerr - 1];
}
}
1 change: 1 addition & 0 deletions src/utility/cmdlib.h
Expand Up @@ -70,6 +70,7 @@ struct FFileList
bool ScanDirectory(TArray<FFileList> &list, const char *dirpath);
bool IsAbsPath(const char*);

FString M_ZLibError(int zerrnum);


#endif
8 changes: 7 additions & 1 deletion src/utility/files.h
Expand Up @@ -70,12 +70,18 @@ class FileReaderInterface

class DecompressorBase : public FileReaderInterface
{
std::function<void(const char*)> ErrorCallback = nullptr;
public:
// These do not work but need to be defined to satisfy the FileReaderInterface.
// They will just error out when called.
long Tell() const override;
long Seek(long offset, int origin) override;
char *Gets(char *strbuf, int len) override;
void DecompressionError(const char* error, ...) const;
void SetErrorCallback(const std::function<void(const char*)>& cb)
{
ErrorCallback = cb;
}
};

class MemoryReader : public FileReaderInterface
Expand Down Expand Up @@ -167,7 +173,7 @@ class FileReader
bool OpenMemory(const void *mem, Size length); // read directly from the buffer
bool OpenMemoryArray(const void *mem, Size length); // read from a copy of the buffer.
bool OpenMemoryArray(std::function<bool(TArray<uint8_t>&)> getter); // read contents to a buffer and return a reader to it
bool OpenDecompressor(FileReader &parent, Size length, int method, bool seekable); // creates a decompressor stream. 'seekable' uses a buffered version so that the Seek and Tell methods can be used.
bool OpenDecompressor(FileReader &parent, Size length, int method, bool seekable, const std::function<void(const char*)>& cb); // creates a decompressor stream. 'seekable' uses a buffered version so that the Seek and Tell methods can be used.

Size Tell() const
{
Expand Down

0 comments on commit 0abc66d

Please sign in to comment.