mirror of
https://github.com/ZDoom/Raze.git
synced 2025-01-31 04:20:42 +00:00
- cleanup of savegame framework
This commit is contained in:
parent
97d8aee2e8
commit
c17ec5fa45
8 changed files with 51 additions and 337 deletions
|
@ -1060,7 +1060,6 @@ set (PCH_SOURCES
|
|||
core/initfs.cpp
|
||||
core/statistics.cpp
|
||||
core/secrets.cpp
|
||||
core/compositesavegame.cpp
|
||||
core/savegamehelp.cpp
|
||||
core/precache.cpp
|
||||
core/quotes.cpp
|
||||
|
|
|
@ -1,44 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include "files.h"
|
||||
#include "zstring.h"
|
||||
#include "tarray.h"
|
||||
#include "resourcefile.h"
|
||||
|
||||
class CompositeSavegameWriter
|
||||
{
|
||||
FString filename;
|
||||
TDeletingArray<BufferWriter*> subfiles;
|
||||
TArray<FCompressedBuffer> subbuffers;
|
||||
TArray<FString> subfilenames;
|
||||
TArray<bool> isCompressed;
|
||||
|
||||
FCompressedBuffer CompressElement(BufferWriter* element, bool compress);
|
||||
public:
|
||||
void Clear()
|
||||
{
|
||||
for (auto& b : subbuffers) b.Clean();
|
||||
isCompressed.Clear();
|
||||
subfilenames.Clear();
|
||||
subfiles.DeleteAndClear();
|
||||
subbuffers.Clear();
|
||||
filename = "";
|
||||
}
|
||||
void SetFileName(const char* fn)
|
||||
{
|
||||
filename = fn;
|
||||
}
|
||||
void SetFileName(const FString& fn)
|
||||
{
|
||||
filename = fn;
|
||||
}
|
||||
~CompositeSavegameWriter()
|
||||
{
|
||||
assert(subfiles.Size() == 0); // must be written out.
|
||||
}
|
||||
FileWriter& NewElement(const char* filename, bool compress = true);
|
||||
void AddCompressedElement(const char* filename, FCompressedBuffer& buffer);
|
||||
bool WriteToFile();
|
||||
};
|
||||
|
|
@ -1,155 +0,0 @@
|
|||
|
||||
/*
|
||||
** compositesavegame.cpp
|
||||
** Container for savegame files with multiple sub-content
|
||||
**
|
||||
**---------------------------------------------------------------------------
|
||||
** Copyright 2019 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 OFf
|
||||
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**---------------------------------------------------------------------------
|
||||
**
|
||||
*/
|
||||
|
||||
#include <zlib.h>
|
||||
#include "compositesaveame.h"
|
||||
#include "file_zip.h"
|
||||
#include "resourcefile.h"
|
||||
#include "m_png.h"
|
||||
#include "gamecontrol.h"
|
||||
|
||||
|
||||
bool WriteZip(const char *filename, TArray<FString> &filenames, TArray<FCompressedBuffer> &content);
|
||||
|
||||
|
||||
FileWriter &CompositeSavegameWriter::NewElement(const char *filename, bool compress)
|
||||
{
|
||||
FCompressedBuffer b{};
|
||||
subfilenames.Push(filename);
|
||||
subbuffers.Push(b);
|
||||
isCompressed.Push(compress);
|
||||
auto bwr = new BufferWriter;
|
||||
subfiles.Push(bwr);
|
||||
return *bwr;
|
||||
}
|
||||
|
||||
void CompositeSavegameWriter::AddCompressedElement(const char* filename, FCompressedBuffer& buffer)
|
||||
{
|
||||
subfilenames.Push(filename);
|
||||
subbuffers.Push(buffer);
|
||||
buffer = {};
|
||||
subfiles.Push(nullptr);
|
||||
isCompressed.Push(true);
|
||||
}
|
||||
|
||||
FCompressedBuffer CompositeSavegameWriter::CompressElement(BufferWriter *bw, bool compress)
|
||||
{
|
||||
FCompressedBuffer buff;
|
||||
|
||||
auto buffer =bw->GetBuffer();
|
||||
buff.mSize = buffer->Size();
|
||||
buff.mZipFlags = 0;
|
||||
buff.mCRC32 = crc32(0, (const Bytef*)buffer->Data(), buffer->Size());
|
||||
|
||||
uint8_t *compressbuf = new uint8_t[buff.mSize+1];
|
||||
|
||||
z_stream stream;
|
||||
int err;
|
||||
|
||||
stream.next_in = (Bytef *)buffer->Data();
|
||||
stream.avail_in = buff.mSize;
|
||||
stream.next_out = (Bytef*)compressbuf;
|
||||
stream.avail_out = buff.mSize;
|
||||
stream.zalloc = (alloc_func)0;
|
||||
stream.zfree = (free_func)0;
|
||||
stream.opaque = (voidpf)0;
|
||||
|
||||
if (!compress) goto error;
|
||||
|
||||
// create output in zip-compatible form as required by FCompressedBuffer
|
||||
err = deflateInit2(&stream, 8, Z_DEFLATED, -15, 9, Z_DEFAULT_STRATEGY);
|
||||
if (err != Z_OK)
|
||||
{
|
||||
goto error;
|
||||
}
|
||||
|
||||
err = deflate(&stream, Z_FINISH);
|
||||
if (err != Z_STREAM_END)
|
||||
{
|
||||
deflateEnd(&stream);
|
||||
goto error;
|
||||
}
|
||||
buff.mCompressedSize = stream.total_out;
|
||||
|
||||
err = deflateEnd(&stream);
|
||||
if (err == Z_OK)
|
||||
{
|
||||
buff.mBuffer = new char[buff.mCompressedSize];
|
||||
buff.mMethod = METHOD_DEFLATE;
|
||||
memcpy(buff.mBuffer, compressbuf, buff.mCompressedSize);
|
||||
delete[] compressbuf;
|
||||
return buff;
|
||||
}
|
||||
|
||||
error:
|
||||
if (buff.mSize) memcpy(compressbuf, buffer->Data(), buff.mSize + 1);
|
||||
buff.mBuffer = (char*)compressbuf;
|
||||
buff.mCompressedSize = buff.mSize;
|
||||
buff.mMethod = METHOD_STORED;
|
||||
return buff;
|
||||
|
||||
}
|
||||
|
||||
bool CompositeSavegameWriter::WriteToFile()
|
||||
{
|
||||
if (subfiles.Size() == 0) return false;
|
||||
TArray<FCompressedBuffer> compressed(subfiles.Size(), 1);
|
||||
for (unsigned i = 0; i < subfiles.Size(); i++)
|
||||
{
|
||||
if (subfiles[i])
|
||||
compressed[i] = CompressElement(subfiles[i], isCompressed[i]);
|
||||
else
|
||||
{
|
||||
compressed[i] = subbuffers[i];
|
||||
subbuffers[i] = {};
|
||||
}
|
||||
}
|
||||
|
||||
if (WriteZip(filename, subfilenames, compressed))
|
||||
{
|
||||
// Check whether the file is ok by trying to open it.
|
||||
//FResourceFile *test = FResourceFile::OpenResourceFile(filename, true);
|
||||
//if (test != nullptr)
|
||||
{
|
||||
Clear();
|
||||
//delete test;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Clear();
|
||||
return false;
|
||||
}
|
||||
|
|
@ -33,7 +33,6 @@
|
|||
**
|
||||
*/
|
||||
|
||||
#include "compositesaveame.h"
|
||||
#include "savegamehelp.h"
|
||||
#include "gstrings.h"
|
||||
#include "i_specialpaths.h"
|
||||
|
@ -56,14 +55,14 @@
|
|||
#include "gamestate.h"
|
||||
#include "razemenu.h"
|
||||
#include "interpolate.h"
|
||||
#include <zlib.h>
|
||||
|
||||
|
||||
sectortype sectorbackup[MAXSECTORS];
|
||||
walltype wallbackup[MAXWALLS];
|
||||
|
||||
static CompositeSavegameWriter savewriter;
|
||||
static FResourceFile *savereader;
|
||||
void WriteSavePic(FileWriter* file, int width, int height);
|
||||
bool WriteZip(const char* filename, TArray<FString>& filenames, TArray<FCompressedBuffer>& content);
|
||||
extern FString BackupSaveGame;
|
||||
void SerializeMap(FSerializer &arc);
|
||||
FixedBitArray<MAXSPRITES> activeSprites;
|
||||
|
@ -111,23 +110,21 @@ static void SerializeSession(FSerializer& arc)
|
|||
//
|
||||
//=============================================================================
|
||||
|
||||
bool ReadSavegame(const char *name)
|
||||
bool ReadSavegame(const char* name)
|
||||
{
|
||||
if (savereader) delete savereader;
|
||||
savereader = FResourceFile::OpenResourceFile(name, true, true);
|
||||
auto savereader = FResourceFile::OpenResourceFile(name, true, true);
|
||||
|
||||
if (savereader != nullptr)
|
||||
{
|
||||
auto file = ReadSavegameChunk("info.json");
|
||||
if (!file.isOpen())
|
||||
auto lump = savereader->FindLump("info.json");
|
||||
if (!lump)
|
||||
{
|
||||
FinishSavegameRead();
|
||||
delete savereader;
|
||||
return false;
|
||||
}
|
||||
auto file = lump->NewReader();
|
||||
if (G_ValidateSavegame(file, nullptr, false) <= 0)
|
||||
{
|
||||
FinishSavegameRead();
|
||||
delete savereader;
|
||||
return false;
|
||||
}
|
||||
|
@ -135,6 +132,7 @@ bool ReadSavegame(const char *name)
|
|||
FResourceLump* info = savereader->FindLump("session.json");
|
||||
if (info == nullptr)
|
||||
{
|
||||
delete savereader;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -142,40 +140,19 @@ bool ReadSavegame(const char *name)
|
|||
FSerializer arc;
|
||||
if (!arc.OpenReader((const char*)data, info->LumpSize))
|
||||
{
|
||||
delete savereader;
|
||||
info->Unlock();
|
||||
return false;
|
||||
}
|
||||
info->Unlock();
|
||||
|
||||
// Load system-side data from savegames.
|
||||
// Load the savegame.
|
||||
loadMapBackup(currentLevel->fileName);
|
||||
SerializeSession(arc);
|
||||
delete savereader;
|
||||
return true;
|
||||
}
|
||||
return savereader != nullptr;
|
||||
}
|
||||
|
||||
FileWriter *WriteSavegameChunk(const char *name)
|
||||
{
|
||||
return &savewriter.NewElement(name);
|
||||
}
|
||||
|
||||
void AddCompressedSavegameChunk(const char* name, FCompressedBuffer& buffer)
|
||||
{
|
||||
savewriter.AddCompressedElement(name, buffer);
|
||||
}
|
||||
|
||||
FileReader ReadSavegameChunk(const char *name)
|
||||
{
|
||||
if (!savereader) return FileReader();
|
||||
auto lump = savereader->FindLump(name);
|
||||
if (!lump) return FileReader();
|
||||
return lump->NewReader();
|
||||
}
|
||||
|
||||
void FinishSavegameRead()
|
||||
{
|
||||
delete savereader;
|
||||
savereader = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
CVAR(Bool, save_formatted, false, 0) // should be set to false once the conversion is done
|
||||
|
@ -188,15 +165,9 @@ CVAR(Bool, save_formatted, false, 0) // should be set to false once the conversi
|
|||
|
||||
bool WriteSavegame(const char* filename, const char *name)
|
||||
{
|
||||
savewriter.Clear();
|
||||
savewriter.SetFileName(filename);
|
||||
|
||||
BufferWriter savepic;
|
||||
FSerializer savegameinfo; // this is for displayable info about the savegame.
|
||||
FSerializer savegamesession; // saved game session settings.
|
||||
FSerializer savegameengine; // saved play state.
|
||||
|
||||
savegameinfo.OpenWriter(true);
|
||||
savegameengine.OpenWriter(save_formatted);
|
||||
|
||||
char buf[100];
|
||||
mysnprintf(buf, countof(buf), GAMENAME " %s", GetVersionString());
|
||||
|
@ -205,6 +176,7 @@ bool WriteSavegame(const char* filename, const char *name)
|
|||
FStringf timeStr("%02d:%02d", gs.timesecnd / 60, gs.timesecnd % 60);
|
||||
auto lev = currentLevel;
|
||||
|
||||
savegameinfo.OpenWriter(true);
|
||||
savegameinfo.AddString("Software", buf)
|
||||
("Save Version", savesig.currentsavever)
|
||||
.AddString("Engine", savesig.savesig)
|
||||
|
@ -226,30 +198,48 @@ bool WriteSavegame(const char* filename, const char *name)
|
|||
if (mapcname) savegameinfo.AddString("Map Resource", mapcname);
|
||||
else
|
||||
{
|
||||
savewriter.Clear();
|
||||
return false; // this should never happen. Saving on a map that isn't present is impossible.
|
||||
}
|
||||
}
|
||||
|
||||
auto buff = savegameinfo.GetCompressedOutput();
|
||||
AddCompressedSavegameChunk("info.json", buff);
|
||||
|
||||
|
||||
// Handle system-side modules that need to persist data in savegames here, in a central place.
|
||||
// Save the game state
|
||||
savegamesession.OpenWriter(save_formatted);
|
||||
SerializeSession(savegamesession);
|
||||
buff = savegamesession.GetCompressedOutput();
|
||||
AddCompressedSavegameChunk("session.json", buff);
|
||||
|
||||
auto picfile = WriteSavegameChunk("savepic.png");
|
||||
WriteSavePic(picfile, 240, 180);
|
||||
WriteSavePic(&savepic, 240, 180);
|
||||
mysnprintf(buf, countof(buf), GAMENAME " %s", GetVersionString());
|
||||
// put some basic info into the PNG so that this isn't lost when the image gets extracted.
|
||||
M_AppendPNGText(picfile, "Software", buf);
|
||||
M_AppendPNGText(picfile, "Title", name);
|
||||
M_AppendPNGText(picfile, "Current Map", lev->labelName);
|
||||
M_FinishPNG(picfile);
|
||||
return savewriter.WriteToFile();
|
||||
M_AppendPNGText(&savepic, "Software", buf);
|
||||
M_AppendPNGText(&savepic, "Title", name);
|
||||
M_AppendPNGText(&savepic, "Current Map", lev->labelName);
|
||||
M_FinishPNG(&savepic);
|
||||
|
||||
auto picdata = savepic.GetBuffer();
|
||||
FCompressedBuffer bufpng = { picdata->Size(), picdata->Size(), METHOD_STORED, 0, static_cast<unsigned int>(crc32(0, &(*picdata)[0], picdata->Size())), (char*)&(*picdata)[0] };
|
||||
|
||||
TArray<FCompressedBuffer> savegame_content;
|
||||
TArray<FString> savegame_filenames;
|
||||
|
||||
savegame_content.Push(bufpng);
|
||||
savegame_filenames.Push("savepic.png");
|
||||
savegame_content.Push(savegameinfo.GetCompressedOutput());
|
||||
savegame_filenames.Push("info.json");
|
||||
savegame_content.Push(savegamesession.GetCompressedOutput());
|
||||
savegame_filenames.Push("session.json");
|
||||
|
||||
if (WriteZip(filename, savegame_filenames, savegame_content))
|
||||
{
|
||||
// Check whether the file is ok by trying to open it.
|
||||
FResourceFile* test = FResourceFile::OpenResourceFile(filename, true);
|
||||
if (test != nullptr)
|
||||
{
|
||||
delete test;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//=============================================================================
|
||||
|
@ -430,23 +420,6 @@ FString G_BuildSaveName (const char *prefix)
|
|||
#include "build.h"
|
||||
#include "mmulti.h"
|
||||
|
||||
static const int magic = 0xbeefcafe;
|
||||
void WriteMagic(FileWriter *fw)
|
||||
{
|
||||
fw->Write(&magic, 4);
|
||||
}
|
||||
|
||||
void CheckMagic(FileReader& fr)
|
||||
{
|
||||
int m = 0;
|
||||
fr.Read(&m, 4);
|
||||
assert(m == magic);
|
||||
#ifndef _DEBUG
|
||||
if (m != magic) I_Error("Savegame corrupt");
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
#define V(x) x
|
||||
static spritetype zsp;
|
||||
static spriteext_t zspx;
|
||||
|
|
|
@ -5,11 +5,6 @@
|
|||
|
||||
extern FixedBitArray<MAXSPRITES> activeSprites;
|
||||
|
||||
FileWriter *WriteSavegameChunk(const char *name);
|
||||
FileReader ReadSavegameChunk(const char *name);
|
||||
|
||||
void FinishSavegameRead();
|
||||
|
||||
// Savegame utilities
|
||||
class FileReader;
|
||||
|
||||
|
|
|
@ -27,8 +27,6 @@ Prepared for public release: 03/28/2005 - Charlie Wiederhold, 3D Realms
|
|||
#ifndef INTERPSO_H
|
||||
#define INTERPSO_H
|
||||
|
||||
#include "mfile.h"
|
||||
|
||||
BEGIN_SW_NS
|
||||
|
||||
extern int32_t so_numinterpolations;
|
||||
|
|
|
@ -1,52 +0,0 @@
|
|||
//-------------------------------------------------------------------------
|
||||
/*
|
||||
Copyright (C) 1997, 2005 - 3D Realms Entertainment
|
||||
|
||||
This file is part of Shadow Warrior version 1.2
|
||||
|
||||
Shadow Warrior is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
|
||||
See the GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
Original Source: 1997 - Frank Maddin and Jim Norwood
|
||||
Prepared for public release: 03/28/2005 - Charlie Wiederhold, 3D Realms
|
||||
*/
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
#include "compat.h"
|
||||
|
||||
#include "savegamehelp.h"
|
||||
|
||||
BEGIN_SW_NS
|
||||
|
||||
typedef FileWriter* MFILE_WRITE;
|
||||
typedef FileReader* MFILE_READ;
|
||||
|
||||
inline size_t MREAD(void* buf, size_t size, size_t nelem, FileReader* handle)
|
||||
{
|
||||
return handle->Read(buf, size * nelem) / size;
|
||||
}
|
||||
|
||||
inline size_t MWRITE(void* buf, size_t size, size_t nelem, FileWriter* handle)
|
||||
{
|
||||
return handle->Write(buf, size * nelem) / size;
|
||||
}
|
||||
|
||||
inline void MCLOSE_READ(FileReader* handle)
|
||||
{
|
||||
handle->Close();
|
||||
FinishSavegameRead();
|
||||
}
|
||||
|
||||
END_SW_NS
|
|
@ -1210,8 +1210,8 @@ FSerializer& Serialize(FSerializer& arc, const char* keyname, TRACK& w, TRACK* d
|
|||
int size = w.NumPoints ? w.NumPoints : 1;
|
||||
w.TrackPoint = (TRACK_POINT*)CallocMem(sizeof(TRACK_POINT), size);
|
||||
}
|
||||
if (w.NumPoints > 0) arc.Array("points", w.TrackPoint, w.NumPoints)
|
||||
.EndObject();
|
||||
if (w.NumPoints > 0) arc.Array("points", w.TrackPoint, w.NumPoints);
|
||||
arc.EndObject();
|
||||
}
|
||||
return arc;
|
||||
}
|
||||
|
@ -1233,7 +1233,7 @@ void GameInterface::SerializeGameState(FSerializer& arc)
|
|||
SerializeSectUser(arc);
|
||||
so_serializeinterpolations(arc);
|
||||
arc("numplayers", numplayers)
|
||||
.Array("players", Player, numplayers)
|
||||
.Array("players", Player, numplayers)
|
||||
("skill", Skill)
|
||||
("screenpeek", screenpeek)
|
||||
("randomseed", randomseed)
|
||||
|
@ -1272,8 +1272,8 @@ void GameInterface::SerializeGameState(FSerializer& arc)
|
|||
("serpwasseen", serpwasseen)
|
||||
("sumowasseen", sumowasseen)
|
||||
("zillawasseen", zillawasseen)
|
||||
.Array("BossSpriteNum", BossSpriteNum, 3)
|
||||
.Array("tracks", Track, countof(Track))
|
||||
.Array("BossSpriteNum", BossSpriteNum, 3);
|
||||
arc.Array("tracks", Track, countof(Track))
|
||||
;
|
||||
postSerializePanelSprites(arc);
|
||||
arc.EndObject();
|
||||
|
|
Loading…
Reference in a new issue