2020-02-18 05:19:52 +00:00
|
|
|
// Copyright 2020 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <chrono>
|
2023-08-01 01:57:38 +01:00
|
|
|
#include <sstream>
|
2020-02-18 05:19:52 +00:00
|
|
|
#include <cryptopp/hex.h>
|
2023-06-24 09:46:33 +01:00
|
|
|
#include <fmt/format.h>
|
2020-02-18 05:19:52 +00:00
|
|
|
#include "common/archives.h"
|
2023-08-01 01:57:38 +01:00
|
|
|
#include "common/file_util.h"
|
2020-02-18 05:19:52 +00:00
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "common/scm_rev.h"
|
2023-08-01 01:57:38 +01:00
|
|
|
#include "common/swap.h"
|
2020-02-18 05:19:52 +00:00
|
|
|
#include "common/zstd_compression.h"
|
|
|
|
#include "core/core.h"
|
2020-06-29 15:32:24 +01:00
|
|
|
#include "core/movie.h"
|
2020-02-18 05:19:52 +00:00
|
|
|
#include "core/savestate.h"
|
2023-08-11 04:55:22 +01:00
|
|
|
#include "core/savestate_data.h"
|
2020-04-11 20:33:38 +01:00
|
|
|
#include "network/network.h"
|
2020-02-18 05:19:52 +00:00
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
#pragma pack(push, 1)
|
|
|
|
struct CSTHeader {
|
2023-08-11 04:55:22 +01:00
|
|
|
std::array<u8, 4> filetype; /// Unique Identifier to check the file type (always "CST"0x1B)
|
|
|
|
u64_le program_id; /// ID of the ROM being executed. Also called title_id
|
|
|
|
std::array<u8, 20> revision; /// Git hash of the revision this savestate was created with
|
|
|
|
u64_le time; /// The time when this save state was created
|
|
|
|
std::array<u8, 20> build_name; /// The build name (Canary/Nightly) with the version number
|
|
|
|
u32_le zero = 0; /// Should be zero, just in case.
|
|
|
|
|
|
|
|
std::array<u8, 192> reserved{}; /// Make heading 256 bytes so it has consistent size
|
2020-02-18 05:19:52 +00:00
|
|
|
};
|
|
|
|
static_assert(sizeof(CSTHeader) == 256, "CSTHeader should be 256 bytes");
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
constexpr std::array<u8, 4> header_magic_bytes{{'C', 'S', 'T', 0x1B}};
|
|
|
|
|
2023-08-01 01:57:38 +01:00
|
|
|
static std::string GetSaveStatePath(u64 program_id, u64 movie_id, u32 slot) {
|
2020-06-29 15:32:24 +01:00
|
|
|
if (movie_id) {
|
|
|
|
return fmt::format("{}{:016X}.movie{:016X}.{:02d}.cst",
|
|
|
|
FileUtil::GetUserPath(FileUtil::UserPath::StatesDir), program_id,
|
|
|
|
movie_id, slot);
|
|
|
|
} else {
|
|
|
|
return fmt::format("{}{:016X}.{:02d}.cst",
|
|
|
|
FileUtil::GetUserPath(FileUtil::UserPath::StatesDir), program_id, slot);
|
|
|
|
}
|
2020-02-18 05:19:52 +00:00
|
|
|
}
|
|
|
|
|
2023-04-05 19:28:56 +01:00
|
|
|
static bool ValidateSaveState(const CSTHeader& header, SaveStateInfo& info, u64 program_id,
|
2023-08-01 01:57:38 +01:00
|
|
|
u64 movie_id, u32 slot) {
|
|
|
|
const auto path = GetSaveStatePath(program_id, movie_id, slot);
|
2023-04-05 19:28:56 +01:00
|
|
|
if (header.filetype != header_magic_bytes) {
|
|
|
|
LOG_WARNING(Core, "Invalid save state file {}", path);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
info.time = header.time;
|
|
|
|
|
|
|
|
if (header.program_id != program_id) {
|
|
|
|
LOG_WARNING(Core, "Save state file isn't for the current game {}", path);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const std::string revision = fmt::format("{:02x}", fmt::join(header.revision, ""));
|
2023-08-11 04:55:22 +01:00
|
|
|
const std::string build_name =
|
|
|
|
header.zero == 0 ? reinterpret_cast<const char*>(header.build_name.data()) : "";
|
|
|
|
|
2023-04-05 19:28:56 +01:00
|
|
|
if (revision == Common::g_scm_rev) {
|
|
|
|
info.status = SaveStateInfo::ValidationStatus::OK;
|
|
|
|
} else {
|
2023-08-11 04:55:22 +01:00
|
|
|
if (!build_name.empty()) {
|
|
|
|
info.build_name = build_name;
|
|
|
|
} else if (hash_to_version.find(revision) != hash_to_version.end()) {
|
|
|
|
info.build_name = hash_to_version.at(revision);
|
|
|
|
}
|
|
|
|
if (info.build_name.empty()) {
|
|
|
|
LOG_WARNING(Core, "Save state file {} created from a different revision {}", path,
|
|
|
|
revision);
|
|
|
|
} else {
|
|
|
|
LOG_WARNING(Core,
|
|
|
|
"Save state file {} created from a different build {} with revision {}",
|
|
|
|
path, info.build_name, revision);
|
|
|
|
}
|
|
|
|
|
2023-04-05 19:28:56 +01:00
|
|
|
info.status = SaveStateInfo::ValidationStatus::RevisionDismatch;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-08-01 01:57:38 +01:00
|
|
|
std::vector<SaveStateInfo> ListSaveStates(u64 program_id, u64 movie_id) {
|
2020-02-18 05:19:52 +00:00
|
|
|
std::vector<SaveStateInfo> result;
|
2023-05-03 16:24:10 +01:00
|
|
|
result.reserve(SaveStateSlotCount);
|
2020-02-18 05:19:52 +00:00
|
|
|
for (u32 slot = 1; slot <= SaveStateSlotCount; ++slot) {
|
2023-08-01 01:57:38 +01:00
|
|
|
const auto path = GetSaveStatePath(program_id, movie_id, slot);
|
2020-02-18 05:19:52 +00:00
|
|
|
if (!FileUtil::Exists(path)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
SaveStateInfo info;
|
|
|
|
info.slot = slot;
|
|
|
|
|
|
|
|
FileUtil::IOFile file(path, "rb");
|
|
|
|
if (!file) {
|
|
|
|
LOG_ERROR(Core, "Could not open file {}", path);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
CSTHeader header;
|
|
|
|
if (file.GetSize() < sizeof(header)) {
|
|
|
|
LOG_ERROR(Core, "File too small {}", path);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (file.ReadBytes(&header, sizeof(header)) != sizeof(header)) {
|
|
|
|
LOG_ERROR(Core, "Could not read from file {}", path);
|
|
|
|
continue;
|
|
|
|
}
|
2023-08-01 01:57:38 +01:00
|
|
|
if (!ValidateSaveState(header, info, program_id, movie_id, slot)) {
|
2020-02-18 05:19:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.emplace_back(std::move(info));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::SaveState(u32 slot) const {
|
|
|
|
std::ostringstream sstream{std::ios_base::binary};
|
2020-04-12 23:12:15 +01:00
|
|
|
// Serialize
|
|
|
|
oarchive oa{sstream};
|
|
|
|
oa&* this;
|
|
|
|
|
2020-02-18 05:19:52 +00:00
|
|
|
const std::string& str{sstream.str()};
|
2023-07-06 23:52:40 +01:00
|
|
|
const auto data = std::span<const u8>{reinterpret_cast<const u8*>(str.data()), str.size()};
|
|
|
|
auto buffer = Common::Compression::CompressDataZSTDDefault(data);
|
2020-02-18 05:19:52 +00:00
|
|
|
|
2023-08-01 01:57:38 +01:00
|
|
|
const u64 movie_id = movie.GetCurrentMovieID();
|
|
|
|
const auto path = GetSaveStatePath(title_id, movie_id, slot);
|
2020-02-18 05:19:52 +00:00
|
|
|
if (!FileUtil::CreateFullPath(path)) {
|
2020-04-12 23:12:15 +01:00
|
|
|
throw std::runtime_error("Could not create path " + path);
|
2020-02-18 05:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FileUtil::IOFile file(path, "wb");
|
|
|
|
if (!file) {
|
2020-04-12 23:12:15 +01:00
|
|
|
throw std::runtime_error("Could not open file " + path);
|
2020-02-18 05:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CSTHeader header{};
|
|
|
|
header.filetype = header_magic_bytes;
|
|
|
|
header.program_id = title_id;
|
|
|
|
std::string rev_bytes;
|
2023-04-05 19:48:03 +01:00
|
|
|
CryptoPP::StringSource ss(Common::g_scm_rev, true,
|
|
|
|
new CryptoPP::HexDecoder(new CryptoPP::StringSink(rev_bytes)));
|
2020-02-18 05:19:52 +00:00
|
|
|
std::memcpy(header.revision.data(), rev_bytes.data(), sizeof(header.revision));
|
|
|
|
header.time = std::chrono::duration_cast<std::chrono::seconds>(
|
|
|
|
std::chrono::system_clock::now().time_since_epoch())
|
|
|
|
.count();
|
2023-08-11 04:55:22 +01:00
|
|
|
const std::string build_fullname = Common::g_build_fullname;
|
|
|
|
std::memset(header.build_name.data(), 0, sizeof(header.build_name));
|
|
|
|
std::memcpy(header.build_name.data(), build_fullname.c_str(),
|
|
|
|
std::min(build_fullname.length(), sizeof(header.build_name) - 1));
|
2020-02-18 05:19:52 +00:00
|
|
|
|
2020-04-12 23:12:15 +01:00
|
|
|
if (file.WriteBytes(&header, sizeof(header)) != sizeof(header) ||
|
|
|
|
file.WriteBytes(buffer.data(), buffer.size()) != buffer.size()) {
|
|
|
|
throw std::runtime_error("Could not write to file " + path);
|
2020-02-18 05:19:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::LoadState(u32 slot) {
|
2020-04-11 20:33:38 +01:00
|
|
|
if (Network::GetRoomMember().lock()->IsConnected()) {
|
2020-04-12 23:12:15 +01:00
|
|
|
throw std::runtime_error("Unable to load while connected to multiplayer");
|
2020-04-11 20:33:38 +01:00
|
|
|
}
|
|
|
|
|
2023-08-01 01:57:38 +01:00
|
|
|
const u64 movie_id = movie.GetCurrentMovieID();
|
|
|
|
const auto path = GetSaveStatePath(title_id, movie_id, slot);
|
2020-02-18 05:19:52 +00:00
|
|
|
|
|
|
|
std::vector<u8> decompressed;
|
|
|
|
{
|
|
|
|
std::vector<u8> buffer(FileUtil::GetSize(path) - sizeof(CSTHeader));
|
|
|
|
|
|
|
|
FileUtil::IOFile file(path, "rb");
|
2023-04-05 19:28:56 +01:00
|
|
|
|
|
|
|
// load header
|
|
|
|
CSTHeader header;
|
|
|
|
if (file.ReadBytes(&header, sizeof(header)) != sizeof(header)) {
|
|
|
|
throw std::runtime_error("Could not read from file at " + path);
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate header
|
|
|
|
SaveStateInfo info;
|
2023-08-01 01:57:38 +01:00
|
|
|
if (!ValidateSaveState(header, info, title_id, movie_id, slot)) {
|
2023-04-05 19:28:56 +01:00
|
|
|
throw std::runtime_error("Invalid savestate");
|
|
|
|
}
|
|
|
|
|
2020-02-18 05:19:52 +00:00
|
|
|
if (file.ReadBytes(buffer.data(), buffer.size()) != buffer.size()) {
|
2020-04-12 23:12:15 +01:00
|
|
|
throw std::runtime_error("Could not read from file at " + path);
|
2020-02-18 05:19:52 +00:00
|
|
|
}
|
|
|
|
decompressed = Common::Compression::DecompressDataZSTD(buffer);
|
|
|
|
}
|
|
|
|
std::istringstream sstream{
|
|
|
|
std::string{reinterpret_cast<char*>(decompressed.data()), decompressed.size()},
|
|
|
|
std::ios_base::binary};
|
|
|
|
decompressed.clear();
|
|
|
|
|
2020-04-12 23:12:15 +01:00
|
|
|
// Deserialize
|
|
|
|
iarchive ia{sstream};
|
|
|
|
ia&* this;
|
2020-02-18 05:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Core
|