2019-11-14 20:07:43 +00:00
|
|
|
/*
|
|
|
|
** savegame.cpp
|
2019-11-26 23:41:26 +00:00
|
|
|
**
|
|
|
|
** common savegame utilities for all front ends.
|
2019-11-14 20:07:43 +00:00
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
** 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.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
2019-11-26 23:41:26 +00:00
|
|
|
*/
|
2019-11-14 20:07:43 +00:00
|
|
|
|
|
|
|
#include "compositesaveame.h"
|
|
|
|
#include "savegamehelp.h"
|
2019-11-26 23:41:26 +00:00
|
|
|
#include "gstrings.h"
|
|
|
|
#include "i_specialpaths.h"
|
|
|
|
#include "cmdlib.h"
|
2020-04-11 21:54:33 +00:00
|
|
|
#include "filesystem.h"
|
2019-11-26 23:41:26 +00:00
|
|
|
#include "statistics.h"
|
|
|
|
#include "secrets.h"
|
2019-12-03 23:28:28 +00:00
|
|
|
#include "quotemgr.h"
|
2019-12-10 21:22:59 +00:00
|
|
|
#include "mapinfo.h"
|
2020-01-12 22:16:21 +00:00
|
|
|
#include "v_video.h"
|
2020-02-05 11:56:54 +00:00
|
|
|
#include "gamecontrol.h"
|
|
|
|
#include "m_argv.h"
|
2020-02-23 08:41:12 +00:00
|
|
|
#include "serializer.h"
|
|
|
|
#include "version.h"
|
2020-04-12 06:07:48 +00:00
|
|
|
#include "raze_music.h"
|
2020-04-12 06:09:38 +00:00
|
|
|
#include "raze_sound.h"
|
2020-06-20 16:01:02 +00:00
|
|
|
#include "gamestruct.h"
|
2020-09-06 11:39:57 +00:00
|
|
|
#include "automap.h"
|
2020-09-08 16:28:41 +00:00
|
|
|
#include "statusbar.h"
|
2020-10-04 16:31:48 +00:00
|
|
|
#include "gamestate.h"
|
|
|
|
#include "razemenu.h"
|
2020-11-25 22:22:13 +00:00
|
|
|
#include "interpolate.h"
|
2019-11-14 20:07:43 +00:00
|
|
|
|
2020-10-11 18:57:20 +00:00
|
|
|
|
|
|
|
sectortype sectorbackup[MAXSECTORS];
|
|
|
|
walltype wallbackup[MAXWALLS];
|
|
|
|
|
2019-11-14 20:07:43 +00:00
|
|
|
static CompositeSavegameWriter savewriter;
|
|
|
|
static FResourceFile *savereader;
|
2020-04-26 21:17:54 +00:00
|
|
|
void WriteSavePic(FileWriter* file, int width, int height);
|
2020-10-07 16:32:57 +00:00
|
|
|
extern FString BackupSaveGame;
|
2020-10-11 16:10:52 +00:00
|
|
|
void SerializeMap(FSerializer &arc);
|
2020-10-13 17:43:45 +00:00
|
|
|
FixedBitArray<MAXSPRITES> activeSprites;
|
2019-11-14 20:07:43 +00:00
|
|
|
|
2020-02-05 11:56:54 +00:00
|
|
|
CVAR(String, cl_savedir, "", CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
|
2020-02-02 15:06:13 +00:00
|
|
|
|
2020-02-23 13:03:03 +00:00
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
2020-11-07 20:30:48 +00:00
|
|
|
static void SerializeGlobals(FSerializer& arc)
|
|
|
|
{
|
|
|
|
if (arc.BeginObject("globals"))
|
|
|
|
{
|
|
|
|
arc("crouch_toggle", crouch_toggle)
|
|
|
|
.EndObject();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
2020-02-23 13:03:03 +00:00
|
|
|
static void SerializeSession(FSerializer& arc)
|
|
|
|
{
|
2020-10-11 16:10:52 +00:00
|
|
|
SerializeMap(arc);
|
2020-02-23 13:03:03 +00:00
|
|
|
SerializeStatistics(arc);
|
|
|
|
SECRET_Serialize(arc);
|
|
|
|
Mus_Serialize(arc);
|
|
|
|
quoteMgr.Serialize(arc);
|
2020-02-29 11:33:35 +00:00
|
|
|
S_SerializeSounds(arc);
|
2020-09-06 11:39:57 +00:00
|
|
|
SerializeAutomap(arc);
|
2020-09-08 16:28:41 +00:00
|
|
|
SerializeHud(arc);
|
2020-11-07 20:30:48 +00:00
|
|
|
SerializeGlobals(arc);
|
2020-11-21 14:09:38 +00:00
|
|
|
gi->SerializeGameState(arc);
|
2020-02-23 13:03:03 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 23:41:26 +00:00
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
// This is for keeping my sanity while working with the horrible mess
|
|
|
|
// that is the savegame code in Duke Nukem.
|
|
|
|
// Without handling this in global variables it is a losing proposition
|
|
|
|
// to save custom data along with the regular snapshot. :(
|
|
|
|
// With this the savegame code can mostly pretend to load from and write
|
|
|
|
// to files while really using a composite archive.
|
|
|
|
//
|
|
|
|
// All global non-game dependent state is also saved right here for convenience.
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
|
2019-11-14 20:07:43 +00:00
|
|
|
bool OpenSaveGameForRead(const char *name)
|
|
|
|
{
|
|
|
|
if (savereader) delete savereader;
|
|
|
|
savereader = FResourceFile::OpenResourceFile(name, true, true);
|
2019-11-26 23:41:26 +00:00
|
|
|
|
|
|
|
if (savereader != nullptr)
|
|
|
|
{
|
2020-07-20 22:07:02 +00:00
|
|
|
auto file = ReadSavegameChunk("info.json");
|
|
|
|
if (!file.isOpen())
|
|
|
|
{
|
|
|
|
FinishSavegameRead();
|
|
|
|
delete savereader;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (G_ValidateSavegame(file, nullptr, false) <= 0)
|
|
|
|
{
|
|
|
|
FinishSavegameRead();
|
|
|
|
delete savereader;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-23 13:03:03 +00:00
|
|
|
FResourceLump* info = savereader->FindLump("session.json");
|
|
|
|
if (info == nullptr)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-11 21:54:33 +00:00
|
|
|
void* data = info->Lock();
|
2020-02-23 13:03:03 +00:00
|
|
|
FSerializer arc;
|
|
|
|
if (!arc.OpenReader((const char*)data, info->LumpSize))
|
|
|
|
{
|
2020-04-11 21:54:33 +00:00
|
|
|
info->Unlock();
|
2020-02-23 13:03:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-11 21:54:33 +00:00
|
|
|
info->Unlock();
|
2020-02-23 08:41:12 +00:00
|
|
|
|
2019-11-28 02:18:58 +00:00
|
|
|
// Load system-side data from savegames.
|
2020-10-11 18:57:20 +00:00
|
|
|
loadMapBackup(currentLevel->fileName);
|
2020-10-13 17:43:45 +00:00
|
|
|
SerializeSession(arc);
|
2019-12-10 21:22:59 +00:00
|
|
|
}
|
2019-11-14 20:07:43 +00:00
|
|
|
return savereader != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileWriter *WriteSavegameChunk(const char *name)
|
|
|
|
{
|
|
|
|
return &savewriter.NewElement(name);
|
|
|
|
}
|
|
|
|
|
2020-02-23 08:41:12 +00:00
|
|
|
void AddCompressedSavegameChunk(const char* name, FCompressedBuffer& buffer)
|
|
|
|
{
|
|
|
|
savewriter.AddCompressedElement(name, buffer);
|
|
|
|
}
|
|
|
|
|
2019-11-14 20:07:43 +00:00
|
|
|
FileReader ReadSavegameChunk(const char *name)
|
|
|
|
{
|
|
|
|
if (!savereader) return FileReader();
|
|
|
|
auto lump = savereader->FindLump(name);
|
|
|
|
if (!lump) return FileReader();
|
|
|
|
return lump->NewReader();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FinishSavegameWrite()
|
|
|
|
{
|
|
|
|
return savewriter.WriteToFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FinishSavegameRead()
|
|
|
|
{
|
|
|
|
delete savereader;
|
|
|
|
savereader = nullptr;
|
|
|
|
}
|
2019-11-26 23:41:26 +00:00
|
|
|
|
2020-07-21 19:32:38 +00:00
|
|
|
CVAR(Bool, save_formatted, false, 0) // should be set to false once the conversion is done
|
2020-02-23 08:41:12 +00:00
|
|
|
|
2019-11-26 23:41:26 +00:00
|
|
|
//=============================================================================
|
|
|
|
//
|
2020-01-12 22:16:21 +00:00
|
|
|
// Creates the savegame and writes all cross-game content.
|
2019-11-26 23:41:26 +00:00
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
2020-01-12 22:16:21 +00:00
|
|
|
bool OpenSaveGameForWrite(const char* filename, const char *name)
|
2019-11-26 23:41:26 +00:00
|
|
|
{
|
2020-01-12 22:16:21 +00:00
|
|
|
savewriter.Clear();
|
|
|
|
savewriter.SetFileName(filename);
|
|
|
|
|
2020-02-23 08:41:12 +00:00
|
|
|
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());
|
2019-11-26 23:41:26 +00:00
|
|
|
auto savesig = gi->GetSaveSig();
|
2020-01-11 18:40:35 +00:00
|
|
|
auto gs = gi->getStats();
|
|
|
|
FStringf timeStr("%02d:%02d", gs.timesecnd / 60, gs.timesecnd % 60);
|
2020-09-08 16:28:41 +00:00
|
|
|
auto lev = currentLevel;
|
2020-02-23 08:41:12 +00:00
|
|
|
|
|
|
|
savegameinfo.AddString("Software", buf)
|
|
|
|
("Save Version", savesig.currentsavever)
|
|
|
|
.AddString("Engine", savesig.savesig)
|
|
|
|
.AddString("Game Resource", fileSystem.GetResourceFileName(1))
|
2020-09-08 16:28:41 +00:00
|
|
|
.AddString("Map Name", lev->DisplayName())
|
2020-02-23 08:41:12 +00:00
|
|
|
.AddString("Creation Time", myasctime())
|
|
|
|
.AddString("Title", name)
|
2020-09-08 16:28:41 +00:00
|
|
|
.AddString("Map File", lev->fileName)
|
|
|
|
.AddString("Map Label", lev->labelName)
|
2020-02-23 08:41:12 +00:00
|
|
|
.AddString("Map Time", timeStr);
|
|
|
|
|
2019-12-10 21:22:59 +00:00
|
|
|
const char *fn = currentLevel->fileName;
|
|
|
|
if (*fn == '/') fn++;
|
2019-12-11 00:10:59 +00:00
|
|
|
if (strncmp(fn, "file://", 7) != 0) // this only has meaning for non-usermaps
|
2019-12-10 21:22:59 +00:00
|
|
|
{
|
|
|
|
auto fileno = fileSystem.FindFile(fn);
|
|
|
|
auto mapfile = fileSystem.GetFileContainer(fileno);
|
|
|
|
auto mapcname = fileSystem.GetResourceFileName(mapfile);
|
2020-02-23 08:41:12 +00:00
|
|
|
if (mapcname) savegameinfo.AddString("Map Resource", mapcname);
|
2020-01-12 22:16:21 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
savewriter.Clear();
|
|
|
|
return false; // this should never happen. Saving on a map that isn't present is impossible.
|
|
|
|
}
|
2019-12-10 21:22:59 +00:00
|
|
|
}
|
2019-11-26 23:41:26 +00:00
|
|
|
|
2020-02-23 08:41:12 +00:00
|
|
|
auto buff = savegameinfo.GetCompressedOutput();
|
|
|
|
AddCompressedSavegameChunk("info.json", buff);
|
2019-11-26 23:41:26 +00:00
|
|
|
|
|
|
|
|
2019-11-28 02:18:58 +00:00
|
|
|
// Handle system-side modules that need to persist data in savegames here, in a central place.
|
2020-02-23 13:03:03 +00:00
|
|
|
savegamesession.OpenWriter(save_formatted);
|
|
|
|
SerializeSession(savegamesession);
|
|
|
|
buff = savegamesession.GetCompressedOutput();
|
|
|
|
AddCompressedSavegameChunk("session.json", buff);
|
|
|
|
|
2020-01-12 22:16:21 +00:00
|
|
|
auto picfile = WriteSavegameChunk("savepic.png");
|
2020-04-26 21:17:54 +00:00
|
|
|
WriteSavePic(picfile, 240, 180);
|
2020-09-11 23:11:32 +00:00
|
|
|
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);
|
|
|
|
|
2020-01-12 22:16:21 +00:00
|
|
|
return true;
|
2019-11-26 23:41:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
static bool CheckSingleFile (const char *name, bool &printRequires, bool printwarn)
|
|
|
|
{
|
|
|
|
if (name == NULL)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2019-12-11 00:10:59 +00:00
|
|
|
if (strncmp(name, "file://", 7) == 0)
|
2019-12-10 21:22:59 +00:00
|
|
|
{
|
|
|
|
return FileExists(name + 7); // User maps must be present to be validated.
|
|
|
|
}
|
2019-11-26 23:41:26 +00:00
|
|
|
if (fileSystem.CheckIfResourceFileLoaded(name) < 0)
|
|
|
|
{
|
|
|
|
if (printwarn)
|
|
|
|
{
|
|
|
|
if (!printRequires)
|
|
|
|
{
|
|
|
|
Printf ("%s:\n%s", GStrings("TXT_SAVEGAMENEEDS"), name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Printf (", %s", name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printRequires = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
// Return false if not all the needed wads have been loaded.
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
2019-11-30 21:46:00 +00:00
|
|
|
static bool G_CheckSaveGameWads (const char *gamegrp, const char *mapgrp, bool printwarn)
|
2019-11-26 23:41:26 +00:00
|
|
|
{
|
|
|
|
bool printRequires = false;
|
2019-11-30 21:46:00 +00:00
|
|
|
CheckSingleFile (gamegrp, printRequires, printwarn);
|
|
|
|
CheckSingleFile (mapgrp, printRequires, printwarn);
|
2019-11-26 23:41:26 +00:00
|
|
|
|
|
|
|
if (printRequires)
|
|
|
|
{
|
|
|
|
if (printwarn)
|
|
|
|
{
|
|
|
|
Printf ("\n");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
// Checks if the savegame is valid. Gets a reader to the included info.json
|
|
|
|
// Returns 1 if valid, 0 if invalid and -1 if old and -2 if content missing
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
2019-12-10 21:22:59 +00:00
|
|
|
int G_ValidateSavegame(FileReader &fr, FString *savetitle, bool formenu)
|
2019-11-26 23:41:26 +00:00
|
|
|
{
|
2020-02-23 13:03:03 +00:00
|
|
|
auto data = fr.Read();
|
|
|
|
FSerializer arc;
|
|
|
|
if (!arc.OpenReader((const char*)data.Data(), data.Size()))
|
2020-02-23 08:41:12 +00:00
|
|
|
{
|
2020-02-23 13:03:03 +00:00
|
|
|
return -2;
|
2020-02-23 08:41:12 +00:00
|
|
|
}
|
|
|
|
|
2020-02-23 13:03:03 +00:00
|
|
|
int savever;
|
2020-07-07 18:27:21 +00:00
|
|
|
FString engine, gamegrp, mapgrp, title, filename, label;
|
2020-02-23 13:03:03 +00:00
|
|
|
|
|
|
|
arc("Save Version", savever)
|
|
|
|
("Engine", engine)
|
|
|
|
("Game Resource", gamegrp)
|
|
|
|
("Map Resource", mapgrp)
|
|
|
|
("Title", title)
|
2020-07-26 10:15:24 +00:00
|
|
|
("Map Label", label)
|
2020-02-23 13:03:03 +00:00
|
|
|
("Map File", filename);
|
|
|
|
|
|
|
|
auto savesig = gi->GetSaveSig();
|
|
|
|
|
|
|
|
if (savetitle) *savetitle = title;
|
|
|
|
if (engine.Compare(savesig.savesig) != 0 || savever > savesig.currentsavever)
|
2019-11-26 23:41:26 +00:00
|
|
|
{
|
2020-02-23 13:03:03 +00:00
|
|
|
// different engine or newer version:
|
|
|
|
// not our business. Leave it alone.
|
|
|
|
return 0;
|
|
|
|
}
|
2019-12-10 21:22:59 +00:00
|
|
|
|
2020-07-07 18:27:21 +00:00
|
|
|
MapRecord *curLevel = FindMapByName(label);
|
2019-12-10 21:22:59 +00:00
|
|
|
|
2020-07-07 18:27:21 +00:00
|
|
|
// If the map does not exist, check if it's a user map.
|
|
|
|
if (!curLevel)
|
2020-02-23 13:03:03 +00:00
|
|
|
{
|
2020-07-07 18:27:21 +00:00
|
|
|
curLevel = AllocateMap();
|
2020-02-23 13:03:03 +00:00
|
|
|
if (!formenu)
|
2019-12-10 21:22:59 +00:00
|
|
|
{
|
2020-07-07 18:27:21 +00:00
|
|
|
curLevel->name = "";
|
|
|
|
curLevel->SetFileName(filename);
|
2019-12-10 21:22:59 +00:00
|
|
|
}
|
2020-02-23 13:03:03 +00:00
|
|
|
}
|
|
|
|
if (!curLevel) return 0;
|
|
|
|
if (!formenu) currentLevel = curLevel;
|
2019-11-26 23:41:26 +00:00
|
|
|
|
2019-11-30 21:46:00 +00:00
|
|
|
|
2020-02-23 13:03:03 +00:00
|
|
|
if (savever < savesig.minsavever)
|
|
|
|
{
|
|
|
|
// old, incompatible savegame. List as not usable.
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto ggfn = ExtractFileBase(fileSystem.GetResourceFileName(1), true);
|
|
|
|
if (gamegrp.CompareNoCase(ggfn) == 0)
|
2019-11-26 23:41:26 +00:00
|
|
|
{
|
2020-02-23 13:03:03 +00:00
|
|
|
return G_CheckSaveGameWads(gamegrp, mapgrp, false) ? 1 : -2;
|
2019-11-26 23:41:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-23 13:03:03 +00:00
|
|
|
// different game. Skip this.
|
|
|
|
return 0;
|
2019-11-26 23:41:26 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-30 21:46:00 +00:00
|
|
|
return 0;
|
2019-11-26 23:41:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
FString G_BuildSaveName (const char *prefix)
|
|
|
|
{
|
2020-02-05 11:56:54 +00:00
|
|
|
FString name;
|
|
|
|
bool usefilter;
|
|
|
|
|
|
|
|
if (const char *const dir = Args->CheckValue("-savedir"))
|
|
|
|
{
|
|
|
|
name = dir;
|
|
|
|
usefilter = false;
|
|
|
|
}
|
|
|
|
else
|
2019-11-26 23:41:26 +00:00
|
|
|
{
|
2020-02-05 11:56:54 +00:00
|
|
|
name = **cl_savedir ? cl_savedir : M_GetSavegamesPath();
|
|
|
|
usefilter = true;
|
2019-11-26 23:41:26 +00:00
|
|
|
}
|
2020-02-05 11:56:54 +00:00
|
|
|
|
|
|
|
const size_t len = name.Len();
|
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
name.Substitute("\\", "/");
|
|
|
|
if (name[len - 1] != '/')
|
|
|
|
name << '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (usefilter)
|
|
|
|
name << LumpFilter << '/';
|
|
|
|
|
|
|
|
CreatePath(name);
|
|
|
|
|
2019-11-26 23:41:26 +00:00
|
|
|
name << prefix;
|
|
|
|
if (!strchr(prefix, '.')) name << SAVEGAME_EXT; // only add an extension if the prefix doesn't have one already.
|
|
|
|
name = NicePath(name);
|
|
|
|
name.Substitute("\\", "/");
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2020-01-21 18:22:38 +00:00
|
|
|
#include "build.h"
|
|
|
|
#include "mmulti.h"
|
2020-01-21 20:51:05 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-10-11 16:10:52 +00:00
|
|
|
|
|
|
|
#define V(x) x
|
|
|
|
static spritetype zsp;
|
2020-10-13 17:43:45 +00:00
|
|
|
static spriteext_t zspx;
|
|
|
|
|
|
|
|
FSerializer &Serialize(FSerializer &arc, const char *key, spritetype &c, spritetype *def)
|
|
|
|
{
|
|
|
|
def = &zsp; // always delta against 0
|
|
|
|
if (arc.BeginObject(key))
|
|
|
|
{
|
|
|
|
arc("x", c.x, def->x)
|
|
|
|
("y", c.y, def->y)
|
|
|
|
("z", c.z, def->z)
|
|
|
|
("cstat", c.cstat, def->cstat)
|
|
|
|
("picnum", c.picnum, def->picnum)
|
|
|
|
("shade", c.shade, def->shade)
|
|
|
|
("pal", c.pal, def->pal)
|
|
|
|
("clipdist", c.clipdist, def->clipdist)
|
|
|
|
("blend", c.blend, def->blend)
|
|
|
|
("xrepeat", c.xrepeat, def->xrepeat)
|
|
|
|
("yrepeat", c.yrepeat, def->yrepeat)
|
|
|
|
("xoffset", c.xoffset, def->xoffset)
|
|
|
|
("yoffset", c.yoffset, def->yoffset)
|
|
|
|
("statnum", c.statnum)
|
|
|
|
("sectnum", c.sectnum)
|
|
|
|
("ang", c.ang, def->ang)
|
|
|
|
("owner", c.owner, def->owner)
|
|
|
|
("xvel", c.xvel, def->xvel)
|
|
|
|
("yvel", c.yvel, def->yvel)
|
|
|
|
("zvel", c.zvel, def->zvel)
|
|
|
|
("lotag", c.lotag, def->lotag)
|
|
|
|
("hitag", c.hitag, def->hitag)
|
|
|
|
("extra", c.extra, def->extra)
|
|
|
|
("detail", c.detail, def->detail)
|
|
|
|
.EndObject();
|
|
|
|
}
|
|
|
|
return arc;
|
|
|
|
}
|
|
|
|
|
|
|
|
FSerializer& Serialize(FSerializer& arc, const char* key, spriteext_t& c, spriteext_t* def)
|
|
|
|
{
|
|
|
|
if (arc.isWriting() && c.mdanimtims)
|
|
|
|
{
|
|
|
|
c.mdanimtims -= mdtims;
|
|
|
|
if (c.mdanimtims == 0) c.mdanimtims++;
|
|
|
|
}
|
|
|
|
|
|
|
|
def = &zspx; // always delta against 0
|
|
|
|
if (arc.BeginObject(key))
|
|
|
|
{
|
|
|
|
arc("mdanimtims", c.mdanimtims, def->mdanimtims)
|
|
|
|
("mdanimcur", c.mdanimcur, def->mdanimcur)
|
|
|
|
("angoff", c.angoff, def->angoff)
|
|
|
|
("pitch", c.pitch, def->pitch)
|
|
|
|
("roll", c.roll, def->roll)
|
|
|
|
("pivot_offset", c.pivot_offset, def->pivot_offset)
|
|
|
|
("position_offset", c.position_offset, def->position_offset)
|
|
|
|
("flags", c.flags, def->flags)
|
|
|
|
("alpha", c.alpha, def->alpha)
|
|
|
|
.EndObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c.mdanimtims) c.mdanimtims += mdtims;
|
|
|
|
return arc;
|
|
|
|
}
|
2020-10-11 16:10:52 +00:00
|
|
|
|
|
|
|
FSerializer &Serialize(FSerializer &arc, const char *key, sectortype &c, sectortype *def)
|
|
|
|
{
|
|
|
|
if (arc.BeginObject(key))
|
|
|
|
{
|
|
|
|
arc("wallptr", c.wallptr, def->wallptr)
|
|
|
|
("wallnum", c.wallnum, def->wallnum)
|
|
|
|
("ceilingz", c.ceilingz, def->ceilingz)
|
|
|
|
("floorz", c.floorz, def->floorz)
|
|
|
|
("ceilingstat", c.ceilingstat, def->ceilingstat)
|
|
|
|
("floorstat", c.floorstat, def->floorstat)
|
|
|
|
("ceilingpicnum", c.ceilingpicnum, def->ceilingpicnum)
|
|
|
|
("ceilingheinum", c.ceilingheinum, def->ceilingheinum)
|
|
|
|
("ceilingshade", c.ceilingshade, def->ceilingshade)
|
|
|
|
("ceilingpal", c.ceilingpal, def->ceilingpal)
|
2020-11-25 19:52:06 +00:00
|
|
|
("ceilingxpanning", c.ceilingxpan_, def->ceilingxpan_)
|
|
|
|
("ceilingypanning", c.ceilingypan_, def->ceilingypan_)
|
2020-10-11 16:10:52 +00:00
|
|
|
("floorpicnum", c.floorpicnum, def->floorpicnum)
|
|
|
|
("floorheinum", c.floorheinum, def->floorheinum)
|
|
|
|
("floorshade", c.floorshade, def->floorshade)
|
|
|
|
("floorpal", c.floorpal, def->floorpal)
|
2020-11-25 19:52:06 +00:00
|
|
|
("floorxpanning", c.floorxpan_, def->floorxpan_)
|
|
|
|
("floorypanning", c.floorypan_, def->floorypan_)
|
2020-10-11 16:10:52 +00:00
|
|
|
("visibility", c.visibility, def->visibility)
|
|
|
|
("fogpal", c.fogpal, def->fogpal)
|
|
|
|
("lotag", c.lotag, def->lotag)
|
|
|
|
("hitag", c.hitag, def->hitag)
|
|
|
|
("extra", c.extra, def->extra)
|
|
|
|
.EndObject();
|
|
|
|
}
|
|
|
|
return arc;
|
|
|
|
}
|
|
|
|
|
|
|
|
FSerializer &Serialize(FSerializer &arc, const char *key, walltype &c, walltype *def)
|
|
|
|
{
|
|
|
|
if (arc.BeginObject(key))
|
|
|
|
{
|
|
|
|
arc("x", c.x, def->x)
|
|
|
|
("y", c.y, def->y)
|
|
|
|
("point2", c.point2, def->point2)
|
|
|
|
("nextwall", c.nextwall, def->nextwall)
|
|
|
|
("nextsector", c.nextsector, def->nextsector)
|
|
|
|
("cstat", c.cstat, def->cstat)
|
|
|
|
("picnum", c.picnum, def->picnum)
|
|
|
|
("overpicnum", c.overpicnum, def->overpicnum)
|
|
|
|
("shade", c.shade, def->shade)
|
|
|
|
("pal", c.pal, def->pal)
|
|
|
|
("xrepeat", c.xrepeat, def->xrepeat)
|
|
|
|
("yrepeat", c.yrepeat, def->yrepeat)
|
2020-11-26 07:38:59 +00:00
|
|
|
("xpanning", c.xpan_, def->xpan_)
|
|
|
|
("ypanning", c.ypan_, def->ypan_)
|
2020-10-11 16:10:52 +00:00
|
|
|
("lotag", c.lotag, def->lotag)
|
|
|
|
("hitag", c.hitag, def->hitag)
|
|
|
|
("extra", c.extra, def->extra)
|
|
|
|
.EndObject();
|
|
|
|
}
|
|
|
|
return arc;
|
|
|
|
}
|
|
|
|
|
2020-10-13 17:43:45 +00:00
|
|
|
|
2020-10-11 16:10:52 +00:00
|
|
|
void SerializeMap(FSerializer& arc)
|
|
|
|
{
|
2020-10-13 17:43:45 +00:00
|
|
|
// create a map of all used sprites so that we can use that elsewhere to only save what's needed.
|
|
|
|
activeSprites.Zero();
|
|
|
|
if (arc.isWriting())
|
|
|
|
{
|
|
|
|
for (int i=0; i<MAXSPRITES;i++)
|
|
|
|
{
|
|
|
|
if (sprite[i].statnum != MAXSTATUS)
|
|
|
|
{
|
|
|
|
activeSprites.Set(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// simplify the data a bit for better compression.
|
|
|
|
for (int i = 0; i < MAXSPRITES; i++)
|
|
|
|
{
|
|
|
|
if (nextspritestat[i] == i + 1) nextspritestat[i] = -2;
|
|
|
|
if (nextspritesect[i] == i + 1) nextspritesect[i] = -2;
|
|
|
|
if (prevspritestat[i] == i - 1) prevspritestat[i] = -2;
|
|
|
|
if (prevspritesect[i] == i - 1) prevspritesect[i] = -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memset(sprite, 0, sizeof(sprite[0]) * MAXSPRITES);
|
|
|
|
initspritelists();
|
|
|
|
zsp = sprite[0];
|
|
|
|
}
|
|
|
|
|
2020-10-11 16:10:52 +00:00
|
|
|
if (arc.BeginObject("engine"))
|
|
|
|
{
|
2020-10-13 17:43:45 +00:00
|
|
|
arc.SerializeMemory("activesprites", activeSprites.Storage(), activeSprites.StorageSize())
|
|
|
|
.SparseArray("sprites", sprite, MAXSPRITES, activeSprites)
|
|
|
|
.SparseArray("spriteext", spriteext, MAXSPRITES, activeSprites)
|
|
|
|
("numsectors", numsectors)
|
2020-10-11 18:57:20 +00:00
|
|
|
.Array("sectors", sector, sectorbackup, numsectors)
|
2020-10-11 16:10:52 +00:00
|
|
|
("numwalls", numwalls)
|
2020-10-11 18:57:20 +00:00
|
|
|
.Array("walls", wall, wallbackup, numwalls)
|
2020-10-13 17:43:45 +00:00
|
|
|
.Array("headspritestat", headspritestat, MAXSTATUS + 1)
|
|
|
|
.Array("nextspritestat", nextspritestat, MAXSPRITES)
|
|
|
|
.Array("prevspritestat", prevspritestat, MAXSPRITES)
|
|
|
|
.Array("headspritesect", headspritesect, MAXSECTORS + 1)
|
|
|
|
.Array("nextspritesect", nextspritesect, MAXSPRITES)
|
|
|
|
.Array("prevspritesect", prevspritesect, MAXSPRITES)
|
2020-11-30 22:45:21 +00:00
|
|
|
|
2020-10-13 17:43:45 +00:00
|
|
|
("tailspritefree", tailspritefree)
|
|
|
|
("myconnectindex", myconnectindex)
|
|
|
|
("connecthead", connecthead)
|
|
|
|
.Array("connectpoint2", connectpoint2, countof(connectpoint2))
|
|
|
|
("randomseed", randomseed)
|
|
|
|
("numshades", numshades) // is this really needed?
|
|
|
|
("visibility", g_visibility)
|
|
|
|
("parallaxtype", parallaxtype)
|
|
|
|
("parallaxvisibility", parallaxvisibility)
|
|
|
|
("parallaxyo", parallaxyoffs_override)
|
|
|
|
("parallaxys", parallaxyscale_override)
|
|
|
|
("pskybits", pskybits_override)
|
2020-11-30 22:45:21 +00:00
|
|
|
("numsprites", Numsprites)
|
|
|
|
("gamesetinput", gamesetinput);
|
2020-10-13 17:43:45 +00:00
|
|
|
|
2020-11-25 22:22:13 +00:00
|
|
|
SerializeInterpolations(arc);
|
|
|
|
|
2020-10-13 17:43:45 +00:00
|
|
|
if (arc.BeginArray("picanm")) // write this in the most compact form available.
|
|
|
|
{
|
|
|
|
for (int i = 0; i < MAXTILES; i++)
|
|
|
|
{
|
|
|
|
arc(nullptr, picanm[i].sf)
|
|
|
|
(nullptr, picanm[i].extra);
|
|
|
|
}
|
|
|
|
arc.EndArray();
|
|
|
|
}
|
2020-10-11 16:10:52 +00:00
|
|
|
|
2020-10-13 17:43:45 +00:00
|
|
|
arc.EndObject();
|
2020-05-24 10:31:38 +00:00
|
|
|
}
|
2020-01-21 20:51:05 +00:00
|
|
|
|
2020-01-21 18:22:38 +00:00
|
|
|
|
2020-10-13 17:43:45 +00:00
|
|
|
// Undo the simplification.
|
|
|
|
for (int i = 0; i < MAXSPRITES; i++)
|
2020-01-21 18:22:38 +00:00
|
|
|
{
|
2020-10-13 17:43:45 +00:00
|
|
|
if (nextspritestat[i] == -2) nextspritestat[i] = i + 1;
|
|
|
|
if (nextspritesect[i] == -2) nextspritesect[i] = i + 1;
|
|
|
|
if (prevspritestat[i] == -2) prevspritestat[i] = i - 1;
|
|
|
|
if (prevspritesect[i] == -2) prevspritesect[i] = i - 1;
|
2020-01-21 18:22:38 +00:00
|
|
|
}
|
2020-02-02 15:06:13 +00:00
|
|
|
}
|
2020-10-04 16:31:48 +00:00
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
CVAR(Bool, saveloadconfirmation, true, CVAR_ARCHIVE | CVAR_GLOBALCONFIG)
|
|
|
|
|
|
|
|
CVAR(Int, autosavenum, 0, CVAR_NOSET | CVAR_ARCHIVE | CVAR_GLOBALCONFIG)
|
|
|
|
static int nextautosave = -1;
|
|
|
|
CVAR(Int, disableautosave, 0, CVAR_ARCHIVE | CVAR_GLOBALCONFIG)
|
|
|
|
CUSTOM_CVAR(Int, autosavecount, 4, CVAR_ARCHIVE | CVAR_GLOBALCONFIG)
|
|
|
|
{
|
|
|
|
if (self < 1)
|
|
|
|
self = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
CVAR(Int, quicksavenum, 0, CVAR_NOSET | CVAR_ARCHIVE | CVAR_GLOBALCONFIG)
|
|
|
|
static int nextquicksave = -1;
|
|
|
|
CUSTOM_CVAR(Int, quicksavecount, 4, CVAR_ARCHIVE | CVAR_GLOBALCONFIG)
|
|
|
|
{
|
|
|
|
if (self < 1)
|
|
|
|
self = 1;
|
|
|
|
}
|
|
|
|
|
2020-10-07 16:32:57 +00:00
|
|
|
void DoLoadGame(const char* name)
|
|
|
|
{
|
|
|
|
if (OpenSaveGameForRead(name))
|
|
|
|
{
|
|
|
|
if (gi->LoadGame())
|
|
|
|
{
|
|
|
|
gameaction = ga_level;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
I_Error("%s: Failed to load savegame", name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
I_Error("%s: Failed to open savegame", name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void G_LoadGame(const char *filename)
|
|
|
|
{
|
|
|
|
inputState.ClearAllInput();
|
|
|
|
gi->FreeLevelData();
|
|
|
|
DoLoadGame(filename);
|
|
|
|
BackupSaveGame = filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
void G_SaveGame(const char *fn, const char *desc, bool ok4q, bool forceq)
|
|
|
|
{
|
|
|
|
if (OpenSaveGameForWrite(fn, desc))
|
|
|
|
{
|
|
|
|
if (gi->SaveGame() && FinishSavegameWrite())
|
|
|
|
{
|
|
|
|
savegameManager.NotifyNewSave(fn, desc, ok4q, forceq);
|
|
|
|
Printf(PRINT_NOTIFY, "%s\n", GStrings("GAME SAVED"));
|
|
|
|
BackupSaveGame = fn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-04 16:31:48 +00:00
|
|
|
void M_Autosave()
|
|
|
|
{
|
|
|
|
if (disableautosave) return;
|
|
|
|
if (!gi->CanSave()) return;
|
|
|
|
FString description;
|
|
|
|
FString file;
|
|
|
|
// Keep a rotating sets of autosaves
|
|
|
|
UCVarValue num;
|
|
|
|
const char* readableTime;
|
|
|
|
int count = autosavecount != 0 ? autosavecount : 1;
|
|
|
|
|
|
|
|
if (nextautosave == -1)
|
|
|
|
{
|
|
|
|
nextautosave = (autosavenum + 1) % count;
|
|
|
|
}
|
|
|
|
|
|
|
|
num.Int = nextautosave;
|
|
|
|
autosavenum.ForceSet(num, CVAR_Int);
|
|
|
|
|
2020-10-07 16:32:57 +00:00
|
|
|
auto Filename = G_BuildSaveName(FStringf("auto%04d", nextautosave));
|
2020-10-04 16:31:48 +00:00
|
|
|
readableTime = myasctime();
|
2020-10-07 16:32:57 +00:00
|
|
|
FStringf SaveTitle("Autosave %s", readableTime);
|
2020-10-04 16:31:48 +00:00
|
|
|
nextautosave = (nextautosave + 1) % count;
|
2020-10-07 16:32:57 +00:00
|
|
|
G_SaveGame(Filename, SaveTitle, false, false);
|
2020-10-04 16:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CCMD(autosave)
|
|
|
|
{
|
|
|
|
gameaction = ga_autosave;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCMD(rotatingquicksave)
|
|
|
|
{
|
|
|
|
if (!gi->CanSave()) return;
|
|
|
|
FString description;
|
|
|
|
FString file;
|
|
|
|
// Keep a rotating sets of quicksaves
|
|
|
|
UCVarValue num;
|
|
|
|
const char* readableTime;
|
|
|
|
int count = quicksavecount != 0 ? quicksavecount : 1;
|
|
|
|
|
|
|
|
if (nextquicksave == -1)
|
|
|
|
{
|
|
|
|
nextquicksave = (quicksavenum + 1) % count;
|
|
|
|
}
|
|
|
|
|
|
|
|
num.Int = nextquicksave;
|
|
|
|
quicksavenum.ForceSet(num, CVAR_Int);
|
|
|
|
|
|
|
|
FSaveGameNode sg;
|
2020-10-07 16:32:57 +00:00
|
|
|
auto Filename = G_BuildSaveName(FStringf("quick%04d", nextquicksave));
|
2020-10-04 16:31:48 +00:00
|
|
|
readableTime = myasctime();
|
2020-10-07 16:32:57 +00:00
|
|
|
FStringf SaveTitle("Quicksave %s", readableTime);
|
2020-10-04 16:31:48 +00:00
|
|
|
nextquicksave = (nextquicksave + 1) % count;
|
2020-10-07 16:32:57 +00:00
|
|
|
G_SaveGame(Filename, SaveTitle, false, false);
|
2020-10-04 16:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|