2021-03-23 13:46:59 +01:00
|
|
|
/* Copyright (C) 2021 Wildfire Games.
|
2011-10-30 02:07:28 +02:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. 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.
|
|
|
|
*
|
|
|
|
* 0 A.D. 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 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
#include "SavedGame.h"
|
|
|
|
|
2014-04-06 18:27:57 +02:00
|
|
|
#include "graphics/GameView.h"
|
2019-04-23 00:12:08 +02:00
|
|
|
#include "i18n/L10n.h"
|
2011-10-30 02:07:28 +02:00
|
|
|
#include "lib/allocators/shared_ptr.h"
|
|
|
|
#include "lib/file/archive/archive_zip.h"
|
2020-12-09 15:39:14 +01:00
|
|
|
#include "lib/file/io/io.h"
|
2014-04-20 22:03:57 +02:00
|
|
|
#include "lib/utf8.h"
|
2019-04-23 00:12:08 +02:00
|
|
|
#include "maths/Vector3D.h"
|
2011-10-30 02:07:28 +02:00
|
|
|
#include "ps/CLogger.h"
|
|
|
|
#include "ps/Filesystem.h"
|
2014-04-06 18:27:57 +02:00
|
|
|
#include "ps/Game.h"
|
2014-08-25 18:02:40 +02:00
|
|
|
#include "ps/Mod.h"
|
2015-08-02 01:03:13 +02:00
|
|
|
#include "ps/Pyrogenesis.h"
|
2011-10-30 02:07:28 +02:00
|
|
|
#include "scriptinterface/ScriptInterface.h"
|
|
|
|
#include "simulation2/Simulation2.h"
|
|
|
|
|
|
|
|
// TODO: we ought to check version numbers when loading files
|
|
|
|
|
2020-11-18 15:39:04 +01:00
|
|
|
Status SavedGames::SavePrefix(const CStrW& prefix, const CStrW& description, CSimulation2& simulation, const ScriptInterface::StructuredClone& guiMetadataClone)
|
2011-10-30 02:07:28 +02:00
|
|
|
{
|
|
|
|
// Determine the filename to save under
|
|
|
|
const VfsPath basenameFormat(L"saves/" + prefix + L"-%04d");
|
|
|
|
const VfsPath filenameFormat = basenameFormat.ChangeExtension(L".0adsave");
|
|
|
|
VfsPath filename;
|
|
|
|
|
|
|
|
// Don't make this a static global like NextNumberedFilename expects, because
|
|
|
|
// that wouldn't work when 'prefix' changes, and because it's not thread-safe
|
|
|
|
size_t nextSaveNumber = 0;
|
|
|
|
vfs::NextNumberedFilename(g_VFS, filenameFormat, nextSaveNumber, filename);
|
|
|
|
|
2016-06-10 21:04:57 +02:00
|
|
|
return Save(filename.Filename().string(), description, simulation, guiMetadataClone);
|
2013-07-21 02:23:57 +02:00
|
|
|
}
|
|
|
|
|
2020-11-18 15:39:04 +01:00
|
|
|
Status SavedGames::Save(const CStrW& name, const CStrW& description, CSimulation2& simulation, const ScriptInterface::StructuredClone& guiMetadataClone)
|
2013-07-21 02:23:57 +02:00
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 19:29:17 +01:00
|
|
|
ScriptRequest rq(simulation.GetScriptInterface());
|
2015-01-24 15:46:52 +01:00
|
|
|
|
2013-07-21 02:23:57 +02:00
|
|
|
// Determine the filename to save under
|
|
|
|
const VfsPath basenameFormat(L"saves/" + name);
|
|
|
|
const VfsPath filename = basenameFormat.ChangeExtension(L".0adsave");
|
|
|
|
|
2011-10-30 02:07:28 +02:00
|
|
|
// ArchiveWriter_Zip can only write to OsPaths, not VfsPaths,
|
|
|
|
// but we'd like to handle saved games via VFS.
|
|
|
|
// To avoid potential confusion from writing with non-VFS then
|
|
|
|
// reading the same file with VFS, we'll just write to a temporary
|
|
|
|
// non-VFS path and then load and save again via VFS,
|
|
|
|
// which is kind of a hack.
|
|
|
|
|
|
|
|
OsPath tempSaveFileRealPath;
|
|
|
|
WARN_RETURN_STATUS_IF_ERR(g_VFS->GetDirectoryRealPath("cache/", tempSaveFileRealPath));
|
|
|
|
tempSaveFileRealPath = tempSaveFileRealPath / "temp.0adsave";
|
|
|
|
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
|
|
|
// Construct the serialized state to be saved
|
|
|
|
|
|
|
|
std::stringstream simStateStream;
|
|
|
|
if (!simulation.SerializeState(simStateStream))
|
|
|
|
WARN_RETURN(ERR::FAIL);
|
|
|
|
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue initAttributes(rq.cx, simulation.GetInitAttributes());
|
|
|
|
JS::RootedValue mods(rq.cx, Mod::GetLoadedModsWithVersions(simulation.GetScriptInterface()));
|
2019-07-22 21:35:14 +02:00
|
|
|
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue metadata(rq.cx);
|
2019-07-22 21:35:14 +02:00
|
|
|
|
2019-09-13 02:56:51 +02:00
|
|
|
ScriptInterface::CreateObject(
|
2020-11-13 14:18:22 +01:00
|
|
|
rq,
|
2019-07-22 21:35:14 +02:00
|
|
|
&metadata,
|
2019-09-13 02:56:51 +02:00
|
|
|
"engine_version", engine_version,
|
2019-07-22 21:35:14 +02:00
|
|
|
"time", static_cast<double>(now),
|
|
|
|
"playerID", g_Game->GetPlayerID(),
|
|
|
|
"mods", mods,
|
|
|
|
"initAttributes", initAttributes);
|
2014-01-04 11:14:53 +01:00
|
|
|
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue guiMetadata(rq.cx);
|
2014-08-01 22:55:16 +02:00
|
|
|
simulation.GetScriptInterface().ReadStructuredClone(guiMetadataClone, &guiMetadata);
|
2014-04-06 18:27:57 +02:00
|
|
|
|
|
|
|
// get some camera data
|
2019-04-23 00:12:08 +02:00
|
|
|
const CVector3D cameraPosition = g_Game->GetView()->GetCameraPosition();
|
|
|
|
const CVector3D cameraRotation = g_Game->GetView()->GetCameraRotation();
|
2019-07-22 21:35:14 +02:00
|
|
|
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue cameraMetadata(rq.cx);
|
2019-07-22 21:35:14 +02:00
|
|
|
|
2019-09-13 02:56:51 +02:00
|
|
|
ScriptInterface::CreateObject(
|
2020-11-13 14:18:22 +01:00
|
|
|
rq,
|
2019-07-22 21:35:14 +02:00
|
|
|
&cameraMetadata,
|
|
|
|
"PosX", cameraPosition.X,
|
|
|
|
"PosY", cameraPosition.Y,
|
|
|
|
"PosZ", cameraPosition.Z,
|
|
|
|
"RotX", cameraRotation.X,
|
|
|
|
"RotY", cameraRotation.Y,
|
|
|
|
"Zoom", g_Game->GetView()->GetCameraZoom());
|
|
|
|
|
2014-07-20 21:45:18 +02:00
|
|
|
simulation.GetScriptInterface().SetProperty(guiMetadata, "camera", cameraMetadata);
|
2016-05-12 13:27:21 +02:00
|
|
|
|
2019-07-22 21:35:14 +02:00
|
|
|
simulation.GetScriptInterface().SetProperty(metadata, "gui", guiMetadata);
|
2014-07-27 00:33:16 +02:00
|
|
|
simulation.GetScriptInterface().SetProperty(metadata, "description", description);
|
2016-05-12 13:27:21 +02:00
|
|
|
|
2014-08-03 00:21:50 +02:00
|
|
|
std::string metadataString = simulation.GetScriptInterface().StringifyJSON(&metadata, true);
|
2016-05-12 13:27:21 +02:00
|
|
|
|
2011-10-30 02:07:28 +02:00
|
|
|
// Write the saved game as zip file containing the various components
|
2011-12-22 15:04:32 +01:00
|
|
|
PIArchiveWriter archiveWriter = CreateArchiveWriter_Zip(tempSaveFileRealPath, false);
|
|
|
|
if (!archiveWriter)
|
|
|
|
WARN_RETURN(ERR::FAIL);
|
2011-10-30 02:07:28 +02:00
|
|
|
|
|
|
|
WARN_RETURN_STATUS_IF_ERR(archiveWriter->AddMemory((const u8*)metadataString.c_str(), metadataString.length(), now, "metadata.json"));
|
|
|
|
WARN_RETURN_STATUS_IF_ERR(archiveWriter->AddMemory((const u8*)simStateStream.str().c_str(), simStateStream.str().length(), now, "simulation.dat"));
|
|
|
|
archiveWriter.reset(); // close the file
|
|
|
|
|
|
|
|
WriteBuffer buffer;
|
2013-09-10 16:17:04 +02:00
|
|
|
CFileInfo tempSaveFile;
|
2011-10-30 02:07:28 +02:00
|
|
|
WARN_RETURN_STATUS_IF_ERR(GetFileInfo(tempSaveFileRealPath, &tempSaveFile));
|
|
|
|
buffer.Reserve(tempSaveFile.Size());
|
|
|
|
WARN_RETURN_STATUS_IF_ERR(io::Load(tempSaveFileRealPath, buffer.Data().get(), buffer.Size()));
|
|
|
|
WARN_RETURN_STATUS_IF_ERR(g_VFS->CreateFile(filename, buffer.Data(), buffer.Size()));
|
|
|
|
|
|
|
|
OsPath realPath;
|
|
|
|
WARN_RETURN_STATUS_IF_ERR(g_VFS->GetRealPath(filename, realPath));
|
2015-01-22 21:36:24 +01:00
|
|
|
LOGMESSAGERENDER(g_L10n.Translate("Saved game to '%s'"), realPath.string8());
|
2016-06-10 15:03:13 +02:00
|
|
|
debug_printf("Saved game to '%s'\n", realPath.string8().c_str());
|
2011-10-30 02:07:28 +02:00
|
|
|
|
|
|
|
return INFO::OK;
|
|
|
|
}
|
|
|
|
|
2012-03-04 06:27:17 +01:00
|
|
|
/**
|
|
|
|
* Helper class for retrieving data from saved game archives
|
|
|
|
*/
|
2011-10-30 02:07:28 +02:00
|
|
|
class CGameLoader
|
|
|
|
{
|
|
|
|
NONCOPYABLE(CGameLoader);
|
|
|
|
public:
|
2016-05-12 13:27:21 +02:00
|
|
|
|
2014-07-31 16:44:51 +02:00
|
|
|
/**
|
|
|
|
* @param scriptInterface the ScriptInterface used for loading metadata.
|
|
|
|
* @param[out] savedState serialized simulation state stored as string of bytes,
|
|
|
|
* loaded from simulation.dat inside the archive.
|
|
|
|
*
|
|
|
|
* Note: We use a different approach for returning the string and the metadata JS::Value.
|
2016-05-12 13:27:21 +02:00
|
|
|
* We use a pointer for the string to avoid copies (efficiency). We don't use this approach
|
|
|
|
* for the metadata because it would be error prone with rooting and the stack-based rooting
|
2014-07-31 16:44:51 +02:00
|
|
|
* types and confusing (a chain of pointers pointing to other pointers).
|
|
|
|
*/
|
2017-08-24 02:32:42 +02:00
|
|
|
CGameLoader(const ScriptInterface& scriptInterface, std::string* savedState) :
|
2015-01-24 15:46:52 +01:00
|
|
|
m_ScriptInterface(scriptInterface),
|
2020-11-18 15:39:04 +01:00
|
|
|
m_Metadata(scriptInterface.GetGeneralJSContext()),
|
2015-01-24 15:46:52 +01:00
|
|
|
m_SavedState(savedState)
|
2011-10-30 02:07:28 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-09-10 16:17:04 +02:00
|
|
|
static void ReadEntryCallback(const VfsPath& pathname, const CFileInfo& fileInfo, PIArchiveFile archiveFile, uintptr_t cbData)
|
2011-10-30 02:07:28 +02:00
|
|
|
{
|
|
|
|
((CGameLoader*)cbData)->ReadEntry(pathname, fileInfo, archiveFile);
|
|
|
|
}
|
|
|
|
|
2013-09-10 16:17:04 +02:00
|
|
|
void ReadEntry(const VfsPath& pathname, const CFileInfo& fileInfo, PIArchiveFile archiveFile)
|
2011-10-30 02:07:28 +02:00
|
|
|
{
|
2014-07-31 16:44:51 +02:00
|
|
|
if (pathname == L"metadata.json")
|
2011-10-30 02:07:28 +02:00
|
|
|
{
|
|
|
|
std::string buffer;
|
|
|
|
buffer.resize(fileInfo.Size());
|
|
|
|
WARN_IF_ERR(archiveFile->Load("", DummySharedPtr((u8*)buffer.data()), buffer.size()));
|
2015-01-24 15:46:52 +01:00
|
|
|
m_ScriptInterface.ParseJSON(buffer, &m_Metadata);
|
2011-10-30 02:07:28 +02:00
|
|
|
}
|
|
|
|
else if (pathname == L"simulation.dat" && m_SavedState)
|
|
|
|
{
|
|
|
|
m_SavedState->resize(fileInfo.Size());
|
|
|
|
WARN_IF_ERR(archiveFile->Load("", DummySharedPtr((u8*)m_SavedState->data()), m_SavedState->size()));
|
|
|
|
}
|
|
|
|
}
|
2016-05-12 13:27:21 +02:00
|
|
|
|
2014-07-31 16:44:51 +02:00
|
|
|
JS::Value GetMetadata()
|
|
|
|
{
|
|
|
|
return m_Metadata.get();
|
|
|
|
}
|
2016-05-12 13:27:21 +02:00
|
|
|
|
2014-07-31 16:44:51 +02:00
|
|
|
private:
|
2011-10-30 02:07:28 +02:00
|
|
|
|
2017-08-24 02:32:42 +02:00
|
|
|
const ScriptInterface& m_ScriptInterface;
|
2015-01-24 15:46:52 +01:00
|
|
|
JS::PersistentRooted<JS::Value> m_Metadata;
|
2011-10-30 02:07:28 +02:00
|
|
|
std::string* m_SavedState;
|
|
|
|
};
|
|
|
|
|
2017-08-24 02:32:42 +02:00
|
|
|
Status SavedGames::Load(const std::wstring& name, const ScriptInterface& scriptInterface, JS::MutableHandleValue metadata, std::string& savedState)
|
2011-10-30 02:07:28 +02:00
|
|
|
{
|
|
|
|
// Determine the filename to load
|
|
|
|
const VfsPath basename(L"saves/" + name);
|
|
|
|
const VfsPath filename = basename.ChangeExtension(L".0adsave");
|
|
|
|
|
2012-03-14 01:23:28 +01:00
|
|
|
// Don't crash just because file isn't found, this can happen if the file is deleted from the OS
|
|
|
|
if (!VfsFileExists(filename))
|
|
|
|
return ERR::FILE_NOT_FOUND;
|
|
|
|
|
2011-10-30 02:07:28 +02:00
|
|
|
OsPath realPath;
|
|
|
|
WARN_RETURN_STATUS_IF_ERR(g_VFS->GetRealPath(filename, realPath));
|
|
|
|
|
2011-12-22 15:04:32 +01:00
|
|
|
PIArchiveReader archiveReader = CreateArchiveReader_Zip(realPath);
|
|
|
|
if (!archiveReader)
|
|
|
|
WARN_RETURN(ERR::FAIL);
|
2011-10-30 02:07:28 +02:00
|
|
|
|
2014-07-31 16:44:51 +02:00
|
|
|
CGameLoader loader(scriptInterface, &savedState);
|
2011-10-30 02:07:28 +02:00
|
|
|
WARN_RETURN_STATUS_IF_ERR(archiveReader->ReadEntries(CGameLoader::ReadEntryCallback, (uintptr_t)&loader));
|
2014-07-31 16:44:51 +02:00
|
|
|
metadata.set(loader.GetMetadata());
|
2011-10-30 02:07:28 +02:00
|
|
|
|
2016-05-12 13:27:21 +02:00
|
|
|
return INFO::OK;
|
2011-10-30 02:07:28 +02:00
|
|
|
}
|
|
|
|
|
2017-08-24 02:32:42 +02:00
|
|
|
JS::Value SavedGames::GetSavedGames(const ScriptInterface& scriptInterface)
|
2011-10-30 02:07:28 +02:00
|
|
|
{
|
|
|
|
TIMER(L"GetSavedGames");
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 19:29:17 +01:00
|
|
|
ScriptRequest rq(scriptInterface);
|
2016-05-12 13:27:21 +02:00
|
|
|
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue games(rq.cx);
|
|
|
|
ScriptInterface::CreateArray(rq, &games);
|
2011-10-30 02:07:28 +02:00
|
|
|
|
|
|
|
Status err;
|
|
|
|
|
|
|
|
VfsPaths pathnames;
|
|
|
|
err = vfs::GetPathnames(g_VFS, "saves/", L"*.0adsave", pathnames);
|
|
|
|
WARN_IF_ERR(err);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < pathnames.size(); ++i)
|
|
|
|
{
|
|
|
|
OsPath realPath;
|
|
|
|
err = g_VFS->GetRealPath(pathnames[i], realPath);
|
|
|
|
if (err < 0)
|
|
|
|
{
|
|
|
|
DEBUG_WARN_ERR(err);
|
|
|
|
continue; // skip this file
|
|
|
|
}
|
|
|
|
|
2011-12-22 15:04:32 +01:00
|
|
|
PIArchiveReader archiveReader = CreateArchiveReader_Zip(realPath);
|
|
|
|
if (!archiveReader)
|
2011-10-30 02:07:28 +02:00
|
|
|
{
|
2011-12-22 15:04:32 +01:00
|
|
|
// Triggered by e.g. the file being open in another program
|
2015-01-22 21:36:24 +01:00
|
|
|
LOGWARNING("Failed to read saved game '%s'", realPath.string8());
|
2011-10-30 02:07:28 +02:00
|
|
|
continue; // skip this file
|
|
|
|
}
|
|
|
|
|
2014-07-31 16:44:51 +02:00
|
|
|
CGameLoader loader(scriptInterface, NULL);
|
2011-10-30 02:07:28 +02:00
|
|
|
err = archiveReader->ReadEntries(CGameLoader::ReadEntryCallback, (uintptr_t)&loader);
|
|
|
|
if (err < 0)
|
|
|
|
{
|
|
|
|
DEBUG_WARN_ERR(err);
|
|
|
|
continue; // skip this file
|
|
|
|
}
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue metadata(rq.cx, loader.GetMetadata());
|
2016-05-12 13:27:21 +02:00
|
|
|
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue game(rq.cx);
|
2019-09-13 02:56:51 +02:00
|
|
|
ScriptInterface::CreateObject(
|
2020-11-13 14:18:22 +01:00
|
|
|
rq,
|
2019-07-22 21:35:14 +02:00
|
|
|
&game,
|
|
|
|
"id", pathnames[i].Basename(),
|
|
|
|
"metadata", metadata);
|
|
|
|
|
|
|
|
scriptInterface.SetPropertyInt(games, i, game);
|
2011-10-30 02:07:28 +02:00
|
|
|
}
|
|
|
|
|
2019-07-22 21:35:14 +02:00
|
|
|
return games;
|
2012-01-31 01:06:56 +01:00
|
|
|
}
|
2012-03-04 06:27:17 +01:00
|
|
|
|
|
|
|
bool SavedGames::DeleteSavedGame(const std::wstring& name)
|
|
|
|
{
|
|
|
|
const VfsPath basename(L"saves/" + name);
|
|
|
|
const VfsPath filename = basename.ChangeExtension(L".0adsave");
|
|
|
|
OsPath realpath;
|
|
|
|
|
2021-03-23 13:46:59 +01:00
|
|
|
// Make sure it exists in VFS and find its path
|
|
|
|
if (!VfsFileExists(filename) || g_VFS->GetOriginalPath(filename, realpath) != INFO::OK)
|
2012-03-04 06:27:17 +01:00
|
|
|
return false; // Error
|
|
|
|
|
|
|
|
// Remove from VFS
|
|
|
|
if (g_VFS->RemoveFile(filename) != INFO::OK)
|
|
|
|
return false; // Error
|
|
|
|
|
|
|
|
// Delete actual file
|
|
|
|
if (wunlink(realpath) != 0)
|
|
|
|
return false; // Error
|
|
|
|
|
|
|
|
// Successfully deleted file
|
|
|
|
return true;
|
|
|
|
}
|