2026-02-02 09:24:57 -08:00
|
|
|
/* Copyright (C) 2026 Wildfire Games.
|
2023-12-02 16:30:12 -08:00
|
|
|
* This file is part of 0 A.D.
|
2011-10-29 17:07:28 -07:00
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2011-10-29 17:07:28 -07:00
|
|
|
* 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.
|
|
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2011-10-29 17:07:28 -07:00
|
|
|
* 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
|
2023-12-02 16:30:12 -08:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2011-10-29 17:07:28 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
|
|
#include "SavedGame.h"
|
|
|
|
|
|
2014-04-06 09:27:57 -07:00
|
|
|
#include "graphics/GameView.h"
|
2011-10-29 17:07:28 -07:00
|
|
|
#include "lib/allocators/shared_ptr.h"
|
2025-07-23 10:19:42 -07:00
|
|
|
#include "lib/code_annotation.h"
|
|
|
|
|
#include "lib/debug.h"
|
|
|
|
|
#include "lib/file/archive/archive.h"
|
2011-10-29 17:07:28 -07:00
|
|
|
#include "lib/file/archive/archive_zip.h"
|
2025-07-23 10:19:42 -07:00
|
|
|
#include "lib/file/file_system.h"
|
2020-12-09 06:39:14 -08:00
|
|
|
#include "lib/file/io/io.h"
|
2025-07-23 10:19:42 -07:00
|
|
|
#include "lib/file/io/write_buffer.h"
|
|
|
|
|
#include "lib/file/vfs/vfs.h"
|
|
|
|
|
#include "lib/file/vfs/vfs_path.h"
|
|
|
|
|
#include "lib/file/vfs/vfs_util.h"
|
|
|
|
|
#include "lib/path.h"
|
|
|
|
|
#include "lib/sysdep/filesystem.h"
|
|
|
|
|
#include "lib/types.h"
|
2019-04-22 15:12:08 -07:00
|
|
|
#include "maths/Vector3D.h"
|
2011-10-29 17:07:28 -07:00
|
|
|
#include "ps/CLogger.h"
|
2025-08-03 08:30:17 -07:00
|
|
|
#include "ps/CStr.h"
|
2011-10-29 17:07:28 -07:00
|
|
|
#include "ps/Filesystem.h"
|
2014-04-06 09:27:57 -07:00
|
|
|
#include "ps/Game.h"
|
2014-08-25 09:02:40 -07:00
|
|
|
#include "ps/Mod.h"
|
2025-07-13 05:21:48 -07:00
|
|
|
#include "ps/Profiler2.h"
|
2015-08-01 16:03:13 -07:00
|
|
|
#include "ps/Pyrogenesis.h"
|
2021-05-14 03:18:03 -07:00
|
|
|
#include "scriptinterface/JSON.h"
|
2025-07-23 10:19:42 -07:00
|
|
|
#include "scriptinterface/Object.h"
|
|
|
|
|
#include "scriptinterface/ScriptConversions.h"
|
|
|
|
|
#include "scriptinterface/ScriptRequest.h"
|
2021-05-10 04:51:32 -07:00
|
|
|
#include "scriptinterface/StructuredClone.h"
|
2011-10-29 17:07:28 -07:00
|
|
|
#include "simulation2/Simulation2.h"
|
|
|
|
|
|
2025-07-23 10:19:42 -07:00
|
|
|
#include <cstdint>
|
|
|
|
|
#include <ctime>
|
|
|
|
|
#include <js/RootingAPI.h>
|
|
|
|
|
#include <js/TypeDecls.h>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
|
|
class ScriptInterface;
|
|
|
|
|
|
2011-10-29 17:07:28 -07:00
|
|
|
// TODO: we ought to check version numbers when loading files
|
|
|
|
|
|
2021-05-10 04:51:32 -07:00
|
|
|
Status SavedGames::SavePrefix(const CStrW& prefix, const CStrW& description, CSimulation2& simulation, const Script::StructuredClone& guiMetadataClone)
|
2011-10-29 17:07:28 -07: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 12:04:57 -07:00
|
|
|
return Save(filename.Filename().string(), description, simulation, guiMetadataClone);
|
2013-07-20 17:23:57 -07:00
|
|
|
}
|
|
|
|
|
|
2021-05-10 04:51:32 -07:00
|
|
|
Status SavedGames::Save(const CStrW& name, const CStrW& description, CSimulation2& simulation, const Script::StructuredClone& guiMetadataClone)
|
2013-07-20 17:23:57 -07: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 10:29:17 -08:00
|
|
|
ScriptRequest rq(simulation.GetScriptInterface());
|
2015-01-24 06:46:52 -08:00
|
|
|
|
2013-07-20 17:23:57 -07:00
|
|
|
// Determine the filename to save under
|
|
|
|
|
const VfsPath basenameFormat(L"saves/" + name);
|
|
|
|
|
const VfsPath filename = basenameFormat.ChangeExtension(L".0adsave");
|
|
|
|
|
|
2011-10-29 17:07:28 -07: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 05:18:22 -08:00
|
|
|
JS::RootedValue initAttributes(rq.cx, simulation.GetInitAttributes());
|
2021-06-01 23:50:16 -07:00
|
|
|
JS::RootedValue mods(rq.cx);
|
|
|
|
|
Script::ToJSVal(rq, &mods, g_Mods.GetEnabledModsData());
|
2019-07-22 12:35:14 -07:00
|
|
|
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue metadata(rq.cx);
|
2019-07-22 12:35:14 -07:00
|
|
|
|
2021-05-13 10:23:52 -07:00
|
|
|
Script::CreateObject(
|
2020-11-13 05:18:22 -08:00
|
|
|
rq,
|
2019-07-22 12:35:14 -07:00
|
|
|
&metadata,
|
2025-10-20 04:54:34 -07:00
|
|
|
"engine_version", PS_VERSION,
|
|
|
|
|
"engine_serialization_version", PS_SERIALIZATION_VERSION,
|
2019-07-22 12:35:14 -07:00
|
|
|
"time", static_cast<double>(now),
|
|
|
|
|
"playerID", g_Game->GetPlayerID(),
|
|
|
|
|
"mods", mods,
|
|
|
|
|
"initAttributes", initAttributes);
|
2014-01-04 02:14:53 -08:00
|
|
|
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue guiMetadata(rq.cx);
|
2021-05-10 04:51:32 -07:00
|
|
|
Script::ReadStructuredClone(rq, guiMetadataClone, &guiMetadata);
|
2014-04-06 09:27:57 -07:00
|
|
|
|
|
|
|
|
// get some camera data
|
2019-04-22 15:12:08 -07:00
|
|
|
const CVector3D cameraPosition = g_Game->GetView()->GetCameraPosition();
|
|
|
|
|
const CVector3D cameraRotation = g_Game->GetView()->GetCameraRotation();
|
2019-07-22 12:35:14 -07:00
|
|
|
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue cameraMetadata(rq.cx);
|
2019-07-22 12:35:14 -07:00
|
|
|
|
2021-05-13 10:23:52 -07:00
|
|
|
Script::CreateObject(
|
2020-11-13 05:18:22 -08:00
|
|
|
rq,
|
2019-07-22 12:35:14 -07:00
|
|
|
&cameraMetadata,
|
|
|
|
|
"PosX", cameraPosition.X,
|
|
|
|
|
"PosY", cameraPosition.Y,
|
|
|
|
|
"PosZ", cameraPosition.Z,
|
|
|
|
|
"RotX", cameraRotation.X,
|
|
|
|
|
"RotY", cameraRotation.Y,
|
|
|
|
|
"Zoom", g_Game->GetView()->GetCameraZoom());
|
|
|
|
|
|
2021-05-13 10:23:52 -07:00
|
|
|
Script::SetProperty(rq, guiMetadata, "camera", cameraMetadata);
|
2016-05-12 04:27:21 -07:00
|
|
|
|
2021-05-13 10:23:52 -07:00
|
|
|
Script::SetProperty(rq, metadata, "gui", guiMetadata);
|
|
|
|
|
Script::SetProperty(rq, metadata, "description", description);
|
2016-05-12 04:27:21 -07:00
|
|
|
|
2021-05-14 03:18:03 -07:00
|
|
|
std::string metadataString = Script::StringifyJSON(rq, &metadata, true);
|
2016-05-12 04:27:21 -07:00
|
|
|
|
2011-10-29 17:07:28 -07:00
|
|
|
// Write the saved game as zip file containing the various components
|
2011-12-22 06:04:32 -08:00
|
|
|
PIArchiveWriter archiveWriter = CreateArchiveWriter_Zip(tempSaveFileRealPath, false);
|
|
|
|
|
if (!archiveWriter)
|
|
|
|
|
WARN_RETURN(ERR::FAIL);
|
2011-10-29 17:07:28 -07: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 07:17:04 -07:00
|
|
|
CFileInfo tempSaveFile;
|
2011-10-29 17:07:28 -07: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));
|
2024-08-05 08:27:12 -07:00
|
|
|
LOGMESSAGERENDER("Saved game to '%s'", realPath.string8());
|
2016-06-10 06:03:13 -07:00
|
|
|
debug_printf("Saved game to '%s'\n", realPath.string8().c_str());
|
2011-10-29 17:07:28 -07:00
|
|
|
|
|
|
|
|
return INFO::OK;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-03 21:27:17 -08:00
|
|
|
/**
|
|
|
|
|
* Helper class for retrieving data from saved game archives
|
|
|
|
|
*/
|
2011-10-29 17:07:28 -07:00
|
|
|
class CGameLoader
|
|
|
|
|
{
|
|
|
|
|
NONCOPYABLE(CGameLoader);
|
|
|
|
|
public:
|
2016-05-12 04:27:21 -07:00
|
|
|
|
2014-07-31 07:44:51 -07: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 04:27:21 -07: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 07:44:51 -07:00
|
|
|
* types and confusing (a chain of pointers pointing to other pointers).
|
|
|
|
|
*/
|
2017-08-23 17:32:42 -07:00
|
|
|
CGameLoader(const ScriptInterface& scriptInterface, std::string* savedState) :
|
2015-01-24 06:46:52 -08:00
|
|
|
m_ScriptInterface(scriptInterface),
|
|
|
|
|
m_SavedState(savedState)
|
2011-10-29 17:07:28 -07:00
|
|
|
{
|
2021-05-14 03:18:03 -07:00
|
|
|
ScriptRequest rq(scriptInterface);
|
|
|
|
|
m_Metadata.init(rq.cx);
|
2011-10-29 17:07:28 -07:00
|
|
|
}
|
|
|
|
|
|
2013-09-10 07:17:04 -07:00
|
|
|
static void ReadEntryCallback(const VfsPath& pathname, const CFileInfo& fileInfo, PIArchiveFile archiveFile, uintptr_t cbData)
|
2011-10-29 17:07:28 -07:00
|
|
|
{
|
|
|
|
|
((CGameLoader*)cbData)->ReadEntry(pathname, fileInfo, archiveFile);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-10 07:17:04 -07:00
|
|
|
void ReadEntry(const VfsPath& pathname, const CFileInfo& fileInfo, PIArchiveFile archiveFile)
|
2011-10-29 17:07:28 -07:00
|
|
|
{
|
2014-07-31 07:44:51 -07:00
|
|
|
if (pathname == L"metadata.json")
|
2011-10-29 17:07:28 -07:00
|
|
|
{
|
|
|
|
|
std::string buffer;
|
|
|
|
|
buffer.resize(fileInfo.Size());
|
|
|
|
|
WARN_IF_ERR(archiveFile->Load("", DummySharedPtr((u8*)buffer.data()), buffer.size()));
|
2021-05-14 03:18:03 -07:00
|
|
|
Script::ParseJSON(ScriptRequest(m_ScriptInterface), buffer, &m_Metadata);
|
2011-10-29 17:07:28 -07: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 04:27:21 -07:00
|
|
|
|
2014-07-31 07:44:51 -07:00
|
|
|
JS::Value GetMetadata()
|
|
|
|
|
{
|
|
|
|
|
return m_Metadata.get();
|
|
|
|
|
}
|
2016-05-12 04:27:21 -07:00
|
|
|
|
2014-07-31 07:44:51 -07:00
|
|
|
private:
|
2011-10-29 17:07:28 -07:00
|
|
|
|
2017-08-23 17:32:42 -07:00
|
|
|
const ScriptInterface& m_ScriptInterface;
|
2015-01-24 06:46:52 -08:00
|
|
|
JS::PersistentRooted<JS::Value> m_Metadata;
|
2011-10-29 17:07:28 -07:00
|
|
|
std::string* m_SavedState;
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-31 01:54:56 -07:00
|
|
|
std::optional<SavedGames::LoadResult> SavedGames::Load(const ScriptInterface& scriptInterface,
|
|
|
|
|
const std::wstring& name)
|
2011-10-29 17:07:28 -07:00
|
|
|
{
|
|
|
|
|
// Determine the filename to load
|
|
|
|
|
const VfsPath basename(L"saves/" + name);
|
|
|
|
|
const VfsPath filename = basename.ChangeExtension(L".0adsave");
|
|
|
|
|
|
2012-03-13 17:23:28 -07:00
|
|
|
// Don't crash just because file isn't found, this can happen if the file is deleted from the OS
|
|
|
|
|
if (!VfsFileExists(filename))
|
2024-08-31 01:54:56 -07:00
|
|
|
return std::nullopt;
|
2012-03-13 17:23:28 -07:00
|
|
|
|
2011-10-29 17:07:28 -07:00
|
|
|
OsPath realPath;
|
2024-08-31 01:54:56 -07:00
|
|
|
{
|
|
|
|
|
const Status status{g_VFS->GetRealPath(filename, realPath)};
|
|
|
|
|
if (status < 0)
|
|
|
|
|
{
|
|
|
|
|
DEBUG_WARN_ERR(status);
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-10-29 17:07:28 -07:00
|
|
|
|
2011-12-22 06:04:32 -08:00
|
|
|
PIArchiveReader archiveReader = CreateArchiveReader_Zip(realPath);
|
|
|
|
|
if (!archiveReader)
|
2024-08-31 01:54:56 -07:00
|
|
|
{
|
|
|
|
|
DEBUG_WARN_ERR(ERR::FAIL);
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
2011-10-29 17:07:28 -07:00
|
|
|
|
2024-08-31 01:54:56 -07:00
|
|
|
std::string savedState;
|
2014-07-31 07:44:51 -07:00
|
|
|
CGameLoader loader(scriptInterface, &savedState);
|
2024-08-31 01:54:56 -07:00
|
|
|
{
|
|
|
|
|
const Status status{archiveReader->ReadEntries(CGameLoader::ReadEntryCallback,
|
|
|
|
|
reinterpret_cast<uintptr_t>(&loader))};
|
|
|
|
|
if (status < 0)
|
|
|
|
|
{
|
|
|
|
|
DEBUG_WARN_ERR(status);
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const ScriptRequest rq{scriptInterface};
|
|
|
|
|
JS::RootedValue metadata{rq.cx, loader.GetMetadata()};
|
2011-10-29 17:07:28 -07:00
|
|
|
|
2024-08-31 01:54:56 -07:00
|
|
|
// `std::make_optional` can't be used since `LoadResult` doesn't have a constructor.
|
|
|
|
|
return {{metadata, std::move(savedState)}};
|
2011-10-29 17:07:28 -07:00
|
|
|
}
|
|
|
|
|
|
2017-08-23 17:32:42 -07:00
|
|
|
JS::Value SavedGames::GetSavedGames(const ScriptInterface& scriptInterface)
|
2011-10-29 17:07:28 -07:00
|
|
|
{
|
2025-07-13 05:21:48 -07:00
|
|
|
PROFILE2("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 10:29:17 -08:00
|
|
|
ScriptRequest rq(scriptInterface);
|
2016-05-12 04:27:21 -07:00
|
|
|
|
2026-02-02 09:24:57 -08:00
|
|
|
JS::RootedValueVector games{rq.cx};
|
2011-10-29 17:07:28 -07:00
|
|
|
|
|
|
|
|
Status err;
|
|
|
|
|
|
|
|
|
|
VfsPaths pathnames;
|
|
|
|
|
err = vfs::GetPathnames(g_VFS, "saves/", L"*.0adsave", pathnames);
|
|
|
|
|
WARN_IF_ERR(err);
|
|
|
|
|
|
2026-02-02 09:24:57 -08:00
|
|
|
for (const VfsPath& pathname : pathnames)
|
2011-10-29 17:07:28 -07:00
|
|
|
{
|
|
|
|
|
OsPath realPath;
|
2026-02-02 09:24:57 -08:00
|
|
|
err = g_VFS->GetRealPath(pathname, realPath);
|
2011-10-29 17:07:28 -07:00
|
|
|
if (err < 0)
|
|
|
|
|
{
|
|
|
|
|
DEBUG_WARN_ERR(err);
|
|
|
|
|
continue; // skip this file
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-22 06:04:32 -08:00
|
|
|
PIArchiveReader archiveReader = CreateArchiveReader_Zip(realPath);
|
|
|
|
|
if (!archiveReader)
|
2011-10-29 17:07:28 -07:00
|
|
|
{
|
2011-12-22 06:04:32 -08:00
|
|
|
// Triggered by e.g. the file being open in another program
|
2015-01-22 12:36:24 -08:00
|
|
|
LOGWARNING("Failed to read saved game '%s'", realPath.string8());
|
2011-10-29 17:07:28 -07:00
|
|
|
continue; // skip this file
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-31 07:44:51 -07:00
|
|
|
CGameLoader loader(scriptInterface, NULL);
|
2011-10-29 17:07:28 -07:00
|
|
|
err = archiveReader->ReadEntries(CGameLoader::ReadEntryCallback, (uintptr_t)&loader);
|
|
|
|
|
if (err < 0)
|
|
|
|
|
{
|
|
|
|
|
DEBUG_WARN_ERR(err);
|
|
|
|
|
continue; // skip this file
|
|
|
|
|
}
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue metadata(rq.cx, loader.GetMetadata());
|
2016-05-12 04:27:21 -07:00
|
|
|
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue game(rq.cx);
|
2021-05-13 10:23:52 -07:00
|
|
|
Script::CreateObject(
|
2020-11-13 05:18:22 -08:00
|
|
|
rq,
|
2019-07-22 12:35:14 -07:00
|
|
|
&game,
|
2026-02-02 09:24:57 -08:00
|
|
|
"id", pathname.Basename(),
|
2019-07-22 12:35:14 -07:00
|
|
|
"metadata", metadata);
|
|
|
|
|
|
2026-02-02 09:24:57 -08:00
|
|
|
if (!games.append(game))
|
|
|
|
|
throw std::runtime_error{"Append failed"};
|
2011-10-29 17:07:28 -07:00
|
|
|
}
|
|
|
|
|
|
2026-02-02 09:24:57 -08:00
|
|
|
return JS::ObjectValue(*JS::NewArrayObject(rq.cx, games));
|
2012-01-30 16:06:56 -08:00
|
|
|
}
|
2012-03-03 21:27:17 -08: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 05:46:59 -07:00
|
|
|
// Make sure it exists in VFS and find its path
|
|
|
|
|
if (!VfsFileExists(filename) || g_VFS->GetOriginalPath(filename, realpath) != INFO::OK)
|
2012-03-03 21:27:17 -08: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;
|
|
|
|
|
}
|