2016-03-01 15:47:10 +00:00
|
|
|
/*
|
|
|
|
** loadsavemenu.cpp
|
|
|
|
** The load game and save game menus
|
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
** Copyright 2001-2010 Randy Heit
|
2017-02-11 16:20:12 +00:00
|
|
|
** Copyright 2010-2017 Christoph Oelckers
|
2016-03-01 15:47:10 +00:00
|
|
|
** 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 OF
|
|
|
|
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "menu/menu.h"
|
|
|
|
#include "i_system.h"
|
|
|
|
#include "version.h"
|
|
|
|
#include "g_game.h"
|
|
|
|
#include "m_png.h"
|
|
|
|
#include "w_wad.h"
|
|
|
|
#include "v_text.h"
|
|
|
|
#include "d_event.h"
|
|
|
|
#include "gstrings.h"
|
|
|
|
#include "v_palette.h"
|
|
|
|
#include "doomstat.h"
|
|
|
|
#include "gi.h"
|
|
|
|
#include "d_gui.h"
|
2016-09-21 19:57:24 +00:00
|
|
|
#include "serializer.h"
|
|
|
|
#include "resourcefiles/resourcefile.h"
|
2017-04-12 23:12:04 +00:00
|
|
|
#include "vm.h"
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2017-02-18 15:40:32 +00:00
|
|
|
// Save name length limit for old binary formats.
|
|
|
|
#define OLDSAVESTRINGSIZE 24
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
//
|
2017-02-11 16:20:12 +00:00
|
|
|
// Save data maintenance
|
2016-03-01 15:47:10 +00:00
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
2017-02-18 13:08:16 +00:00
|
|
|
void FSavegameManager::ClearSaveGames()
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-02-11 16:20:12 +00:00
|
|
|
for (unsigned i = 0; i<SaveGames.Size(); i++)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-02-11 16:20:12 +00:00
|
|
|
if (!SaveGames[i]->bNoDelete)
|
|
|
|
delete SaveGames[i];
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
2017-02-11 16:20:12 +00:00
|
|
|
SaveGames.Clear();
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
2017-02-18 13:18:27 +00:00
|
|
|
FSavegameManager::~FSavegameManager()
|
|
|
|
{
|
|
|
|
ClearSaveGames();
|
|
|
|
}
|
2017-02-18 17:01:24 +00:00
|
|
|
|
2016-03-01 15:47:10 +00:00
|
|
|
//=============================================================================
|
|
|
|
//
|
2017-02-11 16:20:12 +00:00
|
|
|
// Save data maintenance
|
2016-03-01 15:47:10 +00:00
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
2017-02-18 13:08:16 +00:00
|
|
|
int FSavegameManager::RemoveSaveSlot(int index)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-02-18 13:08:16 +00:00
|
|
|
int listindex = SaveGames[0]->bNoDelete ? index - 1 : index;
|
|
|
|
if (listindex < 0) return index;
|
|
|
|
|
|
|
|
remove(SaveGames[index]->Filename.GetChars());
|
|
|
|
UnloadSaveData();
|
|
|
|
|
2016-03-01 15:47:10 +00:00
|
|
|
FSaveGameNode *file = SaveGames[index];
|
|
|
|
|
|
|
|
if (quickSaveSlot == SaveGames[index])
|
|
|
|
{
|
2017-02-11 16:20:12 +00:00
|
|
|
quickSaveSlot = nullptr;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
if (!file->bNoDelete) delete file;
|
2017-02-18 13:08:16 +00:00
|
|
|
|
|
|
|
if (LastSaved == listindex) LastSaved = -1;
|
|
|
|
else if (LastSaved > listindex) LastSaved--;
|
|
|
|
if (LastAccessed == listindex) LastAccessed = -1;
|
|
|
|
else if (LastAccessed > listindex) LastAccessed--;
|
|
|
|
|
2016-03-01 15:47:10 +00:00
|
|
|
SaveGames.Delete(index);
|
|
|
|
if ((unsigned)index >= SaveGames.Size()) index--;
|
2017-02-18 13:08:16 +00:00
|
|
|
ExtractSaveData(index);
|
2016-03-01 15:47:10 +00:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2017-02-18 16:35:01 +00:00
|
|
|
DEFINE_ACTION_FUNCTION(FSavegameManager, RemoveSaveSlot)
|
|
|
|
{
|
|
|
|
PARAM_SELF_STRUCT_PROLOGUE(FSavegameManager);
|
|
|
|
PARAM_INT(sel);
|
|
|
|
ACTION_RETURN_INT(self->RemoveSaveSlot(sel));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-01 15:47:10 +00:00
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
2017-02-18 13:08:16 +00:00
|
|
|
int FSavegameManager::InsertSaveNode(FSaveGameNode *node)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
if (SaveGames.Size() == 0)
|
|
|
|
{
|
|
|
|
return SaveGames.Push(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node->bOldVersion)
|
|
|
|
{ // Add node at bottom of list
|
|
|
|
return SaveGames.Push(node);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // Add node at top of list
|
|
|
|
unsigned int i;
|
2017-02-11 16:20:12 +00:00
|
|
|
for (i = 0; i < SaveGames.Size(); i++)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-02-18 11:36:31 +00:00
|
|
|
if (SaveGames[i]->bOldVersion || node->SaveTitle.CompareNoCase(SaveGames[i]->SaveTitle) <= 0)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SaveGames.Insert(i, node);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
// M_ReadSaveStrings
|
|
|
|
//
|
|
|
|
// Find savegames and read their titles
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
2017-02-18 13:08:16 +00:00
|
|
|
void FSavegameManager::ReadSaveStrings()
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
if (SaveGames.Size() == 0)
|
|
|
|
{
|
|
|
|
void *filefirst;
|
|
|
|
findstate_t c_file;
|
|
|
|
FString filter;
|
|
|
|
|
|
|
|
LastSaved = LastAccessed = -1;
|
2017-02-11 16:20:12 +00:00
|
|
|
quickSaveSlot = nullptr;
|
|
|
|
filter = G_BuildSaveName("*." SAVEGAME_EXT, -1);
|
|
|
|
filefirst = I_FindFirst(filter.GetChars(), &c_file);
|
2016-03-01 15:47:10 +00:00
|
|
|
if (filefirst != ((void *)(-1)))
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
// I_FindName only returns the file's name and not its full path
|
2017-02-11 16:20:12 +00:00
|
|
|
FString filepath = G_BuildSaveName(I_FindName(&c_file), -1);
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2016-09-21 19:57:24 +00:00
|
|
|
FResourceFile *savegame = FResourceFile::OpenResourceFile(filepath, nullptr, true, true);
|
|
|
|
if (savegame != nullptr)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2016-09-21 19:57:24 +00:00
|
|
|
bool oldVer = false;
|
2016-03-01 15:47:10 +00:00
|
|
|
bool missing = false;
|
2016-09-21 19:57:24 +00:00
|
|
|
FResourceLump *info = savegame->FindLump("info.json");
|
|
|
|
if (info == nullptr)
|
|
|
|
{
|
|
|
|
// savegame info not found. This is not a savegame so leave it alone.
|
|
|
|
delete savegame;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
void *data = info->CacheLump();
|
|
|
|
FSerializer arc;
|
2016-09-22 22:45:41 +00:00
|
|
|
if (arc.OpenReader((const char *)data, info->LumpSize))
|
2016-09-21 19:57:24 +00:00
|
|
|
{
|
|
|
|
int savever = 0;
|
|
|
|
FString engine;
|
|
|
|
FString iwad;
|
|
|
|
FString title;
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2016-09-21 19:57:24 +00:00
|
|
|
arc("Save Version", savever);
|
|
|
|
arc("Engine", engine);
|
|
|
|
arc("Game WAD", iwad);
|
|
|
|
arc("Title", title);
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2016-09-21 19:57:24 +00:00
|
|
|
if (engine.Compare(GAMESIG) != 0 || savever > SAVEVER)
|
|
|
|
{
|
|
|
|
// different engine or newer version:
|
|
|
|
// not our business. Leave it alone.
|
|
|
|
delete savegame;
|
|
|
|
continue;
|
|
|
|
}
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2016-09-21 19:57:24 +00:00
|
|
|
if (savever < MINSAVEVER)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2016-09-21 19:57:24 +00:00
|
|
|
// old, incompatible savegame. List as not usable.
|
|
|
|
oldVer = true;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
2016-09-21 19:57:24 +00:00
|
|
|
else if (iwad.CompareNoCase(Wads.GetWadName(FWadCollection::IWAD_FILENUM)) == 0)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2016-09-21 19:57:24 +00:00
|
|
|
missing = !G_CheckSaveGameWads(arc, false);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
2016-09-22 15:39:09 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// different game. Skip this.
|
|
|
|
delete savegame;
|
|
|
|
continue;
|
|
|
|
}
|
2016-09-21 19:57:24 +00:00
|
|
|
|
|
|
|
FSaveGameNode *node = new FSaveGameNode;
|
|
|
|
node->Filename = filepath;
|
|
|
|
node->bOldVersion = oldVer;
|
|
|
|
node->bMissingWads = missing;
|
2017-02-18 11:36:31 +00:00
|
|
|
node->SaveTitle = title;
|
2016-09-21 19:57:24 +00:00
|
|
|
InsertSaveNode(node);
|
|
|
|
delete savegame;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
2016-09-21 19:57:24 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
else // check for old formats.
|
|
|
|
{
|
2017-02-11 16:20:12 +00:00
|
|
|
FILE *file = fopen(filepath, "rb");
|
|
|
|
if (file != nullptr)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2016-09-21 19:57:24 +00:00
|
|
|
PNGHandle *png;
|
|
|
|
char sig[16];
|
2017-02-18 15:40:32 +00:00
|
|
|
char title[OLDSAVESTRINGSIZE + 1];
|
2016-09-21 19:57:24 +00:00
|
|
|
bool oldVer = true;
|
|
|
|
bool addIt = false;
|
|
|
|
bool missing = false;
|
|
|
|
|
|
|
|
// ZDoom 1.23 betas 21-33 have the savesig first.
|
|
|
|
// Earlier versions have the savesig second.
|
|
|
|
// Later versions have the savegame encapsulated inside a PNG.
|
|
|
|
//
|
|
|
|
// Old savegame versions are always added to the menu so
|
|
|
|
// the user can easily delete them if desired.
|
|
|
|
|
2017-02-18 15:40:32 +00:00
|
|
|
title[OLDSAVESTRINGSIZE] = 0;
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2016-09-21 19:57:24 +00:00
|
|
|
|
2017-02-11 16:20:12 +00:00
|
|
|
if (nullptr != (png = M_VerifyPNG(file)))
|
2016-09-21 19:57:24 +00:00
|
|
|
{
|
2017-02-11 16:20:12 +00:00
|
|
|
char *ver = M_GetPNGText(png, "ZDoom Save Version");
|
|
|
|
if (ver != nullptr)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2016-09-21 19:57:24 +00:00
|
|
|
// An old version
|
2017-02-18 15:40:32 +00:00
|
|
|
if (!M_GetPNGText(png, "Title", title, OLDSAVESTRINGSIZE))
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-02-18 15:40:32 +00:00
|
|
|
strncpy(title, I_FindName(&c_file), OLDSAVESTRINGSIZE);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
2016-09-21 19:57:24 +00:00
|
|
|
addIt = true;
|
|
|
|
delete[] ver;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
2016-09-21 19:57:24 +00:00
|
|
|
delete png;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-11 16:20:12 +00:00
|
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
if (fread(sig, 1, 16, file) == 16)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2016-09-21 19:57:24 +00:00
|
|
|
|
2017-02-11 16:20:12 +00:00
|
|
|
if (strncmp(sig, "ZDOOMSAVE", 9) == 0)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-02-18 15:40:32 +00:00
|
|
|
if (fread(title, 1, OLDSAVESTRINGSIZE, file) == OLDSAVESTRINGSIZE)
|
2016-09-21 19:57:24 +00:00
|
|
|
{
|
|
|
|
addIt = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-11 16:20:12 +00:00
|
|
|
memcpy(title, sig, 16);
|
2017-02-18 15:40:32 +00:00
|
|
|
if (fread(title + 16, 1, OLDSAVESTRINGSIZE - 16, file) == OLDSAVESTRINGSIZE - 16 &&
|
2017-02-11 16:20:12 +00:00
|
|
|
fread(sig, 1, 16, file) == 16 &&
|
|
|
|
strncmp(sig, "ZDOOMSAVE", 9) == 0)
|
2016-09-21 19:57:24 +00:00
|
|
|
{
|
|
|
|
addIt = true;
|
|
|
|
}
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 19:57:24 +00:00
|
|
|
if (addIt)
|
|
|
|
{
|
|
|
|
FSaveGameNode *node = new FSaveGameNode;
|
|
|
|
node->Filename = filepath;
|
|
|
|
node->bOldVersion = true;
|
|
|
|
node->bMissingWads = false;
|
2017-02-18 11:36:31 +00:00
|
|
|
node->SaveTitle = title;
|
2017-02-11 16:20:12 +00:00
|
|
|
InsertSaveNode(node);
|
2016-09-21 19:57:24 +00:00
|
|
|
}
|
2017-02-11 16:20:12 +00:00
|
|
|
fclose(file);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-11 16:20:12 +00:00
|
|
|
} while (I_FindNext(filefirst, &c_file) == 0);
|
|
|
|
I_FindClose(filefirst);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-18 17:01:24 +00:00
|
|
|
DEFINE_ACTION_FUNCTION(FSavegameManager, ReadSaveStrings)
|
|
|
|
{
|
|
|
|
PARAM_SELF_STRUCT_PROLOGUE(FSavegameManager);
|
|
|
|
self->ReadSaveStrings();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-01 15:47:10 +00:00
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
2017-02-18 13:08:16 +00:00
|
|
|
void FSavegameManager::NotifyNewSave(const FString &file, const FString &title, bool okForQuicksave)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
FSaveGameNode *node;
|
|
|
|
|
2017-02-18 12:05:33 +00:00
|
|
|
if (file.IsEmpty())
|
2016-03-01 15:47:10 +00:00
|
|
|
return;
|
|
|
|
|
2017-02-11 16:20:12 +00:00
|
|
|
ReadSaveStrings();
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
// See if the file is already in our list
|
2017-02-11 16:20:12 +00:00
|
|
|
for (unsigned i = 0; i<SaveGames.Size(); i++)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
FSaveGameNode *node = SaveGames[i];
|
|
|
|
#ifdef __unix__
|
2017-02-11 16:20:12 +00:00
|
|
|
if (node->Filename.Compare(file) == 0)
|
2016-03-01 15:47:10 +00:00
|
|
|
#else
|
2017-02-11 16:20:12 +00:00
|
|
|
if (node->Filename.CompareNoCase(file) == 0)
|
2016-03-01 15:47:10 +00:00
|
|
|
#endif
|
|
|
|
{
|
2017-02-18 11:36:31 +00:00
|
|
|
node->SaveTitle = title;
|
2016-03-01 15:47:10 +00:00
|
|
|
node->bOldVersion = false;
|
|
|
|
node->bMissingWads = false;
|
|
|
|
if (okForQuicksave)
|
|
|
|
{
|
2017-02-11 16:20:12 +00:00
|
|
|
if (quickSaveSlot == nullptr) quickSaveSlot = node;
|
2016-03-01 15:47:10 +00:00
|
|
|
LastAccessed = LastSaved = i;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
node = new FSaveGameNode;
|
2017-02-18 11:36:31 +00:00
|
|
|
node->SaveTitle = title;
|
2016-03-01 15:47:10 +00:00
|
|
|
node->Filename = file;
|
|
|
|
node->bOldVersion = false;
|
|
|
|
node->bMissingWads = false;
|
2017-02-11 16:20:12 +00:00
|
|
|
int index = InsertSaveNode(node);
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
if (okForQuicksave)
|
|
|
|
{
|
2017-02-11 16:20:12 +00:00
|
|
|
if (quickSaveSlot == nullptr) quickSaveSlot = node;
|
2016-03-01 15:47:10 +00:00
|
|
|
LastAccessed = LastSaved = index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
//
|
2017-02-11 16:20:12 +00:00
|
|
|
// Loads the savegame
|
2016-03-01 15:47:10 +00:00
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
2017-02-18 13:08:16 +00:00
|
|
|
void FSavegameManager::LoadSavegame(int Selected)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-02-11 16:20:12 +00:00
|
|
|
G_LoadGame(SaveGames[Selected]->Filename.GetChars(), true);
|
|
|
|
if (quickSaveSlot == (FSaveGameNode*)1)
|
|
|
|
{
|
|
|
|
quickSaveSlot = SaveGames[Selected];
|
|
|
|
}
|
|
|
|
M_ClearMenus();
|
|
|
|
V_SetBorderNeedRefresh();
|
|
|
|
LastAccessed = Selected;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
2017-02-18 13:08:16 +00:00
|
|
|
DEFINE_ACTION_FUNCTION(FSavegameManager, LoadSavegame)
|
|
|
|
{
|
|
|
|
PARAM_SELF_STRUCT_PROLOGUE(FSavegameManager);
|
|
|
|
PARAM_INT(sel);
|
|
|
|
self->LoadSavegame(sel);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-01 15:47:10 +00:00
|
|
|
//=============================================================================
|
|
|
|
//
|
2017-02-11 16:20:12 +00:00
|
|
|
//
|
2016-03-01 15:47:10 +00:00
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
2017-02-18 13:08:16 +00:00
|
|
|
void FSavegameManager::DoSave(int Selected, const char *savegamestring)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-02-11 16:20:12 +00:00
|
|
|
if (Selected != 0)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-02-11 16:20:12 +00:00
|
|
|
auto node = SaveGames[Selected];
|
|
|
|
G_SaveGame(node->Filename.GetChars(), savegamestring);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
2017-02-11 16:20:12 +00:00
|
|
|
else
|
2016-09-21 19:57:24 +00:00
|
|
|
{
|
2017-02-11 16:20:12 +00:00
|
|
|
// Find an unused filename and save as that
|
|
|
|
FString filename;
|
|
|
|
int i;
|
|
|
|
FILE *test;
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2017-02-11 16:20:12 +00:00
|
|
|
for (i = 0;; ++i)
|
|
|
|
{
|
|
|
|
filename = G_BuildSaveName("save", i);
|
|
|
|
test = fopen(filename, "rb");
|
|
|
|
if (test == nullptr)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fclose(test);
|
|
|
|
}
|
|
|
|
G_SaveGame(filename, savegamestring);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
2017-02-11 16:20:12 +00:00
|
|
|
M_ClearMenus();
|
|
|
|
V_SetBorderNeedRefresh();
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
2017-02-18 15:40:32 +00:00
|
|
|
DEFINE_ACTION_FUNCTION(FSavegameManager, DoSave)
|
|
|
|
{
|
|
|
|
PARAM_SELF_STRUCT_PROLOGUE(FSavegameManager);
|
|
|
|
PARAM_INT(sel);
|
|
|
|
PARAM_STRING(name);
|
|
|
|
self->DoSave(sel, name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-01 15:47:10 +00:00
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
2017-02-18 13:08:16 +00:00
|
|
|
unsigned FSavegameManager::ExtractSaveData(int index)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2016-09-21 19:57:24 +00:00
|
|
|
FResourceFile *resf;
|
2016-03-01 15:47:10 +00:00
|
|
|
FSaveGameNode *node;
|
|
|
|
|
2017-02-18 13:08:16 +00:00
|
|
|
if (index == -1)
|
|
|
|
{
|
|
|
|
if (SaveGames.Size() > 0 && SaveGames[0]->bNoDelete)
|
|
|
|
{
|
|
|
|
index = LastSaved + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-18 15:40:32 +00:00
|
|
|
index = LastAccessed < 0? 0 : LastAccessed;
|
2017-02-18 13:08:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-11 16:20:12 +00:00
|
|
|
UnloadSaveData();
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
if ((unsigned)index < SaveGames.Size() &&
|
|
|
|
(node = SaveGames[index]) &&
|
|
|
|
!node->Filename.IsEmpty() &&
|
|
|
|
!node->bOldVersion &&
|
2016-09-21 19:57:24 +00:00
|
|
|
(resf = FResourceFile::OpenResourceFile(node->Filename.GetChars(), nullptr, true)) != nullptr)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2016-09-21 19:57:24 +00:00
|
|
|
FResourceLump *info = resf->FindLump("info.json");
|
|
|
|
if (info == nullptr)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2016-09-21 19:57:24 +00:00
|
|
|
// this should not happen because the file has already been verified.
|
2017-02-18 13:08:16 +00:00
|
|
|
return index;
|
2016-09-21 19:57:24 +00:00
|
|
|
}
|
|
|
|
void *data = info->CacheLump();
|
|
|
|
FSerializer arc;
|
2016-09-22 22:45:41 +00:00
|
|
|
if (arc.OpenReader((const char *)data, info->LumpSize))
|
2016-09-21 19:57:24 +00:00
|
|
|
{
|
|
|
|
FString time, pcomment, comment;
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2016-09-21 19:57:24 +00:00
|
|
|
arc("Creation Time", time);
|
|
|
|
arc("Comment", pcomment);
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2016-09-21 19:57:24 +00:00
|
|
|
comment = time;
|
|
|
|
if (time.Len() > 0) comment += "\n\n";
|
|
|
|
comment += pcomment;
|
2017-02-11 16:20:12 +00:00
|
|
|
|
|
|
|
SaveComment = V_BreakLines(SmallFont, WindowSize, comment.GetChars());
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
// Extract pic
|
2016-09-21 19:57:24 +00:00
|
|
|
FResourceLump *pic = resf->FindLump("savepic.png");
|
|
|
|
if (pic != nullptr)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2016-09-21 19:57:24 +00:00
|
|
|
FileReader *reader = pic->NewReader();
|
|
|
|
if (reader != nullptr)
|
|
|
|
{
|
|
|
|
// copy to a memory buffer which gets accessed through a memory reader and PNGHandle.
|
|
|
|
// We cannot use the actual lump as backing for the texture because that requires keeping the
|
|
|
|
// savegame file open.
|
|
|
|
SavePicData.Resize(pic->LumpSize);
|
|
|
|
reader->Read(&SavePicData[0], pic->LumpSize);
|
|
|
|
reader = new MemoryReader(&SavePicData[0], SavePicData.Size());
|
|
|
|
PNGHandle *png = M_VerifyPNG(reader);
|
|
|
|
if (png != nullptr)
|
|
|
|
{
|
|
|
|
SavePic = PNGTexture_CreateFromFile(png, node->Filename);
|
|
|
|
currentSavePic = reader; // must be kept so that the texture can read from it.
|
|
|
|
delete png;
|
|
|
|
if (SavePic->GetWidth() == 1 && SavePic->GetHeight() == 1)
|
|
|
|
{
|
|
|
|
delete SavePic;
|
|
|
|
SavePic = nullptr;
|
|
|
|
delete currentSavePic;
|
|
|
|
currentSavePic = nullptr;
|
|
|
|
SavePicData.Clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-21 19:57:24 +00:00
|
|
|
delete resf;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
2017-02-18 13:08:16 +00:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFINE_ACTION_FUNCTION(FSavegameManager, ExtractSaveData)
|
|
|
|
{
|
|
|
|
PARAM_SELF_STRUCT_PROLOGUE(FSavegameManager);
|
|
|
|
PARAM_INT(sel);
|
|
|
|
ACTION_RETURN_INT(self->ExtractSaveData(sel));
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
2017-02-18 13:08:16 +00:00
|
|
|
void FSavegameManager::UnloadSaveData()
|
2017-02-11 16:20:12 +00:00
|
|
|
{
|
|
|
|
if (SavePic != nullptr)
|
|
|
|
{
|
|
|
|
delete SavePic;
|
|
|
|
}
|
|
|
|
if (SaveComment != nullptr)
|
|
|
|
{
|
|
|
|
V_FreeBrokenLines(SaveComment);
|
|
|
|
}
|
|
|
|
if (currentSavePic != nullptr)
|
|
|
|
{
|
|
|
|
delete currentSavePic;
|
|
|
|
}
|
|
|
|
|
|
|
|
SavePic = nullptr;
|
|
|
|
SaveComment = nullptr;
|
|
|
|
currentSavePic = nullptr;
|
|
|
|
SavePicData.Clear();
|
|
|
|
}
|
|
|
|
|
2017-02-18 15:40:32 +00:00
|
|
|
DEFINE_ACTION_FUNCTION(FSavegameManager, UnloadSaveData)
|
|
|
|
{
|
|
|
|
PARAM_SELF_STRUCT_PROLOGUE(FSavegameManager);
|
|
|
|
self->UnloadSaveData();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-11 16:20:12 +00:00
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
2017-02-18 13:08:16 +00:00
|
|
|
void FSavegameManager::ClearSaveStuff()
|
2017-02-11 16:20:12 +00:00
|
|
|
{
|
|
|
|
UnloadSaveData();
|
|
|
|
if (quickSaveSlot == (FSaveGameNode*)1)
|
|
|
|
{
|
|
|
|
quickSaveSlot = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-18 17:01:24 +00:00
|
|
|
DEFINE_ACTION_FUNCTION(FSavegameManager, ClearSaveStuff)
|
|
|
|
{
|
|
|
|
PARAM_SELF_STRUCT_PROLOGUE(FSavegameManager);
|
|
|
|
self->ClearSaveStuff();
|
|
|
|
return 0;
|
|
|
|
}
|
2017-02-11 16:20:12 +00:00
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
2017-02-18 13:08:16 +00:00
|
|
|
bool FSavegameManager::DrawSavePic(int x, int y, int w, int h)
|
2017-02-11 16:20:12 +00:00
|
|
|
{
|
|
|
|
if (SavePic == nullptr) return false;
|
|
|
|
screen->DrawTexture(SavePic, x, y, DTA_DestWidth, w, DTA_DestHeight, h, DTA_Masked, false, TAG_DONE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-18 16:51:40 +00:00
|
|
|
DEFINE_ACTION_FUNCTION(FSavegameManager, DrawSavePic)
|
|
|
|
{
|
|
|
|
PARAM_SELF_STRUCT_PROLOGUE(FSavegameManager);
|
|
|
|
PARAM_INT(x);
|
|
|
|
PARAM_INT(y);
|
|
|
|
PARAM_INT(w);
|
|
|
|
PARAM_INT(h);
|
|
|
|
ACTION_RETURN_BOOL(self->DrawSavePic(x, y, w, h));
|
|
|
|
}
|
|
|
|
|
2017-02-11 16:20:12 +00:00
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
2017-02-18 13:08:16 +00:00
|
|
|
void FSavegameManager::DrawSaveComment(FFont *font, int cr, int x, int y, int scalefactor)
|
|
|
|
{
|
|
|
|
int sx = CleanXfac;
|
|
|
|
int sy = CleanYfac;
|
|
|
|
|
|
|
|
CleanXfac = CleanYfac = scalefactor;
|
|
|
|
|
|
|
|
// I'm not sure why SaveComment would go nullptr in this loop, but I got
|
|
|
|
// a crash report where it was nullptr when i reached 1, so now I check
|
|
|
|
// for that.
|
|
|
|
for (int i = 0; SaveComment != nullptr && SaveComment[i].Width >= 0 && i < 6; ++i)
|
|
|
|
{
|
|
|
|
screen->DrawText(font, cr, x, y + font->GetHeight() * i * scalefactor, SaveComment[i].Text, DTA_CleanNoMove, true, TAG_DONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
CleanXfac = sx;
|
|
|
|
CleanYfac = sy;
|
|
|
|
}
|
|
|
|
|
2017-02-18 16:51:40 +00:00
|
|
|
DEFINE_ACTION_FUNCTION(FSavegameManager, DrawSaveComment)
|
|
|
|
{
|
|
|
|
PARAM_SELF_STRUCT_PROLOGUE(FSavegameManager);
|
|
|
|
PARAM_POINTER(fnt, FFont);
|
|
|
|
PARAM_INT(cr);
|
|
|
|
PARAM_INT(x);
|
|
|
|
PARAM_INT(y);
|
|
|
|
PARAM_INT(fac);
|
|
|
|
self->DrawSaveComment(fnt, cr, x, y, fac);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-18 13:08:16 +00:00
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
void FSavegameManager::SetFileInfo(int Selected)
|
2017-02-11 16:20:12 +00:00
|
|
|
{
|
|
|
|
if (!SaveGames[Selected]->Filename.IsEmpty())
|
|
|
|
{
|
|
|
|
char workbuf[512];
|
|
|
|
|
|
|
|
mysnprintf(workbuf, countof(workbuf), "File on disk:\n%s", SaveGames[Selected]->Filename.GetChars());
|
|
|
|
if (SaveComment != nullptr)
|
|
|
|
{
|
|
|
|
V_FreeBrokenLines(SaveComment);
|
|
|
|
}
|
|
|
|
SaveComment = V_BreakLines(SmallFont, WindowSize, workbuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-18 15:40:32 +00:00
|
|
|
DEFINE_ACTION_FUNCTION(FSavegameManager, SetFileInfo)
|
|
|
|
{
|
|
|
|
PARAM_SELF_STRUCT_PROLOGUE(FSavegameManager);
|
|
|
|
PARAM_INT(i);
|
|
|
|
self->SetFileInfo(i);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-18 12:05:33 +00:00
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
2017-02-18 13:08:16 +00:00
|
|
|
unsigned FSavegameManager::SavegameCount()
|
2017-02-18 12:05:33 +00:00
|
|
|
{
|
|
|
|
return SaveGames.Size();
|
|
|
|
}
|
|
|
|
|
2017-02-18 13:08:16 +00:00
|
|
|
DEFINE_ACTION_FUNCTION(FSavegameManager, SavegameCount)
|
|
|
|
{
|
|
|
|
PARAM_SELF_STRUCT_PROLOGUE(FSavegameManager);
|
|
|
|
ACTION_RETURN_INT(self->SavegameCount());
|
|
|
|
}
|
|
|
|
|
2017-02-18 12:05:33 +00:00
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
2017-02-18 16:35:01 +00:00
|
|
|
FSaveGameNode *FSavegameManager::GetSavegame(int i)
|
2017-02-18 12:05:33 +00:00
|
|
|
{
|
|
|
|
return SaveGames[i];
|
|
|
|
}
|
|
|
|
|
2017-02-18 15:40:32 +00:00
|
|
|
DEFINE_ACTION_FUNCTION(FSavegameManager, GetSavegame)
|
|
|
|
{
|
|
|
|
PARAM_SELF_STRUCT_PROLOGUE(FSavegameManager);
|
|
|
|
PARAM_INT(i);
|
|
|
|
ACTION_RETURN_POINTER(self->GetSavegame(i));
|
|
|
|
}
|
|
|
|
|
2017-02-18 12:05:33 +00:00
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
2017-02-18 13:08:16 +00:00
|
|
|
void FSavegameManager::InsertNewSaveNode()
|
2017-02-18 12:05:33 +00:00
|
|
|
{
|
|
|
|
NewSaveNode.SaveTitle = GStrings["NEWSAVE"];
|
|
|
|
NewSaveNode.bNoDelete = true;
|
|
|
|
SaveGames.Insert(0, &NewSaveNode);
|
|
|
|
}
|
|
|
|
|
2017-02-18 15:40:32 +00:00
|
|
|
DEFINE_ACTION_FUNCTION(FSavegameManager, InsertNewSaveNode)
|
|
|
|
{
|
|
|
|
PARAM_SELF_STRUCT_PROLOGUE(FSavegameManager);
|
|
|
|
self->InsertNewSaveNode();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-18 12:05:33 +00:00
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
2017-02-18 13:08:16 +00:00
|
|
|
bool FSavegameManager::RemoveNewSaveNode()
|
2017-02-18 12:05:33 +00:00
|
|
|
{
|
|
|
|
if (SaveGames[0] == &NewSaveNode)
|
|
|
|
{
|
|
|
|
SaveGames.Delete(0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-02-18 15:40:32 +00:00
|
|
|
DEFINE_ACTION_FUNCTION(FSavegameManager, RemoveNewSaveNode)
|
|
|
|
{
|
|
|
|
PARAM_SELF_STRUCT_PROLOGUE(FSavegameManager);
|
|
|
|
ACTION_RETURN_INT(self->RemoveNewSaveNode());
|
|
|
|
}
|
2017-02-18 12:05:33 +00:00
|
|
|
|
|
|
|
|
2017-02-18 13:08:16 +00:00
|
|
|
FSavegameManager savegameManager;
|
2017-02-11 16:20:12 +00:00
|
|
|
|
2017-02-18 17:01:24 +00:00
|
|
|
DEFINE_ACTION_FUNCTION(FSavegameManager, GetManager)
|
2017-02-11 16:20:12 +00:00
|
|
|
{
|
2017-02-18 17:01:24 +00:00
|
|
|
PARAM_PROLOGUE;
|
|
|
|
ACTION_RETURN_POINTER(&savegameManager);
|
2017-02-11 16:20:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2017-02-18 13:08:16 +00:00
|
|
|
DEFINE_FIELD(FSaveGameNode, SaveTitle);
|
|
|
|
DEFINE_FIELD(FSaveGameNode, Filename);
|
|
|
|
DEFINE_FIELD(FSaveGameNode, bOldVersion);
|
|
|
|
DEFINE_FIELD(FSaveGameNode, bMissingWads);
|
|
|
|
DEFINE_FIELD(FSaveGameNode, bNoDelete);
|
|
|
|
|
|
|
|
DEFINE_FIELD(FSavegameManager, WindowSize);
|
|
|
|
DEFINE_FIELD(FSavegameManager, quickSaveSlot);
|
|
|
|
|