From 5dfc396bb96d296016c81673dfc464fbd10288dc Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Thu, 22 Sep 2016 01:28:05 +0200 Subject: [PATCH] - read snapshots from zip. --- src/g_game.cpp | 7 +-- src/g_game.h | 1 - src/g_hub.h | 1 - src/g_level.cpp | 71 ++++++++++++++++-------------- src/g_level.h | 3 +- src/m_random.h | 1 - src/p_acs.h | 1 - src/p_saveg.h | 1 - src/resourcefiles/file_zip.cpp | 15 +++---- src/resourcefiles/file_zip.h | 24 +--------- src/resourcefiles/resourcefile.cpp | 18 ++++++++ src/resourcefiles/resourcefile.h | 23 ++++++++++ 12 files changed, 90 insertions(+), 76 deletions(-) diff --git a/src/g_game.cpp b/src/g_game.cpp index f646b2bd1..ee826aa68 100644 --- a/src/g_game.cpp +++ b/src/g_game.cpp @@ -1962,7 +1962,8 @@ void G_DoLoadGame () // dearchive all the modifications level.time = Scale (time[1], TICRATE, time[0]); - //G_ReadSnapshots(png); + G_ReadSnapshots(resfile); + G_ReadVisited(arc); // load a base level savegamerestore = true; // Use the player actors in the savegame @@ -2268,8 +2269,8 @@ void G_DoSaveGame (bool okForQuicksave, FString filename, const char *descriptio // delete the JSON buffers we created just above. Everything else will // either still be needed or taken care of automatically. - delete[] savegame_content[1].mBuffer; - delete[] savegame_content[2].mBuffer; + savegame_content[1].Clean(); + savegame_content[2].Clean(); // Check whether the file is ok by trying to open it. FResourceFile *test = FResourceFile::OpenResourceFile(filename, nullptr, true); diff --git a/src/g_game.h b/src/g_game.h index 0b0d094f5..0a11527fd 100644 --- a/src/g_game.h +++ b/src/g_game.h @@ -24,7 +24,6 @@ #define __G_GAME__ struct event_t; -struct PNGHandle; // diff --git a/src/g_hub.h b/src/g_hub.h index 245bb8f93..372546b83 100644 --- a/src/g_hub.h +++ b/src/g_hub.h @@ -1,7 +1,6 @@ #ifndef __G_HUB_H #define __G_HUB_H -struct PNGHandle; struct cluster_info_t; struct wbstartstruct_t; class FSerializer; diff --git a/src/g_level.cpp b/src/g_level.cpp index b5b0b6972..3cf054b67 100644 --- a/src/g_level.cpp +++ b/src/g_level.cpp @@ -1568,7 +1568,7 @@ void G_WriteSnapshots(TArray &filenames, TArray &buf { if (wadlevelinfos[i].Snapshot.mCompressedSize > 0) { - filename << wadlevelinfos[i].MapName << ".json"; + filename << wadlevelinfos[i].MapName << ".map.json"; filename.ToLower(); filenames.Push(filename); buffers.Push(wadlevelinfos[i].Snapshot); @@ -1576,7 +1576,7 @@ void G_WriteSnapshots(TArray &filenames, TArray &buf } if (TheDefaultLevelInfo.Snapshot.mCompressedSize > 0) { - filename << TheDefaultLevelInfo.MapName << ".json"; + filename << TheDefaultLevelInfo.MapName << ".mapd.json"; filename.ToLower(); filenames.Push(filename); buffers.Push(TheDefaultLevelInfo.Snapshot); @@ -1629,45 +1629,48 @@ void G_WriteVisited(FSerializer &arc) // //========================================================================== -void G_ReadSnapshots(PNGHandle *png) +void G_ReadSnapshots(FResourceFile *resf) { - DWORD chunkLen; FString MapName; level_info_t *i; G_ClearSnapshots(); - chunkLen = (DWORD)M_FindPNGChunk(png, SNAP_ID); - while (chunkLen != 0) + for (unsigned j = 0; j < resf->LumpCount(); j++) { -#if 0 - FPNGChunkArchive arc(png->File->GetFile(), SNAP_ID, chunkLen); - DWORD snapver; - - arc << snapver; - arc << MapName; - i = FindLevelInfo(MapName); - i->snapshot = new FCompressedMemFile; - i->snapshot->Serialize(arc); - chunkLen = (DWORD)M_NextPNGChunk(png, SNAP_ID); -#endif - } - - chunkLen = (DWORD)M_FindPNGChunk(png, DSNP_ID); - if (chunkLen != 0) - { -#if 0 - FPNGChunkArchive arc(png->File->GetFile(), DSNP_ID, chunkLen); - DWORD snapver; - - arc << snapver; - arc << MapName; - TheDefaultLevelInfo.snapshot = new FCompressedMemFile; - TheDefaultLevelInfo.snapshot->Serialize(arc); -#endif + FResourceLump * resl = resf->GetLump(j); + if (resl != nullptr) + { + auto ptr = strstr(resl->FullName, ".map.json"); + if (ptr != nullptr) + { + ptrdiff_t maplen = ptr - resl->FullName.GetChars(); + FString mapname(resl->FullName.GetChars(), (size_t)maplen); + i = FindLevelInfo(mapname); + if (i != nullptr) + { + i->Snapshot = resl->GetRawData(); + } + } + else + { + auto ptr = strstr(resl->FullName, ".mapd.json"); + if (ptr != nullptr) + { + ptrdiff_t maplen = ptr - resl->FullName.GetChars(); + FString mapname(resl->FullName.GetChars(), (size_t)maplen); + TheDefaultLevelInfo.Snapshot = resl->GetRawData(); + } + } + } } } +//========================================================================== +// +// +//========================================================================== + void G_ReadVisited(FSerializer &arc) { if (arc.BeginArray("visited")) @@ -1696,6 +1699,9 @@ void G_ReadVisited(FSerializer &arc) } } +//========================================================================== +// +// //========================================================================== CCMD(listsnapshots) @@ -1752,8 +1758,7 @@ void P_WriteACSDefereds (FSerializer &arc) void P_ReadACSDefereds (FSerializer &arc) { FString MapName; - size_t chunklen; - + P_RemoveDefereds (); if (arc.BeginObject("deferred")) diff --git a/src/g_level.h b/src/g_level.h index 55a6647ab..9f8c5efd5 100644 --- a/src/g_level.h +++ b/src/g_level.h @@ -532,8 +532,7 @@ void G_ClearSnapshots (void); void P_RemoveDefereds (); void G_SnapshotLevel (void); void G_UnSnapshotLevel (bool keepPlayers); -struct PNGHandle; -void G_ReadSnapshots (PNGHandle *png); +void G_ReadSnapshots (FResourceFile *); void G_WriteSnapshots (TArray &, TArray &); void G_WriteVisited(FSerializer &arc); void G_ReadVisited(FSerializer &arc); diff --git a/src/m_random.h b/src/m_random.h index d8c94cb9f..b3e0f3b28 100644 --- a/src/m_random.h +++ b/src/m_random.h @@ -39,7 +39,6 @@ #include "basictypes.h" #include "sfmt/SFMT.h" -struct PNGHandle; class FSerializer; class FRandom diff --git a/src/p_acs.h b/src/p_acs.h index 9cd209297..7e899d269 100644 --- a/src/p_acs.h +++ b/src/p_acs.h @@ -45,7 +45,6 @@ class FFont; class FileReader; struct line_t; -struct PNGHandle; enum diff --git a/src/p_saveg.h b/src/p_saveg.h index 05d7e3fd4..6c4c74f46 100644 --- a/src/p_saveg.h +++ b/src/p_saveg.h @@ -34,7 +34,6 @@ #ifndef __P_SAVEG_H__ #define __P_SAVEG_H__ -struct PNGHandle; class FSerializer; // Persistent storage/archiving. diff --git a/src/resourcefiles/file_zip.cpp b/src/resourcefiles/file_zip.cpp index e2574d045..73193062c 100644 --- a/src/resourcefiles/file_zip.cpp +++ b/src/resourcefiles/file_zip.cpp @@ -306,17 +306,12 @@ FZipFile::~FZipFile() // //========================================================================== -FCompressedBuffer FZipFile::GetRawLump(int lumpnum) +FCompressedBuffer FZipLump::GetRawData() { - if ((unsigned)lumpnum >= NumLumps) - { - return{ 0,0,0,0,0,nullptr }; - } - FZipLump *lmp = &Lumps[lumpnum]; - - FCompressedBuffer cbuf = { (unsigned)lmp->LumpSize, (unsigned)lmp->CompressedSize, lmp->Method, lmp->GPFlags, lmp->CRC32, new char[lmp->CompressedSize] }; - Reader->Seek(lmp->Position, SEEK_SET); - Reader->Read(cbuf.mBuffer, lmp->CompressedSize); + FCompressedBuffer cbuf = { (unsigned)LumpSize, (unsigned)CompressedSize, Method, GPFlags, CRC32, new char[CompressedSize] }; + if (Flags & LUMPFZIP_NEEDFILESTART) SetLumpAddress(); + Owner->Reader->Seek(Position, SEEK_SET); + Owner->Reader->Read(cbuf.mBuffer, CompressedSize); return cbuf; } diff --git a/src/resourcefiles/file_zip.h b/src/resourcefiles/file_zip.h index e802b41e8..427d9cbf3 100644 --- a/src/resourcefiles/file_zip.h +++ b/src/resourcefiles/file_zip.h @@ -3,28 +3,6 @@ #include "resourcefile.h" -// This holds a compresed Zip entry with all needed info to decompress it. -struct FCompressedBuffer -{ - unsigned mSize; - unsigned mCompressedSize; - int mMethod; - int mZipFlags; - unsigned mCRC32; - char *mBuffer; - - bool Decompress(char *destbuffer); - void Clean() - { - mSize = mCompressedSize = 0; - if (mBuffer != nullptr) - { - delete[] mBuffer; - mBuffer = nullptr; - } - } -}; - enum { LUMPFZIP_NEEDFILESTART = 128 @@ -50,6 +28,7 @@ struct FZipLump : public FResourceLump private: void SetLumpAddress(); virtual int GetFileOffset(); + FCompressedBuffer GetRawData(); }; @@ -68,7 +47,6 @@ public: virtual ~FZipFile(); bool Open(bool quiet); virtual FResourceLump *GetLump(int no) { return ((unsigned)no < NumLumps)? &Lumps[no] : NULL; } - FCompressedBuffer GetRawLump(int lumpnum); }; diff --git a/src/resourcefiles/resourcefile.cpp b/src/resourcefiles/resourcefile.cpp index 2daf4aee1..912998d9a 100644 --- a/src/resourcefiles/resourcefile.cpp +++ b/src/resourcefiles/resourcefile.cpp @@ -40,6 +40,7 @@ #include "doomerrors.h" #include "gi.h" #include "doomstat.h" +#include "w_zip.h" //========================================================================== @@ -191,6 +192,23 @@ void FResourceLump::CheckEmbedded() } +//========================================================================== +// +// this is just for completeness. For non-Zips only an uncompressed lump can +// be returned. +// +//========================================================================== + +FCompressedBuffer FResourceLump::GetRawData() +{ + FCompressedBuffer cbuf = { (unsigned)LumpSize, (unsigned)LumpSize, METHOD_STORED, 0, 0, new char[LumpSize] }; + memcpy(cbuf.mBuffer, CacheLump(), LumpSize); + cbuf.mCRC32 = crc32(0, (BYTE*)cbuf.mBuffer, LumpSize); + ReleaseCache(); + return cbuf; +} + + //========================================================================== // // Returns the owner's FileReader if it can be used to access this lump diff --git a/src/resourcefiles/resourcefile.h b/src/resourcefiles/resourcefile.h index b79c2cc59..bedcad3bb 100644 --- a/src/resourcefiles/resourcefile.h +++ b/src/resourcefiles/resourcefile.h @@ -8,6 +8,28 @@ class FResourceFile; class FTexture; +// This holds a compresed Zip entry with all needed info to decompress it. +struct FCompressedBuffer +{ + unsigned mSize; + unsigned mCompressedSize; + int mMethod; + int mZipFlags; + unsigned mCRC32; + char *mBuffer; + + bool Decompress(char *destbuffer); + void Clean() + { + mSize = mCompressedSize = 0; + if (mBuffer != nullptr) + { + delete[] mBuffer; + mBuffer = nullptr; + } + } +}; + struct FResourceLump { friend class FResourceFile; @@ -46,6 +68,7 @@ struct FResourceLump virtual int GetIndexNum() const { return 0; } void LumpNameSetup(FString iname); void CheckEmbedded(); + virtual FCompressedBuffer GetRawData(); void *CacheLump(); int ReleaseCache();